From 993705abbad6e3ae49f691833bcef1c383d897a3 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Thu, 8 Nov 2018 17:49:57 +0100 Subject: [PATCH] move config loading to its own module --- inventory_provider/config.py | 84 +++++++++++++++++++++++++ inventory_provider/constants.py | 31 --------- inventory_provider/router_interfaces.py | 78 ++++------------------- 3 files changed, 97 insertions(+), 96 deletions(-) create mode 100644 inventory_provider/config.py diff --git a/inventory_provider/config.py b/inventory_provider/config.py new file mode 100644 index 00000000..af13e2f4 --- /dev/null +++ b/inventory_provider/config.py @@ -0,0 +1,84 @@ +import json +import logging +import re + +import jsonschema + +CONFIG_SCHEMA = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "properties": { + "alarms-db": { + "type": "object", + "properties": { + "hostname": {"type": "string"}, + "dbname": {"type": "string"}, + "username": {"type": "string"}, + "password": {"type": "string"} + }, + "required": ["hostname", "dbname", "username", "password"], + "additionalProperties": False + }, + "oid_list.conf": {"type": "string"}, + "routers_community.conf": {"type": "string"}, + "ssh": { + "type": "object", + "properties": { + "private-key": {"type": "string"}, + "known-hosts": {"type": "string"} + }, + "required": ["private-key", "known-hosts"], + "additionalProperties": False + } + }, + "required": ["alarms-db", "oid_list.conf", "routers_community.conf"], + "additionalProperties": False +} + + +def _load_oids(config_file): + """ + :param config_file: file-like object + :return: + """ + result = {} + for line in config_file: + m = re.match(r'^([^=]+)=(.*)\s*$', line) + if m: + result[m.group(1)] = m.group(2) + return result + + +def _load_routers(config_file): + """ + :param config_file: file-like object + :return: + """ + for line in config_file: + m = re.match(r'^([a-z\d]+\.[a-z\d]{3,4}\.[a-z\d]{2}\.(geant|eumedconnect)\d*\.net)\s*=([^,]+)\s*,(.*)\s*$', line) + if not m: + logging.warning("malformed config file line: '%s'" % line.strip()) + continue + yield { + "hostname": m.group(1), + "community": m.group(3), + "address": m.group(4) + } + + + +def load(f): + """ + loads, validates and returns configuration parameters + + :param f: file-like object that produces the config file + :return: + """ + config = json.loads(f.read()) + jsonschema.validate(config, CONFIG_SCHEMA) + with open(config["oid_list.conf"]) as f: + config["oids"] = _load_oids(f) + with open(config["routers_community.conf"]) as f: + config["routers"] = list(_load_routers(f)) + return config + diff --git a/inventory_provider/constants.py b/inventory_provider/constants.py index 8319a4de..6c515645 100644 --- a/inventory_provider/constants.py +++ b/inventory_provider/constants.py @@ -2,34 +2,3 @@ SNMP_LOGGER_NAME = "snmp-logger" THREADING_LOGGER_NAME = "threading-logger" JUNIPER_LOGGER_NAME = "juniper-logger" DATABASE_LOGGER_NAME = "database-logger" - -CONFIG_SCHEMA = { - "$schema": "http://json-schema.org/draft-07/schema#", - "type": "object", - "properties": { - "alarms-db": { - "type": "object", - "properties": { - "hostname": {"type": "string"}, - "dbname": {"type": "string"}, - "username": {"type": "string"}, - "password": {"type": "string"} - }, - "required": ["hostname", "dbname", "username", "password"], - "additionalProperties": False - }, - "oid_list.conf": {"type": "string"}, - "routers_community.conf": {"type": "string"}, - "ssh": { - "type": "object", - "properties": { - "private-key": {"type": "string"}, - "known-hosts": {"type": "string"} - }, - "required": ["private-key", "known-hosts"], - "additionalProperties": False - } - }, - "required": ["alarms-db", "oid_list.conf", "routers_community.conf"], - "additionalProperties": False -} diff --git a/inventory_provider/router_interfaces.py b/inventory_provider/router_interfaces.py index e7f0d1c1..ee82b181 100644 --- a/inventory_provider/router_interfaces.py +++ b/inventory_provider/router_interfaces.py @@ -9,68 +9,13 @@ import jsonschema from inventory_provider import constants from inventory_provider import snmp from inventory_provider import juniper +from inventory_provider import config - - -def _load_oids(config_file): - """ - :param config_file: file-like object - :return: - """ - result = {} - for line in config_file: - m = re.match(r'^([^=]+)=(.*)\s*$', line) - if m: - result[m.group(1)] = m.group(2) - return result - - -def _load_routers(config_file): - """ - :param config_file: file-like object - :return: - """ - for line in config_file: - m = re.match(r'^([a-z\d]+\.[a-z\d]{3,4}\.[a-z\d]{2}\.(geant|eumedconnect)\d*\.net)\s*=([^,]+)\s*,(.*)\s*$', line) - if not m: - logging.warning("malformed config file line: '%s'" % line.strip()) - continue - yield { - "hostname": m.group(1), - "community": m.group(3), - "address": m.group(4) - } - - -def _validate_config(ctx, param, value): - """ - loads, validates and returns configuration parameters - - :param ctx: - :param param: - :param value: - :return: - """ - config = json.loads(value.read()) - jsonschema.validate(config, constants.CONFIG_SCHEMA) - with open(config["oid_list.conf"]) as f: - config["oids"] = _load_oids(f) - with open(config["routers_community.conf"]) as f: - config["routers"] = list(_load_routers(f)) - return config - - - - - - - - -def get_router_interfaces_q(router, config, q): +def get_router_interfaces_q(router, params, q): threading_logger = logging.getLogger(constants.THREADING_LOGGER_NAME) threading_logger.debug("[ENTER>>] get_router_interfaces_q: %r" % router) - q.put(list(snmp.get_router_interfaces(router, config))) + q.put(list(snmp.get_router_interfaces(router, params))) threading_logger.debug("[<<EXIT] get_router_interfaces_q: %r" % router) @@ -122,14 +67,14 @@ def get_router_details(router, params, q): threading_logger.debug("[<<EXIT]get_router_details: %r" % router) -def load_network_details(config): +def load_network_details(params): threading_logger = logging.getLogger(constants.THREADING_LOGGER_NAME) processes = [] - for r in config["routers"]: + for r in params["routers"]: q = Queue() - p = Process(target=get_router_details, args=(r, config, q)) + p = Process(target=get_router_details, args=(r, params, q)) p.start() processes.append({"router": r, "process": p, "queue": q}) @@ -143,17 +88,20 @@ def load_network_details(config): return result +def _validate_config(ctx, param, value): + return config.load(value) + + @click.command() @click.option( - "--config", + "--params", # required=True, type=click.File(), help="Configuration filename", - default=open("config.json"), callback=_validate_config) -def cli(config): - network_details = load_network_details(config) +def cli(params): + network_details = load_network_details(params) filename = "/tmp/router-info.json" logging.debug("writing output to: " + filename) with open(filename, "w") as f: -- GitLab