blob: ebc696ffd382f71bc62ba31ed1a142d3cc54a43f [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
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 Scandolo56879722017-05-17 21:39:54 -070017import os
18import sys
19import yaml
20import requests
21import default
22from pykwalify.core import Core as PyKwalify
Matteo Scandoloc59372a2018-06-11 15:17:40 -070023import pykwalify
24
25pykwalify.init_logging(1)
Matteo Scandolo56879722017-05-17 21:39:54 -070026
Matteo Scandolo6bc017c2017-05-25 18:37:42 -070027DEFAULT_CONFIG_FILE = "/opt/xos/xos_config.yaml"
Matteo Scandolo1879ce72017-05-30 15:45:26 -070028DEFAULT_CONFIG_SCHEMA = 'xos-config-schema.yaml'
Matteo Scandolo56879722017-05-17 21:39:54 -070029INITIALIZED = False
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070030CONFIG_FILE = None
Matteo Scandolo56879722017-05-17 21:39:54 -070031CONFIG = {}
32
Matteo Scandoloc59372a2018-06-11 15:17:40 -070033OVERRIDE_CONFIG = {}
Zack Williams37845ca2017-07-18 15:39:20 -070034
Matteo Scandolo56879722017-05-17 21:39:54 -070035class Config:
36 """
37 XOS Configuration APIs
38 """
39
40 @staticmethod
Matteo Scandoloc59372a2018-06-11 15:17:40 -070041 def init(config_file=DEFAULT_CONFIG_FILE, config_schema=DEFAULT_CONFIG_SCHEMA, override_config_file=None):
Matteo Scandolo1879ce72017-05-30 15:45:26 -070042
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 Scandolo56879722017-05-17 21:39:54 -070047 global INITIALIZED
48 global CONFIG
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070049 global CONFIG_FILE
Zack Williams37845ca2017-07-18 15:39:20 -070050
Matteo Scandoloc59372a2018-06-11 15:17:40 -070051 global OVERRIDE_CONFIG
52 global OVERRIDE_CONFIG_FILE
53 global OVERRIDE_CONFIG_SCHEMA
Zack Williams37845ca2017-07-18 15:39:20 -070054
55 # Use same schema for both provided and global config by default
Matteo Scandoloc59372a2018-06-11 15:17:40 -070056 OVERRIDE_CONFIG_SCHEMA = config_schema
57 OVERRIDE_CONFIG_FILE = override_config_file
Zack Williams37845ca2017-07-18 15:39:20 -070058
Matteo Scandolo56879722017-05-17 21:39:54 -070059 # the config module can be initialized only one
60 if INITIALIZED:
61 raise Exception('[XOS-Config] Module already initialized')
62 INITIALIZED = True
63
Matteo Scandolo1879ce72017-05-30 15:45:26 -070064 # 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 Scandolo56879722017-05-17 21:39:54 -070073
Matteo Scandoloc59372a2018-06-11 15:17:40 -070074 # 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 Williams37845ca2017-07-18 15:39:20 -070079
Matteo Scandolo56879722017-05-17 21:39:54 -070080 # 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 Scandolo1879ce72017-05-30 15:45:26 -070083 config_schema = Config.get_cli_param(sys.argv)
Matteo Scandolo56879722017-05-17 21:39:54 -070084
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070085
86 CONFIG_FILE = config_file
Matteo Scandolo1879ce72017-05-30 15:45:26 -070087 CONFIG = Config.read_config(config_file, config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -070088
Matteo Scandoloc59372a2018-06-11 15:17:40 -070089 # 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 Williams37845ca2017-07-18 15:39:20 -070092
Matteo Scandolo56879722017-05-17 21:39:54 -070093 @staticmethod
Matteo Scandoloe0fc6852017-06-07 16:01:54 -070094 def get_config_file():
95 return CONFIG_FILE
96
97 @staticmethod
Matteo Scandolo56879722017-05-17 21:39:54 -070098 def clear():
99 global INITIALIZED
100 INITIALIZED = False
101
102 @staticmethod
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700103 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 Scandolo56879722017-05-17 21:39:54 -0700111 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 Williams37845ca2017-07-18 15:39:20 -0700123 def read_config(config_file, config_schema, ignore_if_not_found=False):
Matteo Scandolo56879722017-05-17 21:39:54 -0700124 """
125 Read the configuration file and return a dictionary
126 :param config_file: string
127 :return: dict
128 """
Zack Williams37845ca2017-07-18 15:39:20 -0700129
130 if(not os.path.exists(config_file) and ignore_if_not_found):
131 return {}
132
Matteo Scandolo56879722017-05-17 21:39:54 -0700133 if not os.path.exists(config_file):
134 raise Exception('[XOS-Config] Config file not found at: %s' % config_file)
135
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700136 if not os.path.exists(config_schema):
137 raise Exception('[XOS-Config] Config schema not found at: %s' % config_schema)
138
Matteo Scandolo56879722017-05-17 21:39:54 -0700139 try:
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700140 Config.validate_config_format(config_file, config_schema)
Matteo Scandolo56879722017-05-17 21:39:54 -0700141 except Exception, e:
Scott Baker1f6a32c2017-11-22 11:24:01 -0800142 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 Scandolo56879722017-05-17 21:39:54 -0700147
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 Scandoloc59372a2018-06-11 15:17:40 -0700160 global OVERRIDE_CONFIG
161 global OVERRIDE_CONFIG_FILE
Matteo Scandolo56879722017-05-17 21:39:54 -0700162
163 if not INITIALIZED:
164 raise Exception('[XOS-Config] Module has not been initialized')
165
166 val = Config.get_param(query, CONFIG)
Matteo Scandoloc59372a2018-06-11 15:17:40 -0700167 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 Williams37845ca2017-07-18 15:39:20 -0700173 if not val:
Matteo Scandolo56879722017-05-17 21:39:54 -0700174 val = Config.get_param(query, default.DEFAULT_VALUES)
175 if not val:
Matteo Scandolo1879ce72017-05-30 15:45:26 -0700176 # 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 Scandolo56879722017-05-17 21:39:54 -0700179 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 Williams37845ca2017-07-18 15:39:20 -0700201
Matteo Scandolo56879722017-05-17 21:39:54 -0700202 :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 Scandolo56879722017-05-17 21:39:54 -0700213if __name__ == '__main__':
Zack Williams37845ca2017-07-18 15:39:20 -0700214 Config.init()