From 04cdb51320a2bac587c3a519edc447d5e6aba711 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Sat, 30 May 2020 08:29:44 +0000 Subject: [PATCH] fix autotasks --- .../migrations/0002_auto_20200530_0731.py | 20 ++++ api/tacticalrmm/autotasks/models.py | 2 +- api/tacticalrmm/autotasks/serializers.py | 4 +- api/tacticalrmm/autotasks/views.py | 76 +++---------- api/tacticalrmm/checks/models.py | 4 +- api/tacticalrmm/checks/serializers.py | 13 +++ web/src/components/AutomatedTasksTab.vue | 3 +- web/src/components/ChecksTab.vue | 3 +- .../modals/tasks/AddAutomatedTask.vue | 102 +++++++++--------- 9 files changed, 108 insertions(+), 119 deletions(-) create mode 100644 api/tacticalrmm/autotasks/migrations/0002_auto_20200530_0731.py diff --git a/api/tacticalrmm/autotasks/migrations/0002_auto_20200530_0731.py b/api/tacticalrmm/autotasks/migrations/0002_auto_20200530_0731.py new file mode 100644 index 00000000..1ccdde35 --- /dev/null +++ b/api/tacticalrmm/autotasks/migrations/0002_auto_20200530_0731.py @@ -0,0 +1,20 @@ +# Generated by Django 3.0.6 on 2020-05-30 07:31 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('checks', '0004_check_svc_policy_mode'), + ('autotasks', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='automatedtask', + name='assigned_check', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='assignedtask', to='checks.Check'), + ), + ] diff --git a/api/tacticalrmm/autotasks/models.py b/api/tacticalrmm/autotasks/models.py index 2ed8e239..a83cd33f 100644 --- a/api/tacticalrmm/autotasks/models.py +++ b/api/tacticalrmm/autotasks/models.py @@ -50,7 +50,7 @@ class AutomatedTask(models.Model): "checks.Check", null=True, blank=True, - related_name="assignedcheck", + related_name="assignedtask", on_delete=models.SET_NULL, ) name = models.CharField(max_length=255) diff --git a/api/tacticalrmm/autotasks/serializers.py b/api/tacticalrmm/autotasks/serializers.py index def7e56b..a2085acb 100644 --- a/api/tacticalrmm/autotasks/serializers.py +++ b/api/tacticalrmm/autotasks/serializers.py @@ -4,17 +4,17 @@ from .models import AutomatedTask from agents.models import Agent from scripts.serializers import ScriptSerializer +from checks.serializers import CheckSerializer class TaskSerializer(serializers.ModelSerializer): - assigned_check = serializers.ReadOnlyField() + assigned_check = CheckSerializer(read_only=True) schedule = serializers.ReadOnlyField() class Meta: model = AutomatedTask fields = "__all__" - depth = 1 class AgentTaskSerializer(serializers.ModelSerializer): diff --git a/api/tacticalrmm/autotasks/views.py b/api/tacticalrmm/autotasks/views.py index 8051c1aa..d8515961 100644 --- a/api/tacticalrmm/autotasks/views.py +++ b/api/tacticalrmm/autotasks/views.py @@ -12,11 +12,12 @@ from rest_framework.decorators import api_view from .models import AutomatedTask from agents.models import Agent +from checks.models import Check from scripts.models import Script from automation.models import Policy -from .serializers import AutoTaskSerializer, AgentTaskSerializer +from .serializers import TaskSerializer, AutoTaskSerializer, AgentTaskSerializer from scripts.serializers import ScriptSerializer from .tasks import ( @@ -29,78 +30,35 @@ from .tasks import ( class AddAutoTask(APIView): def post(self, request): - daily, checkfailure, manual = False, False, False + data = request.data + script = get_object_or_404(Script, pk=data["autotask"]["script"]) # Determine if adding check to Policy or Agent if "policy" in data: policy = get_object_or_404(Policy, id=data["policy"]) - # Object used for filter and save parent = {"policy": policy} else: agent = get_object_or_404(Agent, pk=data["agent"]) - # Object used for filter and save parent = {"agent": agent} - script = Script.objects.only("pk").get(pk=data["script"]) - if data["trigger"] == "daily": - daily = True - days = data["days"] - time = data["time"] - elif data["trigger"] == "checkfailure": - checkfailure = True - check = data["check"] - elif data["trigger"] == "manual": - manual = True + check = None + if data["autotask"]["assigned_check"]: + check = get_object_or_404(Check, pk=data["autotask"]["assigned_check"]) - rand_name = AutomatedTask.generate_task_name() + serializer = TaskSerializer(data=data["autotask"], partial=True, context=parent) + serializer.is_valid(raise_exception=True) + obj = serializer.save( + **parent, + script=script, + win_task_name=AutomatedTask.generate_task_name(), + assigned_check=check, + ) - try: - timeout = data["timeout"] - except KeyError: - timeout = 130 + create_win_task_schedule.delay(pk=obj.pk) - if daily: - task = AutomatedTask( - **parent, - name=data["name"], - script=script, - timeout=timeout, - task_type="scheduled", - win_task_name=rand_name, - run_time_days=data["days"], - run_time_minute=data["time"], - ) - task.save() - - elif checkfailure: - task = AutomatedTask( - **parent, - name=data["name"], - script=script, - timeout=timeout, - win_task_name=rand_name, - task_type="checkfailure", - ) - task.save() - related_check = AutomatedTask.get_related_check(check) - related_check.task_on_failure = task - related_check.save(update_fields=["task_on_failure"]) - - elif manual: - task = AutomatedTask( - **parent, - name=data["name"], - timeout=timeout, - win_task_name=rand_name, - script=script, - task_type="manual", - ) - task.save() - - create_win_task_schedule.delay(pk=task.pk) - return Response("ok") + return Response("Task will be created shortly!") class AutoTask(APIView): diff --git a/api/tacticalrmm/checks/models.py b/api/tacticalrmm/checks/models.py index 2839b7d4..75c50709 100644 --- a/api/tacticalrmm/checks/models.py +++ b/api/tacticalrmm/checks/models.py @@ -130,9 +130,9 @@ class Check(models.Model): def __str__(self): if self.agent: - return f"{self.agent.hostname} - {self.check_type}" + return f"{self.agent.hostname} - {self.readable_desc}" else: - return f"{self.policy.name} - {self.check_type}" + return f"{self.policy.name} - {self.readable_desc}" @property def readable_desc(self): diff --git a/api/tacticalrmm/checks/serializers.py b/api/tacticalrmm/checks/serializers.py index 64aeb70e..a529b4cc 100644 --- a/api/tacticalrmm/checks/serializers.py +++ b/api/tacticalrmm/checks/serializers.py @@ -3,13 +3,26 @@ import validators as _v from rest_framework import serializers from .models import Check +from autotasks.models import AutomatedTask from scripts.serializers import ScriptSerializer +class AssignedTaskField(serializers.ModelSerializer): + class Meta: + model = AutomatedTask + fields = "__all__" + + class CheckSerializer(serializers.ModelSerializer): readable_desc = serializers.ReadOnlyField() script = ScriptSerializer(read_only=True) + assigned_task = serializers.SerializerMethodField() + + def get_assigned_task(self, obj): + if obj.assignedtask.exists(): + task = obj.assignedtask.get() + return AssignedTaskField(task).data class Meta: model = Check diff --git a/web/src/components/AutomatedTasksTab.vue b/web/src/components/AutomatedTasksTab.vue index 77fd0c32..d7ab57e3 100644 --- a/web/src/components/AutomatedTasksTab.vue +++ b/web/src/components/AutomatedTasksTab.vue @@ -81,7 +81,8 @@ {{ props.row.last_run }} Has not run yet {{ props.row.schedule }} - {{ props.row.assigned_check }} + {{ props.row.assigned_check.readable_desc }} + diff --git a/web/src/components/ChecksTab.vue b/web/src/components/ChecksTab.vue index 4d0edd6d..663474b5 100644 --- a/web/src/components/ChecksTab.vue +++ b/web/src/components/ChecksTab.vue @@ -161,7 +161,8 @@ {{ props.row.more_info }} {{ props.row.last_run }} - {{ props.row.assigned_task }} + {{ props.row.assigned_task.name }} + diff --git a/web/src/components/modals/tasks/AddAutomatedTask.vue b/web/src/components/modals/tasks/AddAutomatedTask.vue index 9b5e0fb4..22688830 100644 --- a/web/src/components/modals/tasks/AddAutomatedTask.vue +++ b/web/src/components/modals/tasks/AddAutomatedTask.vue @@ -6,7 +6,7 @@ -

