testing remotely update agents

This commit is contained in:
wh1te909 2019-11-17 05:51:34 +00:00
parent 83848b77dd
commit a583ae70c2
3 changed files with 106 additions and 8 deletions

View File

@ -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"
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"

View File

@ -14,4 +14,5 @@ urlpatterns = [
path("uninstallagent/", views.uninstall_agent),
path("editagent/", views.edit_agent),
path("<pk>/geteventlog/<logtype>/<days>/", views.get_event_log),
path("<pk>/updateagent/", views.update_agent),
]

View File

@ -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"]