From abd767deff24f0f085d990356644f0da39f81d24 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Thu, 29 Oct 2020 20:00:03 +0000 Subject: [PATCH] fixes #154 --- api/tacticalrmm/agents/views.py | 36 ++++++++++++++++++++------------ api/tacticalrmm/scripts/tasks.py | 18 ++++++++++++++++ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/api/tacticalrmm/agents/views.py b/api/tacticalrmm/agents/views.py index d53d86ae..a5984693 100644 --- a/api/tacticalrmm/agents/views.py +++ b/api/tacticalrmm/agents/views.py @@ -37,7 +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 scripts.tasks import run_script_bg_task, run_bulk_script_task from tacticalrmm.utils import notify_error @@ -841,20 +841,30 @@ def bulk(request): elif request.data["mode"] == "script": script = get_object_or_404(Script, pk=request.data["scriptPK"]) - r = Agent.salt_batch_async( - minions=minions, - func="win_agent.run_script", - kwargs={ - "filepath": script.filepath, - "filename": script.filename, - "shell": script.shell, + if script.shell == "python": + r = Agent.salt_batch_async( + minions=minions, + func="win_agent.run_script", + kwargs={ + "filepath": script.filepath, + "filename": script.filename, + "shell": script.shell, + "timeout": request.data["timeout"], + "args": request.data["args"], + "bg": True, + }, + ) + if r == "timeout": + return notify_error("Salt API not running") + else: + data = { + "minions": minions, + "scriptpk": script.pk, "timeout": request.data["timeout"], "args": request.data["args"], - "bg": True, - }, - ) - if r == "timeout": - return notify_error("Salt API not running") + } + run_bulk_script_task.delay(data) + return Response(f"{script.name} will now be run on {len(minions)} agents") elif request.data["mode"] == "install": diff --git a/api/tacticalrmm/scripts/tasks.py b/api/tacticalrmm/scripts/tasks.py index a9817139..d3fa63ec 100644 --- a/api/tacticalrmm/scripts/tasks.py +++ b/api/tacticalrmm/scripts/tasks.py @@ -18,3 +18,21 @@ def run_script_bg_task(data): "args": data["args"], }, ) + + +@app.task +def run_bulk_script_task(data): + # for powershell and batch scripts only, workaround for salt bg script bug + script = Script.objects.get(pk=data["scriptpk"]) + + Agent.salt_batch_async( + minions=data["minions"], + func="win_agent.run_script", + kwargs={ + "filepath": script.filepath, + "filename": script.filename, + "shell": script.shell, + "timeout": data["timeout"], + "args": data["args"], + }, + )