Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import yaml |
| 4 | import requests |
| 5 | import default |
| 6 | from pykwalify.core import Core as PyKwalify |
| 7 | |
Matteo Scandolo | 6bc017c | 2017-05-25 18:37:42 -0700 | [diff] [blame] | 8 | DEFAULT_CONFIG_FILE = "/opt/xos/xos_config.yaml" |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 9 | DEFAULT_CONFIG_SCHEMA = 'xos-config-schema.yaml' |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 10 | INITIALIZED = False |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 11 | CONFIG_FILE = None |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 12 | CONFIG = {} |
| 13 | |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 14 | GLOBAL_CONFIG_FILE = DEFAULT_CONFIG_FILE |
| 15 | GLOBAL_CONFIG_SCHEMA = DEFAULT_CONFIG_SCHEMA |
| 16 | GLOBAL_CONFIG = {} |
| 17 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 18 | class Config: |
| 19 | """ |
| 20 | XOS Configuration APIs |
| 21 | """ |
| 22 | |
| 23 | @staticmethod |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 24 | 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 30 | global INITIALIZED |
| 31 | global CONFIG |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 32 | global CONFIG_FILE |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 33 | |
| 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 41 | # the config module can be initialized only one |
| 42 | if INITIALIZED: |
| 43 | raise Exception('[XOS-Config] Module already initialized') |
| 44 | INITIALIZED = True |
| 45 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 46 | # 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 55 | |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 56 | # 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 62 | # 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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 65 | config_schema = Config.get_cli_param(sys.argv) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 66 | |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 67 | |
| 68 | CONFIG_FILE = config_file |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 69 | CONFIG = Config.read_config(config_file, config_schema) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 70 | |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 71 | # Load global schema |
| 72 | GLOBAL_CONFIG = Config.read_config(GLOBAL_CONFIG_FILE, GLOBAL_CONFIG_SCHEMA, True) |
| 73 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 74 | @staticmethod |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 75 | def get_config_file(): |
| 76 | return CONFIG_FILE |
| 77 | |
| 78 | @staticmethod |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 79 | def clear(): |
| 80 | global INITIALIZED |
| 81 | INITIALIZED = False |
| 82 | |
| 83 | @staticmethod |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 84 | 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 92 | 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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 104 | def read_config(config_file, config_schema, ignore_if_not_found=False): |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 105 | """ |
| 106 | Read the configuration file and return a dictionary |
| 107 | :param config_file: string |
| 108 | :return: dict |
| 109 | """ |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 110 | |
| 111 | if(not os.path.exists(config_file) and ignore_if_not_found): |
| 112 | return {} |
| 113 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 114 | if not os.path.exists(config_file): |
| 115 | raise Exception('[XOS-Config] Config file not found at: %s' % config_file) |
| 116 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 117 | if not os.path.exists(config_schema): |
| 118 | raise Exception('[XOS-Config] Config schema not found at: %s' % config_schema) |
| 119 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 120 | try: |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 121 | Config.validate_config_format(config_file, config_schema) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 122 | 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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 137 | global GLOBAL_CONFIG |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 138 | |
| 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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 144 | val = Config.get_param(query, GLOBAL_CONFIG) |
| 145 | if not val: |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 146 | val = Config.get_param(query, default.DEFAULT_VALUES) |
| 147 | if not val: |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 148 | # 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 151 | 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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 173 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 174 | :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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 190 | :return: a list of service names |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 191 | """ |
| 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 228 | if __name__ == '__main__': |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 229 | Config.init() |