diff --git a/README.md b/README.md index 4dc103f5d6545239f0bd7eb7f13fc2c0eda46973..9f3e2f02248046abe4bac0479acf4ef917bf4649 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,14 @@ 该选项使用mirrors.aliyun.com加速下载。 +* `--log_dir` 设置自定义日志路径 + + 该选项会在用户自定义路径下创建日志文件`centos2anolis.log`,不使用该选项则会在默认路径`/var/log/`下创建日志`centos2anolis.log`。 + +* `--progress_file` 设置自定义执行状态信息json文件 + + 该选项会在用户自定义json文件路径下创建文件,不使用该选项则创建默认文件`/var/log/centostoanolis.json`。 + ## 问题反馈 任何问题,需求或者疑问,都可以在[bugzilla.openanolis.cn][1]上提交。 diff --git a/centos2anolis.py b/centos2anolis.py index eeec5a4bed46c66a2a72b9738c73669071cb33ee..8718d96f071f46aa591035da2f2a94c44c85df7e 100644 --- a/centos2anolis.py +++ b/centos2anolis.py @@ -12,6 +12,7 @@ import sys import shutil import argparse import platform +import time import urllib.request import json import logging @@ -71,10 +72,23 @@ centosv = "" internal_ecs = False dst_release = ['anolis-release', 'anolis-repos', 'anolis-gpg-keys'] old_release_pkg = "" +jd={ + "Timestamp":0, + "Progress":0, + "ProgressInfo":"", + "ErrorCode":0, + "ErrorMsg":"" +} +# Count of functions executed +jsoncount=0 +# The total number of functions to be executed in ccontinue. +jsonsum=28 +custom_json_file1="" def get_current_version(continue_after_sync = False): global centosversion global centosv + global jd log_it(logging.INFO, "Get current OS version.") crtversion = "centos" @@ -87,6 +101,9 @@ def get_current_version(continue_after_sync = False): float(centosv) except Exception as e: log_it(logging.ERROR, "Version not found, Please Check!!") + jd["ErrorCode"] = 1 + jd["ErrorMsg"] = str(e) + update_json_info() sys.exit(1) centosversion=int(float(centosv)) if centosversion == 0: @@ -183,7 +200,7 @@ def change_repo_mirror(reposdir, prefix): def backup_repofiles(): global old_release_pkg - log_it(logging.INFO, "Backing up and removing old repository files...") + log_it(logging.INFO, "Backing up and removing old repository files...", True) repos = [] if re.match('centos-release-' + str(centosversion) + '\.*|centos-linux-release-' + str(centosversion) + '\.*', old_release_pkg): old_version_r = subprocess.check_output('rpm -qa centos*repos', shell=True) @@ -223,7 +240,7 @@ def check_version(version): global repostr_an7 global repostr_an8 - log_it(logging.INFO, "Checking the version of Anolis OS") + log_it(logging.INFO, "Checking the version of Anolis OS", True) if float(centosv) == 7.0: log_it(logging.INFO, "Set 'setenforce 0' in CentOS 7.0") subprocess.run('setenforce 0', shell=True) @@ -266,7 +283,7 @@ def check_version(version): sys.exit(1) def process_special_pkgs(): - log_it(logging.INFO, "Swap centos-logos related packages with Anolis OS packages") + log_it(logging.INFO, "Swap centos-logos related packages with Anolis OS packages", True) subprocess.run('rpm -q centos-logos-ipa && yum swap -y centos-logos-ipa anolis-logos-ipa', shell=True) subprocess.run('rpm -q centos-logos-httpd && yum swap -y centos-logos-httpd anolis-logos-httpd', shell=True) @@ -291,6 +308,7 @@ def process_special_pkgs(): subprocess.run('rpm -e $(rpm -q gpg-pubkey --qf "%{NAME}-%{VERSION}-%{RELEASE} %{PACKAGER}\\n" | grep CentOS | awk \'{print $1}\')', shell=True) def process_pkgs_for_c7(): + log_it(logging.INFO, "process pkgs for c7", True) if centosversion != 7: return @@ -334,7 +352,7 @@ def get_version_from_rpm(string): return version def check_mysql(version): - log_it(logging.INFO, 'Checking mysql version') + log_it(logging.INFO, 'Checking mysql version', True) mysql_install = False try: subprocess.check_call('rpm -q mysql-server', shell=True) @@ -385,13 +403,24 @@ def process_yum_utils_install(internal_ecs): if not check_pkg('yumdownloader'): clean_and_exit() -def log_it(level=logging.INFO, msg=""): +def log_it(level=logging.INFO, msg="", js=False): + if js: + update_json_info(msg) print(msg) logging.log(level, msg) -def set_log_info(): +def set_log_info(custom_log_dir): + fn = '/var/log/centos2anolis.log' + if custom_log_dir: + if custom_log_dir[0] == '/' and '\\' not in custom_log_dir: + if not os.path.exists(custom_log_dir): + os.makedirs(custom_log_dir) + fn = custom_log_dir + '/centos2anolis.log' + else: + print("Please enter a directory in the correct format") + sys.exit(1) logging.basicConfig( - filename='/var/log/centos2anolis.log', + filename=fn, level=logging.INFO, format="%(asctime)s.%(msecs)03d[%(levelname)-8s]: %(message)s", datefmt="%Y-%m-%d %H:%M:%S" @@ -406,14 +435,14 @@ def check_user(): def check_required_pkgs(): # check required packages - log_it(logging.INFO, 'Checking required packages') + log_it(logging.INFO, 'Checking required packages', True) for pkg in ['rpm', 'yum', 'curl']: if not check_pkg(pkg): log_it(logging.ERROR, "Could not found " + pkg) sys.exit(1) def check_i686_pkgs(): - log_it(logging.INFO, 'Checking i686 packages') + log_it(logging.INFO, 'Checking i686 packages', True) 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: @@ -424,7 +453,7 @@ def check_i686_pkgs(): def verify_before(verify_all_rpms): if verify_all_rpms: - log_it(logging.INFO, "Creating a list of RPMs installed before the switch") + log_it(logging.INFO, "Creating a list of RPMs installed before the switch", True) log_it(logging.INFO, "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" \ @@ -436,12 +465,14 @@ def verify_before(verify_all_rpms): for f in files: if re.match(hostname+'-rpms-(.*)\.log', f): log_it(logging.INFO, f) + else: + log_it(logging.INFO, "Verify before", True) def check_distribution(): global old_release_pkg # check if the os old_version is supported - log_it(logging.INFO, "Checking: distribution") + log_it(logging.INFO, "Checking: distribution", True) 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] @@ -474,7 +505,7 @@ def check_distribution(): sys.exit(1) def check_yum_lock(): - log_it(logging.INFO, "Checking: yum lock") + log_it(logging.INFO, "Checking: yum lock", True) if os.path.exists('/var/run/yum.pid'): with open('/var/run/yum.pid', 'r') as f: pid = f.read() @@ -487,11 +518,13 @@ def check_yum_lock(): def check_dnf_modules(): global internal_ecs + global jd if centosversion != 8: + log_it(logging.INFO, "Check dnf modules", True) return # check dnf - log_it(logging.INFO, "Checking: dnf modules enable list") + log_it(logging.INFO, "Checking: dnf modules enable list", True) log_it(logging.INFO, "Identifying dnf modules that are enabled...") repostr_centos_vault_8 = '''[c8_vault_baseos] @@ -521,6 +554,9 @@ enabled=1 json.dump(enabled_modules, f) except Exception as e: log_it(logging.ERROR, "Save enabled_modules file failed") + jd["ErrorCode"] = 1 + jd["ErrorMsg"] = str(e) + update_json_info() sys.exit(1) unknown_mods = [] if len(enabled_modules) > 0: @@ -545,7 +581,7 @@ enabled=1 def check_network(): global internal_ecs - log_it(logging.INFO, "Checking network environment") + log_it(logging.INFO, "Checking network environment", True) dst_url = "http://mirrors.cloud.aliyuncs.com" internal_ecs = False try: @@ -557,14 +593,14 @@ def check_network(): pass def check_yumdownloader(): - log_it(logging.INFO, "Looking for yumdownloader") + log_it(logging.INFO, "Looking for yumdownloader", True) if not check_pkg('yumdownloader'): process_yum_utils_install(internal_ecs) def check_repodir(): global reposdir - log_it(logging.INFO, "Finding your repository directory") + log_it(logging.INFO, "Finding your repository directory", True) dir = '/etc/yum.repos.d/' if os.path.isdir(dir): reposdir = dir @@ -577,7 +613,7 @@ def check_repodir(): sys.exit(1) def process_third_repos(): - log_it(logging.INFO, "Processing contentdir in repo files...") + log_it(logging.INFO, "Processing contentdir in repo files...", True) subprocess.run("sed -i \'s/\$contentdir/centos/g\' /etc/yum.repos.d/*.repo", shell=True) log_it(logging.INFO, "Backing up and removing 3rd-part repository files...") @@ -585,6 +621,7 @@ def process_third_repos(): subprocess.run("mv $(find /etc/yum.repos.d/*.repo) /etc/yum.repos.d/migration-bak", shell=True) def write_switch_repo(accelerate): + log_it(logging.INFO, "switch to anolis.repo", True) repofile = os.path.join(reposdir, 'switch-to-anolis.repo') with open(repofile, 'w') as f: if centosversion == 7: @@ -600,7 +637,7 @@ def write_switch_repo(accelerate): subprocess.run('sed -i \'s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) def modify_yum_conf(): - log_it(logging.INFO, "Removing CentOS-specific yum configuration from /etc/yum.conf ...") + log_it(logging.INFO, "Removing CentOS-specific yum configuration from /etc/yum.conf ...", True) with open('/etc/yum.conf', 'r') as f: content = f.read() if re.search(r'^distroverpkg=', content, re.MULTILINE): @@ -611,10 +648,11 @@ def modify_yum_conf(): f.write(content) def download_anolis_release(): - log_it(logging.INFO, "Downloading Anolis OS release package...") + global jd + log_it(logging.INFO, "Downloading Anolis OS release package...", True) try: stat = subprocess.check_output("yumdownloader "+' '.join(dst_release), shell=True) - except Exception: + except Exception as e: log_it(logging.ERROR, "Could not download the following packages from " + yum_url) log_it(logging.ERROR, '\n'.join(dst_release)) log_it(logging.ERROR, "") @@ -623,10 +661,13 @@ def download_anolis_release(): log_it(logging.ERROR, "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.") + jd["ErrorCode"] = 1 + jd["ErrorMsg"] = str(e) + update_json_info() clean_and_exit() def switch_release_pkgs(): - log_it(logging.INFO, "Switching old release package with Anolis OS...") + log_it(logging.INFO, "Switching old release package with Anolis OS...", True) dst_rpms = [s + '*.rpm' for s in dst_release] subprocess.run('rpm -i --force ' + ' '.join(dst_rpms) + ' --nodeps', shell=True) if centosversion == 7: @@ -636,6 +677,7 @@ def switch_release_pkgs(): os.remove(os.path.join(reposdir, 'switch-to-anolis.repo')) def optimise_repofile(accelerate): + log_it(logging.INFO, "optimise repofile", True) if accelerate: change_repo_mirror(reposdir, 'AnolisOS-') @@ -647,7 +689,7 @@ def optimise_repofile(accelerate): def install_base_pkgs(): base_packages=['basesystem','initscripts','anolis-logos','plymouth','grub2','grubby'] - log_it(logging.INFO, "Installing base packages for Anolis OS...") + log_it(logging.INFO, "Installing base packages for Anolis OS...", True) subprocess.run('yum clean all', shell=True) cmd = 'yum shell -y < - 0.1-5 +- add custom log path +- add custom json file + * Fri Mar 25 2022 mgb0110571 - 0.1-4 - fix module virt