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 | |
| 14 | class Config: |
| 15 | """ |
| 16 | XOS Configuration APIs |
| 17 | """ |
| 18 | |
| 19 | @staticmethod |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 20 | 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 26 | global INITIALIZED |
| 27 | global CONFIG |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 28 | global CONFIG_FILE |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 29 | # the config module can be initialized only one |
| 30 | if INITIALIZED: |
| 31 | raise Exception('[XOS-Config] Module already initialized') |
| 32 | INITIALIZED = True |
| 33 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 34 | # 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 43 | |
| 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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 47 | config_schema = Config.get_cli_param(sys.argv) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 48 | |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 49 | |
| 50 | CONFIG_FILE = config_file |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 51 | CONFIG = Config.read_config(config_file, config_schema) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 52 | |
| 53 | @staticmethod |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 54 | def get_config_file(): |
| 55 | return CONFIG_FILE |
| 56 | |
| 57 | @staticmethod |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 58 | def clear(): |
| 59 | global INITIALIZED |
| 60 | INITIALIZED = False |
| 61 | |
| 62 | @staticmethod |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 63 | 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 71 | 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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 83 | def read_config(config_file, config_schema): |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 84 | """ |
| 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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 92 | if not os.path.exists(config_schema): |
| 93 | raise Exception('[XOS-Config] Config schema not found at: %s' % config_schema) |
| 94 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 95 | try: |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 96 | Config.validate_config_format(config_file, config_schema) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 97 | 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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 120 | # 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 123 | 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 200 | if __name__ == '__main__': |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 201 | Config.init() |