2017-03-15 16:43:43 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2020-12-29 13:05:18 +00:00
|
|
|
import os
|
2021-01-19 06:36:41 +00:00
|
|
|
import uuid
|
2023-07-17 05:59:45 +00:00
|
|
|
from django.conf import settings
|
2020-12-29 13:05:18 +00:00
|
|
|
|
2023-07-17 05:59:45 +00:00
|
|
|
from common.utils import get_logger, make_dirs
|
2021-01-19 06:36:41 +00:00
|
|
|
from jumpserver.const import PROJECT_DIR
|
2024-11-07 02:38:34 +00:00
|
|
|
from perms.models import PermNode
|
|
|
|
from perms.utils import UserPermAssetUtil
|
|
|
|
from assets.models import Asset, Node
|
2020-03-12 08:24:38 +00:00
|
|
|
|
2017-03-15 16:43:43 +00:00
|
|
|
logger = get_logger(__file__)
|
2017-12-06 10:31:51 +00:00
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
|
2020-12-29 13:05:18 +00:00
|
|
|
def get_task_log_path(base_path, task_id, level=2):
|
|
|
|
task_id = str(task_id)
|
2021-01-19 06:36:41 +00:00
|
|
|
try:
|
|
|
|
uuid.UUID(task_id)
|
|
|
|
except:
|
|
|
|
return os.path.join(PROJECT_DIR, 'data', 'caution.txt')
|
|
|
|
|
2020-12-29 13:05:18 +00:00
|
|
|
rel_path = os.path.join(*task_id[:level], task_id + '.log')
|
|
|
|
path = os.path.join(base_path, rel_path)
|
2022-10-18 09:47:13 +00:00
|
|
|
make_dirs(os.path.dirname(path), exist_ok=True)
|
2020-12-29 13:05:18 +00:00
|
|
|
return path
|
2022-09-07 11:49:42 +00:00
|
|
|
|
2023-07-17 05:59:45 +00:00
|
|
|
|
|
|
|
def get_ansible_log_verbosity(verbosity=0):
|
|
|
|
if settings.DEBUG_ANSIBLE:
|
|
|
|
return 10
|
|
|
|
if verbosity is None and settings.DEBUG:
|
|
|
|
return 1
|
|
|
|
return verbosity
|
|
|
|
|
2024-11-07 02:38:34 +00:00
|
|
|
|
|
|
|
def merge_nodes_and_assets(nodes, assets, user):
|
|
|
|
if not nodes:
|
|
|
|
return assets
|
|
|
|
perm_util = UserPermAssetUtil(user=user)
|
|
|
|
for node_id in nodes:
|
|
|
|
if isinstance(node_id, Node):
|
|
|
|
node_id = node_id.id
|
|
|
|
if node_id == PermNode.FAVORITE_NODE_KEY:
|
|
|
|
node_assets = perm_util.get_favorite_assets()
|
|
|
|
elif node_id == PermNode.UNGROUPED_NODE_KEY:
|
|
|
|
node_assets = perm_util.get_ungroup_assets()
|
|
|
|
else:
|
2024-11-08 09:50:33 +00:00
|
|
|
node, node_assets = perm_util.get_node_all_assets(node_id)
|
2024-11-07 02:38:34 +00:00
|
|
|
assets.extend(node_assets.exclude(id__in=[asset.id for asset in assets]))
|
|
|
|
return assets
|