Add system reboot and shutdown controls
This commit is contained in:
@@ -321,6 +321,24 @@ def password_change(username):
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
def system_power(action):
|
||||
commands = {
|
||||
'reboot': '/usr/bin/systemctl',
|
||||
'poweroff': '/usr/bin/systemctl',
|
||||
}
|
||||
if action not in commands:
|
||||
return 2
|
||||
|
||||
subprocess.run(
|
||||
[commands[action], '--no-block', action],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
return 0
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3: return 2
|
||||
action, value = sys.argv[1:]
|
||||
@@ -334,6 +352,7 @@ def main():
|
||||
if value not in {'yes', 'no'}: return 2
|
||||
run('/usr/bin/timedatectl', 'set-ntp', 'true' if value == 'yes' else 'false'); return 0
|
||||
if action == 'password-change': return password_change(value)
|
||||
if action == 'system-power': return system_power(value)
|
||||
if action == 'network-read': return network_read(value)
|
||||
if action == 'network-status': return network_status()
|
||||
if action == 'network-apply': return network_apply(value)
|
||||
|
||||
@@ -373,6 +373,39 @@ async def update_admin_password(
|
||||
return RedirectResponse("/admin/login?result=password-changed", status_code=303)
|
||||
|
||||
|
||||
|
||||
@app.post("/admin/system/reboot")
|
||||
async def admin_reboot(request: Request, csrf_token: str = Form(...)):
|
||||
if not admin_session_valid(request):
|
||||
return RedirectResponse("/admin/login", status_code=303)
|
||||
|
||||
session_token = request.session.get("password_csrf", "")
|
||||
if not session_token or not hmac.compare_digest(csrf_token, session_token):
|
||||
return RedirectResponse("/admin?error=system-csrf", status_code=303)
|
||||
|
||||
ok, _ = run_helper("system-power", "reboot")
|
||||
if not ok:
|
||||
return RedirectResponse("/admin?error=system-power", status_code=303)
|
||||
|
||||
return RedirectResponse("/admin", status_code=303)
|
||||
|
||||
|
||||
@app.post("/admin/system/poweroff")
|
||||
async def admin_poweroff(request: Request, csrf_token: str = Form(...)):
|
||||
if not admin_session_valid(request):
|
||||
return RedirectResponse("/admin/login", status_code=303)
|
||||
|
||||
session_token = request.session.get("password_csrf", "")
|
||||
if not session_token or not hmac.compare_digest(csrf_token, session_token):
|
||||
return RedirectResponse("/admin?error=system-csrf", status_code=303)
|
||||
|
||||
ok, _ = run_helper("system-power", "poweroff")
|
||||
if not ok:
|
||||
return RedirectResponse("/admin?error=system-power", status_code=303)
|
||||
|
||||
return RedirectResponse("/admin", status_code=303)
|
||||
|
||||
|
||||
@app.post("/admin/settings/network")
|
||||
async def update_network(
|
||||
request: Request,
|
||||
|
||||
@@ -275,6 +275,30 @@
|
||||
</div>
|
||||
</details>
|
||||
|
||||
<details class="card bg-dark border-secondary mb-4">
|
||||
<summary class="card-header bg-transparent text-light fw-semibold">
|
||||
<h2 class="h5 mb-0"><i class="bi bi-power text-warning me-2"></i>Управление станцией</h2></summary>
|
||||
<div class="card-body p-4">
|
||||
<p class="text-secondary small">Перезагрузка или полное выключение сервера.</p>
|
||||
<div class="d-flex flex-wrap gap-2">
|
||||
<form method="post" action="/admin/system/reboot"
|
||||
onsubmit="return confirm('Перезагрузить станцию? Все активные задачи будут прерваны.')">
|
||||
<input type="hidden" name="csrf_token" value="{{ password_csrf }}">
|
||||
<button class="btn btn-outline-warning" type="submit">
|
||||
<i class="bi bi-arrow-clockwise me-1"></i>Перезагрузить
|
||||
</button>
|
||||
</form>
|
||||
<form method="post" action="/admin/system/poweroff"
|
||||
onsubmit="return confirm('Выключить станцию? Для повторного запуска потребуется физически включить сервер.')">
|
||||
<input type="hidden" name="csrf_token" value="{{ password_csrf }}">
|
||||
<button class="btn btn-outline-danger" type="submit">
|
||||
<i class="bi bi-power me-1"></i>Выключить
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
|
||||
<details class="card bg-dark border-secondary mb-4">
|
||||
<summary class="card-header bg-transparent text-light fw-semibold">
|
||||
<h2 class="h5 mb-0"><i class="bi bi-ethernet text-info me-2"></i>Сеть</h2></summary>
|
||||
|
||||
Reference in New Issue
Block a user