From a583ae70c227a4299c91c7d4285e4f3db9790c63 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Sun, 17 Nov 2019 05:51:34 +0000 Subject: [PATCH] testing remotely update agents --- api/djangormm/agents/tasks.py | 101 +++++++++++++++++++++++++++++++--- api/djangormm/agents/urls.py | 1 + api/djangormm/agents/views.py | 12 +++- 3 files changed, 106 insertions(+), 8 deletions(-) diff --git a/api/djangormm/agents/tasks.py b/api/djangormm/agents/tasks.py index 6690aa77..231e364b 100644 --- a/api/djangormm/agents/tasks.py +++ b/api/djangormm/agents/tasks.py @@ -41,12 +41,9 @@ def sync_salt_modules_task(pk): logger.error(f"Unable to contact agent {agent.hostname}: {f}") return f"Unable to contact agent {agent.hostname}: {f}" else: - if data2["return"][0][agent.hostname]: - logger.info(f"Successfully synced salt modules on {agent.hostname}") - return f"Successfully synced salt modules on {agent.hostname}" - else: - logger.critical(f"Failed to sync salt modules on {agent.hostname}") - return f"Failed to sync salt modules on {agent.hostname}" + # TODO fix return type + logger.info(f"Successfully synced salt modules on {agent.hostname}") + return f"Successfully synced salt modules on {agent.hostname}" @@ -80,4 +77,94 @@ def uninstall_agent_task(pk, wait=True): ) logger.info(f"{agent.hostname} was successfully uninstalled") - return f"{agent.hostname} was successfully uninstalled" \ No newline at end of file + return f"{agent.hostname} was successfully uninstalled" + + +@app.task +def update_agent_task(pk, version="0.2.0"): + app_dir = "C:\\Program Files\\TacticalAgent" + temp_dir = "C:\\Windows\\Temp" + agent = Agent.objects.get(pk=pk) + logger.info(f"{agent.hostname} is updating") + + cp_installer = agent.salt_api_cmd( + hostname=agent.hostname, + timeout=120, + func="cp.get_file", + arg=[f"salt://scripts/winagent-{version}.exe", temp_dir] + ) + cp_resp = cp_installer.json() + if not cp_resp["return"][0][agent.hostname]: + logger.error(f"{agent.hostname} unable to copy installer") + return f"{agent.hostname} unable to copy installer" + + + resp1 = agent.salt_api_cmd( + hostname=agent.hostname, + timeout=45, + func="cmd.script", + arg=f"{app_dir}\\nssm.exe", + kwargs={"args": "stop tacticalagent"} + ) + data1 = resp1.json() + if not data1["return"][0][agent.hostname]: + logger.error(f"{agent.hostname} unable to stop tacticalagent service") + return f"{agent.hostname} unable to stop tacticalagent service" + + + resp2 = agent.salt_api_cmd( + hostname=agent.hostname, + timeout=45, + func="cmd.script", + arg=f"{app_dir}\\nssm.exe", + kwargs={"args": "stop checkrunner"} + ) + data2 = resp2.json() + if not data2["return"][0][agent.hostname]: + logger.error(f"{agent.hostname} unable to stop checkrunner service") + return f"{agent.hostname} unable to stop checkrunner service" + + + resp3 = agent.salt_api_cmd( + hostname=agent.hostname, + timeout=120, + func="cmd.script", + arg=f"{temp_dir}\\winagent-{version}.exe", + kwargs={"args": "/VERYSILENT /SUPPRESSMSGBOXES"} + ) + data3 = resp3.json() + if not data3["return"][0][agent.hostname]: + logger.error(f"{agent.hostname} unable to install the update") + return f"{agent.hostname} unable to install the update" + + + resp4 = agent.salt_api_cmd( + hostname=agent.hostname, + timeout=45, + func="cmd.script", + arg=f"{app_dir}\\nssm.exe", + kwargs={"args": "start tacticalagent"} + ) + data4 = resp4.json() + if not data4["return"][0][agent.hostname]: + logger.error(f"{agent.hostname} unable to start tacticalagent service") + return f"{agent.hostname} unable to start tacticalagent service" + + + resp5 = agent.salt_api_cmd( + hostname=agent.hostname, + timeout=45, + func="cmd.script", + arg=f"{app_dir}\\nssm.exe", + kwargs={"args": "start checkrunner"} + ) + data5 = resp5.json() + if not data5["return"][0][agent.hostname]: + logger.error(f"{agent.hostname} unable to start checkrunner service") + return f"{agent.hostname} unable to start checkrunner service" + + + + + logger.info(f"{agent.hostname} was successfully updated") + return f"{agent.hostname} was successfully updated" \ No newline at end of file diff --git a/api/djangormm/agents/urls.py b/api/djangormm/agents/urls.py index cbc608e9..3bdcbd0a 100644 --- a/api/djangormm/agents/urls.py +++ b/api/djangormm/agents/urls.py @@ -14,4 +14,5 @@ urlpatterns = [ path("uninstallagent/", views.uninstall_agent), path("editagent/", views.edit_agent), path("/geteventlog///", views.get_event_log), + path("/updateagent/", views.update_agent), ] \ No newline at end of file diff --git a/api/djangormm/agents/views.py b/api/djangormm/agents/views.py index f683a40c..d6cd6836 100644 --- a/api/djangormm/agents/views.py +++ b/api/djangormm/agents/views.py @@ -15,10 +15,20 @@ from rest_framework.authentication import BasicAuthentication, TokenAuthenticati from .models import Agent from .serializers import AgentSerializer -from .tasks import uninstall_agent_task +from .tasks import uninstall_agent_task, update_agent_task logger.configure(**settings.LOG_CONFIG) +@api_view() +@permission_classes([]) +@authentication_classes([]) +def update_agent(request, pk): + agent = get_object_or_404(Agent, pk=pk) + update_agent_task.delay(agent.pk) + + return Response(f"updating {agent.hostname}") + + @api_view(["DELETE"]) def uninstall_agent(request): pk = request.data["pk"]