agent update improvements

This commit is contained in:
wh1te909 2020-07-18 09:47:49 +00:00
parent 3b123f7521
commit 1273be1265
4 changed files with 52 additions and 36 deletions

View File

@ -13,6 +13,7 @@ import winerror
from time import sleep
import requests
import subprocess
import random
PROGRAM_DIR = "C:\\Program Files\\TacticalAgent"
TAC_RMM = os.path.join(PROGRAM_DIR, "tacticalrmm.exe")
@ -62,8 +63,9 @@ def install_updates():
def agent_update(version, url):
sleep(random.randint(1, 90))
try:
r = requests.get(url, stream=True, timeout=300)
r = requests.get(url, stream=True, timeout=600)
except Exception:
return "failed"
@ -83,9 +85,9 @@ def agent_update(version, url):
for svc in services:
subprocess.run([NSSM, "stop", svc], timeout=120)
sleep(5)
r = subprocess.run([exe, "/VERYSILENT", "/SUPPRESSMSGBOXES"], timeout=300)
sleep(10)
r = subprocess.run([exe, "/VERYSILENT", "/SUPPRESSMSGBOXES"], timeout=300)
sleep(20)
for svc in services:
subprocess.run([NSSM, "start", svc], timeout=120)

View File

@ -80,7 +80,7 @@ class Agent(models.Model):
@property
def status(self):
offline = dt.datetime.now(dt.timezone.utc) - dt.timedelta(minutes=4)
offline = dt.datetime.now(dt.timezone.utc) - dt.timedelta(minutes=6)
overdue = dt.datetime.now(dt.timezone.utc) - dt.timedelta(
minutes=self.overdue_time
)

View File

@ -3,6 +3,7 @@ import subprocess
from loguru import logger
from time import sleep
import requests
from packaging import version as pyver
from django.conf import settings
@ -14,6 +15,49 @@ from agents.models import Agent, AgentOutage
logger.configure(**settings.LOG_CONFIG)
@app.task
def send_agent_update_task(pks, version):
assert isinstance(pks, list)
ver = version.split("winagent-v")[1]
q = Agent.objects.only("pk").filter(pk__in=pks)
agents = [
i
for i in q
if pyver.parse(i.version) < pyver.parse(ver) and i.status == "online"
]
if agents:
for agent in agents:
agent.update_pending = True
agent.save(update_fields=["update_pending"])
minions = [i.salt_id for i in agents]
r = Agent.get_github_versions()
git_versions = r["versions"]
data = r["data"] # full response from github
versions = {}
for i, release in enumerate(data):
versions[i] = release["name"]
key = [k for k, v in versions.items() if v == version][0]
download_url = data[key]["assets"][0]["browser_download_url"]
# split into chunks to not overload salt
chunks = (minions[i : i + 30] for i in range(0, len(minions), 30))
for chunk in chunks:
r = Agent.salt_batch_async(
minions=chunk,
func="win_agent.do_agent_update",
kwargs={"version": ver, "url": download_url},
)
sleep(5)
@app.task
def get_wmi_detail_task(pk):
agent = Agent.objects.get(pk=pk)

View File

@ -1,6 +1,5 @@
from loguru import logger
import subprocess
from packaging import version as pyver
import zlib
import json
import base64
@ -27,7 +26,7 @@ from core.models import CoreSettings
from .serializers import AgentSerializer, AgentHostnameSerializer, AgentTableSerializer
from winupdate.serializers import WinUpdatePolicySerializer
from .tasks import uninstall_agent_task
from .tasks import uninstall_agent_task, send_agent_update_task
from tacticalrmm.utils import notify_error
@ -51,36 +50,7 @@ def get_agent_versions(request):
def update_agents(request):
pks = request.data["pks"]
version = request.data["version"]
ver = version.split("winagent-v")[1]
q = Agent.objects.only("pk").filter(pk__in=pks)
agents = [i for i in q if pyver.parse(i.version) < pyver.parse(ver)]
if agents:
for agent in agents:
agent.update_pending = True
agent.save(update_fields=["update_pending"])
minions = [i.salt_id for i in agents]
r = Agent.get_github_versions()
git_versions = r["versions"]
data = r["data"] # full response from github
versions = {}
for i, release in enumerate(data):
versions[i] = release["name"]
key = [k for k, v in versions.items() if v == version][0]
download_url = data[key]["assets"][0]["browser_download_url"]
r = Agent.salt_batch_async(
minions=minions,
func="win_agent.do_agent_update",
kwargs={"version": ver, "url": download_url},
)
send_agent_update_task.delay(pks=pks, version=version)
return Response("ok")