1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 01:16:10 +00:00
webvirtcloud/appsettings/migrations/0027_alert_settings.py
catborise dcd7e32df7 feat(alerts): mail and webhook alerts, and /healthz for a monitor
A failed backup, a scheduled backup not taken, backups the daily check
finds missing and a job that ends uncertain could only be seen on the
pages. They are now queued as alerts in the transaction of the change
they tell of (never stopping it) and sent by the worker's periodic pass
to the Settings page's alert e-mail addresses (the server's SMTP
settings) and alert webhook (a JSON POST), each tried again for ten
passes on its own. A skip for a host in maintenance is no alert.
Setting values take 255 characters. /healthz answers 200 while the
database answers and the job worker runs, 503 otherwise, without a
login.
2026-11-19 16:10:37 +03:00

35 lines
1.5 KiB
Python

from django.core.management.color import no_style
from django.db import migrations, models
from django.utils.translation import gettext_noop as _
SETTINGS = (
(_("Alert e-mail addresses"), "ALERT_EMAILS",
_("Comma-separated; mailed when a backup fails, is skipped, missing or damaged, or a job is uncertain")),
(_("Alert webhook"), "ALERT_WEBHOOK", _("An http(s) URL that gets the same alerts as a JSON POST (subject, text, time)")),
)
def add_settings(apps, schema_editor):
setting = apps.get_model("appsettings", "AppSettings")
db_alias = schema_editor.connection.alias
# see 0014: move a PostgreSQL id sequence past the fixed ids first
with schema_editor.connection.cursor() as cursor:
for sql in schema_editor.connection.ops.sequence_reset_sql(no_style(), [setting]):
cursor.execute(sql)
for name, key, description in SETTINGS:
setting.objects.using(db_alias).get_or_create(
key=key, defaults={"name": name, "value": "", "choices": "", "description": description})
def del_settings(apps, schema_editor):
apps.get_model("appsettings", "AppSettings").objects.using(schema_editor.connection.alias).filter(
key__in=[key for _name, key, _description in SETTINGS]).delete()
class Migration(migrations.Migration):
dependencies = [("appsettings", "0026_repair_number_settings")]
operations = [
migrations.AlterField("appsettings", "value", models.CharField(max_length=255, verbose_name="value")),
migrations.RunPython(add_settings, del_settings),
]