diff --git a/apps/i18n/lina/en.json b/apps/i18n/lina/en.json index 34b8ace80..390aab8f0 100644 --- a/apps/i18n/lina/en.json +++ b/apps/i18n/lina/en.json @@ -1534,5 +1534,5 @@ "ConfirmRunningAssets": "Confirm running assets", "RunnableAssets": "Runnable assets", "NonRunnableAssets": "Non-runnable assets", - "AssetsSelected": "{count} Assets Selected" + "AssetsSelected": " Assets Selected" } diff --git a/apps/i18n/lina/zh.json b/apps/i18n/lina/zh.json index a7790929d..5f6a44f8f 100644 --- a/apps/i18n/lina/zh.json +++ b/apps/i18n/lina/zh.json @@ -1532,14 +1532,10 @@ "disallowSelfUpdateFields": "不允许自己修改当前字段", "forceEnableMFAHelpText": "如果强制启用,用户无法自行禁用", "removeWarningMsg": "你确定要移除", -<<<<<<< HEAD - "setVariable": "设置参数" -} -======= "setVariable": "设置参数", "ConfirmRunningAssets": "确认运行资产", "RunnableAssets": "可运行资产", "NonRunnableAssets": "不可运行资产", - "AssetsSelected": "已选择{count}个资产" + "AssetsSelected": " 个资产已选择" } ->>>>>>> d7d93e2f7 (perf: Translate) + diff --git a/apps/ops/ansible/inventory.py b/apps/ops/ansible/inventory.py index 01f8147d3..38627419e 100644 --- a/apps/ops/ansible/inventory.py +++ b/apps/ops/ansible/inventory.py @@ -283,11 +283,6 @@ class JMSInventory: return asset_protocols def get_classified_hosts(self, path_dir): - """ - 返回三种类型的主机:可运行的、错误的和跳过的 - :param path_dir: 存储密钥的路径 - :return: dict,包含三类主机信息 - """ hosts = [] platform_assets = self.group_by_platform(self.assets) runnable_hosts = [] diff --git a/apps/ops/api/__init__.py b/apps/ops/api/__init__.py index 4cdb987c9..0a96bbbdb 100644 --- a/apps/ops/api/__init__.py +++ b/apps/ops/api/__init__.py @@ -2,7 +2,6 @@ # from .adhoc import * from .celery import * -from .inventory import * from .job import * from .playbook import * from .variable import * diff --git a/apps/ops/api/inventory.py b/apps/ops/api/inventory.py deleted file mode 100644 index c013048e3..000000000 --- a/apps/ops/api/inventory.py +++ /dev/null @@ -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) diff --git a/apps/ops/api/job.py b/apps/ops/api/job.py index d257d2209..23add9baa 100644 --- a/apps/ops/api/job.py +++ b/apps/ops/api/job.py @@ -1,5 +1,6 @@ import json import os +import uuid from celery.result import AsyncResult from django.conf import settings @@ -18,14 +19,15 @@ from common.const.http import POST from common.permissions import IsValidUser from ops.celery import app from ops.const import Types -from ops.models import Job, JobExecution +from ops.models import Job, JobExecution, JMSPermedInventory from ops.serializers.job import ( JobSerializer, JobExecutionSerializer, FileSerializer, JobTaskStopSerializer ) from ops.utils import merge_nodes_and_assets __all__ = [ - 'JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView', 'JobExecutionTaskDetail', 'UsernameHintsAPI' + 'JobViewSet', 'JobExecutionViewSet', 'JobRunVariableHelpAPIView', 'JobExecutionTaskDetail', 'UsernameHintsAPI', + 'ClassifiedHostsAPI' ] from ops.tasks import run_ops_job_execution @@ -309,3 +311,28 @@ class UsernameHintsAPI(APIView): .annotate(total=Count('username')) \ .order_by('-total', '-username')[:10] 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) diff --git a/apps/ops/models/job.py b/apps/ops/models/job.py index 673548f75..7381196df 100644 --- a/apps/ops/models/job.py +++ b/apps/ops/models/job.py @@ -1,11 +1,11 @@ import json import logging import os -import sys import uuid from collections import defaultdict from datetime import timedelta +import sys from celery import current_task from django.conf import settings from django.db import models @@ -63,7 +63,8 @@ class JMSPermedInventory(JMSInventory): self.module = module 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: host['error'] = _("No account available") return host @@ -96,7 +97,8 @@ class JMSPermedInventory(JMSInventory): } host['jms_asset']['port'] = protocol.port 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): accounts = self.assets_accounts_mapper.get(asset.id, []) diff --git a/apps/ops/urls/api_urls.py b/apps/ops/urls/api_urls.py index c410cbcf2..4cd182f4e 100644 --- a/apps/ops/urls/api_urls.py +++ b/apps/ops/urls/api_urls.py @@ -28,7 +28,7 @@ urlpatterns = [ path('job-execution/task-detail//', api.JobExecutionTaskDetail.as_view(), name='task-detail'), path('username-hints/', api.UsernameHintsAPI.as_view(), name='username-hints'), path('ansible/job-execution//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//task-execution//log/', api.CeleryTaskExecutionLogApi.as_view(), name='celery-task-execution-log'),