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 |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 23 | import pykwalify |
| 24 | |
| 25 | pykwalify.init_logging(1) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 26 | |
Matteo Scandolo | 6bc017c | 2017-05-25 18:37:42 -0700 | [diff] [blame] | 27 | DEFAULT_CONFIG_FILE = "/opt/xos/xos_config.yaml" |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 28 | DEFAULT_CONFIG_SCHEMA = 'xos-config-schema.yaml' |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 29 | INITIALIZED = False |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 30 | CONFIG_FILE = None |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 31 | CONFIG = {} |
| 32 | |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 33 | OVERRIDE_CONFIG = {} |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 34 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 35 | class Config: |
| 36 | """ |
| 37 | XOS Configuration APIs |
| 38 | """ |
| 39 | |
| 40 | @staticmethod |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 41 | def init(config_file=DEFAULT_CONFIG_FILE, config_schema=DEFAULT_CONFIG_SCHEMA, override_config_file=None): |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 42 | |
| 43 | # make schema relative to this directory |
| 44 | # TODO give the possibility to specify an absolute path |
| 45 | config_schema = Config.get_abs_path(config_schema) |
| 46 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 47 | global INITIALIZED |
| 48 | global CONFIG |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 49 | global CONFIG_FILE |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 50 | |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 51 | global OVERRIDE_CONFIG |
| 52 | global OVERRIDE_CONFIG_FILE |
| 53 | global OVERRIDE_CONFIG_SCHEMA |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 54 | |
| 55 | # Use same schema for both provided and global config by default |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 56 | OVERRIDE_CONFIG_SCHEMA = config_schema |
| 57 | OVERRIDE_CONFIG_FILE = override_config_file |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 58 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 59 | # the config module can be initialized only one |
| 60 | if INITIALIZED: |
| 61 | raise Exception('[XOS-Config] Module already initialized') |
| 62 | INITIALIZED = True |
| 63 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 64 | # if XOS_CONFIG_FILE is defined override the config_file |
| 65 | # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method |
| 66 | if os.environ.get('XOS_CONFIG_FILE'): |
| 67 | config_file = os.environ['XOS_CONFIG_FILE'] |
| 68 | |
| 69 | # if XOS_CONFIG_SCHEMA is defined override the config_schema |
| 70 | # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method |
| 71 | if os.environ.get('XOS_CONFIG_SCHEMA'): |
| 72 | config_schema = Config.get_abs_path(os.environ['XOS_CONFIG_SCHEMA']) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 73 | |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 74 | # allow OVERRIDE_CONFIG_* to be overridden by env vars |
| 75 | if os.environ.get('XOS_OVERRIDE_CONFIG_FILE'): |
| 76 | OVERRIDE_CONFIG_FILE = os.environ['XOS_OVERRIDE_CONFIG_FILE'] |
| 77 | if os.environ.get('XOS_OVERRIDE_CONFIG_SCHEMA'): |
| 78 | OVERRIDE_CONFIG_SCHEMA = Config.get_abs_path(os.environ['XOS_OVERRIDE_CONFIG_SCHEMA']) |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 79 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 80 | # if a -C parameter is set in the cli override the config_file |
| 81 | # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method |
| 82 | if Config.get_cli_param(sys.argv): |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 83 | config_schema = Config.get_cli_param(sys.argv) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 84 | |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 85 | |
| 86 | CONFIG_FILE = config_file |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 87 | CONFIG = Config.read_config(config_file, config_schema) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 88 | |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 89 | # if an override is set |
| 90 | if OVERRIDE_CONFIG_FILE is not None: |
| 91 | OVERRIDE_CONFIG = Config.read_config(OVERRIDE_CONFIG_FILE, OVERRIDE_CONFIG_SCHEMA, True) |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 92 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 93 | @staticmethod |
Matteo Scandolo | e0fc685 | 2017-06-07 16:01:54 -0700 | [diff] [blame] | 94 | def get_config_file(): |
| 95 | return CONFIG_FILE |
| 96 | |
| 97 | @staticmethod |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 98 | def clear(): |
| 99 | global INITIALIZED |
| 100 | INITIALIZED = False |
| 101 | |
| 102 | @staticmethod |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 103 | def get_abs_path(path): |
| 104 | if os.path.isabs(path): |
| 105 | return path |
| 106 | return os.path.dirname(os.path.realpath(__file__)) + '/' + path |
| 107 | |
| 108 | @staticmethod |
| 109 | def validate_config_format(config_file, config_schema): |
| 110 | schema = os.path.abspath(config_schema) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 111 | c = PyKwalify(source_file=config_file, schema_files=[schema]) |
| 112 | c.validate(raise_exception=True) |
| 113 | |
| 114 | @staticmethod |
| 115 | def get_cli_param(args): |
| 116 | last = None |
| 117 | for arg in args: |
| 118 | if last == '-C': |
| 119 | return arg |
| 120 | last = arg |
| 121 | |
| 122 | @staticmethod |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 123 | def read_config(config_file, config_schema, ignore_if_not_found=False): |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 124 | """ |
| 125 | Read the configuration file and return a dictionary |
| 126 | :param config_file: string |
| 127 | :return: dict |
| 128 | """ |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 129 | |
| 130 | if(not os.path.exists(config_file) and ignore_if_not_found): |
| 131 | return {} |
| 132 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 133 | if not os.path.exists(config_file): |
| 134 | raise Exception('[XOS-Config] Config file not found at: %s' % config_file) |
| 135 | |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 136 | if not os.path.exists(config_schema): |
| 137 | raise Exception('[XOS-Config] Config schema not found at: %s' % config_schema) |
| 138 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 139 | try: |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 140 | Config.validate_config_format(config_file, config_schema) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 141 | except Exception, e: |
Scott Baker | 1f6a32c | 2017-11-22 11:24:01 -0800 | [diff] [blame] | 142 | try: |
| 143 | error_msg = e.msg |
| 144 | except AttributeError: |
| 145 | error_msg = str(e) |
| 146 | raise Exception('[XOS-Config] The config format is wrong: %s' % error_msg) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 147 | |
| 148 | with open(config_file, 'r') as stream: |
| 149 | return yaml.safe_load(stream) |
| 150 | |
| 151 | @staticmethod |
| 152 | def get(query): |
| 153 | """ |
| 154 | Read a parameter from the config |
| 155 | :param query: a dot separated selector for configuration options (eg: database.username) |
| 156 | :return: the requested parameter in any format the parameter is specified |
| 157 | """ |
| 158 | global INITIALIZED |
| 159 | global CONFIG |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 160 | global OVERRIDE_CONFIG |
| 161 | global OVERRIDE_CONFIG_FILE |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 162 | |
| 163 | if not INITIALIZED: |
| 164 | raise Exception('[XOS-Config] Module has not been initialized') |
| 165 | |
| 166 | val = Config.get_param(query, CONFIG) |
Matteo Scandolo | c59372a | 2018-06-11 15:17:40 -0700 | [diff] [blame] | 167 | if OVERRIDE_CONFIG_FILE or not val: |
| 168 | # if we specified an override configuration, we should override the value |
| 169 | # we also look for the value in case it's missing |
| 170 | over_val = Config.get_param(query, OVERRIDE_CONFIG) |
| 171 | if over_val is not None: |
| 172 | val = over_val |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 173 | if not val: |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 174 | val = Config.get_param(query, default.DEFAULT_VALUES) |
| 175 | if not val: |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 176 | # TODO if no val return none |
| 177 | # raise Exception('[XOS-Config] Config does not have a value (or a default) parameter %s' % query) |
| 178 | return None |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 179 | return val |
| 180 | |
| 181 | @staticmethod |
| 182 | def get_param(query, config): |
| 183 | """ |
| 184 | Search for a parameter in config's first level, other call get_nested_param |
| 185 | :param query: a dot separated selector for configuration options (eg: database.username) |
| 186 | :param config: the config source to read from (can be the config file or the defaults) |
| 187 | :return: the requested parameter in any format the parameter is specified |
| 188 | """ |
| 189 | keys = query.split('.') |
| 190 | if len(keys) == 1: |
| 191 | key = keys[0] |
| 192 | if not config.has_key(key): |
| 193 | return None |
| 194 | return config[key] |
| 195 | else: |
| 196 | return Config.get_nested_param(keys, config) |
| 197 | |
| 198 | @staticmethod |
| 199 | def get_nested_param(keys, config): |
| 200 | """ |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 201 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 202 | :param keys: a list of descending selector |
| 203 | :param config: the config source to read from (can be the config file or the defaults) |
| 204 | :return: the requested parameter in any format the parameter is specified |
| 205 | """ |
| 206 | param = config |
| 207 | for k in keys: |
| 208 | if not param.has_key(k): |
| 209 | return None |
| 210 | param = param[k] |
| 211 | return param |
| 212 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 213 | if __name__ == '__main__': |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 214 | Config.init() |