From 92f396761cbd21291feed8d87df4b86a1bf1a7b9 Mon Sep 17 00:00:00 2001 From: Administrator Date: Tue, 1 Nov 2016 10:37:03 +0800 Subject: [PATCH] =?UTF-8?q?[future]=20=E5=AE=8C=E6=88=90=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=A1=AC=E4=BB=B6=E5=92=8C=E9=93=BE=E6=8E=A5=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=9A=84=E7=AC=AC=E4=B8=80=E7=89=88v1=201.=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=9C=E7=9A=84=E5=B0=81?= =?UTF-8?q?=E8=A3=85,=20=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E6=A0=B7=E5=88=97=202.=20=E5=AE=8C=E6=88=90=E7=A1=AC?= =?UTF-8?q?=E4=BB=B6=E5=92=8C=E9=93=BE=E6=8E=A5=E6=B5=8B=E8=AF=95=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3v1=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/ansible_api.py | 7 ++-- apps/ops/models.py | 7 ++-- apps/ops/taskers.py | 90 ++++++++++++++++++++++++++++++++--------- 3 files changed, 78 insertions(+), 26 deletions(-) diff --git a/apps/ops/ansible_api.py b/apps/ops/ansible_api.py index 09fb06cd5..48a313767 100644 --- a/apps/ops/ansible_api.py +++ b/apps/ops/ansible_api.py @@ -21,7 +21,6 @@ from ansible.plugins.callback import CallbackBase from models import Tasker, AnsiblePlay, AnsibleTask, AnsibleHostResult - logger = logging.getLogger(__name__) @@ -30,13 +29,13 @@ class AnsibleError(StandardError): class Config(object): - """Ansible运行时配置类, 用于初始化Ansible. + """Ansible运行时配置类, 用于初始化Ansible的一些默认配置. """ def __init__(self, verbosity=None, inventory=None, listhosts=None, subset=None, module_paths=None, extra_vars=None, - forks=None, ask_vault_pass=False, vault_password_files=None, new_vault_password_file=None, + forks=10, ask_vault_pass=False, vault_password_files=None, new_vault_password_file=None, output_file=None, tags=None, skip_tags=None, one_line=None, tree=None, ask_sudo_pass=False, ask_su_pass=False, sudo=None, sudo_user=None, become=None, become_method=None, become_user=None, become_ask_pass=False, - ask_pass=False, private_key_file=None, remote_user=None, connection="smart", timeout=None, ssh_common_args=None, + ask_pass=False, private_key_file=None, remote_user=None, connection="smart", timeout=10, ssh_common_args=None, sftp_extra_args=None, scp_extra_args=None, ssh_extra_args=None, poll_interval=None, seconds=None, check=False, syntax=None, diff=None, force_handlers=None, flush_cache=None, listtasks=None, listtags=None, module_path=None): self.verbosity = verbosity diff --git a/apps/ops/models.py b/apps/ops/models.py index d3e52566e..812445531 100644 --- a/apps/ops/models.py +++ b/apps/ops/models.py @@ -142,8 +142,9 @@ class AnsibleHostResult(models.Model): """ result = {} for key in interfaces: - if "ansible_" + key in facts.keys(): - result[key] = facts.get(key) + gather_key = "ansible_" + key + if gather_key in facts.keys(): + result[key] = facts.get(gather_key) return result def __deal_setup(self): @@ -175,7 +176,7 @@ class AnsibleHostResult(models.Model): data['interface'] = self.__gather_interface(facts, interfaces) return {"msg": None, "data": data} else: - return {"msg": "there isn't module_name field! can't process this data format", "data": None} + return {"msg": "there result isn't ansible setup module result! can't process this data format", "data": None} @property def deal_setup(self): diff --git a/apps/ops/taskers.py b/apps/ops/taskers.py index 17dbd3a5b..814a202ea 100644 --- a/apps/ops/taskers.py +++ b/apps/ops/taskers.py @@ -1,8 +1,9 @@ +# ~*~ coding: utf-8 ~*~ from __future__ import unicode_literals from .tasks import * -from .models import Tasker, AnsiblePlay, AnsibleTask, AnsibleHostResult +from .models import Tasker from uuid import uuid1 from celery.result import AsyncResult @@ -15,6 +16,39 @@ def get_result(task_id): return {"Completed": False, "data": None} +def __get_result_by_tasker_id(tasker_uuid, deal_method): + tasker = Tasker.objects.get(uuid=tasker_uuid) + total = tasker.total_hosts + total_len = len(total) + host_results = [] + + # 存储数据 + for play in tasker.plays.all(): + for t in play.tasks.all(): + task = {'name': t.name, 'uuid': t.uuid, 'percentage': 0, 'completed': {'success': {}, 'failed': {}}} + completed = [] + count = 0 + for h in t.host_results.all(): + completed.append(h.name) + count += 1 + if h.is_success: + result = getattr(h, deal_method) + if result.get('msg') is None: + task['completed']['success'][h.name] = result.get('data') + else: + task['completed']['failed'][h.name] = result.get('msg') + else: + task['completed']['failed'][h.name] = h.failed_msg + + # 计算进度 + task['percentage'] = float(count * 100 / total_len) + task['waited'] = list(set(total) - set(completed)) + + host_results.append(task) + + return host_results + + def start_get_hardware_info(*assets): name = "Get host hardware information" uuid = "tasker-" + uuid1().hex @@ -23,18 +57,30 @@ def start_get_hardware_info(*assets): def __get_hardware_info(tasker_uuid): - tasker = Tasker.objects.get(uuid=tasker_uuid) - host_results = [] - - for play in tasker.plays.all(): - for t in play.tasks.all(): - for h in t.host_results.all(): - host_results.append(h) - - return host_results + return __get_result_by_tasker_id(tasker_uuid, 'deal_setup') def get_hardware_info(tasker_uuid): + """ + + :param assets: 资产列表 + :return: 返回数据结构样列 + {u'data': [{u'completed': { + u'failed': {u'192.168.232.135': u'Authentication failure.'}, + u'success': {u'192.168.1.119': {u'cpu': u'GenuineIntel Intel Xeon E312xx (Sandy Bridge) 6\u6838', + u'disk': {: }, + u'env': {: }, + u'interface': {: }, + u'mem': 3951, + u'os': u'Ubuntu 16.04(xenial)', + u'sn': u'NA'}}}, + u'name': u'', + u'percentage': 100.0, + u'uuid': u'87cfedfe-ba55-44ff-bc43-e7e73b869ca1', + u'waited': []} + ], + u'msg': None} + """ try: return {"msg": None, "data": __get_hardware_info(tasker_uuid)} except Exception as e: @@ -49,18 +95,24 @@ def start_ping_test(*assets): def __get_ping_test(tasker_uuid): - tasker = Tasker.objects.get(uuid=tasker_uuid) - host_results = [] - - for play in tasker.plays.all(): - for t in play.tasks.all(): - for h in t.host_results.all(): - host_results.append(h) - - return host_results + return __get_result_by_tasker_id(tasker_uuid, 'deal_ping') def get_ping_test(tasker_uuid): + """ + + :param assets: 资产列表 + :return: 返回数据结构样列 + {u'data': [{u'completed': { + u'failed': {u'192.168.232.135': u'Authentication failure.'}, + u'success': {u'192.168.1.119': {u'success': True}}}, + u'name': u'', + u'percentage': 100.0, + u'uuid': u'3e6e0d3b-bee0-4383-b19e-bec6ba55d346', + u'waited': []} + ], + u'msg': None} + """ try: return {"msg": None, "data": __get_ping_test(tasker_uuid)} except Exception as e: