fix uninstall for older agents
This commit is contained in:
		@@ -229,42 +229,43 @@ def batch_sysinfo_task():
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@app.task
 | 
			
		||||
def uninstall_agent_task(salt_id):
 | 
			
		||||
def uninstall_agent_task(salt_id, has_nats):
 | 
			
		||||
    attempts = 0
 | 
			
		||||
    error = False
 | 
			
		||||
 | 
			
		||||
    while 1:
 | 
			
		||||
        try:
 | 
			
		||||
    if not has_nats:
 | 
			
		||||
        while 1:
 | 
			
		||||
            try:
 | 
			
		||||
 | 
			
		||||
            r = requests.post(
 | 
			
		||||
                f"http://{settings.SALT_HOST}:8123/run",
 | 
			
		||||
                json=[
 | 
			
		||||
                    {
 | 
			
		||||
                        "client": "local",
 | 
			
		||||
                        "tgt": salt_id,
 | 
			
		||||
                        "fun": "win_agent.uninstall_agent",
 | 
			
		||||
                        "timeout": 8,
 | 
			
		||||
                        "username": settings.SALT_USERNAME,
 | 
			
		||||
                        "password": settings.SALT_PASSWORD,
 | 
			
		||||
                        "eauth": "pam",
 | 
			
		||||
                    }
 | 
			
		||||
                ],
 | 
			
		||||
                timeout=10,
 | 
			
		||||
            )
 | 
			
		||||
            ret = r.json()["return"][0][salt_id]
 | 
			
		||||
        except Exception:
 | 
			
		||||
            attempts += 1
 | 
			
		||||
        else:
 | 
			
		||||
            if ret != "ok":
 | 
			
		||||
                r = requests.post(
 | 
			
		||||
                    f"http://{settings.SALT_HOST}:8123/run",
 | 
			
		||||
                    json=[
 | 
			
		||||
                        {
 | 
			
		||||
                            "client": "local",
 | 
			
		||||
                            "tgt": salt_id,
 | 
			
		||||
                            "fun": "win_agent.uninstall_agent",
 | 
			
		||||
                            "timeout": 8,
 | 
			
		||||
                            "username": settings.SALT_USERNAME,
 | 
			
		||||
                            "password": settings.SALT_PASSWORD,
 | 
			
		||||
                            "eauth": "pam",
 | 
			
		||||
                        }
 | 
			
		||||
                    ],
 | 
			
		||||
                    timeout=10,
 | 
			
		||||
                )
 | 
			
		||||
                ret = r.json()["return"][0][salt_id]
 | 
			
		||||
            except Exception:
 | 
			
		||||
                attempts += 1
 | 
			
		||||
            else:
 | 
			
		||||
                attempts = 0
 | 
			
		||||
                if ret != "ok":
 | 
			
		||||
                    attempts += 1
 | 
			
		||||
                else:
 | 
			
		||||
                    attempts = 0
 | 
			
		||||
 | 
			
		||||
        if attempts >= 10:
 | 
			
		||||
            error = True
 | 
			
		||||
            break
 | 
			
		||||
        elif attempts == 0:
 | 
			
		||||
            break
 | 
			
		||||
            if attempts >= 10:
 | 
			
		||||
                error = True
 | 
			
		||||
                break
 | 
			
		||||
            elif attempts == 0:
 | 
			
		||||
                break
 | 
			
		||||
 | 
			
		||||
    if error:
 | 
			
		||||
        logger.error(f"{salt_id} uninstall failed")
 | 
			
		||||
 
 | 
			
		||||
@@ -123,7 +123,7 @@ class TestAgentViews(TacticalTestCase):
 | 
			
		||||
 | 
			
		||||
        nats_cmd.assert_called_with({"func": "uninstall"}, wait=False)
 | 
			
		||||
        reload_nats.assert_called_once()
 | 
			
		||||
        mock_task.assert_called_with(self.agent.salt_id)
 | 
			
		||||
        mock_task.assert_called_with(self.agent.salt_id, True)
 | 
			
		||||
 | 
			
		||||
        self.check_not_authenticated("delete", url)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -61,32 +61,32 @@ def update_agents(request):
 | 
			
		||||
@api_view()
 | 
			
		||||
