From 8842fb1b441ed4d2ef441a71a5adc585465d765e Mon Sep 17 00:00:00 2001 From: Hemna Date: Wed, 10 Nov 2021 11:52:51 -0500 Subject: [PATCH] Healthcheck command doesn't need the aprsd.yml config This patch updates the healthcheck command to not require the aprsd.yml config file to exist. The healthcheck calls a running aprsd, collects the stats to determine if it's healthy. --- aprsd/cli_helper.py | 21 +++++++++++++++++++++ aprsd/cmds/healthcheck.py | 3 +-- aprsd/log.py | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/aprsd/cli_helper.py b/aprsd/cli_helper.py index ab8bf7e..45a5b94 100644 --- a/aprsd/cli_helper.py +++ b/aprsd/cli_helper.py @@ -65,3 +65,24 @@ def process_standard_options(f: F) -> F: return f(*args, **kwargs) return update_wrapper(t.cast(F, new_func), f) + + +def process_standard_options_no_config(f: F) -> F: + """Use this as a decorator when config isn't needed.""" + def new_func(*args, **kwargs): + ctx = args[0] + ctx.ensure_object(dict) + ctx.obj["loglevel"] = kwargs["loglevel"] + ctx.obj["config_file"] = kwargs["config_file"] + ctx.obj["quiet"] = kwargs["quiet"] + log.setup_logging_no_config( + ctx.obj["loglevel"], + ctx.obj["quiet"], + ) + + del kwargs["loglevel"] + del kwargs["config_file"] + del kwargs["quiet"] + return f(*args, **kwargs) + + return update_wrapper(t.cast(F, new_func), f) diff --git a/aprsd/cmds/healthcheck.py b/aprsd/cmds/healthcheck.py index 333dd05..8b57ace 100644 --- a/aprsd/cmds/healthcheck.py +++ b/aprsd/cmds/healthcheck.py @@ -40,10 +40,9 @@ LOG = logging.getLogger("APRSD") help="How long to wait for healtcheck url to come back", ) @click.pass_context -@cli_helper.process_standard_options +@cli_helper.process_standard_options_no_config def healthcheck(ctx, health_url, timeout): """Check the health of the running aprsd server.""" - ctx.obj["config"] LOG.debug(f"APRSD HealthCheck version: {aprsd.__version__}") try: diff --git a/aprsd/log.py b/aprsd/log.py index 71ecf2c..a6ad1de 100644 --- a/aprsd/log.py +++ b/aprsd/log.py @@ -51,3 +51,20 @@ def setup_logging(config, loglevel, quiet): LOG.addHandler(sh) if imap_logger: imap_logger.addHandler(sh) + + +def setup_logging_no_config(loglevel, quiet): + log_level = aprsd_config.LOG_LEVELS[loglevel] + LOG.setLevel(log_level) + log_format = aprsd_config.DEFAULT_LOG_FORMAT + date_format = aprsd_config.DEFAULT_DATE_FORMAT + log_formatter = logging.Formatter(fmt=log_format, datefmt=date_format) + fh = NullHandler() + + fh.setFormatter(log_formatter) + LOG.addHandler(fh) + + if not quiet: + sh = logging.StreamHandler(sys.stdout) + sh.setFormatter(log_formatter) + LOG.addHandler(sh)