diff --git a/backend/lib/get_info.py b/backend/lib/get_info.py index babd8843cb065d2031f3efcb1a312112df4b196c..8c7d7cd97bc56ffad4b4cbad9c6f0ef06e882f29 100644 --- a/backend/lib/get_info.py +++ b/backend/lib/get_info.py @@ -4,12 +4,17 @@ from lib.common import get_remote_connect app = None AGENT_ID = '' -def get_info(ssh, agent_model, db): +def get_info(ssh, sftp, agent_model, db): try: - stdin, stdout, stderr = ssh.exec_command('cat /etc/os-release') - result_list = stdout.readlines() - os_release_content = [i.rstrip('\n') for i in result_list] - system_version = list(filter(lambda x: 'PRETTY_NAME' in x, os_release_content))[0].split('=')[-1].replace('"', '') + # stdin, stdout, stderr = ssh.exec_command('cat /etc/os-release') + # result_list = stdout.readlines() + # os_release_content = [i.rstrip('\n') for i in result_list] + # system_version = list(filter(lambda x: 'PRETTY_NAME' in x, os_release_content))[0].split('=')[-1].replace('"', '') + stdin, stdout, stderr = ssh.exec_command(f'mkdir -p /opt/nfs/tmp/python3') + + stdin, stdout, stderr = ssh.exec_command('cat /etc/centos-release') + system_version = stdout.read().decode() + stdin, stdout, stderr = ssh.exec_command('uname -rm') kernel, schema = stdout.read().decode().rstrip('\n').split(' ') # backup_path = agent_model.backup_path @@ -22,17 +27,69 @@ def get_info(ssh, agent_model, db): # split_list = [i for i in stdout.read().decode().split(' ') if i] # avail = split_list[9] # agent_model.avail = avail + stdin, stdout, stderr = ssh.exec_command(f'cat /etc/yum.conf|grep cachedir') + yum_backup_path = stdout.read().decode().split('=')[-1] + stdin, stdout, stderr = ssh.exec_command(f'df -h {yum_backup_path}') + split_list = [i for i in stdout.read().decode().split(' ') if i] + avail = split_list[9] agent_model.schema = schema agent_model.system_version = system_version agent_model.kernel = kernel db.session.add(agent_model) db.session.commit() + upload_file(sftp) + check_python3(ssh, sftp, system_version) app.logger.info(f"agent_id: {AGENT_ID}, 操作系统数据更新完成") return 1 except Exception as e: app.logger.error(f"agent_id: {AGENT_ID}, 操作系统数据更新失败, 原因为 {str(e)}") return 0 +def upload_file(sftp): + sftp.put('/opt/nfs/nfs_migration/backend/lib/migrate/centos82nfs.py', f'/opt/nfs/tmp/centos82nfs.py', confirm=True) + sftp.put('/opt/nfs/nfs_migration/backend/lib/migrate/centos62nfs.py', f'/opt/nfs/tmp/centos62nfs.py', confirm=True) + +def install_python3_for_centos6(ssh, sftp): + import time + stdin, stdout, stderr = ssh.exec_command('rpm -qa |grep python3') + if not stdout.read(): + sftp.put('/opt/nfs/nfs_migration/backend/lib/install_python3_centos6.sh', '/opt/nfs/tmp/python3/install_python3_centos6.sh', confirm=True) + stdin, stdout, stderr = ssh.exec_command('chmod 777 /opt/nfs/tmp/python3/install_python3_centos6.sh; ') + time.sleep(60) + +def install_python3_for_centos7(ssh, sftp): + stdin, stdout, stderr = ssh.exec_command('rpm -qa |grep python3') + if not stdout.read(): + sftp.put('/opt/nfs/nfs_migration/backend/lib/migrate/libtirpc-0.2.4-0.16.el7.x86_64.rpm', '/opt/nfs/tmp/python3/libtirpc-0.2.4-0.16.el7.x86_64.rpm', confirm=True) + sftp.put('/opt/nfs/nfs_migration/backend/lib/migrate/python3-3.6.8-19.el7_9.x86_64.rpm', '/opt/nfs/tmp/python3/python3-3.6.8-19.el7_9.x86_64.rpm', confirm=True) + sftp.put('/opt/nfs/nfs_migration/backend/lib/migrate/python3-libs-3.6.8-19.el7_9.x86_64.rpm', '/opt/nfs/tmp/python3/python3-libs-3.6.8-19.el7_9.x86_64.rpm', confirm=True) + sftp.put('/opt/nfs/nfs_migration/backend/lib/migrate/python3-pip-9.0.3-8.el7.noarch.rpm', '/opt/nfs/tmp/python3/python3-pip-9.0.3-8.el7.noarch.rpm', confirm=True) + sftp.put('/opt/nfs/nfs_migration/backend/lib/migrate/python3-setuptools-39.2.0-10.el7.noarch.rpm', '/opt/nfs/tmp/python3/python3-setuptools-39.2.0-10.el7.noarch.rpm', confirm=True) + stdin, stdout, stderr = ssh.exec_command('rpm -ivh /opt/nfs/tmp/python3/python3*') + +def check_python3(ssh, sftp, system_version): + # 提取匹配到的大版本号 + major_version = get_system_version(system_version) + + if major_version: + if major_version == '6': + install_python3_for_centos6(ssh, sftp) + elif major_version == '7': + install_python3_for_centos7(ssh, sftp) + else: + app.logger.error(f"agent_id: {AGENT_ID}, 未找到匹配的大版本号") + +def get_system_version(system_version): + import re + # 定义匹配模式 + pattern = r"(\d+)" + # 使用正则表达式进行匹配 + match = re.search(pattern, system_version) + if match: + return match.group(1) + else: + return None + def run(params): global AGENT_ID, app start_time = datetime.now().strftime('%Y%m%d %H:%M:%S') @@ -43,7 +100,7 @@ def run(params): result = 0 try: with app.app_context(): - result = get_info(ssh, agent_model, params['db']) + result = get_info(ssh, sftp, agent_model, params['db']) except Exception as e: app.logger.error(f"操作系统数据获取失败,原因为: {str(e)}") finally: