From e505d0768cf099e6670650bd18b6f31d86e9ce99 Mon Sep 17 00:00:00 2001 From: wh1te909 <7434746+wh1te909@users.noreply.github.com> Date: Fri, 22 Nov 2024 19:14:55 +0000 Subject: [PATCH] add server maintenance mode mgmt command --- .../management/commands/server_maint_mode.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 api/tacticalrmm/core/management/commands/server_maint_mode.py diff --git a/api/tacticalrmm/core/management/commands/server_maint_mode.py b/api/tacticalrmm/core/management/commands/server_maint_mode.py new file mode 100644 index 00000000..911b95d0 --- /dev/null +++ b/api/tacticalrmm/core/management/commands/server_maint_mode.py @@ -0,0 +1,50 @@ +import json +import os + +from django.core.management.base import BaseCommand + +from agents.models import Agent + + +class Command(BaseCommand): + help = "Toggle server maintenance mode, preserving existing state" + + def add_arguments(self, parser): + parser.add_argument("--enable", action="store_true") + parser.add_argument("--disable", action="store_true") + parser.add_argument("--force-enable", action="store_true") + parser.add_argument("--force-disable", action="store_true") + + def handle(self, *args, **kwargs): + enable = kwargs["enable"] + disable = kwargs["disable"] + force_enable = kwargs["force_enable"] + force_disable = kwargs["force_disable"] + + home_dir = os.path.expanduser("~") + fp = os.path.join(home_dir, "agents_maint_mode.json") + + if enable: + current = list( + Agent.objects.filter(maintenance_mode=True).values_list("id", flat=True) + ) + + with open(fp, "w") as f: + json.dump(current, f) + + Agent.objects.update(maintenance_mode=True) + + elif disable: + with open(fp, "r") as f: + state = json.load(f) + + Agent.objects.exclude(pk__in=state).update(maintenance_mode=False) + + elif force_enable: + Agent.objects.update(maintenance_mode=True) + + elif force_disable: + if os.path.exists(fp): + os.remove(fp) + + Agent.objects.update(maintenance_mode=False)