perf: File directory adjustment

pull/15183/head
wangruidong 2025-04-09 18:44:33 +08:00 committed by w940853815
parent 467f4c5d4f
commit 5ab85d3561
8 changed files with 38 additions and 61 deletions

View File

@ -1534,5 +1534,5 @@
"ConfirmRunningAssets": "Confirm running assets", "ConfirmRunningAssets": "Confirm running assets",
"RunnableAssets": "Runnable assets", "RunnableAssets": "Runnable assets",
"NonRunnableAssets": "Non-runnable assets", "NonRunnableAssets": "Non-runnable assets",
"AssetsSelected": "{count} Assets Selected" "AssetsSelected": " Assets Selected"
} }

View File

@ -1532,14 +1532,10 @@
"disallowSelfUpdateFields": "不允许自己修改当前字段", "disallowSelfUpdateFields": "不允许自己修改当前字段",
"forceEnableMFAHelpText": "如果强制启用,用户无法自行禁用", "forceEnableMFAHelpText": "如果强制启用,用户无法自行禁用",
"removeWarningMsg": "你确定要移除", "removeWarningMsg": "你确定要移除",
<<<<<<< HEAD
"setVariable": "设置参数"
}
=======
"setVariable": "设置参数", "setVariable": "设置参数",
"ConfirmRunningAssets": "确认运行资产", "ConfirmRunningAssets": "确认运行资产",
"RunnableAssets": "可运行资产", "RunnableAssets": "可运行资产",
"NonRunnableAssets": "不可运行资产", "NonRunnableAssets": "不可运行资产",
"AssetsSelected": "已选择{count}个资产" "AssetsSelected": " 个资产已选择"
} }
>>>>>>> d7d93e2f7 (perf: Translate)

View File

@ -283,11 +283,6 @@ class JMSInventory:
return asset_protocols return asset_protocols
def get_classified_hosts(self, path_dir): def get_classified_hosts(self, path_dir):
"""
返回三种类型的主机可运行的错误的和跳过的
:param path_dir: 存储密钥的路径
:return: dict包含三类主机信息
"""
hosts = [] hosts = []
platform_assets = self.group_by_platform(self.assets) platform_assets = self.group_by_platform(self.assets)
runnable_hosts = [] runnable_hosts = []

View File

@ -2,7 +2,6 @@
# #
from .adhoc import * from .adhoc import *
from .celery import * from .celery import *
from .inventory import *
from .job import * from .job import *
from .playbook import * from .playbook import *
from .variable import * from .variable import *

View File

@ -1,42 +0,0 @@
import os
import uuid
from django.conf import settings
from rest_framework.response import Response
from rest_framework.views import APIView
from assets.models import Asset
from common.permissions import IsValidUser
from ops.models.job import JMSPermedInventory
__all__ = ['InventoryClassifiedHostsAPI']
class InventoryClassifiedHostsAPI(APIView):
permission_classes = [IsValidUser]
def post(self, request, **kwargs):
asset_ids = request.data.get('assets', [])
node_ids = request.data.get('nodes', [])
runas_policy = request.data.get('runas_policy', 'privileged_first')
account_prefer = request.data.get('runas', 'root,Administrator')
module = request.data.get('module', 'shell')
# 合并节点和资产
assets = list(Asset.objects.filter(id__in=asset_ids).all())
# 创建临时目录
tmp_dir = os.path.join(settings.PROJECT_DIR, 'inventory', str(uuid.uuid4()))
os.makedirs(tmp_dir, exist_ok=True)
# 创建库存对象并获取分类的主机
inventory = JMSPermedInventory(
assets=assets,
nodes=node_ids,
module=module,
account_policy=runas_policy,
account_prefer=account_prefer,
user=self.request.user
)
classified_hosts = inventory.get_classified_hosts(tmp_dir)
return Response(data=classified_hosts)

View File

