def service_action(hostname, action, service): return Agent.salt_api_cmd( hostname=hostname, timeout=30, func="cmd.script", arg="C:\\Program Files\\TacticalAgent\\nssm.exe", kwargs={"args": f"{action} {service}"}, )
def uninstall_agent_task(salt_id): attempts = 0 error = False while 1: try: r = Agent.salt_api_cmd(hostname=salt_id, timeout=10, func="win_agent.uninstall_agent") ret = r.json()["return"][0][salt_id] except Exception: attempts += 1 else: if ret != "ok": attempts += 1 else: attempts = 0 if attempts >= 10: error = True break elif attempts == 0: break if error: logger.error(f"{salt_id} uninstall failed") else: logger.info(f"{salt_id} was successfully uninstalled") try: r = requests.post( f"http://{settings.SALT_HOST}:8123/run", json=[{ "client": "wheel", "fun": "key.delete", "match": salt_id, "username": settings.SALT_USERNAME, "password": settings.SALT_PASSWORD, "eauth": "pam", }], timeout=30, ) except Exception: logger.error(f"{salt_id} unable to remove salt-key") return "ok"
def cancel_pending_action_task(data): if data["action_type"] == "schedreboot" and data["status"] == "pending": from agents.models import Agent task_name = data["details"]["taskname"] try: resp = Agent.salt_api_cmd( hostname=data["salt_id"], timeout=45, func="task.delete_task", arg=[f"name={task_name}"], ) except Exception as e: logger.error( f"Unable to contact {data['salt_id']}. Task {task_name} will need to cancelled manually." ) else: logger.info(f"Scheduled reboot cancellled: {resp.json()}") return "ok"