From 451c0b62d70f56bb83b485b7db66e9603ff9fb87 Mon Sep 17 00:00:00 2001 From: mgb01105731 Date: Fri, 11 Feb 2022 10:13:09 +0800 Subject: [PATCH] Add -c usage for centos2anolis.py --- README.md | 4 + centos2anolis.py | 462 ++++++++++++++++++++++++--------------------- centos2anolis.spec | 5 +- 3 files changed, 256 insertions(+), 215 deletions(-) diff --git a/README.md b/README.md index 924b606..0316000 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,10 @@ Please ensure you have a **complete backup** of the system before you execute th * `Note that CentOS 8.0-8.2 supports migration to Anolis OS 8.2 or Anolis OS 8.4`. * `Note that CentOS 8.3/8.4 only supports migration to Anolis OS 8.4`. +* `-c` continue to migration + + When the script is interrupted during the execution of distro-sync, you can use `-c` to continue the execution after solving the problem. + ## Support Open an issue on [bugs.openanolis.cn][1] for any bugs, questions and requirements. diff --git a/centos2anolis.py b/centos2anolis.py index 9b5726a..7442518 100644 --- a/centos2anolis.py +++ b/centos2anolis.py @@ -14,6 +14,7 @@ import shutil import argparse import platform import urllib.request +import json yum_url="https://mirrors.openanolis.org/anolis/" github_url="https://gitee.com/anolis/centos2anolis" @@ -65,15 +66,24 @@ python-oauth sl-logos yum-rhn-plugin centos-indexhtml' reposdir ='' anolislist7 = ['7.7','7.9'] anolislist8 = ['8.2','8.4','8'] -try: - centosv = str(subprocess.check_output("cat /etc/centos-release | awk '{print $4}'", shell=True), 'utf-8')[:3] - print("CentOS version is " + centosv) - float(centosv) -except: - print("CentOS version not found or CentOS version is wrong type") - print("please check the file of centos-release") - sys.exit(1) -centosversion=int(float(centosv)) +centosversion = 0 +centosv = "" + +def get_current_version(continue_after_sync = False): + global centosversion + global centosv + crtversion = "centos" + if continue_after_sync: + print("Using -c !!") + crtversion = "anolis" + try: + centosv = str(subprocess.check_output("cat /etc/"+ crtversion +"-release | awk '{print $4}'", shell=True), 'utf-8')[:3] + print("Version is " + centosv) + float(centosv) + except Exception as e: + print("Version not found, Please Check!!") + sys.exit(1) + centosversion=int(float(centosv)) def check_pkg(pkg): if pkg.split('/')[0] == '': @@ -397,7 +407,7 @@ enabled=1 clean_and_exit() -def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, version=False): +def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, version=False, continue_after_sync = False): global reposdir print("Checking if the tool is executed by root user") @@ -406,246 +416,269 @@ def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, vers print("Please run the tool as root user.") sys.exit(1) - print("Checking the version of Anolis OS") - check_version(version) - - # check required packages - print('Checking required packages') - for pkg in ['rpm', 'yum', 'curl']: - if not check_pkg(pkg): - print("Could not found " + pkg) - sys.exit(1) - - print('Checking i686 packages') - i686_pkgs = subprocess.check_output('rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} %{VENDOR}\n" | grep CentOS | grep i686 | awk \'{print $1}\'', shell=True) - i686_pkgs = str(i686_pkgs, 'utf-8') - if len(i686_pkgs) != 0: - print("Anolis OS does not provide i686 packages, please remove them before migration.") - print("Installed i686 packages:") - print(i686_pkgs) + print("Get current version.") + get_current_version(continue_after_sync) + if centosversion == 0: + print("get current version failed.") sys.exit(1) + if not continue_after_sync: + print("Checking the version of Anolis OS") + check_version(version) + + # check required packages + print('Checking required packages') + for pkg in ['rpm', 'yum', 'curl']: + if not check_pkg(pkg): + print("Could not found " + pkg) + sys.exit(1) + + print('Checking i686 packages') + i686_pkgs = subprocess.check_output('rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} %{VENDOR}\n" | grep CentOS | grep i686 | awk \'{print $1}\'', shell=True) + i686_pkgs = str(i686_pkgs, 'utf-8') + if len(i686_pkgs) != 0: + print("Anolis OS does not provide i686 packages, please remove them before migration.") + print("Installed i686 packages:") + print(i686_pkgs) + sys.exit(1) + + + if verify_all_rpms: + print("Creating a list of RPMs installed before the switch") + print("Verifying RPMs installed before the switch against RPM database") + out1 = subprocess.check_output('rpm -qa --qf \ + "%{NAME}|%{VERSION}|%{RELEASE}|%{INSTALLTIME}|%{VENDOR}|%{BUILDTIME}|%{BUILDHOST}|%{SOURCERPM}|%{LICENSE}|%{PACKAGER}\\n" \ + | sort > "/var/tmp/$(hostname)-rpms-list-before.log"', shell=True) + out2 = subprocess.check_output('rpm -Va | sort -k3 > "/var/tmp/$(hostname)-rpms-verified-before.log"',shell=True) + files = os.listdir('/var/tmp/') + hostname = socket.gethostname() + print("Review the output of following files:") + for f in files: + if re.match(hostname+'-rpms-(.*)\.log', f): + print(f) + + # check if the os old_version is supported + print("========= Checking: distribution =========") + old_version = subprocess.check_output("rpm -q --whatprovides /etc/redhat-release", shell=True) + old_version = str(old_version, 'utf-8') + old_version = old_version.split('\n')[:-1] + if len(old_version) == 0: + print("You appear to be running an unsupported distribution.") + sys.exit(1) + if len(old_version) > 1: + print("Could not determine your distribution because multiple packages are providing redhat-release:") + print('\n'.join(old_version)) + sys.exit(1) - if verify_all_rpms: - print("Creating a list of RPMs installed before the switch") - print("Verifying RPMs installed before the switch against RPM database") - out1 = subprocess.check_output('rpm -qa --qf \ - "%{NAME}|%{VERSION}|%{RELEASE}|%{INSTALLTIME}|%{VENDOR}|%{BUILDTIME}|%{BUILDHOST}|%{SOURCERPM}|%{LICENSE}|%{PACKAGER}\\n" \ - | sort > "/var/tmp/$(hostname)-rpms-list-before.log"', shell=True) - out2 = subprocess.check_output('rpm -Va | sort -k3 > "/var/tmp/$(hostname)-rpms-verified-before.log"',shell=True) - files = os.listdir('/var/tmp/') - hostname = socket.gethostname() - print("Review the output of following files:") - for f in files: - if re.match(hostname+'-rpms-(.*)\.log', f): - print(f) - - # check if the os old_version is supported - print("========= Checking: distribution =========") - old_version = subprocess.check_output("rpm -q --whatprovides /etc/redhat-release", shell=True) - old_version = str(old_version, 'utf-8') - old_version = old_version.split('\n')[:-1] - if len(old_version) == 0: - print("You appear to be running an unsupported distribution.") - sys.exit(1) - if len(old_version) > 1: - print("Could not determine your distribution because multiple packages are providing redhat-release:") - print('\n'.join(old_version)) - sys.exit(1) - - old_version = old_version[0] - print(old_version) - if re.match('anolis-release', old_version): - print("You are already using Anolis OS.") - sys.exit(1) - - elif re.match('centos-linux-release', old_version): - subver = old_version.split('-')[3] - - elif re.match('redhat-release|centos-release|sl-release', old_version): - subver = old_version.split('-')[2] - - else: - print("Your are using an unsupported distribution.") - sys.exit(1) + old_version = old_version[0] + print(old_version) + if re.match('anolis-release', old_version): + print("You are already using Anolis OS.") + sys.exit(1) - if not re.match(str(centosversion),subver): - print("You appear to be running an unsupported distribution.") - sys.exit(1) + elif re.match('centos-linux-release', old_version): + subver = old_version.split('-')[3] - base_packages=['basesystem','initscripts','anolis-logos','plymouth','grub2','grubby'] - - print("========= Checking: yum lock ===========") - if os.path.exists('/var/run/yum.pid'): - with open('/var/run/yum.pid', 'r') as f: - pid = f.read() - with open('/proc/' + pid + '/comm', 'r') as ff: - comm = ff.read() - print('Another app is currently holding the yum lock: ' + comm) - print('Running as pid: ' + pid) - print('Please kill it and run the tool again.') - sys.exit(1) + elif re.match('redhat-release|centos-release|sl-release', old_version): + subver = old_version.split('-')[2] - if centosversion == 8: - # check dnf - print("========= Checking: dnf =========") - print("Identifying dnf modules that are enabled...") - enabled_modules = str( - subprocess.check_output("dnf module list --enabled --disablerepo=* | grep rhel | awk '{print $1}'", shell=True), - 'utf-8') - enabled_modules = enabled_modules.split('\n')[:-1] - unknown_mods = [] - if len(enabled_modules) > 0: - for mod in enabled_modules: - if re.fullmatch('container-tools|llvm-toolset', mod): - subprocess.run('dnf module --disablerepo=* reset -y ' + mod, shell=True) - if not re.fullmatch('container-tools|go-toolset|jmc|llvm-toolset|rust-toolset|virt', mod): - unknown_mods.append(mod) - if len(unknown_mods) > 0: - print('This tool is unable to automatically switch module(s) ' \ - + ','.join(unknown_mods) \ - + ' from a CentOS \'rhel\' stream to an Anolis OS equivalent.' \ - ) - opt = input('Do you want to continue and resolve it manually? (Yes or No)\n' + \ - 'You may want select No to stop and raise an issue on ' \ - + github_url \ - + ' for advice. ' \ - ) - if opt != 'Yes': - sys.exit(1) - - print("check network environment") - dst_url = "http://mirrors.cloud.aliyuncs.com" - internal_ecs = False - try: - dst_status = urllib.request.urlopen(dst_url, timeout=5).code - internal_ecs = True - print("check internal network environment is True") - except Exception as err: - print(err) - pass - - print("========= Looking for yumdownloader ==========") - if not check_pkg('yumdownloader'): - process_yum_utils_install(internal_ecs) + else: + print("Your are using an unsupported distribution.") + sys.exit(1) - print("========= Finding your repository directory =========") - dir = '/etc/yum.repos.d/' - if os.path.isdir(dir): - reposdir = dir - else: - print("repository directory not found") - sys.exit(1) + if not re.match(str(centosversion),subver): + print("You appear to be running an unsupported distribution.") + sys.exit(1) - if len(reposdir) == 0: - print("Could not locate your repository directory.") - sys.exit(1) + base_packages=['basesystem','initscripts','anolis-logos','plymouth','grub2','grubby'] + + print("========= Checking: yum lock ===========") + if os.path.exists('/var/run/yum.pid'): + with open('/var/run/yum.pid', 'r') as f: + pid = f.read() + with open('/proc/' + pid + '/comm', 'r') as ff: + comm = ff.read() + print('Another app is currently holding the yum lock: ' + comm) + print('Running as pid: ' + pid) + print('Please kill it and run the tool again.') + sys.exit(1) - print("Backing up and removing old repository files...") - old_version = backup_repofiles(old_version) + if centosversion == 8: + # check dnf + print("========= Checking: dnf =========") + print("Identifying dnf modules that are enabled...") + enabled_modules = str( + subprocess.check_output("dnf module list --enabled --disablerepo=* | grep rhel | awk '{print $1}'", shell=True), + 'utf-8') + enabled_modules = enabled_modules.split('\n')[:-1] + try: + with open('/root/enabled_modules.json', 'w') as f: + json.dump(enabled_modules, f) + except Exception as e: + print("set enabled_modules file failed") + sys.exit(1) + unknown_mods = [] + if len(enabled_modules) > 0: + for mod in enabled_modules: + if re.fullmatch('container-tools|llvm-toolset', mod): + subprocess.run('dnf module --disablerepo=* reset -y ' + mod, shell=True) + if not re.fullmatch('container-tools|go-toolset|jmc|llvm-toolset|rust-toolset|virt', mod): + unknown_mods.append(mod) + if len(unknown_mods) > 0: + print('This tool is unable to automatically switch module(s) ' \ + + ','.join(unknown_mods) \ + + ' from a CentOS \'rhel\' stream to an Anolis OS equivalent.' \ + ) + opt = input('Do you want to continue and resolve it manually? (Yes or No)\n' + \ + 'You may want select No to stop and raise an issue on ' \ + + github_url \ + + ' for advice. ' \ + ) + if opt != 'Yes': + sys.exit(1) + + print("check network environment") + dst_url = "http://mirrors.cloud.aliyuncs.com" + internal_ecs = False + try: + dst_status = urllib.request.urlopen(dst_url, timeout=5).code + internal_ecs = True + print("check internal network environment is True") + except Exception as err: + print(err) + pass - print("Processing contentdir in repo files...") - subprocess.run("sed -i \'s/\$contentdir/centos/g\' /etc/yum.repos.d/*.repo", shell=True) + print("========= Looking for yumdownloader ==========") + if not check_pkg('yumdownloader'): + process_yum_utils_install(internal_ecs) - print("Backing up and removing 3rd-part repository files...") - subprocess.run("mkdir /etc/yum.repos.d/migration-bak", shell=True) - subprocess.run("mv $(find /etc/yum.repos.d/*.repo) /etc/yum.repos.d/migration-bak", shell=True) + print("========= Start Check: mysql server =========") + check_mysql(internal_ecs) - repofile = os.path.join(reposdir, 'switch-to-anolis.repo') - with open(repofile, 'w') as f: - if centosversion == 7: - repostr_an=repostr_an7 + print("========= Finding your repository directory =========") + dir = '/etc/yum.repos.d/' + if os.path.isdir(dir): + reposdir = dir else: - repostr_an=repostr_an8 - f.write(repostr_an) - if accelerate: - change_repo_mirror(reposdir, 'switch-to-anolis.repo') - if internal_ecs: - subprocess.run('sed -i \'s/mirrors.openanolis.org/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) - subprocess.run('sed -i \'s/mirrors.openanolis.cn/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) - subprocess.run('sed -i \'s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) - - print("========= Start Check: mysql server =========") - check_mysql(internal_ecs) + print("repository directory not found") + sys.exit(1) - print("========= Start converting =========") + if len(reposdir) == 0: + print("Could not locate your repository directory.") + sys.exit(1) + print("Backing up and removing old repository files...") + old_version = backup_repofiles(old_version) - print("Removing CentOS-specific yum configuration from /etc/yum.conf ...") - with open('/etc/yum.conf', 'r') as f: - content = f.read() - if re.search(r'^distroverpkg=', content, re.MULTILINE): - content = re.sub(r"\n(distroverpkg=)", r"\n#\1", content) - if re.search(r'bugtracker_url=', content, re.MULTILINE): - content = re.sub(r"\n(bugtracker_url=)", r"\n#\1", content) - with open('/etc/yum.conf', 'w') as f: - f.write(content) + print("Processing contentdir in repo files...") + subprocess.run("sed -i \'s/\$contentdir/centos/g\' /etc/yum.repos.d/*.repo", shell=True) - print("Downloading Anolis OS release package...") - dst_release = ['anolis-release', 'anolis-repos', 'anolis-gpg-keys'] - try: - stat = subprocess.check_output("yumdownloader "+' '.join(dst_release), shell=True) - except Exception: - print("Could not download the following packages from " + yum_url) - print('\n'.join(dst_release)) - print() - print("Are you behind a proxy? If so, make sure the 'http_proxy' environmen") - print("variable is set with your proxy address.") - print("An error occurred while attempting to switch this system to Anolis OS" + \ - "and it may be in an unstable/unbootable state. To avoid further issues, " +\ - "the script has terminated.") - clean_and_exit() + print("Backing up and removing 3rd-part repository files...") + subprocess.run("mkdir /etc/yum.repos.d/migration-bak", shell=True) + subprocess.run("mv $(find /etc/yum.repos.d/*.repo) /etc/yum.repos.d/migration-bak", shell=True) - print("Switching old release package with Anolis OS...") - dst_rpms = [s + '*.rpm' for s in dst_release] - subprocess.run('rpm -i --force ' + ' '.join(dst_rpms) + ' --nodeps', shell=True) - if centosversion ==7: - subprocess.run('rpm -e --nodeps ' + old_version, shell=True) - else: - subprocess.run('rpm -e --nodeps ' + old_version + ' centos-gpg-keys', shell=True) - os.remove(repofile) - # switch completed + repofile = os.path.join(reposdir, 'switch-to-anolis.repo') + with open(repofile, 'w') as f: + if centosversion == 7: + repostr_an=repostr_an7 + else: + repostr_an=repostr_an8 + f.write(repostr_an) + if accelerate: + change_repo_mirror(reposdir, 'switch-to-anolis.repo') + if internal_ecs: + subprocess.run('sed -i \'s/mirrors.openanolis.org/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) + subprocess.run('sed -i \'s/mirrors.openanolis.cn/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) + subprocess.run('sed -i \'s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) + + print("========= Start converting =========") + + + print("Removing CentOS-specific yum configuration from /etc/yum.conf ...") + with open('/etc/yum.conf', 'r') as f: + content = f.read() + if re.search(r'^distroverpkg=', content, re.MULTILINE): + content = re.sub(r"\n(distroverpkg=)", r"\n#\1", content) + if re.search(r'bugtracker_url=', content, re.MULTILINE): + content = re.sub(r"\n(bugtracker_url=)", r"\n#\1", content) + with open('/etc/yum.conf', 'w') as f: + f.write(content) + + print("Downloading Anolis OS release package...") + dst_release = ['anolis-release', 'anolis-repos', 'anolis-gpg-keys'] + try: + stat = subprocess.check_output("yumdownloader "+' '.join(dst_release), shell=True) + except Exception: + print("Could not download the following packages from " + yum_url) + print('\n'.join(dst_release)) + print() + print("Are you behind a proxy? If so, make sure the 'http_proxy' environmen") + print("variable is set with your proxy address.") + print("An error occurred while attempting to switch this system to Anolis OS" + \ + "and it may be in an unstable/unbootable state. To avoid further issues, " +\ + "the script has terminated.") + clean_and_exit() + + print("Switching old release package with Anolis OS...") + dst_rpms = [s + '*.rpm' for s in dst_release] + subprocess.run('rpm -i --force ' + ' '.join(dst_rpms) + ' --nodeps', shell=True) + if centosversion ==7: + subprocess.run('rpm -e --nodeps ' + old_version, shell=True) + else: + subprocess.run('rpm -e --nodeps ' + old_version + ' centos-gpg-keys', shell=True) + os.remove(repofile) + # switch completed - if accelerate: - change_repo_mirror(reposdir, 'AnolisOS-') + if accelerate: + change_repo_mirror(reposdir, 'AnolisOS-') - if internal_ecs: - subprocess.run('sed -i \'s/mirrors.openanolis.cn/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', shell=True) - subprocess.run('sed -i \'s/mirrors.openanolis.org/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', shell=True) - subprocess.run('sed -i \'s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', shell=True) + if internal_ecs: + subprocess.run('sed -i \'s/mirrors.openanolis.cn/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', shell=True) + subprocess.run('sed -i \'s/mirrors.openanolis.org/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', shell=True) + subprocess.run('sed -i \'s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', shell=True) - print("Installing base packages for Anolis OS...") - subprocess.run('yum clean all', shell=True) - cmd = 'yum shell -y < 0: for mod in enabled_modules: subprocess.run('dnf module reset -y '+mod, shell=True) @@ -734,6 +767,7 @@ if __name__ == "__main__": parser.add_argument('-V', action='store_true', help='Verify RPM information before and after the switch') parser.add_argument('-s', action='store_true', help='Accelerate download') parser.add_argument('-v', help='Choose the version of Anolis OS') + parser.add_argument('-c', action='store_true', help='Continue to migration after distro-sync') args = parser.parse_args() - sys.exit(main(args.r, args.V, args.s, args.v)) + sys.exit(main(args.r, args.V, args.s, args.v, args.c)) diff --git a/centos2anolis.spec b/centos2anolis.spec index 2392d34..ba1482a 100644 --- a/centos2anolis.spec +++ b/centos2anolis.spec @@ -1,6 +1,6 @@ Name: centos2anolis Version: 0.1 -Release: 4%{?dist} +Release: 5%{?dist} Summary: Transfer CentOS to Anolis. License: MIT @@ -37,6 +37,9 @@ install -m 755 %{SOURCE0} %{buildroot}/%{_sbindir} %changelog +* Fri Feb 11 2022 mgb0110571 - 0.1-5 +- Add -c usage to continue migration + * Fri Jan 28 2022 mgb0110571 - 0.1-4 - Add mysql-server check -- Gitee