From 2a60e3a842ef3dba20c7782eb22a9fce312b6577 Mon Sep 17 00:00:00 2001 From: z00793368 Date: Wed, 26 Jul 2023 15:31:35 +0800 Subject: [PATCH] this is a first commit --- script/gspylib/os/gsplatform.py | 172 ++++++++++++++++++++++++++++ script/local/LocalCheckOS.py | 39 ++++++- script/os_platform/common.py | 1 + script/os_platform/linux_distro.py | 177 +++++++++++++++++++++++++++++ script/osid.conf | 33 ++++++ 5 files changed, 419 insertions(+), 3 deletions(-) create mode 100644 script/osid.conf diff --git a/script/gspylib/os/gsplatform.py b/script/gspylib/os/gsplatform.py index 1e12c30..d496684 100644 --- a/script/gspylib/os/gsplatform.py +++ b/script/gspylib/os/gsplatform.py @@ -34,6 +34,7 @@ import subprocess import platform import socket import time +import select sys.path.append(sys.path[0] + "/../../") from gspylib.common.ErrorCode import ErrorCode @@ -42,6 +43,10 @@ localDirPath = os.path.dirname(os.path.realpath(__file__)) sys.path.insert(0, localDirPath + "/../../../lib/netifaces/") from netifaces import interfaces, ifaddresses, AF_INET, AF_INET6 +ISCONFIGURETRUE = "# isConfigure = TRUE" +ISCONFIGUREFALSE = "# isConfigure = FALSE" +SESSIONTIMEOUT = 300 + # ---------------platforms-------------------- # global variable for our platform _supported_dists = ( @@ -76,6 +81,7 @@ SUPPORT_RHEL6X_VERSION_LIST = ["6.4", "6.5", "6.6", "6.7", "6.8", "6.9", "10"] SUPPORT_RHEL7X_VERSION_LIST = ["7.0", "7.1", "7.2", "7.3", "7.4", "7.5", "7.6", "7.7", "7.8", "7.9", "10"] SUPPORT_RHEL8X_VERSION_LIST = ["8.0", "8.1", "8.2", "8.3", "8.4", "8.5"] +SUPPORT_RHEL_LEAST_VERSION = ["6.4"] SUPPORT_RHEL_SERIES_VERSION_LIST = (SUPPORT_RHEL6X_VERSION_LIST + SUPPORT_RHEL7X_VERSION_LIST + SUPPORT_RHEL8X_VERSION_LIST) @@ -149,6 +155,144 @@ def _parse_release_file(firstline): idNum = l[1] return '', version, idNum +def parse_linux_osid(filename): + """ + Tries to determine the name of the Linux OS distribution name. + + The function first looks for a distribution release file in + /etc and then reverts to _dist_try_harder() in case no + suitable files are found. + + Returns a tuple (distname,version,id) which default to the + args given as parameters. + + """ + valid_info = [] + lines_to_choose = [] + with open(filename, 'r') as file: + for line in file: + line = line.strip() + # Ignore comment lines starting with # and empty lines + if line and not line.startswith('#'): + # Separate the release name, version and bitness with spaces + distro, version = line.split() + valid_info.append({ + 'os': distro, + 'version': version + }) + lines_to_choose.append(line.strip()) + if len(lines_to_choose) == 1 or len(lines_to_choose) == 0: + return valid_info + for i, line in enumerate(lines_to_choose, 1): + print(f"{i}. {line}") + + while True: + try: + choice = int(input("Please enter the serial number of the option:")) + if (1 <= choice and choice <= len(lines_to_choose)): + chosen_line = lines_to_choose[choice - 1] + valid_info = valid_info[choice - 1] + with open(filename, 'r+') as file: + lines = file.readlines() + file.seek(0) + file.truncate() + + for line in lines: + if line.strip().startswith('#') or line.strip() == '': + file.write(line) + continue + elif chosen_line in line: + file.write(line) + else: + file.write('#' + line) + write_is_configure_true(filename, ISCONFIGURETRUE) + break + else: + print("Invalid input: Please re-enter") + except ValueError: + print("Invalid input: Please re-enter") + return valid_info + + +def write_is_configure_true(file_path, target_line): + # open the file and read all the lines + with open(file_path, 'r') as file: + lines = file.readlines() + + # Check if any row contains target content + has_target_line = any(target_line in line for line in lines) + + # If there is no target row, insert the target row before row 21 + if not has_target_line: + lines.insert(20, target_line + '\n') + + # Write the modified content back to the file + with open(file_path, 'w') as file: + file.writelines(lines) + + +def parse_linux_distributions(filename): + """ + Tries to determine the name of the Linux OS distribution name. + + The function first looks for a distribution release file in + /etc and then reverts to _dist_try_harder() in case no + + Returns a tuple (distname,version,id) which default to the + args given as parameters. + + """ + is_configure = True + valid_info = parse_linux_osid(filename) + + if len(valid_info) == 1: + write_is_configure_true(filename, ISCONFIGURETRUE) + + with open(filename, 'r') as file: + for line in file: + if ISCONFIGURETRUE in line or ISCONFIGUREFALSE in line: + is_configure = False + + # Remind the user if new content is added to the file + if len(valid_info) == 0 and is_configure: + print(f"File '{filename}' has not been configured yet," + "do you still need to configure it?" + "Enter 'yes' or 'no' to configure " + "the file: ", end='', flush=True) + rlist, _, _ = select.select([sys.stdin], [], [], SESSIONTIMEOUT) + + if rlist: + user_input = input().lower() + else: + user_input = "no" + + while True: + if user_input in ('y', 'yes'): + write_is_configure_true(filename, ISCONFIGURETRUE) + os_name = input("Please enter an operating system name:") + version = input("Please enter the version number:") + with open(filename, 'a') as file: + file.write(f"\n{os_name} {version}\n") + valid_info = parse_linux_osid(filename) + break + elif user_input in ('n', 'no'): + write_is_configure_true(filename, ISCONFIGUREFALSE) + break + else: + write_is_configure_true(filename, ISCONFIGUREFALSE) + break + return valid_info + + +def select_linux_distribution(valid_info): + # If there is no legal information, return None + if not valid_info: + return None + + # If there is only one line of legal information, return directly + if len(valid_info) == 1: + return valid_info[0] + def linux_distribution(distname='', version='', idNum='', supported_dists=_supported_dists, @@ -173,11 +317,39 @@ def linux_distribution(distname='', version='', idNum='', args given as parameters. """ + is_flag_osid = False try: etc = os.listdir('/etc') except os.error: # Probably not a Unix system return distname, version, idNum + + # Read system information from configuration file + # Call the function and pass in the filename + osid_path = os.path.realpath( + os.path.join(os.path.realpath(__file__), "../../../osid.in")) + + if os.path.exists(osid_path): + file_data = parse_linux_distributions(osid_path) + + # Output the parsed content + selected_data = select_linux_distribution(file_data) + + if selected_data: + is_flag_osid = True + + else: + print(f"The file '{osid_path}' does not exist.") + + if is_flag_osid: + if selected_data['os']: + distname = selected_data['os'] + if selected_data['version']: + version = selected_data['version'] + if selected_data['bit']: + idNum = selected_data['bit'] + return distname, version, idNum + sortEtc = sorted(etc) gFile = None for file in sortEtc: diff --git a/script/local/LocalCheckOS.py b/script/local/LocalCheckOS.py index 1fef3d3..6f39fd6 100644 --- a/script/local/LocalCheckOS.py +++ b/script/local/LocalCheckOS.py @@ -46,7 +46,8 @@ from domain_utils.domain_common.cluster_constants import ClusterConstants from os_platform.linux_distro import LinuxDistro from os_platform.common import SUPPORT_RHEL6X_VERSION_LIST, \ SUPPORT_RHEL7X_VERSION_LIST, SUPPORT_SUSE12X_VERSION_LIST, \ - SUPPORT_SUSE11X_VERSION_LIST, SUPPORT_RHEL8X_VERSION_LIST + SUPPORT_SUSE11X_VERSION_LIST, SUPPORT_RHEL8X_VERSION_LIST, \ + SUPPORT_RHEL_LEAST_VERSION sys.path.insert(0, localDirPath + "/../../lib") import psutil @@ -1786,6 +1787,10 @@ def CheckPlatformInfo(): mixed_type = "%s8" % data.distname platform_str = "%s_%s_%s" % (data.distname, data.version, data.bits) + elif int(data.version[0:3]) >= int(SUPPORT_RHEL_LEAST_VERSION[0]): + mixed_type = "%s" % data.distname + platform_str = "%s_%s_%s" % (data.distname, data.version, + data.bits) else: platform_str = "%s_%s_%s" % (data.distname, data.version, data.bits) @@ -1798,14 +1803,42 @@ def CheckPlatformInfo(): mixed_type = "%s" % data.distname platform_str = "%s_%s_%s" % (data.distname, data.version, data.bits) else: - platform_str = "%s_%s_%s" % (data.distname, data.version, data.bits) - g_logger.log("False unknown %s" % platform_str) + g_logger.log("Warning reason: %s version is not the official version" + "supported by OM, but you can still deploy and install it" % + platform_str) + if ask_to_continue(): + mixed_type = "%s" % data.distname + platform_str = "%s_%s_%s" % (data.distname, data.version, data.bits) + g_logger.log("True %s %s" % (mixed_type, platform_str)) + else: + g_logger.log("False unknown %s" % platform_str) return g_logger.log("True %s %s" % (mixed_type, platform_str)) return +############################################################################# +def ask_to_continue(): + """ + function : Check proceed with the installation + input : NA + output : NA + """ + while True: + response = input("Do you wish to proceed with the installation?" + "(yes/no): ").strip().lower() + if response == 'yes': + print("Executing the operation.") + return True + elif response == 'no': + print("Operation canceled.") + return False + else: + print("Invalid input. Please enter 'yes' or 'no'.") + + + ############################################################################# def CheckUname(): """ diff --git a/script/os_platform/common.py b/script/os_platform/common.py index fa4eaa8..933177c 100644 --- a/script/os_platform/common.py +++ b/script/os_platform/common.py @@ -40,6 +40,7 @@ SUPPORT_RHEL6X_VERSION_LIST = ["6.4", "6.5", "6.6", "6.7", "6.8", "6.9", "10"] SUPPORT_RHEL7X_VERSION_LIST = ["7.0", "7.1", "7.2", "7.3", "7.4", "7.5", "7.6", "7.7", "7.8", "7.9", "10"] SUPPORT_RHEL8X_VERSION_LIST = ["8.0", "8.1", "8.2", "8.3", "8.4", "8.5"] +SUPPORT_RHEL_LEAST_VERSION = ["6.4"] SUPPORT_RHEL_SERIES_VERSION_LIST = (SUPPORT_RHEL6X_VERSION_LIST + SUPPORT_RHEL7X_VERSION_LIST + SUPPORT_RHEL8X_VERSION_LIST) diff --git a/script/os_platform/linux_distro.py b/script/os_platform/linux_distro.py index c434796..559a9aa 100644 --- a/script/os_platform/linux_distro.py +++ b/script/os_platform/linux_distro.py @@ -21,9 +21,14 @@ import os import re +import select +import sys from os_platform.common import _supported_dists +ISCONFIGURETRUE = "# isConfigure = TRUE" +ISCONFIGUREFALSE = "# isConfigure = FALSE" +SESSIONTIMEOUT = 300 class LinuxDistro(object): """ @@ -64,6 +69,148 @@ class LinuxDistro(object): if len(line) > 1: id_num = line[1] return '', version, id_num + + @staticmethod + def parse_linux_osid(filename): + """ + Tries to determine the name of the Linux OS distribution name. + + The function first looks for a distribution release file in + /etc and then reverts to _dist_try_harder() in case no + suitable files are found. + + Returns a tuple (distname,version,id) which default to the + args given as parameters. + + """ + valid_info = [] + lines_to_choose = [] + with open(filename, 'r') as file: + for line in file: + line = line.strip() + # Ignore comment lines starting with # and empty lines + if line and not line.startswith('#'): + # Separate the release name, version with spaces + distro, version = line.split() + valid_info.append({ + 'os': distro, + 'version': version + }) + lines_to_choose.append(line.strip()) + if len(lines_to_choose) == 1 or len(lines_to_choose) == 0: + return valid_info + for i, line in enumerate(lines_to_choose, 1): + print(f"{i}. {line}") + + while True: + try: + choice = int(input("Please enter the serial number of the option:")) + if (1 <= choice and choice <= len(lines_to_choose)): + chosen_line = lines_to_choose[choice - 1] + valid_info = valid_info[choice - 1] + with open(filename, 'r+') as file: + lines = file.readlines() + file.seek(0) + file.truncate() + + for line in lines: + if line.strip().startswith('#') or line.strip() == '': + file.write(line) + continue + elif chosen_line in line: + file.write(line) + else: + file.write('#' + line) + LinuxDistro.write_is_configure_true(filename, ISCONFIGURETRUE) + break + else: + print("Invalid input: Please re-enter") + except ValueError: + print("Invalid input: Please re-enter") + return valid_info + + + @staticmethod + def write_is_configure_true(file_path, target_line): + # open the file and read all the lines + with open(file_path, 'r') as file: + lines = file.readlines() + + # Check if any row contains target content + has_target_line = any(target_line in line for line in lines) + + # If there is no target row, insert the target row before row 21 + if not has_target_line: + lines.insert(20, target_line + '\n') + + # Write the modified content back to the file + with open(file_path, 'w') as file: + file.writelines(lines) + + + @staticmethod + def parse_linux_distributions(filename): + """ + Tries to determine the name of the Linux OS distribution name. + + The function first looks for a distribution release file in + /etc and then reverts to _dist_try_harder() in case no + + Returns a tuple (distname,version,id) which default to the + args given as parameters. + + """ + is_configure = True + valid_info = LinuxDistro.parse_linux_osid(filename) + + if len(valid_info) == 1: + LinuxDistro.write_is_configure_true(filename, ISCONFIGURETRUE) + + with open(filename, 'r') as file: + for line in file: + if ISCONFIGURETRUE in line or ISCONFIGUREFALSE in line: + is_configure = False + + # Remind the user if new content is added to the file + if len(valid_info) == 0 and is_configure: + print(f"File '{filename}' has not been configured yet," + "do you still need to configure it?" + "Enter 'yes' or 'no' to configure " + "the file: ", end='', flush=True) + rlist, _, _ = select.select([sys.stdin], [], [], SESSIONTIMEOUT) + + if rlist: + user_input = input().lower() + else: + user_input = "no" + + while True: + if user_input in ('y', 'yes'): + LinuxDistro.write_is_configure_true(filename, ISCONFIGURETRUE) + os_name = input("Please enter an operating system name:") + version = input("Please enter the version number:") + with open(filename, 'a') as file: + file.write(f"\n{os_name} {version}\n") + valid_info = LinuxDistro.parse_linux_osid(filename) + break + elif user_input in ('n', 'no'): + LinuxDistro.write_is_configure_true(filename, ISCONFIGUREFALSE) + break + else: + LinuxDistro.write_is_configure_true(filename, ISCONFIGUREFALSE) + break + return valid_info + + + @staticmethod + def select_linux_distribution(valid_info): + # If there is no legal information, return None + if not valid_info: + return None + + # If there is only one line of legal information, return directly + if len(valid_info) == 1: + return valid_info[0] @staticmethod def linux_distribution(distname='', version='', idNum='', @@ -89,11 +236,41 @@ class LinuxDistro(object): args given as parameters. """ + is_flag_osid = False try: etc_dir = os.listdir('/etc') except os.error: # Probably not a Unix system return distname, version, idNum + + # Read system information from configuration file + # Call the function and pass in the filename + osid_path = os.path.realpath( + os.path.join(os.path.realpath(__file__), "../../osid.in")) + + if os.path.exists(osid_path): + file_data = LinuxDistro.parse_linux_distributions(osid_path) + + # Output the parsed content + selected_data = LinuxDistro.select_linux_distribution(file_data) + + if selected_data: + is_flag_osid = True + + else: + print(f"The file '{osid_path}' does not exist.") + + if is_flag_osid: + if selected_data['os']: + distname = selected_data['os'] + if selected_data['version']: + version = selected_data['version'] + if selected_data['bit']: + idNum = selected_data['bit'] + return distname, version, idNum + # else: + # g_logger.debug("Start to distributing the check context dump file") + etc_dir.sort() gFile = None _release_filename = re.compile(r'(\w+)[-_](release|version)') diff --git a/script/osid.conf b/script/osid.conf new file mode 100644 index 0000000..b7ce1e4 --- /dev/null +++ b/script/osid.conf @@ -0,0 +1,33 @@ +# ---------------now we support this platform--------------- + # RHEL/CentOS "6.4", "6.5", "6.6", "6.7", "6.8", "6.9", + # "7.0", "7.1", "7.2", "7.3", "7.4", "7.5 "64bit + # EulerOS "2.0", "2.3" 64bit + # SuSE11 sp1/2/3/4 64bit + # SuSE12 sp0/1/2/3 64bit + # Kylin "10" 64bit + # Ubuntu "18.04" 64bit + + +# -------------------why to configure------------------------------- +# OM工具对OS版本是强依赖,目前各大厂商基于openEuler、centos等开源 +# 操作系统做了相关适配,当利用OM对openGauss安装时,checkos等OS检查项 +# 会报错,所以我们设置一个osid.configure文件,在该文件中对openEuler、 +# centos做相关设置,可以使用户顺利做相关校验,安装成功。 + +# -------------------how to configure------------------------------- +# 如果我们在script/osid.configure文件中配置了OS、version等信息,则OM +# 管理工具在gs_preintsall和gs_install过程中,其中读取OS系统的项中,会 +# 首先从该配置文件中读取。 + +# 如果该文件用户没有配置,gs_preinstall预安装过程中,会提示用户该文件 +# 没有配置,是否配置,用户可以根据自己的选择在此时是否需要配置。如果配置: +# 只需要配置OS、version、bit这三个选项。并且将配置内容写入该文件中。 +# 如果用户在该文件中直接进行配置,在————define-platform————行下边也是配 +# 置OS、version、bit这三个选项,如第29行所示,并且将行首的#号取消。 + +# ---------------define-platform------------------------------ +# centos 7.5 +# openEuler 20.03 +# openEuler 22.03 +# redhat 6.4 +# RHEL 7.6 \ No newline at end of file