def generate_tasks_from_policies(self, clear=False): from autotasks.tasks import delete_win_task_schedule from automation.models import Policy # Clear agent tasks managed by policy if clear: for task in self.autotasks.filter(managed_by_policy=True): delete_win_task_schedule.delay(task.pk) # Generate tasks based on policies Policy.generate_policy_tasks(self)
def delete(self, request, pk): from automation.tasks import delete_policy_autotasks_task from autotasks.tasks import delete_win_task_schedule task = get_object_or_404(AutomatedTask, pk=pk) if task.agent: delete_win_task_schedule.delay(pk=task.pk) elif task.policy: delete_policy_autotasks_task.delay(task=task.pk) task.delete() return Response(f"{task.name} will be deleted shortly")
def core_maintenance_tasks(): # cleanup expired runonce tasks tasks = AutomatedTask.objects.filter( task_type="runonce", remove_if_not_scheduled=True, ).exclude(last_run=None) for task in tasks: agent_tz = pytz.timezone(task.agent.timezone) task_time_utc = task.run_time_date.replace(tzinfo=agent_tz).astimezone( pytz.utc) now = djangotime.now() if now > task_time_utc: delete_win_task_schedule.delay(task.pk)
def core_maintenance_tasks(): # cleanup expired runonce tasks tasks = AutomatedTask.objects.filter( task_type="runonce", remove_if_not_scheduled=True, ).exclude(last_run=None) for task in tasks: agent_tz = pytz.timezone(task.agent.timezone) task_time_utc = task.run_time_date.replace(tzinfo=agent_tz).astimezone( pytz.utc) now = djangotime.now() if now > task_time_utc: delete_win_task_schedule.delay(task.pk) # remove old CheckHistory data older_than = CoreSettings.objects.first().check_history_prune_days prune_check_history.delay(older_than)
def handle_pending_actions(self): pending_actions = self.pendingactions.filter(status="pending") for action in pending_actions: if action.action_type == "taskaction": from autotasks.tasks import ( create_win_task_schedule, enable_or_disable_win_task, delete_win_task_schedule, ) task_id = action.details["task_id"] if action.details["action"] == "taskcreate": create_win_task_schedule.delay(task_id, pending_action=action.id) elif action.details["action"] == "tasktoggle": enable_or_disable_win_task.delay( task_id, action.details["value"], pending_action=action.id ) elif action.details["action"] == "taskdelete": delete_win_task_schedule.delay(task_id, pending_action=action.id)
def core_maintenance_tasks(): # cleanup any leftover agent user accounts agents = Agent.objects.values_list("agent_id", flat=True) users = User.objects.exclude(username__in=agents).filter(last_login=None) if users: users.delete() logger.info("Removed leftover agent user accounts:", str([i.username for i in users])) # cleanup expired runonce tasks tasks = AutomatedTask.objects.filter( task_type="runonce", remove_if_not_scheduled=True, ).exclude(last_run=None) for task in tasks: agent_tz = pytz.timezone(task.agent.timezone) task_time_utc = task.run_time_date.replace(tzinfo=agent_tz).astimezone( pytz.utc) now = djangotime.now() if now > task_time_utc: delete_win_task_schedule.delay(task.pk)
def cascade_policy_tasks(agent): from autotasks.tasks import delete_win_task_schedule from autotasks.models import AutomatedTask from logs.models import PendingAction # List of all tasks to be applied tasks = list() added_task_pks = list() agent_tasks_parent_pks = [ task.parent_task for task in agent.autotasks.filter(managed_by_policy=True) ] # Get policies applied to agent and agent site and client client = agent.client site = agent.site default_policy = None client_policy = None site_policy = None agent_policy = agent.policy # Get the Client/Site policy based on if the agent is server or workstation if agent.monitoring_type == "server": default_policy = CoreSettings.objects.first().server_policy client_policy = client.server_policy site_policy = site.server_policy elif agent.monitoring_type == "workstation": default_policy = CoreSettings.objects.first().workstation_policy client_policy = client.workstation_policy site_policy = site.workstation_policy if agent_policy and agent_policy.active: for task in agent_policy.autotasks.all(): if task.pk not in added_task_pks: tasks.append(task) added_task_pks.append(task.pk) if site_policy and site_policy.active: for task in site_policy.autotasks.all(): if task.pk not in added_task_pks: tasks.append(task) added_task_pks.append(task.pk) if client_policy and client_policy.active: for task in client_policy.autotasks.all(): if task.pk not in added_task_pks: tasks.append(task) added_task_pks.append(task.pk) if default_policy and default_policy.active: for task in default_policy.autotasks.all(): if task.pk not in added_task_pks: tasks.append(task) added_task_pks.append(task.pk) # remove policy tasks from agent not included in policy for task in agent.autotasks.filter(parent_task__in=[ taskpk for taskpk in agent_tasks_parent_pks if taskpk not in added_task_pks ]): delete_win_task_schedule.delay(task.pk) # handle matching tasks that haven't synced to agent yet or pending deletion due to agent being offline for action in agent.pendingactions.exclude(status="completed"): task = AutomatedTask.objects.get(pk=action.details["task_id"]) if (task.parent_task in agent_tasks_parent_pks and task.parent_task in added_task_pks): agent.remove_matching_pending_task_actions(task.id) PendingAction( agent=agent, action_type="taskaction", details={ "action": "taskcreate", "task_id": task.id }, ).save() task.sync_status = "notsynced" task.save(update_fields=["sync_status"]) return [ task for task in tasks if task.pk not in agent_tasks_parent_pks ]
def delete_policy_autotask_task(taskpk): from autotasks.tasks import delete_win_task_schedule from autotasks.models import AutomatedTask for task in AutomatedTask.objects.filter(parent_task=taskpk): delete_win_task_schedule.delay(task.pk)