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: |
Scott Baker | 1f6a32c | 2017-11-22 11:24:01 -0800 | [diff] [blame] | 139 | try: |
| 140 | error_msg = e.msg |
| 141 | except AttributeError: |
| 142 | error_msg = str(e) |
| 143 | raise Exception('[XOS-Config] The config format is wrong: %s' % error_msg) |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 144 | |
| 145 | with open(config_file, 'r') as stream: |
| 146 | return yaml.safe_load(stream) |
| 147 | |
| 148 | @staticmethod |
| 149 | def get(query): |
| 150 | """ |
| 151 | Read a parameter from the config |
| 152 | :param query: a dot separated selector for configuration options (eg: database.username) |
| 153 | :return: the requested parameter in any format the parameter is specified |
| 154 | """ |
| 155 | global INITIALIZED |
| 156 | global CONFIG |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 157 | global GLOBAL_CONFIG |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 158 | |
| 159 | if not INITIALIZED: |
| 160 | raise Exception('[XOS-Config] Module has not been initialized') |
| 161 | |
| 162 | val = Config.get_param(query, CONFIG) |
| 163 | if not val: |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 164 | val = Config.get_param(query, GLOBAL_CONFIG) |
| 165 | if not val: |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 166 | val = Config.get_param(query, default.DEFAULT_VALUES) |
| 167 | if not val: |
Matteo Scandolo | 1879ce7 | 2017-05-30 15:45:26 -0700 | [diff] [blame] | 168 | # TODO if no val return none |
| 169 | # raise Exception('[XOS-Config] Config does not have a value (or a default) parameter %s' % query) |
| 170 | return None |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 171 | return val |
| 172 | |
| 173 | @staticmethod |
| 174 | def get_param(query, config): |
| 175 | """ |
| 176 | Search for a parameter in config's first level, other call get_nested_param |
| 177 | :param query: a dot separated selector for configuration options (eg: database.username) |
| 178 | :param config: the config source to read from (can be the config file or the defaults) |
| 179 | :return: the requested parameter in any format the parameter is specified |
| 180 | """ |
| 181 | keys = query.split('.') |
| 182 | if len(keys) == 1: |
| 183 | key = keys[0] |
| 184 | if not config.has_key(key): |
| 185 | return None |
| 186 | return config[key] |
| 187 | else: |
| 188 | return Config.get_nested_param(keys, config) |
| 189 | |
| 190 | @staticmethod |
| 191 | def get_nested_param(keys, config): |
| 192 | """ |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 193 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 194 | :param keys: a list of descending selector |
| 195 | :param config: the config source to read from (can be the config file or the defaults) |
| 196 | :return: the requested parameter in any format the parameter is specified |
| 197 | """ |
| 198 | param = config |
| 199 | for k in keys: |
| 200 | if not param.has_key(k): |
| 201 | return None |
| 202 | param = param[k] |
| 203 | return param |
| 204 | |
Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 205 | if __name__ == '__main__': |
Zack Williams | 37845ca | 2017-07-18 15:39:20 -0700 | [diff] [blame] | 206 | Config.init() |