2016-11-01 02:37:03 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2016-10-28 09:28:32 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-11-23 03:45:50 +00:00
|
|
|
from ops.tasks import _celery_tasks
|
2016-10-28 09:28:32 +00:00
|
|
|
|
2016-12-19 06:07:21 +00:00
|
|
|
from ops.models import TaskRecord
|
2016-10-31 09:41:26 +00:00
|
|
|
from uuid import uuid1
|
|
|
|
from celery.result import AsyncResult
|
2016-10-28 09:28:32 +00:00
|
|
|
|
2016-12-11 04:05:11 +00:00
|
|
|
__all__ = ["get_result",
|
|
|
|
"start_get_hardware_info",
|
|
|
|
"start_ping_test",
|
|
|
|
"get_hardware_info",
|
|
|
|
"get_ping_test"]
|
|
|
|
|
2016-10-28 09:28:32 +00:00
|
|
|
|
2016-10-28 09:43:56 +00:00
|
|
|
def get_result(task_id):
|
2016-10-28 09:28:32 +00:00
|
|
|
result = AsyncResult(task_id)
|
|
|
|
if result.ready():
|
2016-10-28 09:43:56 +00:00
|
|
|
return {"Completed": True, "data": result.get()}
|
2016-10-28 09:28:32 +00:00
|
|
|
else:
|
2016-10-28 09:43:56 +00:00
|
|
|
return {"Completed": False, "data": None}
|
|
|
|
|
|
|
|
|
2016-11-01 02:37:03 +00:00
|
|
|
def __get_result_by_tasker_id(tasker_uuid, deal_method):
|
2016-12-19 06:07:21 +00:00
|
|
|
tasker = TaskRecord.objects.get(uuid=tasker_uuid)
|
2016-11-01 02:37:03 +00:00
|
|
|
total = tasker.total_hosts
|
|
|
|
total_len = len(total)
|
2016-10-31 09:41:26 +00:00
|
|
|
host_results = []
|
|
|
|
|
2016-11-01 02:37:03 +00:00
|
|
|
# 存储数据
|
2016-10-31 09:41:26 +00:00
|
|
|
for play in tasker.plays.all():
|
|
|
|
for t in play.tasks.all():
|
2016-11-01 02:37:03 +00:00
|
|
|
task = {'name': t.name, 'uuid': t.uuid, 'percentage': 0, 'completed': {'success': {}, 'failed': {}}}
|
|
|
|
completed = []
|
|
|
|
count = 0
|
2016-10-31 09:41:26 +00:00
|
|
|
for h in t.host_results.all():
|
2016-11-01 02:37:03 +00:00
|
|
|
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)
|
2016-10-31 09:41:26 +00:00
|
|
|
|
|
|
|
return host_results
|
|
|
|
|
|
|
|
|
2016-11-01 02:37:03 +00:00
|
|
|
def start_get_hardware_info(*assets):
|
|
|
|
name = "Get host hardware information"
|
|
|
|
uuid = "tasker-" + uuid1().hex
|
2016-11-23 03:45:50 +00:00
|
|
|
_celery_tasks.get_asset_hardware_info.delay(name, uuid, *assets)
|
2016-11-01 02:37:03 +00:00
|
|
|
return uuid
|
|
|
|
|
|
|
|
|
|
|
|
def __get_hardware_info(tasker_uuid):
|
|
|
|
return __get_result_by_tasker_id(tasker_uuid, 'deal_setup')
|
|
|
|
|
|
|
|
|
2016-10-31 09:41:26 +00:00
|
|
|
def get_hardware_info(tasker_uuid):
|
2016-11-01 02:37:03 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
: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}
|
|
|
|
"""
|
2016-10-31 09:41:26 +00:00
|
|
|
try:
|
|
|
|
return {"msg": None, "data": __get_hardware_info(tasker_uuid)}
|
|
|
|
except Exception as e:
|
|
|
|
return {"msg": "query data failed!, %s" % e.message, "data": None}
|
|
|
|
|
|
|
|
|
2016-10-28 09:43:56 +00:00
|
|
|
def start_ping_test(*assets):
|
2016-10-31 09:41:26 +00:00
|
|
|
name = "Test host connection"
|
|
|
|
uuid = "tasker-" + uuid1().hex
|
2016-11-23 03:45:50 +00:00
|
|
|
_celery_tasks.asset_test_ping_check.delay(name, uuid, *assets)
|
2016-10-31 09:41:26 +00:00
|
|
|
return uuid
|
|
|
|
|
|
|
|
|
|
|
|
def __get_ping_test(tasker_uuid):
|
2016-11-01 02:37:03 +00:00
|
|
|
return __get_result_by_tasker_id(tasker_uuid, 'deal_ping')
|
2016-10-31 09:41:26 +00:00
|
|
|
|
2016-10-28 09:43:56 +00:00
|
|
|
|
2016-10-31 09:41:26 +00:00
|
|
|
def get_ping_test(tasker_uuid):
|
2016-11-01 02:37:03 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
: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}
|
|
|
|
"""
|
2016-10-31 09:41:26 +00:00
|
|
|
try:
|
|
|
|
return {"msg": None, "data": __get_ping_test(tasker_uuid)}
|
|
|
|
except Exception as e:
|
|
|
|
return {"msg": "query data failed!, %s" % e.message, "data": None}
|
2016-10-28 09:43:56 +00:00
|
|
|
|