Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 17 | import os |
| 18 | import sys |
| 19 | import yaml |
| 20 | import requests |
| 21 | import default |
| 22 | from pykwalify.core import Core as PyKwalify |
| 23 | |
Matteo Scandolo | 6bc017c | 2017-05-25 18:37:42 -0700 | [diff] [blame] | 24 | DEFAULT_CONFIG_FILE = "/opt/xos/xos_config.yaml" |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 25 | DEFAULT_CONFIG_SCHEMA = 'xos-config-schema.yaml' |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 26 | INITIALIZED = False |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 27 | CONFIG_FILE = None |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 28 | CONFIG = {} |
| 29 | |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 30 | GLOBAL_CONFIG_FILE = DEFAULT_CONFIG_FILE |
| 31 | GLOBAL_CONFIG_SCHEMA = DEFAULT_CONFIG_SCHEMA |
| 32 | GLOBAL_CONFIG = {} |
| 33 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 34 | class Config: |
| 35 | """ |
| 36 | XOS Configuration APIs |
| 37 | """ |
| 38 | |
| 39 | @staticmethod |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 40 | 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 46 | global INITIALIZED |
| 47 | global CONFIG |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 48 | global CONFIG_FILE |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 49 | |
| 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 57 | # the config module can be initialized only one |
| 58 | if INITIALIZED: |
| 59 | raise Exception('[XOS-Config] Module already initialized') |
| 60 | INITIALIZED = True |
| 61 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 62 | # 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 71 | |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 72 | # 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 78 | # 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 Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 81 | config_schema = Config.get_cli_param(sys.argv) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 82 | |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 83 | |
| 84 | CONFIG_FILE = config_file |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 85 | CONFIG = Config.read_config(config_file, config_schema) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 86 | |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 87 | # Load global schema |
| 88 | GLOBAL_CONFIG = Config.read_config(GLOBAL_CONFIG_FILE, GLOBAL_CONFIG_SCHEMA, True) |
| 89 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 90 | @staticmethod |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 91 | def get_config_file(): |
| 92 | return CONFIG_FILE |
| 93 | |
| 94 | @staticmethod |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 95 | def clear(): |
| 96 | global INITIALIZED |
| 97 | INITIALIZED = False |
| 98 | |
| 99 | @staticmethod |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 100 | 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 108 | 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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 120 | def read_config(config_file, config_schema, ignore_if_not_found=False): |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 121 | """ |
| 122 | Read the configuration file and return a dictionary |
| 123 | :param config_file: string |
| 124 | :return: dict |
| 125 | """ |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 126 | |
| 127 | if(not os.path.exists(config_file) and ignore_if_not_found): |
| 128 | return {} |
| 129 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 130 | if not os.path.exists(config_file): |
| 131 | raise Exception('[XOS-Config] Config file not found at: %s' % config_file) |
| 132 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 133 | if not os.path.exists(config_schema): |
| 134 | raise Exception('[XOS-Config] Config schema not found at: %s' % config_schema) |
| 135 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 136 | try: |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 137 | Config.validate_config_format(config_file, config_schema) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 138 | 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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 153 | global GLOBAL_CONFIG |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 154 | |
| 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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 160 | val = Config.get_param(query, GLOBAL_CONFIG) |
| 161 | if not val: |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 162 | val = Config.get_param(query, default.DEFAULT_VALUES) |
| 163 | if not val: |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 164 | # 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 167 | 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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 189 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 190 | :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 Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 206 | :return: a list of service names |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 207 | """ |
| 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 Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 244 | if __name__ == '__main__': |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 245 | Config.init() |