|
|
|
@ -1,18 +1,16 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
from celery import signals
|
|
|
|
|
from django.db.models.signals import post_save
|
|
|
|
|
from django.utils.translation import gettext_lazy as _, gettext_noop
|
|
|
|
|
|
|
|
|
|
from accounts.const import AutomationTypes
|
|
|
|
|
from accounts.models import AccountBackupAutomation
|
|
|
|
|
from assets.models import Asset, Node
|
|
|
|
|
from audits.models import ActivityLog
|
|
|
|
|
from common.utils import get_object_or_none, i18n_fmt, get_logger
|
|
|
|
|
from common.utils import i18n_fmt, get_logger
|
|
|
|
|
from jumpserver.utils import current_request
|
|
|
|
|
from ops.celery import app
|
|
|
|
|
from orgs.models import Organization
|
|
|
|
|
from orgs.utils import tmp_to_root_org, current_org
|
|
|
|
|
from orgs.utils import current_org
|
|
|
|
|
from terminal.models import Session
|
|
|
|
|
from users.models import User
|
|
|
|
|
from ..const import ActivityChoices
|
|
|
|
@ -21,89 +19,6 @@ from ..models import UserLoginLog
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TaskActivityHandler(object):
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _func_accounts_execute_automation(*args, **kwargs):
|
|
|
|
|
asset_ids = []
|
|
|
|
|
pid, tp = kwargs.get('pid'), kwargs.get('tp')
|
|
|
|
|
model = AutomationTypes.get_type_model(tp)
|
|
|
|
|
task_type_label = tp.label
|
|
|
|
|
with tmp_to_root_org():
|
|
|
|
|
instance = get_object_or_none(model, pk=pid)
|
|
|
|
|
if instance is not None:
|
|
|
|
|
asset_ids = instance.get_all_assets().values_list('id', flat=True)
|
|
|
|
|
return task_type_label, asset_ids
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _func_accounts_push_accounts_to_assets(*args, **kwargs):
|
|
|
|
|
return '', args[0][1]
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _func_accounts_execute_account_backup_plan(*args, **kwargs):
|
|
|
|
|
asset_ids, pid = [], kwargs.get('pid')
|
|
|
|
|
with tmp_to_root_org():
|
|
|
|
|
instance = get_object_or_none(AccountBackupAutomation, pk=pid)
|
|
|
|
|
if instance is not None:
|
|
|
|
|
asset_ids = Asset.objects.filter(
|
|
|
|
|
platform__type__in=instance.types
|
|
|
|
|
).values_list('id', flat=True)
|
|
|
|
|
return '', asset_ids
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _func_assets_verify_accounts_connectivity(*args, **kwargs):
|
|
|
|
|
return '', args[0][1]
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _func_accounts_verify_accounts_connectivity(*args, **kwargs):
|
|
|
|
|
return '', args[0][1]
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _func_assets_test_assets_connectivity_manual(*args, **kwargs):
|
|
|
|
|
return '', args[0][0]
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _func_assets_test_node_assets_connectivity_manual(*args, **kwargs):
|
|
|
|
|
asset_ids = []
|
|
|
|
|
node = get_object_or_none(Node, pk=args[0][0])
|
|
|
|
|
if node is not None:
|
|
|
|
|
asset_ids = node.get_all_assets().values_list('id', flat=True)
|
|
|
|
|
return '', asset_ids
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _func_assets_update_assets_hardware_info_manual(*args, **kwargs):
|
|
|
|
|
return '', args[0][0]
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def _func_assets_update_node_assets_hardware_info_manual(*args, **kwargs):
|
|
|
|
|
asset_ids = []
|
|
|
|
|
node = get_object_or_none(Node, pk=args[0][0])
|
|
|
|
|
if node is not None:
|
|
|
|
|
asset_ids = node.get_all_assets().values_list('id', flat=True)
|
|
|
|
|
return '', asset_ids
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_task_display(task_name, **kwargs):
|
|
|
|
|
task = app.tasks.get(task_name)
|
|
|
|
|
return getattr(task, 'verbose_name', _('Unknown'))
|
|
|
|
|
|
|
|
|
|
def get_info_by_task_name(self, task_name, *args, **kwargs):
|
|
|
|
|
resource_ids = []
|
|
|
|
|
task_name_list = str(task_name).split('.')
|
|
|
|
|
if len(task_name_list) < 2:
|
|
|
|
|
return '', resource_ids
|
|
|
|
|
|
|
|
|
|
task_display = self.get_task_display(task_name)
|
|
|
|
|
model, name = task_name_list[0], task_name_list[-1]
|
|
|
|
|
func_name = '_func_%s_%s' % (model, name)
|
|
|
|
|
handle_func = getattr(self, func_name, None)
|
|
|
|
|
if handle_func is not None:
|
|
|
|
|
task_type, resource_ids = handle_func(*args, **kwargs)
|
|
|
|
|
if task_type:
|
|
|
|
|
task_display = '%s-%s' % (task_display, task_type)
|
|
|
|
|
return task_display, resource_ids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ActivityLogHandler:
|
|
|
|
|
@staticmethod
|
|
|
|
|
def session_for_activity(obj):
|
|
|
|
|