blob: ff221a9e4d5ceaeb9c23ce23b923d9be1e56eb4d [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Matteo Scandolo56879722017-05-17 21:39:54 -070017import os
18import sys
19import yaml
20import requests
21import default
22from pykwalify.core import Core as PyKwalify
23
Matteo Scandolo6bc017c2017-05-25 18:37:42 -070024DEFAULT_CONFIG_FILE = "/opt/xos/xos_config.yaml"
Matteo Scandolo1879ce72017-05-30 15:45:26 -070025DEFAULT_CONFIG_SCHEMA = 'xos-config-schema.yaml'
Matteo Scandolo56879722017-05-17 21:39:54 -070026INITIALIZED = False
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070027CONFIG_FILE = None
Matteo Scandolo56879722017-05-17 21:39:54 -070028CONFIG = {}
29
Zack Williams37845ca2017-07-18 15:39:20 -070030GLOBAL_CONFIG_FILE = DEFAULT_CONFIG_FILE
31GLOBAL_CONFIG_SCHEMA = DEFAULT_CONFIG_SCHEMA
32GLOBAL_CONFIG = {}
33
Matteo Scandolo56879722017-05-17 21:39:54 -070034class Config:
35 """
36 XOS Configuration APIs
37 """
38
39 @staticmethod
Matteo Scandolo1879ce72017-05-30 15:45:26 -070040 def init(config_file=DEFAULT_CONFIG_FILE, config_schema=DEFAULT_CONFIG_SCHEMA):
41
42 # make schema relative to this directory
43 # TODO give the possibility to specify an absolute path
44 config_schema = Config.get_abs_path(config_schema)
45
Matteo Scandolo56879722017-05-17 21:39:54 -070046 global INITIALIZED
47 global CONFIG
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070048 global CONFIG_FILE
Zack Williams37845ca2017-07-18 15:39:20 -070049
50 global GLOBAL_CONFIG
51 global GLOBAL_CONFIG_FILE
52 global GLOBAL_CONFIG_SCHEMA
53
54 # Use same schema for both provided and global config by default
55 GLOBAL_CONFIG_SCHEMA = config_schema
56
Matteo Scandolo56879722017-05-17 21:39:54 -070057 # the config module can be initialized only one
58 if INITIALIZED:
59 raise Exception('[XOS-Config] Module already initialized')
60 INITIALIZED = True
61
Matteo Scandolo1879ce72017-05-30 15:45:26 -070062 # if XOS_CONFIG_FILE is defined override the config_file
63 # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
64 if os.environ.get('XOS_CONFIG_FILE'):
65 config_file = os.environ['XOS_CONFIG_FILE']
66
67 # if XOS_CONFIG_SCHEMA is defined override the config_schema
68 # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
69 if os.environ.get('XOS_CONFIG_SCHEMA'):
70 config_schema = Config.get_abs_path(os.environ['XOS_CONFIG_SCHEMA'])
Matteo Scandolo56879722017-05-17 21:39:54 -070071
Zack Williams37845ca2017-07-18 15:39:20 -070072 # allow GLOBAL_CONFIG_* to be overridden by env vars
73 if os.environ.get('XOS_GLOBAL_CONFIG_FILE'):
74 GLOBAL_CONFIG_FILE = os.environ['XOS_GLOBAL_CONFIG_FILE']
75 if os.environ.get('XOS_GLOBAL_CONFIG_SCHEMA'):
76 GLOBAL_CONFIG_SCHEMA = Config.get_abs_path(os.environ['XOS_GLOBAL_CONFIG_SCHEMA'])
77
Matteo Scandolo56879722017-05-17 21:39:54 -070078 # if a -C parameter is set in the cli override the config_file
79 # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
80 if Config.get_cli_param(sys.argv):
Matteo Scandolo1879ce72017-05-30 15:45:26 -070081 config_schema = Config.get_cli_param(sys.argv)
Matteo Scandolo56879722017-05-17 21:39:54 -070082
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070083
84 CONFIG_FILE = config_file
Matteo Scandolo1879ce72017-05-30 15:45:26 -070085 CONFIG = Config.read_config(config_file, config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -070086
Zack Williams37845ca2017-07-18 15:39:20 -070087 # Load global schema
88 GLOBAL_CONFIG = Config.read_config(GLOBAL_CONFIG_FILE, GLOBAL_CONFIG_SCHEMA, True)
89
Matteo Scandolo56879722017-05-17 21:39:54 -070090 @staticmethod
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070091 def get_config_file():
92 return CONFIG_FILE
93
94 @staticmethod
Matteo Scandolo56879722017-05-17 21:39:54 -070095 def clear():
96 global INITIALIZED
97 INITIALIZED = False
98
99 @staticmethod
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700100 def get_abs_path(path):
101 if os.path.isabs(path):
102 return path
103 return os.path.dirname(os.path.realpath(__file__)) + '/' + path
104
105 @staticmethod
106 def validate_config_format(config_file, config_schema):
107 schema = os.path.abspath(config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -0700108 c = PyKwalify(source_file=config_file, schema_files=[schema])
109 c.validate(raise_exception=True)
110
111 @staticmethod
112 def get_cli_param(args):
113 last = None
114 for arg in args:
115 if last == '-C':
116 return arg
117 last = arg
118
119 @staticmethod
Zack Williams37845ca2017-07-18 15:39:20 -0700120 def read_config(config_file, config_schema, ignore_if_not_found=False):
Matteo Scandolo56879722017-05-17 21:39:54 -0700121 """
122 Read the configuration file and return a dictionary
123 :param config_file: string
124 :return: dict
125 """
Zack Williams37845ca2017-07-18 15:39:20 -0700126
127 if(not os.path.exists(config_file) and ignore_if_not_found):
128 return {}
129
Matteo Scandolo56879722017-05-17 21:39:54 -0700130 if not os.path.exists(config_file):
131 raise Exception('[XOS-Config] Config file not found at: %s' % config_file)
132
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700133 if not os.path.exists(config_schema):
134 raise Exception('[XOS-Config] Config schema not found at: %s' % config_schema)
135
Matteo Scandolo56879722017-05-17 21:39:54 -0700136 try:
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700137 Config.validate_config_format(config_file, config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -0700138 except Exception, e:
139 raise Exception('[XOS-Config] The config format is wrong: %s' % e.msg)
140
141 with open(config_file, 'r') as stream:
142 return yaml.safe_load(stream)
143
144 @staticmethod
145 def get(query):
146 """
147 Read a parameter from the config
148 :param query: a dot separated selector for configuration options (eg: database.username)
149 :return: the requested parameter in any format the parameter is specified
150 """
151 global INITIALIZED
152 global CONFIG
Zack Williams37845ca2017-07-18 15:39:20 -0700153 global GLOBAL_CONFIG
Matteo Scandolo56879722017-05-17 21:39:54 -0700154
155 if not INITIALIZED:
156 raise Exception('[XOS-Config] Module has not been initialized')
157
158 val = Config.get_param(query, CONFIG)
159 if not val:
Zack Williams37845ca2017-07-18 15:39:20 -0700160 val = Config.get_param(query, GLOBAL_CONFIG)
161 if not val:
Matteo Scandolo56879722017-05-17 21:39:54 -0700162 val = Config.get_param(query, default.DEFAULT_VALUES)
163 if not val:
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700164 # TODO if no val return none
165 # raise Exception('[XOS-Config] Config does not have a value (or a default) parameter %s' % query)
166 return None
Matteo Scandolo56879722017-05-17 21:39:54 -0700167 return val
168
169 @staticmethod
170 def get_param(query, config):
171 """
172 Search for a parameter in config's first level, other call get_nested_param
173 :param query: a dot separated selector for configuration options (eg: database.username)
174 :param config: the config source to read from (can be the config file or the defaults)
175 :return: the requested parameter in any format the parameter is specified
176 """
177 keys = query.split('.')
178 if len(keys) == 1:
179 key = keys[0]
180 if not config.has_key(key):
181 return None
182 return config[key]
183 else:
184 return Config.get_nested_param(keys, config)
185
186 @staticmethod
187 def get_nested_param(keys, config):
188 """
Zack Williams37845ca2017-07-18 15:39:20 -0700189
Matteo Scandolo56879722017-05-17 21:39:54 -0700190 :param keys: a list of descending selector
191 :param config: the config source to read from (can be the config file or the defaults)
192 :return: the requested parameter in any format the parameter is specified
193 """
194 param = config
195 for k in keys:
196 if not param.has_key(k):
197 return None
198 param = param[k]
199 return param
200
201 @staticmethod
202 def get_service_list():
203 """
204 Query registrator to get the list of services
205 NOTE: we assume that consul is a valid URL
Zack Williams37845ca2017-07-18 15:39:20 -0700206 :return: a list of service names
Matteo Scandolo56879722017-05-17 21:39:54 -0700207 """
208 service_dict = requests.get('http://consul:8500/v1/catalog/services').json()
209 service_list = []
210 for s in service_dict:
211 service_list.append(s)
212 return service_list
213
214 @staticmethod
215 def get_service_info(service_name):
216 """
217 Query registrator to get the details about a service
218 NOTE: we assume that consul is a valid URL
219 :param service_name: the name of the service, can be retrieved from get_service_list
220 :return: the informations about a service
221 """
222 response = requests.get('http://consul:8500/v1/catalog/service/%s' % service_name)
223 if not response.ok:
224 raise Exception('[XOS-Config] Registrator is down')
225 service = response.json()
226 if not service or len(service) == 0:
227 raise Exception('[XOS-Config] The service missing-service looking for does not exist')
228 return {
229 'name': service[0]['ServiceName'],
230 'url': service[0]['ServiceAddress'],
231 'port': service[0]['ServicePort']
232 }
233
234 @staticmethod
235 def get_service_endpoint(service_name):
236 """
237 Query registrator to get the details about a service and return the endpoint in for of a string
238 :param service_name: the name of the service, can be retrieved from get_service_list
239 :return: the endpoint of the service
240 """
241 service = Config.get_service_info(service_name)
242 return 'http://%s:%s' % (service['url'], service['port'])
243
Matteo Scandolo56879722017-05-17 21:39:54 -0700244if __name__ == '__main__':
Zack Williams37845ca2017-07-18 15:39:20 -0700245 Config.init()