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