From d614443c942baa48dd7e877b9996fc806cec132e Mon Sep 17 00:00:00 2001 From: Arseniy Kuznetsov Date: Mon, 4 Jan 2021 09:47:38 +0100 Subject: [PATCH] cli options --- mktxp/basep.py | 7 ++++-- mktxp/cli/config/.mktxp.conf | 15 +++++++++++++ mktxp/cli/config/config.py | 42 ++++++++++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 mktxp/cli/config/.mktxp.conf diff --git a/mktxp/basep.py b/mktxp/basep.py index da9f5f4..4d70517 100644 --- a/mktxp/basep.py +++ b/mktxp/basep.py @@ -15,6 +15,7 @@ from http.server import HTTPServer from prometheus_client.core import REGISTRY from prometheus_client import MetricsHandler, start_http_server +from mktxp.cli.config.config import config_handler from mktxp.collectors_handler import CollectorsHandler from mktxp.metrics_handler import RouterMetricsHandler @@ -26,10 +27,12 @@ class MKTXPProcessor: def start(): router_metrics_handler = RouterMetricsHandler() REGISTRY.register(CollectorsHandler(router_metrics_handler)) - MKTXPProcessor.run() + port=config_handler.mktxp_port() + MKTXPProcessor.run(port=port) @staticmethod - def run(server_class=HTTPServer, handler_class=MetricsHandler, port=8000): + def run(server_class=HTTPServer, handler_class=MetricsHandler, port = None): server_address = ('', port) httpd = server_class(server_address, handler_class) + print(f'Running HTTP collector server on port {port}') httpd.serve_forever() diff --git a/mktxp/cli/config/.mktxp.conf b/mktxp/cli/config/.mktxp.conf new file mode 100644 index 0000000..41ddb6c --- /dev/null +++ b/mktxp/cli/config/.mktxp.conf @@ -0,0 +1,15 @@ +## Copyright (c) 2020 Arseniy Kuznetsov +## +## This program is free software; you can redistribute it and/or +## modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either version 2 +## of the License, or (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. + + +[MKTXP] + port = 49090 diff --git a/mktxp/cli/config/config.py b/mktxp/cli/config/config.py index bf55df1..4db1326 100755 --- a/mktxp/cli/config/config.py +++ b/mktxp/cli/config/config.py @@ -44,7 +44,6 @@ class MKTXPConfigKeys: FE_WIRELESS_KEY = 'wireless' FE_CAPSMAN_KEY = 'capsman' - # UnRegistered entries placeholder NO_ENTRIES_REGISTERED = 'NoEntriesRegistered' @@ -55,12 +54,16 @@ class MKTXPConfigKeys: # Default ports DEFAULT_API_PORT = 8728 DEFAULT_API_SSL_PORT = 8729 + DEFAULT_MKTXP_PORT = 49090 BOOLEAN_KEYS = [ENABLED_KEY, SSL_KEY, SSL_CERTIFICATE, FE_DHCP_KEY, FE_DHCP_LEASE_KEY, FE_DHCP_POOL_KEY, FE_INTERFACE_KEY, FE_MONITOR_KEY, FE_ROUTE_KEY, FE_WIRELESS_KEY, FE_CAPSMAN_KEY] STR_KEYS = [HOST_KEY, USER_KEY, PASSWD_KEY] + # MKTXP config entry nane + MKTXP_CONFIG_ENTRY_NAME = 'MKTXP' + class ConfigEntry: MKTXPEntry = namedtuple('MKTXPEntry', [MKTXPConfigKeys.ENABLED_KEY, MKTXPConfigKeys.HOST_KEY, MKTXPConfigKeys.PORT_KEY, @@ -121,17 +124,24 @@ class MKTXPConfigHandler: # if needed, stage the user config data self.usr_conf_data_path = os.path.join(self.os_config.mktxp_user_dir_path, 'mktxp.conf') - if not os.path.exists(self.usr_conf_data_path): - # stage from the mktxp conf template - lookup_path = resource_filename(Requirement.parse("mktxp"), "mktxp/cli/config/mktxp.conf") - shutil.copy(lookup_path, self.usr_conf_data_path) + self.mktxp_conf_path = os.path.join(self.os_config.mktxp_user_dir_path, '.mktxp.conf') - self.read_from_disk() + self._create_os_path(self.usr_conf_data_path, 'mktxp/cli/config/mktxp.conf') + self._create_os_path(self.mktxp_conf_path, 'mktxp/cli/config/.mktxp.conf') - def read_from_disk(self): + self._read_from_disk() + + def _read_from_disk(self): ''' (Force-)Read conf data from disk ''' self.config = ConfigObj(self.usr_conf_data_path) + self.mktxp_config = ConfigObj(self.mktxp_conf_path) + + def _create_os_path(self, os_path, resource_path): + if not os.path.exists(os_path): + # stage from the conf templates + lookup_path = resource_filename(Requirement.parse("mktxp"), resource_path) + shutil.copy(lookup_path, os_path) # MKTXP entries @@ -179,6 +189,12 @@ class MKTXPConfigHandler: entry_reader = self._entry_reader(entry_name) return ConfigEntry.MKTXPEntry(**entry_reader) + def mktxp_port(self): + if self.mktxp_config.get(MKTXPConfigKeys.MKTXP_CONFIG_ENTRY_NAME) and \ + self.mktxp_config[MKTXPConfigKeys.MKTXP_CONFIG_ENTRY_NAME].get(MKTXPConfigKeys.PORT_KEY): + return self.mktxp_config[MKTXPConfigKeys.MKTXP_CONFIG_ENTRY_NAME].as_int(MKTXPConfigKeys.PORT_KEY) + return MKTXPConfigKeys.DEFAULT_MKTXP_PORT + # Helpers def _entry_reader(self, entry_name): entry = {} @@ -206,3 +222,15 @@ class MKTXPConfigHandler: # Simplest possible Singleton impl config_handler = MKTXPConfigHandler() + +''' if not os.path.exists(self.usr_conf_data_path): + # stage from the mktxp conf template + lookup_path = resource_filename(Requirement.parse("mktxp"), "mktxp/cli/config/mktxp.conf") + shutil.copy(lookup_path, self.usr_conf_data_path) + + if not os.path.exists(self.mktxp_conf_path): + # stage from the mktxp conf template + lookup_path = resource_filename(Requirement.parse("mktxp"), "mktxp/cli/config/.mktxp.conf") + shutil.copy(lookup_path, self.mktxp_conf_path) + +'''