@ -1,5 +1,6 @@
import json import json
import os import os
import uuid
from celery.result import AsyncResult from celery.result import AsyncResult
from django.conf import settings from django.conf import settings
@ -18,14 +19,15 @@ from common.const.http import POST
from common.permissions import IsValidUser from common.permissions import IsValidUser
from ops.celery import app from ops.celery import app
from ops.const import Types from ops.const import Types
from ops.models import Job, JobExecution from ops.models import Job, JobExecution, JMSPermedInventory
from ops.serializers.job import ( from ops.serializers.job import (
JobSerializer, JobExecutionSerializer, FileSerializer, JobTaskStopSerializer JobSerializer, JobExecutionSerializer, FileSerializer, JobTaskStopSerializer
) )
from ops.utils import merge_nodes_and_assets from ops.utils import merge_nodes_and_assets
__all__ = [ __all__ = [
'JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView', 'JobExecutionTaskDetail', 'UsernameHintsAPI' 'JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView', 'JobExecutionTaskDetail', 'UsernameHintsAPI',
'ClassifiedHostsAPI'
] ]
from ops.tasks import run_ops_job_execution from ops.tasks import run_ops_job_execution
@ -309,3 +311,28 @@ class UsernameHintsAPI(APIView):
.annotate(total=Count('username')) \ .annotate(total=Count('username')) \
.order_by('-total', '-username')[:10] .order_by('-total', '-username')[:10]
return Response(data=top_accounts) return Response(data=top_accounts)
class ClassifiedHostsAPI(APIView):
permission_classes = [IsValidUser]
def post(self, request, **kwargs):
asset_ids = request.data.get('assets', [])
node_ids = request.data.get('nodes', [])
runas_policy = request.data.get('runas_policy', 'privileged_first')
account_prefer = request.data.get('runas', 'root,Administrator')
module = request.data.get('module', 'shell')
assets = list(Asset.objects.filter(id__in=asset_ids).all())
tmp_dir = os.path.join(settings.PROJECT_DIR, 'inventory', str(uuid.uuid4()))
os.makedirs(tmp_dir, exist_ok=True)
inventory = JMSPermedInventory(
assets=assets,
nodes=node_ids,
module=module,
account_policy=runas_policy,
account_prefer=account_prefer,
user=self.request.user
)
classified_hosts = inventory.get_classified_hosts(tmp_dir)
return Response(data=classified_hosts)

View File

@ -1,11 +1,11 @@
import json import json
import logging import logging
import os import os
import sys
import uuid import uuid
from collections import defaultdict from collections import defaultdict
from datetime import timedelta from datetime import timedelta
import sys
from celery import current_task from celery import current_task
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
@ -63,7 +63,8 @@ class JMSPermedInventory(JMSInventory):
self.module = module self.module = module
self.assets_accounts_mapper = self.get_assets_accounts_mapper() self.assets_accounts_mapper = self.get_assets_accounts_mapper()
def make_account_vars(self, host, asset, account, automation, protocol, platform, gateway, path_dir): def make_account_vars(self, host, asset, account, automation, protocol, platform, gateway, path_dir,
ansible_config):
if not account: if not account:
host['error'] = _("No account available") host['error'] = _("No account available")
return host return host
@ -96,7 +97,8 @@ class JMSPermedInventory(JMSInventory):
} }
host['jms_asset']['port'] = protocol.port host['jms_asset']['port'] = protocol.port
return host return host
return super().make_account_vars(host, asset, account, automation, protocol, platform, gateway, path_dir) return super().make_account_vars(host, asset, account, automation, protocol, platform, gateway, path_dir,
ansible_config)
def get_asset_sorted_accounts(self, asset): def get_asset_sorted_accounts(self, asset):
accounts = self.assets_accounts_mapper.get(asset.id, []) accounts = self.assets_accounts_mapper.get(asset.id, [])

View File

@ -28,7 +28,7 @@ urlpatterns = [
path('job-execution/task-detail/<uuid:task_id>/', api.JobExecutionTaskDetail.as_view(), name='task-detail'), path('job-execution/task-detail/<uuid:task_id>/', api.JobExecutionTaskDetail.as_view(), name='task-detail'),
path('username-hints/', api.UsernameHintsAPI.as_view(), name='username-hints'), path('username-hints/', api.UsernameHintsAPI.as_view(), name='username-hints'),
path('ansible/job-execution/<uuid:pk>/log/', api.AnsibleTaskLogApi.as_view(), name='job-execution-log'), path('ansible/job-execution/<uuid:pk>/log/', api.AnsibleTaskLogApi.as_view(), name='job-execution-log'),
path('inventory/classified-hosts/', api.InventoryClassifiedHostsAPI.as_view(), name='inventory-classified-hosts'), path('classified-hosts/', api.ClassifiedHostsAPI.as_view(), name='classified-hosts'),
path('celery/task/<uuid:name>/task-execution/<uuid:pk>/log/', api.CeleryTaskExecutionLogApi.as_view(), path('celery/task/<uuid:name>/task-execution/<uuid:pk>/log/', api.CeleryTaskExecutionLogApi.as_view(),
name='celery-task-execution-log'), name='celery-task-execution-log'),