def ping(request, pk):
 | 
			
		||||
    agent = get_object_or_404(Agent, pk=pk)
 | 
			
		||||
    if not agent.has_nats:
 | 
			
		||||
        return notify_error("Requires agent version 1.1.0 or greater")
 | 
			
		||||
    r = asyncio.run(agent.nats_cmd({"func": "ping"}, timeout=10))
 | 
			
		||||
    status = "offline"
 | 
			
		||||
    if agent.has_nats:
 | 
			
		||||
        r = asyncio.run(agent.nats_cmd({"func": "ping"}, timeout=5))
 | 
			
		||||
        if r == "pong":
 | 
			
		||||
            status = "online"
 | 
			
		||||
    else:
 | 
			
		||||
        r = agent.salt_api_cmd(timeout=5, func="test.ping")
 | 
			
		||||
        if isinstance(r, bool) and r:
 | 
			
		||||
            status = "online"
 | 
			
		||||
 | 
			
		||||
    if r == "timeout" or r == "natsdown":
 | 
			
		||||
        return Response({"name": agent.hostname, "status": "offline"})
 | 
			
		||||
    elif r == "pong":
 | 
			
		||||
        return Response({"name": agent.hostname, "status": "online"})
 | 
			
		||||
 | 
			
		||||
    return Response({"name": agent.hostname, "status": "offline"})
 | 
			
		||||
    return Response({"name": agent.hostname, "status": status})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@api_view(["DELETE"])
 | 
			
		||||
def uninstall(request):
 | 
			
		||||
    agent = get_object_or_404(Agent, pk=request.data["pk"])
 | 
			
		||||
    if not agent.has_nats:
 | 
			
		||||
        return notify_error("Requires agent version 1.1.0 or greater")
 | 
			
		||||
 | 
			
		||||
    asyncio.run(agent.nats_cmd({"func": "uninstall"}, wait=False))
 | 
			
		||||
    if agent.has_nats:
 | 
			
		||||
        asyncio.run(agent.nats_cmd({"func": "uninstall"}, wait=False))
 | 
			
		||||
 | 
			
		||||
    salt_id = agent.salt_id
 | 
			
		||||
    name = agent.hostname
 | 
			
		||||
    has_nats = agent.has_nats
 | 
			
		||||
    agent.delete()
 | 
			
		||||
    reload_nats()
 | 
			
		||||
 | 
			
		||||
    uninstall_agent_task.delay(salt_id)
 | 
			
		||||
    uninstall_agent_task.delay(salt_id, has_nats)
 | 
			
		||||
    return Response(f"{name} will now be uninstalled.")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -550,7 +550,7 @@ def install_agent(request):
 | 
			
		||||
            "&&",
 | 
			
		||||
            "timeout",
 | 
			
		||||
            "/t",
 | 
			
		||||
            "20",
 | 
			
		||||
            "10",
 | 
			
		||||
            "/nobreak",
 | 
			
		||||
            ">",
 | 
			
		||||
            "NUL",
 | 
			
		||||
 
 | 
			
		||||
@@ -133,7 +133,7 @@ func main() {
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	time.Sleep(20 * time.Second)
 | 
			
		||||
	time.Sleep(10 * time.Second)
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Installation starting.")
 | 
			
		||||
	cmd := exec.Command(tacrmm, cmdArgs...)
 | 
			
		||||
 
 | 
			
		||||
@@ -36,7 +36,7 @@ If (Get-Service $serviceName -ErrorAction SilentlyContinue) {
 | 
			
		||||
        Invoke-WebRequest -Uri $downloadlink -OutFile $OutPath\$output
 | 
			
		||||
        Start-Process -FilePath $OutPath\$output -ArgumentList ('/VERYSILENT /SUPPRESSMSGBOXES') -Wait
 | 
			
		||||
        write-host ('Extracting...')
 | 
			
		||||
        Start-Sleep -s 20
 | 
			
		||||
        Start-Sleep -s 10
 | 
			
		||||
        Start-Process -FilePath "C:\Program Files\TacticalAgent\tacticalrmm.exe" -ArgumentList $installArgs -Wait
 | 
			
		||||
        exit 0
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ EXE_DIR = os.path.join(BASE_DIR, "tacticalrmm/private/exe")
 | 
			
		||||
AUTH_USER_MODEL = "accounts.User"
 | 
			
		||||
 | 
			
		||||
# latest release
 | 
			
		||||
TRMM_VERSION = "0.2.0"
 | 
			
		||||
TRMM_VERSION = "0.2.1"
 | 
			
		||||
 | 
			
		||||
# bump this version everytime vue code is changed
 | 
			
		||||
# to alert user they need to manually refresh their browser
 | 
			
		||||
@@ -123,9 +123,15 @@ AUTH_PASSWORD_VALIDATORS = [
 | 
			
		||||
    {
 | 
			
		||||
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
 | 
			
		||||
    },
 | 
			
		||||
    {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
 | 
			
		||||
    {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
 | 
			
		||||
    {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
 | 
			
		||||
    {
 | 
			
		||||
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
 | 
			
		||||
    },
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user