Exemplo n.º 1
0
def bulk(request):
    if request.data["target"] == "agents" and not request.data["agentPKs"]:
        return notify_error("Must select at least 1 agent")

    if request.data["target"] == "client":
        q = Agent.objects.filter(site__client_id=request.data["client"])
    elif request.data["target"] == "site":
        q = Agent.objects.filter(site_id=request.data["site"])
    elif request.data["target"] == "agents":
        q = Agent.objects.filter(pk__in=request.data["agentPKs"])
    elif request.data["target"] == "all":
        q = Agent.objects.only("pk", "monitoring_type")
    else:
        return notify_error("Something went wrong")

    if request.data["monType"] == "servers":
        q = q.filter(monitoring_type="server")
    elif request.data["monType"] == "workstations":
        q = q.filter(monitoring_type="workstation")

    agents: list[int] = [agent.pk for agent in q]

    AuditLog.audit_bulk_action(request.user, request.data["mode"],
                               request.data)

    if request.data["mode"] == "command":
        handle_bulk_command_task.delay(agents, request.data["cmd"],
                                       request.data["shell"],
                                       request.data["timeout"])
        return Response(f"Command will now be run on {len(agents)} agents")

    elif request.data["mode"] == "script":
        script = get_object_or_404(Script, pk=request.data["scriptPK"])
        handle_bulk_script_task.delay(script.pk, agents, request.data["args"],
                                      request.data["timeout"])
        return Response(
            f"{script.name} will now be run on {len(agents)} agents")

    elif request.data["mode"] == "install":
        bulk_install_updates_task.delay(agents)
        return Response(
            f"Pending updates will now be installed on {len(agents)} agents")
    elif request.data["mode"] == "scan":
        bulk_check_for_updates_task.delay(agents)
        return Response(
            f"Patch status scan will now run on {len(agents)} agents")

    return notify_error("Something went wrong")
Exemplo n.º 2
0
def bulk(request):
    if request.data["target"] == "agents" and not request.data["agentPKs"]:
        return notify_error("Must select at least 1 agent")

    if request.data["target"] == "client":
        q = Agent.objects.filter(site__client_id=request.data["client"])
    elif request.data["target"] == "site":
        q = Agent.objects.filter(site_id=request.data["site"])
    elif request.data["target"] == "agents":
        q = Agent.objects.filter(pk__in=request.data["agentPKs"])
    elif request.data["target"] == "all":
        q = Agent.objects.all()
    else:
        return notify_error("Something went wrong")

    minions = [agent.salt_id for agent in q]
    agents = [agent.pk for agent in q]

    AuditLog.audit_bulk_action(request.user, request.data["mode"],
                               request.data)

    if request.data["mode"] == "command":
        handle_bulk_command_task.delay(agents, request.data["cmd"],
                                       request.data["shell"],
                                       request.data["timeout"])
        return Response(f"Command will now be run on {len(agents)} agents")

    elif request.data["mode"] == "script":
        script = get_object_or_404(Script, pk=request.data["scriptPK"])
        handle_bulk_script_task.delay(script.pk, agents, request.data["args"],
                                      request.data["timeout"])
        return Response(
            f"{script.name} will now be run on {len(agents)} agents")

    elif request.data["mode"] == "install":
        r = Agent.salt_batch_async(minions=minions,
                                   func="win_agent.install_updates")
        if r == "timeout":
            return notify_error("Salt API not running")
        return Response(
            f"Pending updates will now be installed on {len(agents)} agents")
    elif request.data["mode"] == "scan":
        bulk_check_for_updates_task.delay(minions=minions)
        return Response(
            f"Patch status scan will now run on {len(agents)} agents")

    return notify_error("Something went wrong")