Restart modem button: silence Gandalf and Uptime Kuma, restart, end both
Lint / Python (flake8) (push) Failing after 15s
Lint / JS (eslint) (push) Successful in 1m43s
Lint / Deploy (push) Skipped
Test / Python Tests (pytest) (push) Failing after 21s
Security / Python Security (bandit) (push) Successful in 2m3s
Lint / Python (flake8) (pull_request) Failing after 15s
Lint / JS (eslint) (pull_request) Failing after 15s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 2m17s
Lint / Notify on failure (push) Successful in 3s
Test / Python Tests (pytest) (pull_request) Successful in 2m58s
Lint / Python (flake8) (push) Failing after 15s
Lint / JS (eslint) (push) Successful in 1m43s
Lint / Deploy (push) Skipped
Test / Python Tests (pytest) (push) Failing after 21s
Security / Python Security (bandit) (push) Successful in 2m3s
Lint / Python (flake8) (pull_request) Failing after 15s
Lint / JS (eslint) (pull_request) Failing after 15s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / Python Security (bandit) (pull_request) Successful in 2m17s
Lint / Notify on failure (push) Successful in 3s
Test / Python Tests (pytest) (pull_request) Successful in 2m58s
Admins get a Restart modem button on the WAN / Fiber panel (needs modem.access_code). After a confirmation it: 1. switches an Uptime Kuma maintenance window on (new kuma.py: logs in over Kuma 2's Socket.IO API and resumes/pauses one pre-made manual window, uptime_kuma.maintenance_id); if Kuma fails it stops and asks whether to go on without it, 2. starts Gandalf maintenance (global suppression, 20 minutes at most), 3. posts the gateway's Restart Device form (ModemAdmin.restart), undoing 1 and 2 if that fails. The monitor waits for the modem to answer with its WAN up and an uptime showing it restarted, then switches Kuma off, ends the suppression and records how long it took; after 30 minutes it ends both regardless. The panel shows the restart in progress and the result for an hour. New dependency: python-socketio[client]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f097e4882c
commit
c2ff072eb2
13 files changed
+488
-10
No files matched your search
+63
-1
@@ -4,7 +4,7 @@ import time
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import os
|
||||
import sys
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -875,3 +875,65 @@ class TestCalmDashboard:
|
||||
with patch.object(gandalf.db, 'get_recent_resolved', return_value=[ev]):
|
||||
html = client.get('/', headers=AUTH).get_data(as_text=True)
|
||||
assert '<details class="g-section resolved-fold">' in html
|
||||
|
||||
|
||||
class TestModemRestart:
|
||||
CFG = dict(CONFIG, modem={'url': 'http://192.0.2.254', 'access_code': 'secret'},
|
||||
uptime_kuma={'url': 'http://kuma.test', 'username': 'u', 'password': 'p', 'maintenance_id': 7})
|
||||
|
||||
def _post(self, client, body=None, headers=AUTH, cfg=None, kuma_error=None, restart=None):
|
||||
admin = MagicMock()
|
||||
if isinstance(restart, Exception):
|
||||
admin.restart.side_effect = restart
|
||||
else:
|
||||
admin.restart.return_value = restart or 'accepted'
|
||||
with patch.object(gandalf, '_cfg', cfg or self.CFG), \
|
||||
patch.object(gandalf.kuma, 'set_maintenance', side_effect=kuma_error) as km, \
|
||||
patch.object(gandalf.modem_api, 'ModemAdmin', return_value=admin):
|
||||
gandalf.db.create_suppression.return_value = 42
|
||||
resp = client.post('/api/modem/restart', json=body if body is not None else {'confirm': True},
|
||||
headers=headers)
|
||||
return resp, km, admin
|
||||
|
||||
def test_admin_only(self, client):
|
||||
resp, _, admin = self._post(client, headers=USER)
|
||||
assert resp.status_code == 403 and not admin.restart.called
|
||||
|
||||
def test_needs_confirm(self, client):
|
||||
resp, km, admin = self._post(client, body={})
|
||||
assert resp.status_code == 400 and not km.called and not admin.restart.called
|
||||
|
||||
def test_needs_the_access_code(self, client):
|
||||
resp, _, admin = self._post(client, cfg=dict(CONFIG, modem={'url': 'http://192.0.2.254'}))
|
||||
assert resp.status_code == 400 and 'access_code' in resp.get_json()['error']
|
||||
|
||||
def test_silences_then_restarts(self, client):
|
||||
resp, km, admin = self._post(client)
|
||||
assert resp.status_code == 200
|
||||
assert resp.get_json() == {'ok': True, 'result': 'accepted', 'kuma': True, 'suppression_id': 42}
|
||||
km.assert_called_once_with(self.CFG['uptime_kuma'], True)
|
||||
args = gandalf.db.create_suppression.call_args.args
|
||||
assert args[:3] == ('all', '', '') and args[4] == 'jared' and args[5] == gandalf.MODEM_RESTART_SILENCE_MIN
|
||||
admin.restart.assert_called_once_with()
|
||||
key, state = gandalf.db.set_state.call_args.args
|
||||
assert key == 'modem_restart' and state['suppression_id'] == 42 and state['kuma'] is True
|
||||
|
||||
def test_kuma_failure_stops_unless_accepted(self, client):
|
||||
resp, _, admin = self._post(client, kuma_error=gandalf.kuma.KumaError('login refused'))
|
||||
assert resp.status_code == 502 and resp.get_json()['kuma_failed'] and not admin.restart.called
|
||||
resp, _, admin = self._post(client, body={'confirm': True, 'without_kuma': True},
|
||||
kuma_error=gandalf.kuma.KumaError('login refused'))
|
||||
assert resp.status_code == 200 and resp.get_json()['kuma'] is False and admin.restart.called
|
||||
|
||||
def test_failed_restart_undoes_the_silence(self, client):
|
||||
resp, km, _ = self._post(client, restart=gandalf.modem_api.ModemUnreachable('timeout'))
|
||||
assert resp.status_code == 502
|
||||
gandalf.db.deactivate_suppression.assert_called_once_with(42)
|
||||
assert km.call_args_list[-1].args == (self.CFG['uptime_kuma'], False)
|
||||
|
||||
def test_one_at_a_time(self, client):
|
||||
pending = json.dumps({'at': time.time() - 60, 'finished_at': None})
|
||||
state = {'modem_restart': pending}
|
||||
with patch.object(gandalf.db, 'get_state', side_effect=lambda k, d=None: state.get(k, d)):
|
||||
resp, _, admin = self._post(client)
|
||||
assert resp.status_code == 409 and not admin.restart.called
|
||||
Reference in new issue
Block a user