Commit 92f39676 authored by Administrator's avatar Administrator

[future] 完成获取硬件和链接测试的第一版v1

1. 完成数据结果的封装, 添加数据结构样列
2. 完成硬件和链接测试接口v1版
parent 97b8bcd5
...@@ -21,7 +21,6 @@ from ansible.plugins.callback import CallbackBase ...@@ -21,7 +21,6 @@ from ansible.plugins.callback import CallbackBase
from models import Tasker, AnsiblePlay, AnsibleTask, AnsibleHostResult from models import Tasker, AnsiblePlay, AnsibleTask, AnsibleHostResult
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -30,13 +29,13 @@ class AnsibleError(StandardError): ...@@ -30,13 +29,13 @@ class AnsibleError(StandardError):
class Config(object): class Config(object):
"""Ansible运行时配置类, 用于初始化Ansible. """Ansible运行时配置类, 用于初始化Ansible的一些默认配置.
""" """
def __init__(self, verbosity=None, inventory=None, listhosts=None, subset=None, module_paths=None, extra_vars=None, 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, 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, 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, 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): syntax=None, diff=None, force_handlers=None, flush_cache=None, listtasks=None, listtags=None, module_path=None):
self.verbosity = verbosity self.verbosity = verbosity
......
...@@ -142,8 +142,9 @@ class AnsibleHostResult(models.Model): ...@@ -142,8 +142,9 @@ class AnsibleHostResult(models.Model):
""" """
result = {} result = {}
for key in interfaces: for key in interfaces:
if "ansible_" + key in facts.keys(): gather_key = "ansible_" + key
result[key] = facts.get(key) if gather_key in facts.keys():
result[key] = facts.get(gather_key)
return result return result
def __deal_setup(self): def __deal_setup(self):
...@@ -175,7 +176,7 @@ class AnsibleHostResult(models.Model): ...@@ -175,7 +176,7 @@ class AnsibleHostResult(models.Model):
data['interface'] = self.__gather_interface(facts, interfaces) data['interface'] = self.__gather_interface(facts, interfaces)
return {"msg": None, "data": data} return {"msg": None, "data": data}
else: 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 @property
def deal_setup(self): def deal_setup(self):
......
# ~*~ coding: utf-8 ~*~
from __future__ import unicode_literals from __future__ import unicode_literals
from .tasks import * from .tasks import *
from .models import Tasker, AnsiblePlay, AnsibleTask, AnsibleHostResult from .models import Tasker
from uuid import uuid1 from uuid import uuid1
from celery.result import AsyncResult from celery.result import AsyncResult
...@@ -15,26 +16,71 @@ def get_result(task_id): ...@@ -15,26 +16,71 @@ def get_result(task_id):
return {"Completed": False, "data": None} return {"Completed": False, "data": None}
def start_get_hardware_info(*assets): def __get_result_by_tasker_id(tasker_uuid, deal_method):
name = "Get host hardware information"
uuid = "tasker-" + uuid1().hex
get_asset_hardware_info.delay(name, uuid, *assets)
return uuid
def __get_hardware_info(tasker_uuid):
tasker = Tasker.objects.get(uuid=tasker_uuid) tasker = Tasker.objects.get(uuid=tasker_uuid)
total = tasker.total_hosts
total_len = len(total)
host_results = [] host_results = []
# 存储数据
for play in tasker.plays.all(): for play in tasker.plays.all():
for t in play.tasks.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(): for h in t.host_results.all():
host_results.append(h) 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 return host_results
def start_get_hardware_info(*assets):
name = "Get host hardware information"
uuid = "tasker-" + uuid1().hex
get_asset_hardware_info.delay(name, uuid, *assets)
return uuid
def __get_hardware_info(tasker_uuid):
return __get_result_by_tasker_id(tasker_uuid, 'deal_setup')
def get_hardware_info(tasker_uuid): 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': {<device_name>: <device_detail_dict>},
u'env': {<env_name>: <env_value>},
u'interface': {<interface_name>: <interface_detail_dict>},
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: try:
return {"msg": None, "data": __get_hardware_info(tasker_uuid)} return {"msg": None, "data": __get_hardware_info(tasker_uuid)}
except Exception as e: except Exception as e:
...@@ -49,18 +95,24 @@ def start_ping_test(*assets): ...@@ -49,18 +95,24 @@ def start_ping_test(*assets):
def __get_ping_test(tasker_uuid): def __get_ping_test(tasker_uuid):
tasker = Tasker.objects.get(uuid=tasker_uuid) return __get_result_by_tasker_id(tasker_uuid, 'deal_ping')
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
def get_ping_test(tasker_uuid): 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: try:
return {"msg": None, "data": __get_ping_test(tasker_uuid)} return {"msg": None, "data": __get_ping_test(tasker_uuid)}
except Exception as e: except Exception as e:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment