From 1466a560a51a789a9ee877364b589af7bbbd8a29 Mon Sep 17 00:00:00 2001 From: qz_cx Date: Wed, 15 Oct 2025 10:13:59 +0800 Subject: [PATCH] After clicking edit, the connection status will automatically update --- appStore/testMachine/models.py | 2 ++ appStore/testMachine/views.py | 11 +++---- appStore/utils/common.py | 25 +++++++++++++- .../src/views/machineViews/ServerList.vue | 33 ++++++++++++------- 4 files changed, 52 insertions(+), 19 deletions(-) diff --git a/appStore/testMachine/models.py b/appStore/testMachine/models.py index 65107bd..0e71842 100644 --- a/appStore/testMachine/models.py +++ b/appStore/testMachine/models.py @@ -14,6 +14,8 @@ class TestMachine(models.Model): Link_Status_Type = ( ("在线", "在线"), ("离线", "离线"), + ("网络未连接", "网络未连接"), + ("用户名或密码错误", "用户名或密码错误"), ) Task_Status_Type = ( ("空闲", "空闲"), diff --git a/appStore/testMachine/views.py b/appStore/testMachine/views.py index 2ca5c22..ba1fb11 100644 --- a/appStore/testMachine/views.py +++ b/appStore/testMachine/views.py @@ -9,7 +9,7 @@ from appStore.testMachine.models import TestMachine from appStore.testMachine.serializers import TestMachineSerializer from rest_framework import status, viewsets -from appStore.utils.common import json_response +from appStore.utils.common import json_response, get_link_status # Create your views here. @@ -75,15 +75,14 @@ class TestMachineViewSet(viewsets.ModelViewSet): machine_data.server_user_name = request.data.get('server_user_name') machine_data.server_password = request.data.get('server_password') machine_data.os_version = request.data.get('os_version') - # todo link 使用impi命令获取当前状态 - # machine_data.link_status = + machine_data.link_status = get_link_status(machine_data.BMC_IP, machine_data.BMC_user_name, + machine_data.BMC_password, machine_data.server_IP, + machine_data.server_user_name, machine_data.server_password) # TODO 通过后端逻辑获取 # machine_data.task_status = machine_data.save() return json_response({}, status.HTTP_200_OK, '修改成功') - - def delete(self, request, *args, **kwargs): id = request.data.get('id', None) if not id or not TestMachine.objects.filter(id=id): @@ -95,4 +94,4 @@ class TestMachineViewSet(viewsets.ModelViewSet): TestMachine.objects.filter(id=id).delete() return json_response({}, status.HTTP_200_OK, '删除成功') else: - return json_response({}, status.HTTP_205_RESET_CONTENT, '只有管理员才能删除该数据') \ No newline at end of file + return json_response({}, status.HTTP_205_RESET_CONTENT, '只有管理员才能删除该数据') diff --git a/appStore/utils/common.py b/appStore/utils/common.py index b42a2e3..b0b99fa 100644 --- a/appStore/utils/common.py +++ b/appStore/utils/common.py @@ -161,7 +161,6 @@ def test_case(test_ip, test_username, test_password, test_case_names, user_confi ssh_command = f'sshpass -p {test_password} ssh {test_username}@{test_ip} "cd /root/run_kytuning-ffdev/;sh /root/run_kytuning-ffdev/run.sh"' return_result = subprocess.run(ssh_command, stdout=f, stderr=f, encoding='utf-8', shell=True) - # 保存kytuning的日志,先获取此次测试测试了哪几项,在获取对应的kytuning.log文件 for test_case_name in test_case_names: klog_command = f'sshpass -p {test_password} scp -o StrictHostKeyChecking=no -r {test_username}@{test_ip}:/root/kytuning/run/{test_case_name}/kytuning.log {result_log_name}_{test_case_name}_kytuning.log' @@ -186,3 +185,27 @@ def test_case(test_ip, test_username, test_password, test_case_names, user_confi return return_result return klog_result + + +def get_link_status(BMC_IP, BMC_user_name, BMC_password, server_IP, server_user_name, server_password): + + # 判断离线状态 f是可以使用变量 + ipmi_cmd = f"ipmitool -H {BMC_IP} -I lanplus -U {BMC_user_name} -P \'{BMC_password}\' chassis power status" + result = subprocess.run(ipmi_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + if result.stdout.strip() == 'Chassis Power is off': + return '离线' + + # 判断网络无法链接状态 + result = subprocess.run(["ping", "-c", "1", "-w", "1", server_IP], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + output = result.stdout + if not "1 packets transmitted, 1 received" in output: + return '网络未连接' + + ssh_cmd = f'sshpass -p {server_password} ssh -o StrictHostKeyChecking=no {server_user_name}@{server_IP}' + result = subprocess.run(ssh_cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + + # 获取返回状态 0 代表成功 + if result.returncode: + return '用户名或密码错误' + + return '在线' \ No newline at end of file diff --git a/templates/front-project/src/views/machineViews/ServerList.vue b/templates/front-project/src/views/machineViews/ServerList.vue index 7c27cb0..65e62b8 100644 --- a/templates/front-project/src/views/machineViews/ServerList.vue +++ b/templates/front-project/src/views/machineViews/ServerList.vue @@ -17,7 +17,16 @@ - + + + @@ -67,11 +76,11 @@ export default { return { allDatas: [], modifyID: 0, - machineData:{ - 'server_IP':'', - 'server_user_name':'', - 'server_password':'', - 'os_version':'', + machineData: { + 'server_IP': '', + 'server_user_name': '', + 'server_password': '', + 'os_version': '', }, dialogModify: false, }; @@ -100,7 +109,7 @@ export default { modify(row) { this.dialogModify = true; this.modifyID = row.id - this.machineData={ + this.machineData = { 'server_IP': row.server_IP, 'server_user_name': row.server_user_name, 'server_password': row.server_password, @@ -119,11 +128,11 @@ export default { os_version: this.machineData.os_version, } modify_server(machineData_).then(response => { - if (response.data.code === 200) { - ElMessage({message: response.data.message, type: 'success'}) - this.getData() - } - }) + if (response.data.code === 200) { + ElMessage({message: response.data.message, type: 'success'}) + this.getData() + } + }) } } }; -- Gitee