diff --git a/_modules/win_agent.py b/_modules/win_agent.py index ade4a9d1..08d518a1 100644 --- a/_modules/win_agent.py +++ b/_modules/win_agent.py @@ -16,3 +16,9 @@ def run_python_script(filename, timeout): return __salt__["cmd.run_all"]( "{0} {1}".format(python_bin, file_path), timeout=timeout ) + + +def uninstall_agent(): + remove_exe = os.path.join("C:\\Program Files\\TacticalAgent", "unins000.exe") + __salt__["cmd.run_bg"]([remove_exe, "/VERYSILENT", "/SUPPRESSMSGBOXES"]) + return "ok" diff --git a/api/tacticalrmm/agents/tasks.py b/api/tacticalrmm/agents/tasks.py index 2a0d6cbb..b043bf2d 100644 --- a/api/tacticalrmm/agents/tasks.py +++ b/api/tacticalrmm/agents/tasks.py @@ -44,6 +44,7 @@ def sync_salt_modules_task(pk): @app.task def uninstall_agent_task(pk, wait=True): agent = Agent.objects.get(pk=pk) + salt_id = agent.salt_id agent.uninstall_inprogress = True agent.save(update_fields=["uninstall_inprogress"]) logger.info(f"{agent.hostname} uninstall task is running") @@ -52,26 +53,29 @@ def uninstall_agent_task(pk, wait=True): logger.info(f"{agent.hostname} waiting 90 seconds before uninstalling") sleep(90) # need to give salt time to startup on the minion - resp2 = agent.salt_api_cmd( - hostname=agent.salt_id, - timeout=60, - func="cp.get_file", - arg=["salt://scripts/removeagent.exe", "C:\\Windows\\Temp\\"], - ) - data2 = resp2.json() - if not data2["return"][0][agent.salt_id]: - logger.error(f"{agent.hostname} unable to copy file") - return f"{agent.hostname} unable to copy file" + attempts = 0 + error = False - agent.salt_api_cmd( - hostname=agent.salt_id, - timeout=500, - func="cmd.script", - arg="salt://scripts/uninstall.bat", - ) + while 1: + attempts += 1 + r = agent.salt_api_cmd( + hostname=salt_id, timeout=60, func="win_agent.uninstall_agent" + ) + if r.json()["return"][0][salt_id] != "ok": + if attempts >= 10: + error = True + break + else: + continue + else: + break - logger.info(f"{agent.hostname} was successfully uninstalled") - return f"{agent.hostname} was successfully uninstalled" + if error: + logger.error(f"{salt_id} uninstall failed") + else: + logger.info(f"{salt_id} was successfully uninstalled") + + return "agent uninstall" def service_action(hostname, action, service): diff --git a/scripts/.gitkeep b/scripts/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/scripts/removeagent.exe b/scripts/removeagent.exe deleted file mode 100644 index 2dbd1f1a..00000000 Binary files a/scripts/removeagent.exe and /dev/null differ diff --git a/scripts/removeagent.go b/scripts/removeagent.go deleted file mode 100644 index 1a3d75b4..00000000 --- a/scripts/removeagent.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import( - "os/exec" - "os" -) - -func main(){ - unins_file := "C:\\Program Files\\TacticalAgent\\unins000.exe" - - if fileExists(unins_file) { - c := exec.Command("cmd", "/C", unins_file, "/VERYSILENT", "/SUPPRESSMSGBOXES") - c.Run() - } -} - -func fileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() -} diff --git a/scripts/uninstall.bat b/scripts/uninstall.bat deleted file mode 100644 index c26fd758..00000000 --- a/scripts/uninstall.bat +++ /dev/null @@ -1 +0,0 @@ -start "" "C:\Windows\Temp\removeagent.exe"