From 8d33b4b09928970df50cdd2145766d82c04f6745 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Thu, 29 Oct 2020 08:04:15 +0000 Subject: [PATCH] workaround for broken salt bg scripts --- api/tacticalrmm/agents/views.py | 25 +++++++++---------------- api/tacticalrmm/scripts/tasks.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 api/tacticalrmm/scripts/tasks.py diff --git a/api/tacticalrmm/agents/views.py b/api/tacticalrmm/agents/views.py index 4241e7ea..d53d86ae 100644 --- a/api/tacticalrmm/agents/views.py +++ b/api/tacticalrmm/agents/views.py @@ -37,6 +37,7 @@ from winupdate.serializers import WinUpdatePolicySerializer from .tasks import uninstall_agent_task, send_agent_update_task from winupdate.tasks import bulk_check_for_updates_task +from scripts.tasks import run_script_bg_task from tacticalrmm.utils import notify_error @@ -709,22 +710,14 @@ def run_script(request): return notify_error(str(r)) else: - r = agent.salt_api_async( - func="win_agent.run_script", - kwargs={ - "filepath": script.filepath, - "filename": script.filename, - "shell": script.shell, - "timeout": request.data["timeout"], - "args": args, - "bg": True, - }, - ) - - if r != "timeout": - return Response(f"{script.name} will now be run on {agent.hostname}") - else: - return notify_error("Something went wrong") + data = { + "agentpk": agent.pk, + "scriptpk": script.pk, + "timeout": request.data["timeout"], + "args": args, + } + run_script_bg_task.delay(data) + return Response(f"{script.name} will now be run on {agent.hostname}") @api_view() diff --git a/api/tacticalrmm/scripts/tasks.py b/api/tacticalrmm/scripts/tasks.py new file mode 100644 index 00000000..a9817139 --- /dev/null +++ b/api/tacticalrmm/scripts/tasks.py @@ -0,0 +1,20 @@ +from tacticalrmm.celery import app +from agents.models import Agent +from .models import Script + + +@app.task +def run_script_bg_task(data): + agent = Agent.objects.get(pk=data["agentpk"]) + script = Script.objects.get(pk=data["scriptpk"]) + + agent.salt_api_async( + func="win_agent.run_script", + kwargs={ + "filepath": script.filepath, + "filename": script.filename, + "shell": script.shell, + "timeout": data["timeout"], + "args": data["args"], + }, + )