You need to upload a script/task first

+

You need to upload a script first

Settings -> Script Manager

@@ -23,7 +23,7 @@ :rules="[val => !!val || '*Required']" dense outlined - v-model="scriptPk" + v-model="autotask.script" :options="scriptOptions" label="Select task" map-options @@ -35,7 +35,7 @@ :rules="[val => !!val || '*Required']" outlined dense - v-model="taskName" + v-model="autotask.name" label="Descriptive name of task" /> @@ -44,7 +44,7 @@ :rules="[val => !!val || '*Required']" outlined dense - v-model.number="timeout" + v-model.number="autotask.timeout" type="number" label="Maximum permitted execution time (seconds)" /> @@ -52,28 +52,38 @@ - - - -
+ + + +
Run on Days: - +
At time: - +
-
+
When Check Fails: { @@ -180,48 +190,34 @@ export default { }, computed: { ...mapGetters(["selectedAgentPk", "scripts"]), - /* ...mapState({ - checks: state => (this.policypk ? state.automation.checks : state.agentChecks) - }), */ - // I have no idea why this works and the above doesn't checks() { return this.policypk ? this.$store.state.automation.checks : this.$store.state.agentChecks; }, - allChecks() { - return [ - ...this.checks.diskchecks, - ...this.checks.cpuloadchecks, - ...this.checks.memchecks, - ...this.checks.scriptchecks, - ...this.checks.winservicechecks, - ...this.checks.pingchecks, - ...this.checks.eventlogchecks - ]; - }, checksOptions() { const r = []; - this.allChecks.forEach(k => { - // some checks may have the same primary key so add the check type to make them unique - r.push({ label: k.readable_desc, value: `${k.id}|${k.check_type}` }); + this.checks.forEach(i => { + r.push({ label: i.readable_desc, value: i.id }); }); return r; }, scriptOptions() { const r = []; - this.scripts.forEach(k => { - r.push({ label: k.name, value: k.id }); + this.scripts.forEach(i => { + r.push({ label: i.name, value: i.id }); }); return r; }, step1Done() { - return this.step > 1 && this.scriptPk !== null && this.taskName !== null ? true : false; + return this.step > 1 && this.autotask.script !== null && this.autotask.name && this.autotask.timeout + ? true + : false; }, step2Done() { - if (this.trigger === "daily") { - return this.days !== null && this.days.length !== 0 && this.time !== null ? true : false; - } else if (this.trigger === "checkfailure") { - return this.assignedCheck !== null && this.assignedCheck.length !== 0 ? true : false; - } else if (this.trigger === "manual") { + if (this.autotask.task_type === "scheduled") { + return this.autotask.run_time_days.length !== 0 && this.autotask.run_time_minute !== null ? true : false; + } else if (this.autotask.task_type === "checkfailure") { + return this.autotask.assigned_check !== null ? true : false; + } else if (this.autotask.task_type === "manual") { return true; } else { return false;