blob: db3b5c15159dcdd70eb5123e82c49685717f51e8 [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
14class Config:
15 """
16 XOS Configuration APIs
17 """
18
19 @staticmethod
Matteo Scandolo1879ce72017-05-30 15:45:26 -070020 def init(config_file=DEFAULT_CONFIG_FILE, config_schema=DEFAULT_CONFIG_SCHEMA):
21
22 # make schema relative to this directory
23 # TODO give the possibility to specify an absolute path
24 config_schema = Config.get_abs_path(config_schema)
25
Matteo Scandolo56879722017-05-17 21:39:54 -070026 global INITIALIZED
27 global CONFIG
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070028 global CONFIG_FILE
Matteo Scandolo56879722017-05-17 21:39:54 -070029 # the config module can be initialized only one
30 if INITIALIZED:
31 raise Exception('[XOS-Config] Module already initialized')
32 INITIALIZED = True
33
Matteo Scandolo1879ce72017-05-30 15:45:26 -070034 # if XOS_CONFIG_FILE is defined override the config_file
35 # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
36 if os.environ.get('XOS_CONFIG_FILE'):
37 config_file = os.environ['XOS_CONFIG_FILE']
38
39 # if XOS_CONFIG_SCHEMA is defined override the config_schema
40 # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
41 if os.environ.get('XOS_CONFIG_SCHEMA'):
42 config_schema = Config.get_abs_path(os.environ['XOS_CONFIG_SCHEMA'])
Matteo Scandolo56879722017-05-17 21:39:54 -070043
44 # if a -C parameter is set in the cli override the config_file
45 # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
46 if Config.get_cli_param(sys.argv):
Matteo Scandolo1879ce72017-05-30 15:45:26 -070047 config_schema = Config.get_cli_param(sys.argv)
Matteo Scandolo56879722017-05-17 21:39:54 -070048
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070049
50 CONFIG_FILE = config_file
Matteo Scandolo1879ce72017-05-30 15:45:26 -070051 CONFIG = Config.read_config(config_file, config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -070052
53 @staticmethod
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070054 def get_config_file():
55 return CONFIG_FILE
56
57 @staticmethod
Matteo Scandolo56879722017-05-17 21:39:54 -070058 def clear():
59 global INITIALIZED
60 INITIALIZED = False
61
62 @staticmethod
Matteo Scandolo1879ce72017-05-30 15:45:26 -070063 def get_abs_path(path):
64 if os.path.isabs(path):
65 return path
66 return os.path.dirname(os.path.realpath(__file__)) + '/' + path
67
68 @staticmethod
69 def validate_config_format(config_file, config_schema):
70 schema = os.path.abspath(config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -070071 c = PyKwalify(source_file=config_file, schema_files=[schema])
72 c.validate(raise_exception=True)
73
74 @staticmethod
75 def get_cli_param(args):
76 last = None
77 for arg in args:
78 if last == '-C':
79 return arg
80 last = arg
81
82 @staticmethod
Matteo Scandolo1879ce72017-05-30 15:45:26 -070083 def read_config(config_file, config_schema):
Matteo Scandolo56879722017-05-17 21:39:54 -070084 """
85 Read the configuration file and return a dictionary
86 :param config_file: string
87 :return: dict
88 """
89 if not os.path.exists(config_file):
90 raise Exception('[XOS-Config] Config file not found at: %s' % config_file)
91
Matteo Scandolo1879ce72017-05-30 15:45:26 -070092 if not os.path.exists(config_schema):
93 raise Exception('[XOS-Config] Config schema not found at: %s' % config_schema)
94
Matteo Scandolo56879722017-05-17 21:39:54 -070095 try:
Matteo Scandolo1879ce72017-05-30 15:45:26 -070096 Config.validate_config_format(config_file, config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -070097 except Exception, e:
98 raise Exception('[XOS-Config] The config format is wrong: %s' % e.msg)
99
100 with open(config_file, 'r') as stream:
101 return yaml.safe_load(stream)
102
103 @staticmethod
104 def get(query):
105 """
106 Read a parameter from the config
107 :param query: a dot separated selector for configuration options (eg: database.username)
108 :return: the requested parameter in any format the parameter is specified
109 """
110 global INITIALIZED
111 global CONFIG
112
113 if not INITIALIZED:
114 raise Exception('[XOS-Config] Module has not been initialized')
115
116 val = Config.get_param(query, CONFIG)
117 if not val:
118 val = Config.get_param(query, default.DEFAULT_VALUES)
119 if not val:
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700120 # TODO if no val return none
121 # raise Exception('[XOS-Config] Config does not have a value (or a default) parameter %s' % query)
122 return None
Matteo Scandolo56879722017-05-17 21:39:54 -0700123 return val
124
125 @staticmethod
126 def get_param(query, config):
127 """
128 Search for a parameter in config's first level, other call get_nested_param
129 :param query: a dot separated selector for configuration options (eg: database.username)
130 :param config: the config source to read from (can be the config file or the defaults)
131 :return: the requested parameter in any format the parameter is specified
132 """
133 keys = query.split('.')
134 if len(keys) == 1:
135 key = keys[0]
136 if not config.has_key(key):
137 return None
138 return config[key]
139 else:
140 return Config.get_nested_param(keys, config)
141
142 @staticmethod
143 def get_nested_param(keys, config):
144 """
145
146 :param keys: a list of descending selector
147 :param config: the config source to read from (can be the config file or the defaults)
148 :return: the requested parameter in any format the parameter is specified
149 """
150 param = config
151 for k in keys:
152 if not param.has_key(k):
153 return None
154 param = param[k]
155 return param
156
157 @staticmethod
158 def get_service_list():
159 """
160 Query registrator to get the list of services
161 NOTE: we assume that consul is a valid URL
162 :return: a list of service names
163 """
164 service_dict = requests.get('http://consul:8500/v1/catalog/services').json()
165 service_list = []
166 for s in service_dict:
167 service_list.append(s)
168 return service_list
169
170 @staticmethod
171 def get_service_info(service_name):
172 """
173 Query registrator to get the details about a service
174 NOTE: we assume that consul is a valid URL
175 :param service_name: the name of the service, can be retrieved from get_service_list
176 :return: the informations about a service
177 """
178 response = requests.get('http://consul:8500/v1/catalog/service/%s' % service_name)
179 if not response.ok:
180 raise Exception('[XOS-Config] Registrator is down')
181 service = response.json()
182 if not service or len(service) == 0:
183 raise Exception('[XOS-Config] The service missing-service looking for does not exist')
184 return {
185 'name': service[0]['ServiceName'],
186 'url': service[0]['ServiceAddress'],
187 'port': service[0]['ServicePort']
188 }
189
190 @staticmethod
191 def get_service_endpoint(service_name):
192 """
193 Query registrator to get the details about a service and return the endpoint in for of a string
194 :param service_name: the name of the service, can be retrieved from get_service_list
195 :return: the endpoint of the service
196 """
197 service = Config.get_service_info(service_name)
198 return 'http://%s:%s' % (service['url'], service['port'])
199
Matteo Scandolo56879722017-05-17 21:39:54 -0700200if __name__ == '__main__':
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700201 Config.init()