Send separate zone update diffs on succses
This commit is contained in:
1 parent
a5c06bdce5
commit
a2fcc7321d
2 files changed
+39
-29
No files matched your search
@@ -55,26 +55,25 @@ class Command(BaseCommand):
|
|||||||
service.sync_all_zones()
|
service.sync_all_zones()
|
||||||
|
|
||||||
if service.changes and not debug:
|
if service.changes and not debug:
|
||||||
errors = "\n\n\n".join(
|
self.reload_and_notify(service, service.changes)
|
||||||
[v["errors"] for v in service.changes.values() if v["errors"]]
|
|
||||||
)
|
|
||||||
diffs = "\n\n\n".join(
|
|
||||||
[v["diff"] for v in service.changes.values() if v["diff"]]
|
|
||||||
)
|
|
||||||
zones = list(service.changes.keys())
|
|
||||||
self.reload_and_notify(service, errors, diffs, zones)
|
|
||||||
|
|
||||||
def reload_and_notify(self, service, errors, diffs, zones):
|
def reload_and_notify(self, service, changes):
|
||||||
self.stdout.write("reloading zones... ")
|
self.stdout.write("reloading zones... ")
|
||||||
success, failed_command, output = service.run_reload_commands()
|
success, failed_command, output = service.run_reload_commands()
|
||||||
|
|
||||||
|
zones = list(changes.keys())
|
||||||
zone_list = "\n".join("- " + z for z in zones)
|
zone_list = "\n".join("- " + z for z in zones)
|
||||||
|
|
||||||
if not success:
|
if not success:
|
||||||
self.stdout.write("fail\n")
|
self.stdout.write("fail\n")
|
||||||
|
errors = "\n\n\n".join(
|
||||||
|
[v["errors"] for v in changes.values() if v["errors"]]
|
||||||
|
)
|
||||||
|
diffs = "\n\n\n".join([v["diff"] for v in changes.values() if v["diff"]])
|
||||||
message = (
|
message = (
|
||||||
"Failed to reload zones, please inspect...\n\n"
|
"Failed to reload zones, please inspect...\n\n"
|
||||||
"%s@%s$ %s\n%s\n%s" % (os.getlogin(), os.uname()[1], failed_command, output, zone_list)
|
"%s@%s$ %s\n%s\n%s"
|
||||||
|
% (os.getlogin(), os.uname()[1], failed_command, output, zone_list)
|
||||||
)
|
)
|
||||||
if errors:
|
if errors:
|
||||||
message += "\n\n" + errors
|
message += "\n\n" + errors
|
||||||
@@ -86,11 +85,12 @@ class Command(BaseCommand):
|
|||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
|
|
||||||
self.stdout.write("ok\n")
|
self.stdout.write("ok\n")
|
||||||
message = "Successfully updated bind zone files for\n" + zone_list
|
for zone, info in changes.items():
|
||||||
if errors:
|
message = "Successfully updated bind zone file for `%s`" % zone
|
||||||
message += "\n\n" + errors
|
if info["errors"]:
|
||||||
discord.notify(
|
message += "\n\n" + info["errors"]
|
||||||
subject="✅ Success: bind",
|
discord.notify(
|
||||||
message=message,
|
subject="✅ Success: bind",
|
||||||
preformatted=diffs or None,
|
message=message,
|
||||||
)
|
preformatted=info["diff"] or None,
|
||||||
|
)
|
||||||
@@ -24,6 +24,7 @@ def make_service(changes=None):
|
|||||||
|
|
||||||
# --- run_reload_commands ---
|
# --- run_reload_commands ---
|
||||||
|
|
||||||
|
|
||||||
def test_run_reload_commands_calls_all_on_success(service):
|
def test_run_reload_commands_calls_all_on_success(service):
|
||||||
with patch("mdb.services.zone_sync.getstatusoutput", return_value=(0, "")) as mock:
|
with patch("mdb.services.zone_sync.getstatusoutput", return_value=(0, "")) as mock:
|
||||||
success, _, _ = service.run_reload_commands()
|
success, _, _ = service.run_reload_commands()
|
||||||
@@ -45,26 +46,34 @@ def test_run_reload_commands_stops_on_first_failure(service):
|
|||||||
|
|
||||||
# --- reload_and_notify ---
|
# --- reload_and_notify ---
|
||||||
|
|
||||||
|
|
||||||
def test_reload_and_notify_success_writes_ok(cmd):
|
def test_reload_and_notify_success_writes_ok(cmd):
|
||||||
svc = make_service()
|
svc = make_service()
|
||||||
with patch("mdb.management.commands.zone_sync.discord") as mock_discord:
|
with patch("mdb.management.commands.zone_sync.discord"):
|
||||||
cmd.reload_and_notify(svc, errors="", diffs="diff", zones=["example.org"])
|
cmd.reload_and_notify(svc, svc.changes)
|
||||||
assert "ok" in cmd.stdout.getvalue()
|
assert "ok" in cmd.stdout.getvalue()
|
||||||
|
|
||||||
|
|
||||||
def test_reload_and_notify_success_calls_discord(cmd):
|
def test_reload_and_notify_success_calls_discord_once_per_zone(cmd):
|
||||||
svc = make_service()
|
changes = {
|
||||||
|
"example.org": {"errors": None, "diff": "- old\n+ new"},
|
||||||
|
"other.net": {"errors": None, "diff": "- x\n+ y"},
|
||||||
|
}
|
||||||
|
svc = make_service(changes=changes)
|
||||||
with patch("mdb.management.commands.zone_sync.discord") as mock_discord:
|
with patch("mdb.management.commands.zone_sync.discord") as mock_discord:
|
||||||
cmd.reload_and_notify(svc, errors="", diffs="diff", zones=["example.org"])
|
cmd.reload_and_notify(svc, svc.changes)
|
||||||
mock_discord.notify.assert_called_once()
|
assert mock_discord.notify.call_count == 2
|
||||||
assert "✅" in mock_discord.notify.call_args.kwargs["subject"]
|
subjects = [c.kwargs["subject"] for c in mock_discord.notify.call_args_list]
|
||||||
assert "- example.org" in mock_discord.notify.call_args.kwargs["message"]
|
messages = [c.kwargs["message"] for c in mock_discord.notify.call_args_list]
|
||||||
|
assert all("✅" in s for s in subjects)
|
||||||
|
assert any("`example.org`" in m for m in messages)
|
||||||
|
assert any("`other.net`" in m for m in messages)
|
||||||
|
|
||||||
|
|
||||||
def test_reload_and_notify_success_passes_diff_as_preformatted(cmd):
|
def test_reload_and_notify_success_passes_diff_as_preformatted(cmd):
|
||||||
svc = make_service()
|
svc = make_service()
|
||||||
with patch("mdb.management.commands.zone_sync.discord") as mock_discord:
|
with patch("mdb.management.commands.zone_sync.discord") as mock_discord:
|
||||||
cmd.reload_and_notify(svc, errors="", diffs="- old\n+ new", zones=["example.org"])
|
cmd.reload_and_notify(svc, svc.changes)
|
||||||
assert mock_discord.notify.call_args.kwargs["preformatted"] == "- old\n+ new"
|
assert mock_discord.notify.call_args.kwargs["preformatted"] == "- old\n+ new"
|
||||||
|
|
||||||
|
|
||||||
@@ -74,7 +83,7 @@ def test_reload_and_notify_failure_writes_fail_and_exits(cmd):
|
|||||||
with patch("mdb.management.commands.zone_sync.discord"):
|
with patch("mdb.management.commands.zone_sync.discord"):
|
||||||
with patch("os.getlogin", return_value="testuser"):
|
with patch("os.getlogin", return_value="testuser"):
|
||||||
with pytest.raises(SystemExit) as exc:
|
with pytest.raises(SystemExit) as exc:
|
||||||
cmd.reload_and_notify(svc, errors="", diffs="", zones=["example.org"])
|
cmd.reload_and_notify(svc, svc.changes)
|
||||||
assert exc.value.code == 1
|
assert exc.value.code == 1
|
||||||
assert "fail" in cmd.stdout.getvalue()
|
assert "fail" in cmd.stdout.getvalue()
|
||||||
|
|
||||||
@@ -85,7 +94,7 @@ def test_reload_and_notify_failure_discord_includes_command_and_output(cmd):
|
|||||||
with patch("mdb.management.commands.zone_sync.discord") as mock_discord:
|
with patch("mdb.management.commands.zone_sync.discord") as mock_discord:
|
||||||
with patch("os.getlogin", return_value="testuser"):
|
with patch("os.getlogin", return_value="testuser"):
|
||||||
with pytest.raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
cmd.reload_and_notify(svc, errors="", diffs="", zones=["example.org"])
|
cmd.reload_and_notify(svc, svc.changes)
|
||||||
subject = mock_discord.notify.call_args.kwargs["subject"]
|
subject = mock_discord.notify.call_args.kwargs["subject"]
|
||||||
message = mock_discord.notify.call_args.kwargs["message"]
|
message = mock_discord.notify.call_args.kwargs["message"]
|
||||||
assert "⚠️" in subject
|
assert "⚠️" in subject
|
||||||
@@ -96,6 +105,7 @@ def test_reload_and_notify_failure_discord_includes_command_and_output(cmd):
|
|||||||
|
|
||||||
# --- handle ---
|
# --- handle ---
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_handle_exits_if_zone_not_found(cmd):
|
def test_handle_exits_if_zone_not_found(cmd):
|
||||||
with pytest.raises(SystemExit) as exc:
|
with pytest.raises(SystemExit) as exc:
|
||||||
|
|||||||
Reference in new issue
Block a user