Matteo Scandolo | 5687972 | 2017-05-17 21:39:54 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | import yaml |
| 4 | import requests |
| 5 | import default |
| 6 | from pykwalify.core import Core as PyKwalify |
| 7 | |
| 8 | DEFAULT_CONFIG_FILE = "/opt/xos/config.yaml" |
| 9 | INITIALIZED = False |
| 10 | CONFIG = {} |
| 11 | |
| 12 | class Config: |
| 13 | """ |
| 14 | XOS Configuration APIs |
| 15 | """ |
| 16 | |
| 17 | @staticmethod |
| 18 | def init(config_file=DEFAULT_CONFIG_FILE): |
| 19 | global INITIALIZED |
| 20 | global CONFIG |
| 21 | # the config module can be initialized only one |
| 22 | if INITIALIZED: |
| 23 | raise Exception('[XOS-Config] Module already initialized') |
| 24 | INITIALIZED = True |
| 25 | |
| 26 | # if XOS-CONFIG is defined override the config_file |
| 27 | if os.environ.get('XOS-CONFIG'): |
| 28 | config_file = os.environ['XOS-CONFIG'] |
| 29 | |
| 30 | # if a -C parameter is set in the cli override the config_file |
| 31 | # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method |
| 32 | if Config.get_cli_param(sys.argv): |
| 33 | config_file = Config.get_cli_param(sys.argv) |
| 34 | |
| 35 | CONFIG = Config.read_config(config_file) |
| 36 | |
| 37 | @staticmethod |
| 38 | def clear(): |
| 39 | global INITIALIZED |
| 40 | INITIALIZED = False |
| 41 | |
| 42 | @staticmethod |
| 43 | def validate_config_format(config_file): |
| 44 | schema = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/config-schema.yaml') |
| 45 | c = PyKwalify(source_file=config_file, schema_files=[schema]) |
| 46 | c.validate(raise_exception=True) |
| 47 | |
| 48 | @staticmethod |
| 49 | def get_cli_param(args): |
| 50 | last = None |
| 51 | for arg in args: |
| 52 | if last == '-C': |
| 53 | return arg |
| 54 | last = arg |
| 55 | |
| 56 | @staticmethod |
| 57 | def read_config(config_file): |
| 58 | """ |
| 59 | Read the configuration file and return a dictionary |
| 60 | :param config_file: string |
| 61 | :return: dict |
| 62 | """ |
| 63 | if not os.path.exists(config_file): |
| 64 | raise Exception('[XOS-Config] Config file not found at: %s' % config_file) |
| 65 | |
| 66 | try: |
| 67 | Config.validate_config_format(config_file) |
| 68 | except Exception, e: |
| 69 | raise Exception('[XOS-Config] The config format is wrong: %s' % e.msg) |
| 70 | |
| 71 | with open(config_file, 'r') as stream: |
| 72 | return yaml.safe_load(stream) |
| 73 | |
| 74 | @staticmethod |
| 75 | def get(query): |
| 76 | """ |
| 77 | Read a parameter from the config |
| 78 | :param query: a dot separated selector for configuration options (eg: database.username) |
| 79 | :return: the requested parameter in any format the parameter is specified |
| 80 | """ |
| 81 | global INITIALIZED |
| 82 | global CONFIG |
| 83 | |
| 84 | if not INITIALIZED: |
| 85 | raise Exception('[XOS-Config] Module has not been initialized') |
| 86 | |
| 87 | val = Config.get_param(query, CONFIG) |
| 88 | if not val: |
| 89 | val = Config.get_param(query, default.DEFAULT_VALUES) |
| 90 | if not val: |
| 91 | raise Exception('[XOS-Config] Config does not have a value (or a default) parameter %s' % query) |
| 92 | return val |
| 93 | |
| 94 | @staticmethod |
| 95 | def get_param(query, config): |
| 96 | """ |
| 97 | Search for a parameter in config's first level, other call get_nested_param |
| 98 | :param query: a dot separated selector for configuration options (eg: database.username) |
| 99 | :param config: the config source to read from (can be the config file or the defaults) |
| 100 | :return: the requested parameter in any format the parameter is specified |
| 101 | """ |
| 102 | keys = query.split('.') |
| 103 | if len(keys) == 1: |
| 104 | key = keys[0] |
| 105 | if not config.has_key(key): |
| 106 | return None |
| 107 | return config[key] |
| 108 | else: |
| 109 | return Config.get_nested_param(keys, config) |
| 110 | |
| 111 | @staticmethod |
| 112 | def get_nested_param(keys, config): |
| 113 | """ |
| 114 | |
| 115 | :param keys: a list of descending selector |
| 116 | :param config: the config source to read from (can be the config file or the defaults) |
| 117 | :return: the requested parameter in any format the parameter is specified |
| 118 | """ |
| 119 | param = config |
| 120 | for k in keys: |
| 121 | if not param.has_key(k): |
| 122 | return None |
| 123 | param = param[k] |
| 124 | return param |
| 125 | |
| 126 | @staticmethod |
| 127 | def get_service_list(): |
| 128 | """ |
| 129 | Query registrator to get the list of services |
| 130 | NOTE: we assume that consul is a valid URL |
| 131 | :return: a list of service names |
| 132 | """ |
| 133 | service_dict = requests.get('http://consul:8500/v1/catalog/services').json() |
| 134 | service_list = [] |
| 135 | for s in service_dict: |
| 136 | service_list.append(s) |
| 137 | return service_list |
| 138 | |
| 139 | @staticmethod |
| 140 | def get_service_info(service_name): |
| 141 | """ |
| 142 | Query registrator to get the details about a service |
| 143 | NOTE: we assume that consul is a valid URL |
| 144 | :param service_name: the name of the service, can be retrieved from get_service_list |
| 145 | :return: the informations about a service |
| 146 | """ |
| 147 | response = requests.get('http://consul:8500/v1/catalog/service/%s' % service_name) |
| 148 | if not response.ok: |
| 149 | raise Exception('[XOS-Config] Registrator is down') |
| 150 | service = response.json() |
| 151 | if not service or len(service) == 0: |
| 152 | raise Exception('[XOS-Config] The service missing-service looking for does not exist') |
| 153 | return { |
| 154 | 'name': service[0]['ServiceName'], |
| 155 | 'url': service[0]['ServiceAddress'], |
| 156 | 'port': service[0]['ServicePort'] |
| 157 | } |
| 158 | |
| 159 | @staticmethod |
| 160 | def get_service_endpoint(service_name): |
| 161 | """ |
| 162 | Query registrator to get the details about a service and return the endpoint in for of a string |
| 163 | :param service_name: the name of the service, can be retrieved from get_service_list |
| 164 | :return: the endpoint of the service |
| 165 | """ |
| 166 | service = Config.get_service_info(service_name) |
| 167 | return 'http://%s:%s' % (service['url'], service['port']) |
| 168 | |
| 169 | # NOTE is this needed if this package is not meant to be execute from the CLI? |
| 170 | if __name__ == '__main__': |
| 171 | config = Config() |
| 172 | config.init() |