blob: 911e286bd3ff3e634a8993576c1588370cd5f689 [file] [log] [blame]
Matteo Scandolo56879722017-05-17 21:39:54 -07001import os
2import sys
3import yaml
4import requests
5import default
6from pykwalify.core import Core as PyKwalify
7
Matteo Scandolo6bc017c2017-05-25 18:37:42 -07008DEFAULT_CONFIG_FILE = "/opt/xos/xos_config.yaml"
Matteo Scandolo1879ce72017-05-30 15:45:26 -07009DEFAULT_CONFIG_SCHEMA = 'xos-config-schema.yaml'
Matteo Scandolo56879722017-05-17 21:39:54 -070010INITIALIZED = False
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070011CONFIG_FILE = None
Matteo Scandolo56879722017-05-17 21:39:54 -070012CONFIG = {}
13
Zack Williams37845ca2017-07-18 15:39:20 -070014GLOBAL_CONFIG_FILE = DEFAULT_CONFIG_FILE
15GLOBAL_CONFIG_SCHEMA = DEFAULT_CONFIG_SCHEMA
16GLOBAL_CONFIG = {}
17
Matteo Scandolo56879722017-05-17 21:39:54 -070018class Config:
19 """
20 XOS Configuration APIs
21 """
22
23 @staticmethod
Matteo Scandolo1879ce72017-05-30 15:45:26 -070024 def init(config_file=DEFAULT_CONFIG_FILE, config_schema=DEFAULT_CONFIG_SCHEMA):
25
26 # make schema relative to this directory
27 # TODO give the possibility to specify an absolute path
28 config_schema = Config.get_abs_path(config_schema)
29
Matteo Scandolo56879722017-05-17 21:39:54 -070030 global INITIALIZED
31 global CONFIG
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070032 global CONFIG_FILE
Zack Williams37845ca2017-07-18 15:39:20 -070033
34 global GLOBAL_CONFIG
35 global GLOBAL_CONFIG_FILE
36 global GLOBAL_CONFIG_SCHEMA
37
38 # Use same schema for both provided and global config by default
39 GLOBAL_CONFIG_SCHEMA = config_schema
40
Matteo Scandolo56879722017-05-17 21:39:54 -070041 # the config module can be initialized only one
42 if INITIALIZED:
43 raise Exception('[XOS-Config] Module already initialized')
44 INITIALIZED = True
45
Matteo Scandolo1879ce72017-05-30 15:45:26 -070046 # if XOS_CONFIG_FILE is defined override the config_file
47 # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
48 if os.environ.get('XOS_CONFIG_FILE'):
49 config_file = os.environ['XOS_CONFIG_FILE']
50
51 # if XOS_CONFIG_SCHEMA is defined override the config_schema
52 # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
53 if os.environ.get('XOS_CONFIG_SCHEMA'):
54 config_schema = Config.get_abs_path(os.environ['XOS_CONFIG_SCHEMA'])
Matteo Scandolo56879722017-05-17 21:39:54 -070055
Zack Williams37845ca2017-07-18 15:39:20 -070056 # allow GLOBAL_CONFIG_* to be overridden by env vars
57 if os.environ.get('XOS_GLOBAL_CONFIG_FILE'):
58 GLOBAL_CONFIG_FILE = os.environ['XOS_GLOBAL_CONFIG_FILE']
59 if os.environ.get('XOS_GLOBAL_CONFIG_SCHEMA'):
60 GLOBAL_CONFIG_SCHEMA = Config.get_abs_path(os.environ['XOS_GLOBAL_CONFIG_SCHEMA'])
61
Matteo Scandolo56879722017-05-17 21:39:54 -070062 # if a -C parameter is set in the cli 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 Config.get_cli_param(sys.argv):
Matteo Scandolo1879ce72017-05-30 15:45:26 -070065 config_schema = Config.get_cli_param(sys.argv)
Matteo Scandolo56879722017-05-17 21:39:54 -070066
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070067
68 CONFIG_FILE = config_file
Matteo Scandolo1879ce72017-05-30 15:45:26 -070069 CONFIG = Config.read_config(config_file, config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -070070
Zack Williams37845ca2017-07-18 15:39:20 -070071 # Load global schema
72 GLOBAL_CONFIG = Config.read_config(GLOBAL_CONFIG_FILE, GLOBAL_CONFIG_SCHEMA, True)
73
Matteo Scandolo56879722017-05-17 21:39:54 -070074 @staticmethod
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070075 def get_config_file():
76 return CONFIG_FILE
77
78 @staticmethod
Matteo Scandolo56879722017-05-17 21:39:54 -070079 def clear():
80 global INITIALIZED
81 INITIALIZED = False
82
83 @staticmethod
Matteo Scandolo1879ce72017-05-30 15:45:26 -070084 def get_abs_path(path):
85 if os.path.isabs(path):
86 return path
87 return os.path.dirname(os.path.realpath(__file__)) + '/' + path
88
89 @staticmethod
90 def validate_config_format(config_file, config_schema):
91 schema = os.path.abspath(config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -070092 c = PyKwalify(source_file=config_file, schema_files=[schema])
93 c.validate(raise_exception=True)
94
95 @staticmethod
96 def get_cli_param(args):
97 last = None
98 for arg in args:
99 if last == '-C':
100 return arg
101 last = arg
102
103 @staticmethod
Zack Williams37845ca2017-07-18 15:39:20 -0700104 def read_config(config_file, config_schema, ignore_if_not_found=False):
Matteo Scandolo56879722017-05-17 21:39:54 -0700105 """
106 Read the configuration file and return a dictionary
107 :param config_file: string
108 :return: dict
109 """
Zack Williams37845ca2017-07-18 15:39:20 -0700110
111 if(not os.path.exists(config_file) and ignore_if_not_found):
112 return {}
113
Matteo Scandolo56879722017-05-17 21:39:54 -0700114 if not os.path.exists(config_file):
115 raise Exception('[XOS-Config] Config file not found at: %s' % config_file)
116
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700117 if not os.path.exists(config_schema):
118 raise Exception('[XOS-Config] Config schema not found at: %s' % config_schema)
119
Matteo Scandolo56879722017-05-17 21:39:54 -0700120 try:
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700121 Config.validate_config_format(config_file, config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -0700122 except Exception, e:
123 raise Exception('[XOS-Config] The config format is wrong: %s' % e.msg)
124
125 with open(config_file, 'r') as stream:
126 return yaml.safe_load(stream)
127
128 @staticmethod
129 def get(query):
130 """
131 Read a parameter from the config
132 :param query: a dot separated selector for configuration options (eg: database.username)
133 :return: the requested parameter in any format the parameter is specified
134 """
135 global INITIALIZED
136 global CONFIG
Zack Williams37845ca2017-07-18 15:39:20 -0700137 global GLOBAL_CONFIG
Matteo Scandolo56879722017-05-17 21:39:54 -0700138
139 if not INITIALIZED:
140 raise Exception('[XOS-Config] Module has not been initialized')
141
142 val = Config.get_param(query, CONFIG)
143 if not val:
Zack Williams37845ca2017-07-18 15:39:20 -0700144 val = Config.get_param(query, GLOBAL_CONFIG)
145 if not val:
Matteo Scandolo56879722017-05-17 21:39:54 -0700146 val = Config.get_param(query, default.DEFAULT_VALUES)
147 if not val:
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700148 # TODO if no val return none
149 # raise Exception('[XOS-Config] Config does not have a value (or a default) parameter %s' % query)
150 return None
Matteo Scandolo56879722017-05-17 21:39:54 -0700151 return val
152
153 @staticmethod
154 def get_param(query, config):
155 """
156 Search for a parameter in config's first level, other call get_nested_param
157 :param query: a dot separated selector for configuration options (eg: database.username)
158 :param config: the config source to read from (can be the config file or the defaults)
159 :return: the requested parameter in any format the parameter is specified
160 """
161 keys = query.split('.')
162 if len(keys) == 1:
163 key = keys[0]
164 if not config.has_key(key):
165 return None
166 return config[key]
167 else:
168 return Config.get_nested_param(keys, config)
169
170 @staticmethod
171 def get_nested_param(keys, config):
172 """
Zack Williams37845ca2017-07-18 15:39:20 -0700173
Matteo Scandolo56879722017-05-17 21:39:54 -0700174 :param keys: a list of descending selector
175 :param config: the config source to read from (can be the config file or the defaults)
176 :return: the requested parameter in any format the parameter is specified
177 """
178 param = config
179 for k in keys:
180 if not param.has_key(k):
181 return None
182 param = param[k]
183 return param
184
185 @staticmethod
186 def get_service_list():
187 """
188 Query registrator to get the list of services
189 NOTE: we assume that consul is a valid URL
Zack Williams37845ca2017-07-18 15:39:20 -0700190 :return: a list of service names
Matteo Scandolo56879722017-05-17 21:39:54 -0700191 """
192 service_dict = requests.get('http://consul:8500/v1/catalog/services').json()
193 service_list = []
194 for s in service_dict:
195 service_list.append(s)
196 return service_list
197
198 @staticmethod
199 def get_service_info(service_name):
200 """
201 Query registrator to get the details about a service
202 NOTE: we assume that consul is a valid URL
203 :param service_name: the name of the service, can be retrieved from get_service_list
204 :return: the informations about a service
205 """
206 response = requests.get('http://consul:8500/v1/catalog/service/%s' % service_name)
207 if not response.ok:
208 raise Exception('[XOS-Config] Registrator is down')
209 service = response.json()
210 if not service or len(service) == 0:
211 raise Exception('[XOS-Config] The service missing-service looking for does not exist')
212 return {
213 'name': service[0]['ServiceName'],
214 'url': service[0]['ServiceAddress'],
215 'port': service[0]['ServicePort']
216 }
217
218 @staticmethod
219 def get_service_endpoint(service_name):
220 """
221 Query registrator to get the details about a service and return the endpoint in for of a string
222 :param service_name: the name of the service, can be retrieved from get_service_list
223 :return: the endpoint of the service
224 """
225 service = Config.get_service_info(service_name)
226 return 'http://%s:%s' % (service['url'], service['port'])
227
Matteo Scandolo56879722017-05-17 21:39:54 -0700228if __name__ == '__main__':
Zack Williams37845ca2017-07-18 15:39:20 -0700229 Config.init()