Skip to content

Commit

Permalink
add saving output of bulk script to custom field and agent note closes
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Oct 6, 2024
1 parent 6fa16e1 commit 9688dbd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
9 changes: 9 additions & 0 deletions api/tacticalrmm/agents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,15 @@ class AgentHistory(models.Model):
on_delete=models.SET_NULL,
)
script_results = models.JSONField(null=True, blank=True)
custom_field = models.ForeignKey(
"core.CustomField",
null=True,
blank=True,
related_name="history",
on_delete=models.SET_NULL,
)
collector_all_output = models.BooleanField(default=False)
save_to_agent_note = models.BooleanField(default=False)

def __str__(self) -> str:
return f"{self.agent.hostname} - {self.type}"
13 changes: 13 additions & 0 deletions api/tacticalrmm/agents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,16 @@ def bulk(request):
elif request.data["mode"] == "script":
script = get_object_or_404(Script, pk=request.data["script"])

# prevent API from breaking for those who haven't updated payload
try:
custom_field_pk = request.data["custom_field"]
collector_all_output = request.data["collector_all_output"]
save_to_agent_note = request.data["save_to_agent_note"]
except KeyError:
custom_field_pk = None
collector_all_output = False
save_to_agent_note = False

bulk_script_task.delay(
script_pk=script.pk,
agent_pks=agents,
Expand All @@ -1016,6 +1026,9 @@ def bulk(request):
username=request.user.username[:50],
run_as_user=request.data["run_as_user"],
env_vars=request.data["env_vars"],
custom_field_pk=custom_field_pk,
collector_all_output=collector_all_output,
save_to_agent_note=save_to_agent_note,
)

return Response(f"{script.name} will now be run on {len(agents)} agents. {ht}")
Expand Down
33 changes: 31 additions & 2 deletions api/tacticalrmm/apiv3/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rest_framework.views import APIView

from accounts.models import User
from agents.models import Agent, AgentHistory
from agents.models import Agent, AgentHistory, Note
from agents.serializers import AgentHistorySerializer
from alerts.tasks import cache_agents_alert_template
from apiv3.utils import get_agent_config
Expand Down Expand Up @@ -40,6 +40,7 @@
AuditActionType,
AuditObjType,
CheckStatus,
CustomFieldModel,
DebugLogType,
GoArch,
MeshAgentIdent,
Expand Down Expand Up @@ -581,11 +582,39 @@ def patch(self, request, agentid, pk):
request.data["script_results"]["retcode"] = 1

hist = get_object_or_404(
AgentHistory.objects.filter(agent__agent_id=agentid), pk=pk
AgentHistory.objects.select_related("custom_field").filter(
agent__agent_id=agentid
),
pk=pk,
)
s = AgentHistorySerializer(instance=hist, data=request.data, partial=True)
s.is_valid(raise_exception=True)
s.save()

if hist.custom_field:
if hist.custom_field.model == CustomFieldModel.AGENT:
field = hist.custom_field.get_or_create_field_value(hist.agent)
elif hist.custom_field.model == CustomFieldModel.CLIENT:
field = hist.custom_field.get_or_create_field_value(hist.agent.client)
elif hist.custom_field.model == CustomFieldModel.SITE:
field = hist.custom_field.get_or_create_field_value(hist.agent.site)

r = request.data["script_results"]["stdout"]
value = (
r.strip()
if hist.collector_all_output
else r.strip().split("\n")[-1].strip()
)

field.save_to_field(value)

if hist.save_to_agent_note:
Note.objects.create(
agent=hist.agent,
user=request.user,
note=request.data["script_results"]["stdout"],
)

return Response("ok")


Expand Down
12 changes: 12 additions & 0 deletions api/tacticalrmm/scripts/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,21 @@ def bulk_script_task(
username: str,
run_as_user: bool = False,
env_vars: list[str] = [],
custom_field_pk: int | None,
collector_all_output: bool = False,
save_to_agent_note: bool = False,
) -> None:
script = Script.objects.get(pk=script_pk)
# always override if set on script model
if script.run_as_user:
run_as_user = True

custom_field = None
if custom_field_pk:
from core.models import CustomField

custom_field = CustomField.objects.get(pk=custom_field_pk)

items = []
agent: "Agent"
for agent in Agent.objects.filter(pk__in=agent_pks):
Expand All @@ -68,6 +77,9 @@ def bulk_script_task(
type=AgentHistoryType.SCRIPT_RUN,
script=script,
username=username,
custom_field=custom_field,
collector_all_output=collector_all_output,
save_to_agent_note=save_to_agent_note,
)
data = {
"func": "runscriptfull",
Expand Down

0 comments on commit 9688dbd

Please sign in to comment.