From a09071cf9ad46a7ce8953538ddba93737f501935 Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 00:10:38 +0800 Subject: [PATCH 01/11] add update tools --- tools/update_os/__init__.py | 7 ++ tools/update_os/check_pre_upgrade.py | 98 +++++++++++++++++++++++++ tools/update_os/extract_pkg_info.py | 40 ++++++++++ tools/update_os/rollback_consistency.sh | 70 ++++++++++++++++++ tools/update_os/ssh.py | 52 +++++++++++++ 5 files changed, 267 insertions(+) create mode 100644 tools/update_os/__init__.py create mode 100644 tools/update_os/check_pre_upgrade.py create mode 100644 tools/update_os/extract_pkg_info.py create mode 100644 tools/update_os/rollback_consistency.sh create mode 100644 tools/update_os/ssh.py diff --git a/tools/update_os/__init__.py b/tools/update_os/__init__.py new file mode 100644 index 0000000..0ef2573 --- /dev/null +++ b/tools/update_os/__init__.py @@ -0,0 +1,7 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +""" +Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. +Create: 2022/12/19 +Content: +""" diff --git a/tools/update_os/check_pre_upgrade.py b/tools/update_os/check_pre_upgrade.py new file mode 100644 index 0000000..29428dc --- /dev/null +++ b/tools/update_os/check_pre_upgrade.py @@ -0,0 +1,98 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +""" +Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. +Create: 2022/12/19 +Content: +""" +import os +import sys +import logging +import Command +from ssh import sshConnect, login + +CLIENT_PKG_PATH = '/etc/x2openEuler/x2openEuler-client-2.0.0-1.noarch.rpm' + +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', + filename='check_pre_upgrade.log', + filemode='w', +) + +logger = logging + +if __name__ == '__main__': + #创建ssh对象 + ssh_info = sshConnect(sys.argv[1], + sys.argv[2], + sys.argv[3], + sys.argv[4]) + #获取ssh连接 + ssh_client = login(ssh_info) + if ssh_client is None: + logger.error('ssh connect failed') + exit() + else: + logger.info('ssh connect successd') + + #获取ftp工具 + ftp_client = ssh_client.open_sftp() + if ftp_client is None: + logger.error('get ftp client failed') + exit() + else: + logger.info('get ftp client successd') + + #1.传输client包到client端 + local_path = CLIENT_PKG_PATH + remote_path = '/root' + try: + put_info = ftp_client.put(local_path, remote_path, confirm=True) + except Exception: + logger.error('upload failed') + logger.info('upload success') + + #2.安装client包 + stdin, stdout, stderr = ssh_client.exec_command(f'rpm -ivh {CLIENT_PKG_PATH}') + if not stdout: + logger.error('install x2openEuler-client failed') + exit() + else: + logger.info('install x2openEuler-client success') + + #3.升级前检查 + stdin, stdout, stderr = ssh_client.exec_command(f'x2openEuler-client upgrade-check-collect') + if not stdout: + logger.error('upgrade-check-collect failed') + exit() + else: + logger.info('upgrade-check-collect success') + result = stdout.readlines() + result_path = result.split('saved: ')[-1] + filename = result_path.split('collect/')[-1] + + #4.将结果传回本地/opt下 + local_path = '/opt' + remote_path = result_path + try: + put_info = ftp_client.put(remote_path, local_path, callback=None) + except Exception: + logger.error('download failed') + logger.info('download success') + + collect_path = os.path.join(local_path, filename) + repo_everything = '' + repo_update = '' + repo_epol = '' + cmd = ['x2openEuler_python39', 'x2openEuler.pyc', 'upgrade-check', + '-repo', repo_everything + ',' + repo_update + ',' + repo_epol, + '-o', collect_path] + try: + result = Command().run_single_cmd(cmd, logging_error=False) + except Exception: + logger.error('upgrade check failed') + logger.info(result) + + + diff --git a/tools/update_os/extract_pkg_info.py b/tools/update_os/extract_pkg_info.py new file mode 100644 index 0000000..d96319c --- /dev/null +++ b/tools/update_os/extract_pkg_info.py @@ -0,0 +1,40 @@ +import json +import os +import stat +import sys + +def extract_pkg_info(check_result_path, local_upgrade_json_path): + if not os.path.exists(check_result_path): + print("Error;{} not found".format(check_result_path)) + return + if not os.access(check_result_path, os.R_OK): + print("Error:{} has no permission to read".format(check_result_path)) + return + + with open(check_result_path, "r") as file_pointer: + ret = json.load(file_pointer) + pkg_info = { + "reserved_rpm": ret.get("src_reserve_pkg", list()), + "deleted_rpm": ret.get("src_pre_delete_pkg", list()), + "swap_rpm_source": ["centos-logos", "centos-release"], + "swap_rpm_target": ["openEuler-logos", "openEuler-release"], + "cleaned_rpm": ret.get("src_post_delete_pkg", list())} + if os.path.exists(local_upgrade_json_path): + os.remove(local_upgrade_json_path) + flags = os.O_WRONLY | os.O_CREAT + modes = stat.S_IWUSR | stat.S_IRUSR + with os.fdopen(os.open(local_upgrade_json_path, flags, modes), "w+") as pkg_file: + pkg_info = json.dumps(pkg_info) + pkg_info = pkg_info.replace(" ", "") + pkg_file.write(pkg_info) + +if __name__ == '__main__': + if len(sys.argv) != 3: + print("Error:error input") + exit(1) + check_result_path = sys.argv[1] + if not os.path.exists(check_result_path): + print("Error: {} is not exists".format(check_result_path)) + exit(1) + local_upgrade_json_path = os.path.realpath(sys.argv[2]) + extract_pkg_info(check_result_path, local_upgrade_json_path) \ No newline at end of file diff --git a/tools/update_os/rollback_consistency.sh b/tools/update_os/rollback_consistency.sh new file mode 100644 index 0000000..30fb955 --- /dev/null +++ b/tools/update_os/rollback_consistency.sh @@ -0,0 +1,70 @@ +#!/bin /bash +# Error return code +SUCCESS=0 +DIR_NO_EXIST=1 +DIR_ALREADY_EXIST=2 +function os_infomation() +{ + local dir=$1 + rpm -qa > $dir/rpm_list.log + rpm -qa | xargs rpm -qc | xargs md5sum > $dir/rpm_config_list.log + systemctl lists-units --all > $dir/service_list.log + sysctl -a > $dir/kernel_parameters.log + tree / > $dir/tree.log +} + +function diff_check() +{ + local dir=$1 + local result_path=$dir/result.log + echo '============rpm_list============' >> $result_path + diff $dir/pre_upgrade/rpm_list.log $dir/after_rollback/rpm_list.log >> $result_path + echo '============rpm_config_list============' >> $result_path + diff $dir/pre_upgrade/rpm_config_list.log $dir/after_rollback/rpm_config_list.log >> $result_path + echo '============service_list============' >> $result_path + diff $dir/pre_upgrade/service_list.log $dir/after_rollback/service_list.log >> $result_path + echo '============kernel_parameters============' >> $result_path + diff $dir/pre_upgrade/kernel_parameters.log $dir/after_rollback/kernel_parameters.log >> $result_path + echo '============tree============' >> $result_path + diff $dir/pre_upgrade/tree.log $dir/after_rollback/tree.log >> $result_path + echo "diff result path: $result_path" +} + +function main() +{ + local cmd=$1 + local bak_dir=$2 + case $cmd in + pre-upgrade) + if [ -d $bak_dir ];then + echo "dir is exists, please check" + return $DIR_ALREADY_EXIST + fi + mkdir $bak_dir + local pre_upgrade_dir=$bak_dir/pre_upgrade + mkdir $pre_upgrade_dir + os_infomation $pre_upgrade_dir + ;; + after-rollback) + if [ ! -d $bak_dir ];then + echo "os information is not backed up before upgrading" + return $DIR_NO_EXIST + fi + local after_rollback_dir=$bak_dir/after_rollback + mkdir $after_rollback_dir + os_infomation $after_rollback_dir + ;; + os-check) + if [ ! -d $bak_dir ] || [ ! -d $bak_dir/pre_upgrade ] || [ ! -d $bak_dir/after_rollback ];then + echo "dir does not exist, please check" + return $DIR_NO_EXIST + fi + diff_check $bak_dir + ;; + *) + echo "please check parameters: pre-upgrade or after-rollback or os-check" + esac + return $SUCCESS +} + +main "$@" \ No newline at end of file diff --git a/tools/update_os/ssh.py b/tools/update_os/ssh.py new file mode 100644 index 0000000..3ae50ac --- /dev/null +++ b/tools/update_os/ssh.py @@ -0,0 +1,52 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +""" +Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. +Create: 2022/12/19 +Content: +""" + +#导入paramiko,(导入前需要先在环境里安装该模块) +import paramiko + +#定义函数ssh,把操作内容写到函数里 +class sshConnect: + + def __init__(self, hostname: str, port: str, username: str, password: str): + #ip地址 + self.hostname = hostname + # 端口号 + self.port = port + #用户名 + self.username = username + #密码 + self.password = password + + def getHostname(self): + return self.hostname + + def getUsername(self): + return self.username + + def getPort(self): + return self.port + + def getPassword(self): + return self.password + +def login(ssh: sshConnect): + #定义一个变量ssh_clint + ssh_client=paramiko.SSHClient() + #使用cnnect类来连接服务器 + ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + try: + ssh_client.connect(ssh.hostname, + ssh.port, + ssh.username, + ssh.password + ) + return ssh_client + except Exception: + print('服务器连接失败!') + return None \ No newline at end of file -- Gitee From 81911b7910e45375e6fdb4eb4bf492e9689901e2 Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 17:58:44 +0800 Subject: [PATCH 02/11] add update tools --- tools/update_os/check_pre_upgrade.py | 64 +++++--- tools/update_os/command.py | 100 ++++++++++++ tools/update_os/install.sh | 235 +++++++++++++++++++++++++++ tools/update_os/upgrade.py | 91 +++++++++++ 4 files changed, 469 insertions(+), 21 deletions(-) create mode 100644 tools/update_os/command.py create mode 100644 tools/update_os/install.sh create mode 100644 tools/update_os/upgrade.py diff --git a/tools/update_os/check_pre_upgrade.py b/tools/update_os/check_pre_upgrade.py index 29428dc..d827e26 100644 --- a/tools/update_os/check_pre_upgrade.py +++ b/tools/update_os/check_pre_upgrade.py @@ -1,33 +1,26 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- """ -Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. +Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved Create: 2022/12/19 Content: """ import os -import sys import logging -import Command +from command import Command from ssh import sshConnect, login -CLIENT_PKG_PATH = '/etc/x2openEuler/x2openEuler-client-2.0.0-1.noarch.rpm' -logging.basicConfig( - level=logging.INFO, - format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', - filename='check_pre_upgrade.log', - filemode='w', -) +if __name__ == '__main__': -logger = logging + logger = logging + CLIENT_PKG_PATH = '/etc/x2openEuler/x2openEuler-client-2.0.0-1.noarch.rpm' -if __name__ == '__main__': #创建ssh对象 - ssh_info = sshConnect(sys.argv[1], - sys.argv[2], - sys.argv[3], - sys.argv[4]) + ssh_info = sshConnect('192.168.137.xx', + '22', + 'root', + 'xxx') #获取ssh连接 ssh_client = login(ssh_info) if ssh_client is None: @@ -51,10 +44,12 @@ if __name__ == '__main__': put_info = ftp_client.put(local_path, remote_path, confirm=True) except Exception: logger.error('upload failed') + exit() logger.info('upload success') #2.安装client包 - stdin, stdout, stderr = ssh_client.exec_command(f'rpm -ivh {CLIENT_PKG_PATH}') + stdin, stdout, stderr = \ + ssh_client.exec_command(f'rpm -ivh {CLIENT_PKG_PATH}') if not stdout: logger.error('install x2openEuler-client failed') exit() @@ -62,7 +57,8 @@ if __name__ == '__main__': logger.info('install x2openEuler-client success') #3.升级前检查 - stdin, stdout, stderr = ssh_client.exec_command(f'x2openEuler-client upgrade-check-collect') + stdin, stdout, stderr = \ + ssh_client.exec_command(f'x2openEuler-client upgrade-check-collect') if not stdout: logger.error('upgrade-check-collect failed') exit() @@ -72,15 +68,17 @@ if __name__ == '__main__': result_path = result.split('saved: ')[-1] filename = result_path.split('collect/')[-1] - #4.将结果传回本地/opt下 + # 4.将结果传回本地/opt下 local_path = '/opt' remote_path = result_path try: put_info = ftp_client.put(remote_path, local_path, callback=None) except Exception: logger.error('download failed') + exit() logger.info('download success') + # 5.执行升级前检查 collect_path = os.path.join(local_path, filename) repo_everything = '' repo_update = '' @@ -89,10 +87,34 @@ if __name__ == '__main__': '-repo', repo_everything + ',' + repo_update + ',' + repo_epol, '-o', collect_path] try: - result = Command().run_single_cmd(cmd, logging_error=False) + result = Command().run_single_cmd(cmd, logging_error=False)\ + .strip().split('\n') + logger.info(f'upgrade check result: {result}') except Exception: logger.error('upgrade check failed') + exit() logger.info(result) + # 6.将upgrade.json传输到client端 + lines = result.split('\n') + upgrade_local_path = '' + for line in lines: + if 'Upgrade results are saved: ' in line: + upgrade_local_path = line.split('Upgrade results are saved: ')[-1] + remote_path = '/root/upgrade.json' + try: + put_info = ftp_client.put(upgrade_local_path, remote_path, confirm=True) + except Exception: + logger.error('upload upgrade.json failed') + exit() + logger.info('upload upgrade.json success') - + # 7.上传repo源文件 + local_path = 'migrate-2003.repo' + remote_path = '/root' + try: + put_info = ftp_client.put(local_path, remote_path, confirm=True) + except Exception: + logger.error('upload repo failed') + exit() + logger.info('upload repo success') \ No newline at end of file diff --git a/tools/update_os/command.py b/tools/update_os/command.py new file mode 100644 index 0000000..91d7a6d --- /dev/null +++ b/tools/update_os/command.py @@ -0,0 +1,100 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +""" +Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. +Create: 2022/12/20 +Content: +""" + +import subprocess +import logging + +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', + filename='command.log', + filemode='w', +) + +logger = logging + +class Command(object): + """ + 系统命令封装,收编挪到公共组件目录 + """ + + @staticmethod + def run_single_cmd_and_get_error(cmd): + result = '' + try: + pipe = subprocess.Popen(cmd, universal_newlines=True, stderr=subprocess.PIPE, + stdout=subprocess.PIPE) + except (ValueError, OSError) as error_except: + logger.error("Command error : " + str(error_except)) + return result, 'error' + + try: + result, error = pipe.communicate() + except (ValueError, OSError) as error_except: + logger.error("Command error : " + str(error_except)) + error = 'error' + error = error.strip() + if error: + error = error.replace("\n", "").replace("\r", "") + logger.error('command execute ERROR, Command: {}, Because:{}'.format(' '.join(cmd), error)) + if error.startswith('Warning:'): + error = '' + return result, error + + @staticmethod + def run_single_cmd(cmd, raise_exception=False, logging_error=True): + result = '' + try: + pipe = subprocess.Popen(cmd, universal_newlines=True, stderr=subprocess.PIPE, + stdout=subprocess.PIPE) + except (ValueError, OSError) as error_except: + logger.error("Command error : " + str(error_except)) + return result + + try: + result, error = pipe.communicate() + except (ValueError, OSError) as e: + logger.error("Command error : " + str(e)) + error = 'error' + if raise_exception: + raise e + if error and logging_error: + error = error.replace("\n", "").replace("\r", "") + logger.error('command execute ERROR, Command: {}, Because:{}'.format(' '.join(cmd), error)) + return result + + @staticmethod + def run_mult_cmd(cmds, raise_exception=False, logging_error=True): + result = '' + pipes = [] + for cmd in cmds: + pipe_stdout = subprocess.PIPE + if len(pipes) != 0: + pipe_stdout = pipes[-1].stdout + try: + pipe = subprocess.Popen(cmd, stdin=pipe_stdout, universal_newlines=True, stderr=subprocess.PIPE, + stdout=subprocess.PIPE) + except (ValueError, OSError) as error_except: + logger.error("Command error : " + str(error_except)) + return result + + if pipe_stdout is not subprocess.PIPE: + pipe_stdout.close() + pipes.append(pipe) + try: + result, error = pipes[-1].communicate() + except (ValueError, OSError) as e: + logger.error("Command error : " + str(e)) + error = 'error' + if raise_exception: + raise e + if pipes[-1].returncode != 0 and len(error) != 0 and logging_error: + error = error.replace("\n", "").replace("\r", "") + logger.error('command execute ERROR,Because:{}'.format(error)) + + return result diff --git a/tools/update_os/install.sh b/tools/update_os/install.sh new file mode 100644 index 0000000..34ee9d0 --- /dev/null +++ b/tools/update_os/install.sh @@ -0,0 +1,235 @@ +#!/usr/bin/bash +currDir=`dirname $(readlink -f $0)` +driverName=$1 +fileType=$2 +filePath=$3 +postTest=$4 + +function install_tar() +{ + if test -z "`rpm -qa | grep tar-`" + then + dnf install -y tar + fi +} + +function rpm_install() +{ + oldfile=`modinfo -F filename $driverName` + newfile=`rpm -ql $filePath | grep ko` + rpm -ivh `basename $filePath` --force 2>&1 | tee tmp.log + if test $? -ne 0 + then + rm -rf tmp.log + echo "$driverName install failure." + exit 1 + fi + grep -qi -e error -e fail tmp.log + if test $? -eq 0 + then + rm -rf tmp.log + exit 1 + fi + rm -rf tmp.log + if test -n "$newfile" + then + rm -rf $oldfile + cp -rf $newfile `dirname $oldfile` + if test $postTest -eq 0 + then + rmmod $driverName + insmod "`dirname $oldfile`/`basename $newfile`" + if test $? -ne 0 + then + echo "$newfile insmod failure..." + exit 1 + fi + fi + fi +} + +function ensure_env() +{ + dependent="" + for d in $@ + do + rpm -qa | grep -q $d + if test $? -ne 0 + then + dependent="$dependent $d" + fi + done + if test -n "$dependent" + then + yum install -y $dependent + fi +} + + +function do_rpm_install() +{ + case $driverName in + qla2xxx) + ensure_env dkms + rpm_install + ;; + *) + rpm_install + ;; + esac +} + +function do_tar_gz_install() +{ + case $driverName in + qla2xxx) + ;; + lpfc) + rm -rf $currDir/tmp + mkdir -p $currDir/tmp + tar -xzvf `basename $filePath` -C $currDir/tmp + cd $currDir/tmp/* + chmod a+x elx_lpfc_install.sh && ./elx_lpfc_install.sh + cp -f ./extra/elx-lpfc/lpfc.ko `modinfo -F filename $driverName` + if test $? -ne 0 + then + rm -rf $currDir/tmp + exit 1 + fi + dracut -f + rm -rf $currDir/tmp + ;; + *) + rpm_install + ;; + esac +} + +function do_tgz_install() +{ + case $driverName in + megaraid_sas) + oldfile=`modinfo -F filename $driverName` + rm -rf $currDir/tmp + mkdir -p $currDir/tmp + tar -xzvf `basename $filePath` -C $currDir/tmp + tmp_dir=$currDir/tmp/openEuler/rpms-2 + cd $tmp_dir + newfile=`rpm -ql ./*megaraid_sas*.rpm | grep ko` + rpm -ivh ./*megaraid_sas*.rpm --force 2>&1 | tee tmp.log + if test $? -ne 0 + then + rm -rf tmp.log + echo "$driverName install failure." + exit 1 + fi + grep -qi -e error -e fail tmp.log + if test $? -eq 0 + then + rm -rf tmp.log + exit 1 + fi + rm -rf tmp.log + if test -n "$newfile" + then + cp -rf $newfile `dirname $oldfile` + fi + ;; + mlx5_core) + ensure_env elfutils-devel python3-devel lsof createrepo pciutils-devel fuse-devel tcsh + ensure_env patch gdb-headless rpm-build make gcc + ensure_env python3-unversioned-command + + rm -rf $currDir/tmp + mkdir -p $currDir/tmp + tar -xzvf `basename $filePath` -C $currDir/tmp + cd $currDir/tmp/* + chmod a+x mlnxofedinstall && ./mlnxofedinstall --add-kernel-support + if test $? -eq 0 + then + dracut -f + /etc/init.d/openibd restart + rm -rf $currDir/tmp + else + rm -rf $currDir/tmp + exit 1 + fi + ;; + lpfc) + rm -rf $currDir/tmp + mkdir -p $currDir/tmp + tar -xzvf `basename $filePath` -C $currDir/tmp + cd $currDir/tmp/* + chmod a+x elx_lpfc_install.sh && ./elx_lpfc_install.sh + if test $? -ne 0 + then + rm -rf $currDir/tmp + exit 1 + fi + + oldfile=`modinfo -F filename $driverName` + newfile=`rpm -ql lpfc-openEuler20-12.8.340.17-ds/kmod-elx-lpfc-12.8.340.17-1.openEuler20.03.aarch64.rpm | grep ko` + rm -rf $oldfile + cp -rf $newfile $oldfile + dracut -f + rm -rf $currDir/tmp + ;; + *) + rpm_install + ;; + esac +} + +function do_run_install() +{ + case $driverName in + nvidia) + rmmod nouveau > /dev/null 2>&1 + bash `basename $filePath` --silent + if test $? -ne 0 + then + exit 1 + fi + ;; + cuda) + rmmod nouveau > /dev/null 2>&1 + bash `basename $filePath` --silent + if test $? -ne 0 + then + exit 1 + fi + ;; + *) + bash `basename $filePath` --silent + ;; + esac +} + +function do_install() +{ + install_tar + +# driverName=$1 +# fileType=$2 +# filePath=$3 + cd `dirname $filePath` + case $fileType in + rpm) + do_rpm_install + ;; + tar.gz) + do_tar_gz_install + ;; + tgz) + do_tgz_install + ;; + run) + do_run_install + ;; + *) + echo "not supported driver filetype: $fileType for driver $driverName" + ;; + esac +} + +do_install diff --git a/tools/update_os/upgrade.py b/tools/update_os/upgrade.py new file mode 100644 index 0000000..b638814 --- /dev/null +++ b/tools/update_os/upgrade.py @@ -0,0 +1,91 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +""" +Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. +Create: 2022/12/20 +Content: +""" +import logging +from ssh import sshConnect, login + +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', + filename='check_pre_upgrade.log', + filemode='w', +) + +logger = logging + +UPGRADE_PKG_PATH='/opt/x2openEuler-upgrade/x2openEuler-upgrade-1.1.3-33.x86_64.rpm' + +if __name__ == '__main__': +#创建ssh对象 + ssh_info = sshConnect('192.168.137.xx', + '22', + 'root', + 'xxx') + #获取ssh连接 + ssh_client = login(ssh_info) + if ssh_client is None: + logger.error('ssh connect failed') + exit() + else: + logger.info('ssh connect successd') + + #获取ftp工具 + ftp_client = ssh_client.open_sftp() + if ftp_client is None: + logger.error('get ftp client failed') + exit() + else: + logger.info('get ftp client successd') + + #1.传输upgrade包到client端 + local_path = UPGRADE_PKG_PATH + remote_path = '/root' + try: + put_info = ftp_client.put(local_path, remote_path, confirm=True) + except Exception: + logger.error('upload failed') + exit() + logger.info('upload success') + + #2.安装client包 + stdin, stdout, stderr = ssh_client.exec_command(f'yum install -y {UPGRADE_PKG_PATH}') + if not stdout: + logger.error('install x2openEuler-upgrade failed') + exit() + else: + logger.info('install x2openEuler-upgrade success') + + # 3.环境变量生效 + stdin, stdout, stderr = ssh_client.exec_command(f'source /root/.bashrc') + if not stdout: + logger.error('validate environment variable failed') + exit() + else: + logger.info('validate environment variable success') + + # 4.执行升级任务 + cmd = ['nohup', 'sh', '/usr/bin/centos2openEuler', + 'upgrade', 'upgrade.json', 'migrate-2003.repo', + '/usr,/run,/boot,/var,/etc', 'empty_dir', + '/.osbak', '> upgrade.log'] + stdin, stdout, stderr = ssh_client.exec_command(cmd) + if not stdout: + logger.error('upgrade fail, please check log') + exit() + else: + logger.info('end of upgrade') + + # 5.检查系统 + stdin, stdout, stderr = ssh_client.exec_command('cat /etc/os-release') + if not stdout: + logger.error('command failed') + exit() + else: + logger.info('command success') + if 'openEuler' in stdout.readlines(): + logger.info('upgrade success') + exit() \ No newline at end of file -- Gitee From 5f1d698e09c3d8704bedc57291f6cd9ad6d53152 Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 18:16:54 +0800 Subject: [PATCH 03/11] add update tools --- tools/update_os/check_pre_upgrade.py | 4 +-- tools/update_os/command.py | 49 ++++++++++++++++------------ tools/update_os/ssh.py | 19 ++++++----- tools/update_os/upgrade.py | 31 ++++++++++-------- 4 files changed, 58 insertions(+), 45 deletions(-) diff --git a/tools/update_os/check_pre_upgrade.py b/tools/update_os/check_pre_upgrade.py index d827e26..ff02bb4 100644 --- a/tools/update_os/check_pre_upgrade.py +++ b/tools/update_os/check_pre_upgrade.py @@ -8,7 +8,7 @@ Content: import os import logging from command import Command -from ssh import sshConnect, login +from ssh import SSHConnect, login if __name__ == '__main__': @@ -17,7 +17,7 @@ if __name__ == '__main__': CLIENT_PKG_PATH = '/etc/x2openEuler/x2openEuler-client-2.0.0-1.noarch.rpm' #创建ssh对象 - ssh_info = sshConnect('192.168.137.xx', + ssh_info = SSHConnect('192.168.137.xx', '22', 'root', 'xxx') diff --git a/tools/update_os/command.py b/tools/update_os/command.py index 91d7a6d..dc3ce60 100644 --- a/tools/update_os/command.py +++ b/tools/update_os/command.py @@ -1,7 +1,7 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- """ -Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. +Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved Create: 2022/12/20 Content: """ @@ -9,39 +9,42 @@ Content: import subprocess import logging -logging.basicConfig( - level=logging.INFO, - format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', - filename='command.log', - filemode='w', -) - -logger = logging class Command(object): """ 系统命令封装,收编挪到公共组件目录 """ + logging.basicConfig( + level=logging.INFO, + format='%(asctime)s %(filename) \ + s[line:%(lineno)d] \ + %(levelname)s %(message)s', + filename='command.log', + filemode='w', + ) + @staticmethod def run_single_cmd_and_get_error(cmd): result = '' try: - pipe = subprocess.Popen(cmd, universal_newlines=True, stderr=subprocess.PIPE, + pipe = subprocess.Popen(cmd, universal_newlines=True, + stderr=subprocess.PIPE, stdout=subprocess.PIPE) except (ValueError, OSError) as error_except: - logger.error("Command error : " + str(error_except)) + logging.error("Command error : " + str(error_except)) return result, 'error' try: result, error = pipe.communicate() except (ValueError, OSError) as error_except: - logger.error("Command error : " + str(error_except)) + logging.error("Command error : " + str(error_except)) error = 'error' error = error.strip() if error: error = error.replace("\n", "").replace("\r", "") - logger.error('command execute ERROR, Command: {}, Because:{}'.format(' '.join(cmd), error)) + logging.error('command execute ERROR, \ + Command: {}, Because:{}'.format(' '.join(cmd), error)) if error.startswith('Warning:'): error = '' return result, error @@ -50,22 +53,24 @@ class Command(object): def run_single_cmd(cmd, raise_exception=False, logging_error=True): result = '' try: - pipe = subprocess.Popen(cmd, universal_newlines=True, stderr=subprocess.PIPE, + pipe = subprocess.Popen(cmd, universal_newlines=True, + stderr=subprocess.PIPE, stdout=subprocess.PIPE) except (ValueError, OSError) as error_except: - logger.error("Command error : " + str(error_except)) + logging.error("Command error : " + str(error_except)) return result try: result, error = pipe.communicate() except (ValueError, OSError) as e: - logger.error("Command error : " + str(e)) + logging.error("Command error : " + str(e)) error = 'error' if raise_exception: raise e if error and logging_error: error = error.replace("\n", "").replace("\r", "") - logger.error('command execute ERROR, Command: {}, Because:{}'.format(' '.join(cmd), error)) + logging.error('command execute ERROR, \ + Command: {}, Because:{}'.format(' '.join(cmd), error)) return result @staticmethod @@ -77,10 +82,12 @@ class Command(object): if len(pipes) != 0: pipe_stdout = pipes[-1].stdout try: - pipe = subprocess.Popen(cmd, stdin=pipe_stdout, universal_newlines=True, stderr=subprocess.PIPE, + pipe = subprocess.Popen(cmd, stdin=pipe_stdout, + universal_newlines=True, + stderr=subprocess.PIPE, stdout=subprocess.PIPE) except (ValueError, OSError) as error_except: - logger.error("Command error : " + str(error_except)) + logging.error("Command error : " + str(error_except)) return result if pipe_stdout is not subprocess.PIPE: @@ -89,12 +96,12 @@ class Command(object): try: result, error = pipes[-1].communicate() except (ValueError, OSError) as e: - logger.error("Command error : " + str(e)) + logging.error("Command error : " + str(e)) error = 'error' if raise_exception: raise e if pipes[-1].returncode != 0 and len(error) != 0 and logging_error: error = error.replace("\n", "").replace("\r", "") - logger.error('command execute ERROR,Because:{}'.format(error)) + logging.error('command execute ERROR,Because:{}'.format(error)) return result diff --git a/tools/update_os/ssh.py b/tools/update_os/ssh.py index 3ae50ac..6d2e94a 100644 --- a/tools/update_os/ssh.py +++ b/tools/update_os/ssh.py @@ -1,7 +1,7 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- """ -Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. +Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved Create: 2022/12/19 Content: """ @@ -9,8 +9,9 @@ Content: #导入paramiko,(导入前需要先在环境里安装该模块) import paramiko + #定义函数ssh,把操作内容写到函数里 -class sshConnect: +class SSHConnect: def __init__(self, hostname: str, port: str, username: str, password: str): #ip地址 @@ -22,21 +23,22 @@ class sshConnect: #密码 self.password = password - def getHostname(self): + def get_hostname(self): return self.hostname - def getUsername(self): + def get_username(self): return self.username - def getPort(self): + def get_port(self): return self.port - def getPassword(self): + def get_password(self): return self.password -def login(ssh: sshConnect): + +def login(ssh: SSHConnect): #定义一个变量ssh_clint - ssh_client=paramiko.SSHClient() + ssh_client = paramiko.SSHClient() #使用cnnect类来连接服务器 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) @@ -49,4 +51,3 @@ def login(ssh: sshConnect): return ssh_client except Exception: print('服务器连接失败!') - return None \ No newline at end of file diff --git a/tools/update_os/upgrade.py b/tools/update_os/upgrade.py index b638814..99a7347 100644 --- a/tools/update_os/upgrade.py +++ b/tools/update_os/upgrade.py @@ -1,27 +1,31 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- """ -Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. +Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved Create: 2022/12/20 Content: """ import logging -from ssh import sshConnect, login +from ssh import SSHConnect, login -logging.basicConfig( - level=logging.INFO, - format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', - filename='check_pre_upgrade.log', - filemode='w', -) -logger = logging +if __name__ == '__main__': -UPGRADE_PKG_PATH='/opt/x2openEuler-upgrade/x2openEuler-upgrade-1.1.3-33.x86_64.rpm' + logging.basicConfig( + level=logging.INFO, + format='%(asctime)s %(filename)s \ + [line:%(lineno)d] \ + %(levelname)s %(message)s', + filename='check_pre_upgrade.log', + filemode='w', + ) -if __name__ == '__main__': + logger = logging + + UPGRADE_PKG_PATH = '/opt/x2openEuler-upgrade\ + /x2openEuler-upgrade-1.1.3-33.x86_64.rpm' #创建ssh对象 - ssh_info = sshConnect('192.168.137.xx', + ssh_info = SSHConnect('192.168.137.xx', '22', 'root', 'xxx') @@ -52,7 +56,8 @@ if __name__ == '__main__': logger.info('upload success') #2.安装client包 - stdin, stdout, stderr = ssh_client.exec_command(f'yum install -y {UPGRADE_PKG_PATH}') + stdin, stdout, stderr = \ + ssh_client.exec_command(f'yum install -y {UPGRADE_PKG_PATH}') if not stdout: logger.error('install x2openEuler-upgrade failed') exit() -- Gitee From 276549fb698d949527ce4314533f06b54e1a1052 Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 18:19:00 +0800 Subject: [PATCH 04/11] add update tools --- tools/update_os/__init__.py | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 tools/update_os/__init__.py diff --git a/tools/update_os/__init__.py b/tools/update_os/__init__.py deleted file mode 100644 index 0ef2573..0000000 --- a/tools/update_os/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -#! /usr/bin/env python -# -*- coding: utf-8 -*- -""" -Copyright: Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. -Create: 2022/12/19 -Content: -""" -- Gitee From 95d05b5ff2a254a8d46f4abfc6de41728930d79f Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 18:42:13 +0800 Subject: [PATCH 05/11] add update tools --- tools/update_os/extract_pkg_info.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tools/update_os/extract_pkg_info.py b/tools/update_os/extract_pkg_info.py index d96319c..26cef19 100644 --- a/tools/update_os/extract_pkg_info.py +++ b/tools/update_os/extract_pkg_info.py @@ -3,6 +3,7 @@ import os import stat import sys + def extract_pkg_info(check_result_path, local_upgrade_json_path): if not os.path.exists(check_result_path): print("Error;{} not found".format(check_result_path)) @@ -23,7 +24,8 @@ def extract_pkg_info(check_result_path, local_upgrade_json_path): os.remove(local_upgrade_json_path) flags = os.O_WRONLY | os.O_CREAT modes = stat.S_IWUSR | stat.S_IRUSR - with os.fdopen(os.open(local_upgrade_json_path, flags, modes), "w+") as pkg_file: + with os.fdopen(os.open(local_upgrade_json_path, + flags, modes), "w+") as pkg_file: pkg_info = json.dumps(pkg_info) pkg_info = pkg_info.replace(" ", "") pkg_file.write(pkg_info) @@ -32,9 +34,9 @@ if __name__ == '__main__': if len(sys.argv) != 3: print("Error:error input") exit(1) - check_result_path = sys.argv[1] - if not os.path.exists(check_result_path): - print("Error: {} is not exists".format(check_result_path)) + check_path = sys.argv[1] + if not os.path.exists(check_path): + print("Error: {} is not exists".format(check_path)) exit(1) - local_upgrade_json_path = os.path.realpath(sys.argv[2]) - extract_pkg_info(check_result_path, local_upgrade_json_path) \ No newline at end of file + upgrade_json_path = os.path.realpath(sys.argv[2]) + extract_pkg_info(check_path, upgrade_json_path) \ No newline at end of file -- Gitee From 625ef15376b43ba112f9f01bba6e8cd6c9e0f50f Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 19:05:57 +0800 Subject: [PATCH 06/11] add update tools --- tools/update_os/check_pre_upgrade.py | 26 ++++++++++++------------ tools/update_os/command.py | 30 +++++++++++++++------------- tools/update_os/extract_pkg_info.py | 7 +++---- tools/update_os/ssh.py | 12 +++++------ 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/tools/update_os/check_pre_upgrade.py b/tools/update_os/check_pre_upgrade.py index ff02bb4..946e0bd 100644 --- a/tools/update_os/check_pre_upgrade.py +++ b/tools/update_os/check_pre_upgrade.py @@ -38,10 +38,10 @@ if __name__ == '__main__': logger.info('get ftp client successd') #1.传输client包到client端 - local_path = CLIENT_PKG_PATH remote_path = '/root' try: - put_info = ftp_client.put(local_path, remote_path, confirm=True) + put_info = ftp_client.put(CLIENT_PKG_PATH + , remote_path, confirm=True) except Exception: logger.error('upload failed') exit() @@ -69,22 +69,22 @@ if __name__ == '__main__': filename = result_path.split('collect/')[-1] # 4.将结果传回本地/opt下 - local_path = '/opt' - remote_path = result_path + local = '/opt' + remote = result_path try: - put_info = ftp_client.put(remote_path, local_path, callback=None) + put_info = ftp_client.put(remote, local, callback=None) except Exception: logger.error('download failed') exit() logger.info('download success') # 5.执行升级前检查 - collect_path = os.path.join(local_path, filename) - repo_everything = '' - repo_update = '' - repo_epol = '' + collect_path = os.path.join(local, filename) + everything = '' + update = '' + epol = '' cmd = ['x2openEuler_python39', 'x2openEuler.pyc', 'upgrade-check', - '-repo', repo_everything + ',' + repo_update + ',' + repo_epol, + '-repo', everything + ',' + update + ',' + epol, '-o', collect_path] try: result = Command().run_single_cmd(cmd, logging_error=False)\ @@ -110,10 +110,10 @@ if __name__ == '__main__': logger.info('upload upgrade.json success') # 7.上传repo源文件 - local_path = 'migrate-2003.repo' - remote_path = '/root' + local = 'migrate-2003.repo' + remote = '/root' try: - put_info = ftp_client.put(local_path, remote_path, confirm=True) + put_info = ftp_client.put(local, remote, confirm=True) except Exception: logger.error('upload repo failed') exit() diff --git a/tools/update_os/command.py b/tools/update_os/command.py index dc3ce60..8ac86e0 100644 --- a/tools/update_os/command.py +++ b/tools/update_os/command.py @@ -22,6 +22,8 @@ class Command(object): filename='command.log', filemode='w', ) + + logger = logging @staticmethod @@ -32,49 +34,49 @@ class Command(object): stderr=subprocess.PIPE, stdout=subprocess.PIPE) except (ValueError, OSError) as error_except: - logging.error("Command error : " + str(error_except)) + logger.error("Command error : " + str(error_except)) return result, 'error' try: - result, error = pipe.communicate() + result, error = pipe.communicate(timeout=10) except (ValueError, OSError) as error_except: - logging.error("Command error : " + str(error_except)) + logger.error("Command error : " + str(error_except)) error = 'error' error = error.strip() if error: error = error.replace("\n", "").replace("\r", "") - logging.error('command execute ERROR, \ + logger.error('command execute ERROR, \ Command: {}, Because:{}'.format(' '.join(cmd), error)) if error.startswith('Warning:'): error = '' return result, error @staticmethod - def run_single_cmd(cmd, raise_exception=False, logging_error=True): + def run_single_cmd(cmd, raise_exception=False, logger_error=True): result = '' try: pipe = subprocess.Popen(cmd, universal_newlines=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) except (ValueError, OSError) as error_except: - logging.error("Command error : " + str(error_except)) + logger.error("Command error : " + str(error_except)) return result try: result, error = pipe.communicate() except (ValueError, OSError) as e: - logging.error("Command error : " + str(e)) + logger.error("Command error : " + str(e)) error = 'error' if raise_exception: raise e - if error and logging_error: + if error and logger_error: error = error.replace("\n", "").replace("\r", "") - logging.error('command execute ERROR, \ + logger.error('command execute ERROR, \ Command: {}, Because:{}'.format(' '.join(cmd), error)) return result @staticmethod - def run_mult_cmd(cmds, raise_exception=False, logging_error=True): + def run_mult_cmd(cmds, raise_exception=False, logger_error=True): result = '' pipes = [] for cmd in cmds: @@ -87,7 +89,7 @@ class Command(object): stderr=subprocess.PIPE, stdout=subprocess.PIPE) except (ValueError, OSError) as error_except: - logging.error("Command error : " + str(error_except)) + logger.error("Command error : " + str(error_except)) return result if pipe_stdout is not subprocess.PIPE: @@ -96,12 +98,12 @@ class Command(object): try: result, error = pipes[-1].communicate() except (ValueError, OSError) as e: - logging.error("Command error : " + str(e)) + logger.error("Command error : " + str(e)) error = 'error' if raise_exception: raise e - if pipes[-1].returncode != 0 and len(error) != 0 and logging_error: + if pipes[-1].returncode != 0 and len(error) != 0 and logger_error: error = error.replace("\n", "").replace("\r", "") - logging.error('command execute ERROR,Because:{}'.format(error)) + logger.error('command execute ERROR,Because:{}'.format(error)) return result diff --git a/tools/update_os/extract_pkg_info.py b/tools/update_os/extract_pkg_info.py index 26cef19..bb24f70 100644 --- a/tools/update_os/extract_pkg_info.py +++ b/tools/update_os/extract_pkg_info.py @@ -6,10 +6,9 @@ import sys def extract_pkg_info(check_result_path, local_upgrade_json_path): if not os.path.exists(check_result_path): - print("Error;{} not found".format(check_result_path)) return + if not os.access(check_result_path, os.R_OK): - print("Error:{} has no permission to read".format(check_result_path)) return with open(check_result_path, "r") as file_pointer: @@ -32,11 +31,11 @@ def extract_pkg_info(check_result_path, local_upgrade_json_path): if __name__ == '__main__': if len(sys.argv) != 3: - print("Error:error input") exit(1) + check_path = sys.argv[1] if not os.path.exists(check_path): - print("Error: {} is not exists".format(check_path)) exit(1) + upgrade_json_path = os.path.realpath(sys.argv[2]) extract_pkg_info(check_path, upgrade_json_path) \ No newline at end of file diff --git a/tools/update_os/ssh.py b/tools/update_os/ssh.py index 6d2e94a..352349f 100644 --- a/tools/update_os/ssh.py +++ b/tools/update_os/ssh.py @@ -36,18 +36,18 @@ class SSHConnect: return self.password -def login(ssh: SSHConnect): +def login(ssh_connect: SSHConnect): #定义一个变量ssh_clint ssh_client = paramiko.SSHClient() #使用cnnect类来连接服务器 ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: - ssh_client.connect(ssh.hostname, - ssh.port, - ssh.username, - ssh.password + ssh_client.connect(ssh_connect.hostname, + ssh_connect.port, + ssh_connect.username, + ssh_connect.password ) return ssh_client except Exception: - print('服务器连接失败!') + raise Exception('连接服务器失败!') -- Gitee From a79ff3580fb56c1f5710cdda9575d1bd0174979b Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 19:27:51 +0800 Subject: [PATCH 07/11] add update tools --- tools/update_os/check_pre_upgrade.py | 27 +++++++++++---------------- tools/update_os/command.py | 11 +++++------ tools/update_os/ssh.py | 4 ++-- tools/update_os/upgrade.py | 5 ++--- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/tools/update_os/check_pre_upgrade.py b/tools/update_os/check_pre_upgrade.py index 946e0bd..7ee2d4c 100644 --- a/tools/update_os/check_pre_upgrade.py +++ b/tools/update_os/check_pre_upgrade.py @@ -38,10 +38,10 @@ if __name__ == '__main__': logger.info('get ftp client successd') #1.传输client包到client端 - remote_path = '/root' + try: put_info = ftp_client.put(CLIENT_PKG_PATH - , remote_path, confirm=True) + , '/root', confirm=True) except Exception: logger.error('upload failed') exit() @@ -69,10 +69,9 @@ if __name__ == '__main__': filename = result_path.split('collect/')[-1] # 4.将结果传回本地/opt下 - local = '/opt' remote = result_path try: - put_info = ftp_client.put(remote, local, callback=None) + put_info = ftp_client.put(remote, '/opt', callback=None) except Exception: logger.error('download failed') exit() @@ -80,40 +79,36 @@ if __name__ == '__main__': # 5.执行升级前检查 collect_path = os.path.join(local, filename) - everything = '' - update = '' - epol = '' + REPO_EVE = '' + REPO_UPDATE = '' + REPO_EPOL = '' cmd = ['x2openEuler_python39', 'x2openEuler.pyc', 'upgrade-check', - '-repo', everything + ',' + update + ',' + epol, + '-repo', REPO_EVE + ',' + REPO_UPDATE + ',' + REPO_EPOL, '-o', collect_path] try: result = Command().run_single_cmd(cmd, logging_error=False)\ .strip().split('\n') - logger.info(f'upgrade check result: {result}') except Exception: logger.error('upgrade check failed') exit() - logger.info(result) + logger.info(f'upgrade check result: {result}') + # 6.将upgrade.json传输到client端 lines = result.split('\n') - upgrade_local_path = '' for line in lines: if 'Upgrade results are saved: ' in line: upgrade_local_path = line.split('Upgrade results are saved: ')[-1] - remote_path = '/root/upgrade.json' try: - put_info = ftp_client.put(upgrade_local_path, remote_path, confirm=True) + put_info = ftp_client.put(upgrade_local_path, '/root/upgrade.json', confirm=True) except Exception: logger.error('upload upgrade.json failed') exit() logger.info('upload upgrade.json success') # 7.上传repo源文件 - local = 'migrate-2003.repo' - remote = '/root' try: - put_info = ftp_client.put(local, remote, confirm=True) + put_info = ftp_client.put('migrate-2003.repo', 'root', confirm=True) except Exception: logger.error('upload repo failed') exit() diff --git a/tools/update_os/command.py b/tools/update_os/command.py index 8ac86e0..6e40ca2 100644 --- a/tools/update_os/command.py +++ b/tools/update_os/command.py @@ -11,9 +11,6 @@ import logging class Command(object): - """ - 系统命令封装,收编挪到公共组件目录 - """ logging.basicConfig( level=logging.INFO, format='%(asctime)s %(filename) \ @@ -24,7 +21,9 @@ class Command(object): ) logger = logging - + """ + 系统命令封装,收编挪到公共组件目录 + """ @staticmethod def run_single_cmd_and_get_error(cmd): @@ -63,7 +62,7 @@ class Command(object): return result try: - result, error = pipe.communicate() + result, error = pipe.communicate(timeout=10) except (ValueError, OSError) as e: logger.error("Command error : " + str(e)) error = 'error' @@ -96,7 +95,7 @@ class Command(object): pipe_stdout.close() pipes.append(pipe) try: - result, error = pipes[-1].communicate() + result, error = pipes[-1].communicate(timeout=10) except (ValueError, OSError) as e: logger.error("Command error : " + str(e)) error = 'error' diff --git a/tools/update_os/ssh.py b/tools/update_os/ssh.py index 352349f..1ffffd6 100644 --- a/tools/update_os/ssh.py +++ b/tools/update_os/ssh.py @@ -49,5 +49,5 @@ def login(ssh_connect: SSHConnect): ssh_connect.password ) return ssh_client - except Exception: - raise Exception('连接服务器失败!') + except Exception as e: + raise Exception('连接服务器失败!') from e diff --git a/tools/update_os/upgrade.py b/tools/update_os/upgrade.py index 99a7347..f1dd077 100644 --- a/tools/update_os/upgrade.py +++ b/tools/update_os/upgrade.py @@ -46,10 +46,9 @@ if __name__ == '__main__': logger.info('get ftp client successd') #1.传输upgrade包到client端 - local_path = UPGRADE_PKG_PATH - remote_path = '/root' + try: - put_info = ftp_client.put(local_path, remote_path, confirm=True) + put_info = ftp_client.put(UPGRADE_PKG_PATH, '/root', confirm=True) except Exception: logger.error('upload failed') exit() -- Gitee From 914d4d63f085b347b338bf05c93ce901360e10be Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 19:37:24 +0800 Subject: [PATCH 08/11] add update tools --- tools/update_os/check_pre_upgrade.py | 4 ++-- tools/update_os/command.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/update_os/check_pre_upgrade.py b/tools/update_os/check_pre_upgrade.py index 7ee2d4c..070859f 100644 --- a/tools/update_os/check_pre_upgrade.py +++ b/tools/update_os/check_pre_upgrade.py @@ -7,7 +7,7 @@ Content: """ import os import logging -from command import Command +from command import Commands from ssh import SSHConnect, login @@ -86,7 +86,7 @@ if __name__ == '__main__': '-repo', REPO_EVE + ',' + REPO_UPDATE + ',' + REPO_EPOL, '-o', collect_path] try: - result = Command().run_single_cmd(cmd, logging_error=False)\ + result = Commands().run_single_cmd(cmd, logging_error=False)\ .strip().split('\n') except Exception: logger.error('upgrade check failed') diff --git a/tools/update_os/command.py b/tools/update_os/command.py index 6e40ca2..12b99cd 100644 --- a/tools/update_os/command.py +++ b/tools/update_os/command.py @@ -10,7 +10,7 @@ import subprocess import logging -class Command(object): +class Commands(object): logging.basicConfig( level=logging.INFO, format='%(asctime)s %(filename) \ -- Gitee From 836de374e95b2cf82baafcf3bc0719f755912c19 Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 19:44:37 +0800 Subject: [PATCH 09/11] add update tools --- tools/update_os/command.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tools/update_os/command.py b/tools/update_os/command.py index 12b99cd..4a21e2c 100644 --- a/tools/update_os/command.py +++ b/tools/update_os/command.py @@ -11,14 +11,6 @@ import logging class Commands(object): - logging.basicConfig( - level=logging.INFO, - format='%(asctime)s %(filename) \ - s[line:%(lineno)d] \ - %(levelname)s %(message)s', - filename='command.log', - filemode='w', - ) logger = logging """ -- Gitee From af0dfe4f48e7c4636f950c48056788db3d84cb65 Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 20:02:13 +0800 Subject: [PATCH 10/11] add update tools --- tools/update_os/command.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/update_os/command.py b/tools/update_os/command.py index 4a21e2c..1ecca0c 100644 --- a/tools/update_os/command.py +++ b/tools/update_os/command.py @@ -9,10 +9,9 @@ Content: import subprocess import logging +logger = logging class Commands(object): - - logger = logging """ 系统命令封装,收编挪到公共组件目录 """ -- Gitee From 2fbb402b97fc8ef3da655f92b343bd2e8b2a0f90 Mon Sep 17 00:00:00 2001 From: "futao (E)" Date: Tue, 20 Dec 2022 20:06:58 +0800 Subject: [PATCH 11/11] add update tools --- tools/update_os/command.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/update_os/command.py b/tools/update_os/command.py index 1ecca0c..f113c4f 100644 --- a/tools/update_os/command.py +++ b/tools/update_os/command.py @@ -11,6 +11,7 @@ import logging logger = logging + class Commands(object): """ 系统命令封装,收编挪到公共组件目录 -- Gitee