From f99d91389342956489faa30066e27d75fabe4547 Mon Sep 17 00:00:00 2001 From: alichinese Date: Mon, 30 Sep 2024 14:59:15 +0800 Subject: [PATCH] rpm_install: add pkgs check * We should understand that package management should provide detection of whether software is already installed rather than simply offering installation and uninstallation * the rpm_manager now had function: install, remove, check(new) * about get_test_device, before install lshw, make a check, because not every user had root authority, so we should provide a more fixable function to avoid no authority Signed-off-by: alichinese --- libs/locallibs/get_test_device.py | 16 +++++++++------- libs/locallibs/rpm_manage.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/libs/locallibs/get_test_device.py b/libs/locallibs/get_test_device.py index 01fc4942a..fe39f593a 100644 --- a/libs/locallibs/get_test_device.py +++ b/libs/locallibs/get_test_device.py @@ -38,13 +38,15 @@ def get_test_nic(node=1): Returns: [str]: 网卡名 """ - exitcode, tmpfile = rpm_manage.rpm_install(pkgs="lshw", node=node) - if exitcode != 0: - mugen_log.logging( - "error", - "Failed to install the dependent software package required to obtain the network card.", - ) - sys.exit(exitcode) + tmpfile = None + if not rpm_manage.rpm_check(node=node, pkgs="lshw"): + exitcode, tmpfile = rpm_manage.rpm_install(pkgs="lshw", node=node) + if exitcode != 0: + mugen_log.logging( + "error", + "Failed to install the dependent software package required to obtain the network card.", + ) + sys.exit(exitcode) if os.environ.get("NODE" + str(node) + "_LOCALTION") == "local": dev_info = subprocess.getoutput("cat /proc/device-tree/model") diff --git a/libs/locallibs/rpm_manage.py b/libs/locallibs/rpm_manage.py index d519694b7..28d59ca2b 100644 --- a/libs/locallibs/rpm_manage.py +++ b/libs/locallibs/rpm_manage.py @@ -168,6 +168,37 @@ def rpm_remove(node=1, pkgs="", tmpfile=""): ssh_cmd.pssh_close(conn) return exitcode +def rpm_check(node=1, pkgs=""): + """检测软件包是否安装 + + Args: + pkgs ([str]): 软包包名,"bc" or "bc vim" + node (int, optional): 节点号. Defaults to 1. + + Returns: + [boolean]: 是否安装 + """ + if pkgs == "": + mugen_log.logging("error", "the following arguments are required:pkgs") + sys.exit(1) + + localtion = os.environ.get("NODE" + str(node) + "_LOCALTION") + if localtion == "local": + conn = None + func = local_cmd + else: + conn = ssh_cmd.pssh_conn( + os.environ.get("NODE" + str(node) + "_IPV4"), + os.environ.get("NODE" + str(node) + "_PASSWORD"), + os.environ.get("NODE" + str(node) + "_SSH_PORT"), + os.environ.get("NODE" + str(node) + "_USER"), + ) + func = ssh_cmd.pssh_cmd + + exitcode = func(conn=conn, cmd="which " + pkgs)[0] + if localtion != "local": + ssh_cmd.pssh_close(conn) + return exitcode == 0 if __name__ == "__main__": -- Gitee