import json
import os.path
import codecs
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common_utils import generate_table
from ansible.module_utils.path_manager import TmpPath
from ansible.module_utils.common_info import TestReport
def to_json(file_path, table_name, data):
"""
description: 将test结果转换成json文件
"""
with codecs.open(file_path, "a+", encoding="utf8") as f:
f.seek(0)
if not f.read().strip():
json_data = dict()
else:
f.seek(0)
json_data = json.load(f)
test_data = {table_name: data}
json_data.update(test_data)
f.seek(0)
f.truncate()
f.write(json.dumps(json_data, ensure_ascii=False))
def list_to_dict(result_list):
result_dict = {}
for item in result_list:
for key, value in item.items():
if key not in result_dict:
result_dict[key] = value
else:
result_dict[key].update(value)
return result_dict
def generate_test_result_table(module, cann_result, dl_result):
table_npu = generate_table(cann_result, "npu-clusters", TestReport.COLUMNS_NPU, "npu")
table_mcu = generate_table(cann_result, "mcu-version", TestReport.COLUMNS_MCU, "ip")
table_cann = generate_table(cann_result, "cann-clusters", TestReport.COLUMNS_CANN, "cann")
table_pypkg = generate_table(cann_result, "pypkg-clusters", TestReport.COLUMNS_PYPKG, "pypkg")
cann_table_result_str = "{}\n\n{}\n\n{}\n\n{}\n".format(table_npu, table_mcu, table_cann, table_pypkg)
table_master = generate_table(dl_result, "dl-clusters(master-node)", TestReport.COLUMNS_MASTER, "master-node")
table_worker_pod = generate_table(dl_result, "dl-clusters(worker-node)", TestReport.COLUMNS_WORKER_POD,
"worker-node")
table_worker_physical = generate_table(dl_result, "dl-clusters(worker-node-physical)",
TestReport.COLUMNS_WORKER_PHYSICAL, "worker-node")
cann_test_result_str = dl_test_result = ""
for node in module.params["cann_test_result"]:
cann_test_result_str += '\n{}'.format(node)
for node in dl_result:
dl_test_result += '\n{}: {}'.format(node, dl_result.get(node))
table_result_str = "[ASCEND]{}\n{}\n\n\n{}\n\n{}\n\n{}\n\n{}\n".format(cann_test_result_str, dl_test_result,
cann_table_result_str, table_master,
table_worker_pod, table_worker_physical)
return table_result_str
def output_test_result_json(cann_result, dl_result):
if os.path.exists(TmpPath.TEST_REPORT_JSON):
os.remove(TmpPath.TEST_REPORT_JSON)
if not os.path.exists(TmpPath.DEPLOY_INFO):
os.makedirs(TmpPath.DEPLOY_INFO, mode=0o750)
to_json(TmpPath.TEST_REPORT_JSON, TestReport.ASCEND_SOFTWARE_TEST_REPORT, cann_result)
to_json(TmpPath.TEST_REPORT_JSON, TestReport.DL_TEST_REPORT, dl_result)
def main():
module = AnsibleModule(argument_spec=dict(
cann_test_result=dict(type="list", required=True),
docker_runtime_result=dict(type="list", required=True),
dl_result=dict(type="dict", required=True)
)
)
dl_result = module.params["dl_result"]
cann_result = list_to_dict(module.params["cann_test_result"])
docker_runtime_result = list_to_dict(module.params["docker_runtime_result"])
temp_updates = {}
for key, value in docker_runtime_result.items():
for host_info in dl_result:
if key == host_info.split(':')[0].strip():
dl_result[host_info].update(value)
break
else:
temp_updates[key] = value
dl_result.update(temp_updates)
table_result_str = generate_test_result_table(module, cann_result, dl_result)
output_test_result_json(cann_result, dl_result)
return module.exit_json(changed=True, rc=0, msg=table_result_str)
if __name__ == "__main__":
main()