From 43b4b7c55eedf22daae39d12f7f69b0a75b6935e Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 12 Mar 2018 11:41:12 +0800 Subject: [PATCH] =?UTF-8?q?[Feature]=20=E8=B5=84=E4=BA=A7=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E5=A2=9E=E5=8A=A0=E6=89=B9=E9=87=8F=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=8F=AF=E8=BF=9E=E6=8E=A5=E6=80=A7=E5=92=8C=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=A1=AC=E4=BB=B6=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/node.py | 32 ++++- apps/assets/signals_handler.py | 2 +- apps/assets/tasks.py | 19 ++- apps/assets/templates/assets/asset_list.html | 45 +++++++ apps/assets/urls/api_urls.py | 2 + apps/i18n/zh/LC_MESSAGES/django.mo | Bin 29902 -> 30169 bytes apps/i18n/zh/LC_MESSAGES/django.po | 119 +++++++++++-------- 7 files changed, 157 insertions(+), 62 deletions(-) diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index 78cd7a27a..3c6ff292d 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -18,10 +18,12 @@ from rest_framework.views import APIView from rest_framework.response import Response from rest_framework_bulk import BulkModelViewSet from django.utils.translation import ugettext_lazy as _ +from django.shortcuts import get_object_or_404 from common.utils import get_logger, get_object_or_none from ..hands import IsSuperUser from ..models import Node +from ..tasks import update_assets_hardware_info_util, test_asset_connectability_util from .. import serializers @@ -29,7 +31,8 @@ logger = get_logger(__file__) __all__ = [ 'NodeViewSet', 'NodeChildrenApi', 'NodeAddAssetsApi', 'NodeRemoveAssetsApi', - 'NodeAddChildrenApi', + 'NodeAddChildrenApi', 'RefreshNodeHardwareInfoApi', + 'TestNodeConnectiveApi' ] @@ -117,3 +120,30 @@ class NodeRemoveAssetsApi(generics.UpdateAPIView): instance = self.get_object() if instance != Node.root(): instance.assets.remove(*tuple(assets)) + + +class RefreshNodeHardwareInfoApi(APIView): + permission_classes = (IsSuperUser,) + model = Node + + def get(self, request, *args, **kwargs): + node_id = kwargs.get('pk') + node = get_object_or_404(self.model, id=node_id) + assets = node.assets.all() + task_name = _("Refresh node assets hardware info: {}".format(node.name)) + update_assets_hardware_info_util.delay(assets, task_name=task_name) + return Response({"msg": "Task created"}) + + +class TestNodeConnectiveApi(APIView): + permission_classes = (IsSuperUser,) + model = Node + + def get(self, request, *args, **kwargs): + node_id = kwargs.get('pk') + node = get_object_or_404(self.model, id=node_id) + assets = node.assets.all() + task_name = _("Test node assets connective: {}".format(node.name)) + test_asset_connectability_util.delay(assets, task_name=task_name) + return Response({"msg": "Task created"}) + diff --git a/apps/assets/signals_handler.py b/apps/assets/signals_handler.py index 2577c7a6c..fe5508720 100644 --- a/apps/assets/signals_handler.py +++ b/apps/assets/signals_handler.py @@ -21,7 +21,7 @@ def update_asset_hardware_info_on_created(asset): def test_asset_conn_on_created(asset): logger.debug("Test asset `{}` connectability".format(asset)) - test_asset_connectability_util.delay(asset) + test_asset_connectability_util.delay([asset]) def set_asset_root_node(asset): diff --git a/apps/assets/tasks.py b/apps/assets/tasks.py index be9f75152..a6ffd2270 100644 --- a/apps/assets/tasks.py +++ b/apps/assets/tasks.py @@ -213,12 +213,12 @@ def test_admin_user_connectability_manual(admin_user): @shared_task -def test_asset_connectability_util(asset, task_name=None): +def test_asset_connectability_util(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: - task_name = _("Test asset connectability") - hosts = [asset.hostname] + task_name = _("Test assets connectability") + hosts = [asset.hostname for asset in assets] if not hosts: logger.info("No hosts, passed") return {} @@ -229,18 +229,17 @@ def test_asset_connectability_util(asset, task_name=None): ) result = task.run() summary = result[1] - if summary.get('dark'): - cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(asset.hostname), 0, - CACHE_MAX_TIME) - else: - cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(asset.hostname), 1, - CACHE_MAX_TIME) + for k in summary.get('dark'): + cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(k), 0, CACHE_MAX_TIME) + + for k in summary.get('contacted'): + cache.set(const.ASSET_ADMIN_CONN_CACHE_KEY.format(k), 1, CACHE_MAX_TIME) return summary @shared_task def test_asset_connectability_manual(asset): - summary = test_asset_connectability_util(asset) + summary = test_asset_connectability_util([asset]) if summary.get('dark'): return False, summary['dark'] diff --git a/apps/assets/templates/assets/asset_list.html b/apps/assets/templates/assets/asset_list.html index 628d0f905..da1b0be58 100644 --- a/apps/assets/templates/assets/asset_list.html +++ b/apps/assets/templates/assets/asset_list.html @@ -119,6 +119,8 @@