From 8072a485a1531566cac194ddd4bb85de8867c3f6 Mon Sep 17 00:00:00 2001 From: Luc Didry Date: Wed, 4 Sep 2024 14:19:51 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E2=80=94=20Add=20command=20to=20te?= =?UTF-8?q?st=20gotify=20configuration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + argos/commands.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++- docs/cli.md | 26 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7db3b83..fad6f94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased] - 💄 — Improve email and gotify notifications +- ✨ — Add command to test gotify configuration ## 0.3.1 diff --git a/argos/commands.py b/argos/commands.py index 5b13764..ffd69f7 100644 --- a/argos/commands.py +++ b/argos/commands.py @@ -607,12 +607,76 @@ async def test_mail(config, domain, severity): notify_by_mail( result, task, - severity="SEVERITY", + severity=severity, old_severity="OLD SEVERITY", config=conf.general.mail, request=_FalseRequest(), ) +@server.command() +@click.option( + "--config", + default="argos-config.yaml", + help="Path of the configuration file. " + "If ARGOS_YAML_FILE environment variable is set, its value will be used instead.", + envvar="ARGOS_YAML_FILE", + callback=validate_config_access, +) +@click.option("--domain", help="Domain for the notification", default="example.org") +@click.option("--severity", help="Severity", default="CRITICAL") +@coroutine +async def test_gotify(config, domain, severity): + """Send a test gotify notification""" + os.environ["ARGOS_YAML_FILE"] = config + + from datetime import datetime + + from argos.logging import set_log_level + from argos.server.alerting import notify_with_gotify + from argos.server.main import read_config + from argos.server.models import Result, Task + + conf = read_config(config) + + if not conf.general.gotify: + click.echo("Gotify notifications are not configured, cannot test", err=True) + sysexit(1) + else: + now = datetime.now() + task = Task( + url=f"https://{domain}", + domain=domain, + check="body-contains", + expected="foo", + frequency=1, + selected_by="test", + selected_at=now, + ) + + result = Result( + submitted_at=now, + status="success", + context={"foo": "bar"}, + task=task, + agent_id="test", + severity="ok", + ) + + class _FalseRequest: + def url_for(*args, **kwargs): + return "/url" + + set_log_level("debug") + notify_with_gotify( + result, + task, + severity=severity, + old_severity="OLD SEVERITY", + config=conf.general.gotify, + request=_FalseRequest(), + ) + + if __name__ == "__main__": cli() diff --git a/docs/cli.md b/docs/cli.md index 67a660d..30cb428 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -85,6 +85,7 @@ Commands: migrate Run database migrations reload-config Load or reload tasks’ configuration start Starts the server (use only for testing or development!) + test-gotify Send a test gotify notification test-mail Send a test email user User management watch-agents Watch agents (to run routinely) @@ -491,3 +492,28 @@ Options: + +#### Test the Gotify settings + +You can verify that your Gotify settings are ok by sending a test notification + + + +```man +Usage: argos server test-gotify [OPTIONS] + + Send a test gotify notification + +Options: + --config TEXT Path of the configuration file. If ARGOS_YAML_FILE + environment variable is set, its value will be used instead. + --domain TEXT Domain for the notification + --severity TEXT Severity + --help Show this message and exit. +``` + +