From bf6c3b589c70cef39c607d799dcb0206f009b788 Mon Sep 17 00:00:00 2001 From: cc <18856836718@163.com> Date: Tue, 5 Mar 2024 22:19:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=9C=AC=E6=9C=BA=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BiShengCompiler/check_install_result.sh | 6 +- .../GCCforOpenEuler/check_install_result.sh | 6 +- .../src/handler/connect_check.py | 26 +++++- .../src/handler/gather_package.py | 13 ++- .../src/handler/install_package.py | 3 +- .../src/machine/local_machine.py | 92 +++++++++++++++++++ 6 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 tools/install_dependency/src/machine/local_machine.py diff --git a/component/BiShengCompiler/check_install_result.sh b/component/BiShengCompiler/check_install_result.sh index 35c1d5b..3de3e88 100644 --- a/component/BiShengCompiler/check_install_result.sh +++ b/component/BiShengCompiler/check_install_result.sh @@ -4,5 +4,9 @@ clang_path=$(which clang) if [[ ${clang_path} == ${HOME}/.local/BiShengCompiler-3.2.0-aarch64-linux/bin/clang ]]; then echo "true" else - echo "false" + if [[ -f ${HOME}/.local/BiShengCompiler-3.2.0-aarch64-linux/bin/clang ]] && [[ $(grep -A1 '^export BISHENG_COMPILER_HOME=${HOME}/.local/BiShengCompiler-3.2.0-aarch64-linux/bin$' ${HOME}/.bashrc | grep '^export PATH=${BISHENG_COMPILER_HOME}:${PATH}$' | wc -l) == "1" ]]; then + echo "true" + else + echo "false" + fi fi diff --git a/component/GCCforOpenEuler/check_install_result.sh b/component/GCCforOpenEuler/check_install_result.sh index a142ad3..9250da8 100644 --- a/component/GCCforOpenEuler/check_install_result.sh +++ b/component/GCCforOpenEuler/check_install_result.sh @@ -4,5 +4,9 @@ gcc_path=$(which gcc) if [[ ${gcc_path} == ${HOME}/.local/gcc-10.3.1-2023.12-aarch64-linux/bin/gcc ]]; then echo "true" else - echo "false" + if [[ -f ${HOME}/.local/gcc-10.3.1-2023.12-aarch64-linux/bin/gcc ]] && [[ $(grep -A1 '^export GCC_HOME=${HOME}/.local/gcc-10.3.1-2023.12-aarch64-linux/bin$' ${HOME}/.bashrc | grep '^export PATH=${GCC_HOME}:${PATH}$' | wc -l) == "1" ]]; then + echo "true" + else + echo "false" + fi fi diff --git a/tools/install_dependency/src/handler/connect_check.py b/tools/install_dependency/src/handler/connect_check.py index ed691c6..eb321f8 100644 --- a/tools/install_dependency/src/handler/connect_check.py +++ b/tools/install_dependency/src/handler/connect_check.py @@ -1,9 +1,12 @@ import logging +import socket + import constant from handler.handler_and_node import Handler from machine.scanner_machine import ScannerMachine from machine.builder_machine import BuilderMachine from machine.executor_machine import ExecutorMachine +from machine.local_machine import LocalMachine from exception.connect_exception import ConnectException LOGGER = logging.getLogger("install_dependency") @@ -18,17 +21,24 @@ class ConnectCheck(Handler): def handle(self, data) -> bool: LOGGER.debug("ConnectCheck start!") + local_ip = ConnectCheck.get_local_ip() + ret = True for role in ConnectCheck.klass_dict: - ret = ret and ConnectCheck.machine_role_check(data, role) + ret = ret and ConnectCheck.machine_role_check(data, role, local_ip) return ret - + @staticmethod - def machine_role_check(data, role): + def machine_role_check(data, role, local_ip): builder_list = data.get(role) klass = ConnectCheck.klass_dict.get(role) data[role + constant.MACHINE] = dict() for ip in builder_list: + if ip == local_ip or ip == "127.0.0.1": + ip = "127.0.0.1" + machine_instance = LocalMachine(ip) + data[role + constant.MACHINE][ip] = machine_instance + continue try: machine_instance = klass(ip, data[constant.USER], data[constant.PKEY], data.get(constant.PASSWORD, None)) @@ -42,3 +52,13 @@ class ConnectCheck(Handler): del data[role + constant.MACHINE] return False return True + + @staticmethod + def get_local_ip(): + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + sock.connect(("8.8.8.8", 80)) + ip = sock.getsockname()[0] + finally: + sock.close() + return ip if ip else "127.0.0.1" diff --git a/tools/install_dependency/src/handler/gather_package.py b/tools/install_dependency/src/handler/gather_package.py index 6e616a3..90f812f 100644 --- a/tools/install_dependency/src/handler/gather_package.py +++ b/tools/install_dependency/src/handler/gather_package.py @@ -14,11 +14,11 @@ class GatherPackage(Handler): if GatherPackage.check_default_path_available(): LOGGER.info("Dependencies ready.") return True - + if os.path.isfile(constant.DEPENDENCY_DIR): LOGGER.error(f"The file {constant.DEPENDENCY_DIR} exists. Please rename or remove this file.") return False - + try: ret = download_dependence() except Exception as e: @@ -29,16 +29,15 @@ class GatherPackage(Handler): return False LOGGER.info("Download dependencies success.") return True - + @staticmethod def check_default_path_available(): if os.path.exists(constant.DEPENDENCY_FILE): try: print(f"Now extract files from {constant.DEPENDENCY_FILE}:") - result = subprocess.run(f"tar -zxvf {constant.DEPENDENCY_FILE}".split(' '), - capture_output=False, shell=False, stderr=subprocess.STDOUT) - print(f"{result.stdout}" if result.stdout else "") - except (FileExistsError, ) as e: + subprocess.run(f"tar -zxvf {constant.DEPENDENCY_FILE}".split(' '), + capture_output=False, shell=False, stderr=subprocess.STDOUT) + except (FileExistsError,) as e: LOGGER.warning(f"{constant.DEPENDENCY_FILE} may already extracted.") except Exception as e: LOGGER.error(f"Extract {constant.DEPENDENCY_FILE} failed. {str(e)}") diff --git a/tools/install_dependency/src/handler/install_package.py b/tools/install_dependency/src/handler/install_package.py index 1444f73..fc88628 100644 --- a/tools/install_dependency/src/handler/install_package.py +++ b/tools/install_dependency/src/handler/install_package.py @@ -3,7 +3,6 @@ import multiprocessing import constant from handler.handler_and_node import Handler -from machine.machine import Machine from machine.scanner_machine import ScannerMachine from machine.builder_machine import BuilderMachine from machine.executor_machine import ExecutorMachine @@ -49,7 +48,7 @@ class InstallPackage(Handler): return True -def process_work(machine: Machine, *components: str): +def process_work(machine, *components: str): try: for component in components: machine.install_component(component) diff --git a/tools/install_dependency/src/machine/local_machine.py b/tools/install_dependency/src/machine/local_machine.py new file mode 100644 index 0000000..86a572f --- /dev/null +++ b/tools/install_dependency/src/machine/local_machine.py @@ -0,0 +1,92 @@ +import os +import logging +import subprocess + +import constant +from exception.connect_exception import NotMatchedMachineTypeException +from download import component_collection_map +from utils import base_path + +LOGGER = logging.getLogger("install_dependency") + + +class LocalMachine: + def __init__(self, ip): + self.ip = ip + self.check_is_aarch64() + + def check_is_aarch64(self): + machine_type = os.uname().machine.lower() + LOGGER.info(f"{self.ip} machine type: {machine_type}") + if machine_type != "aarch64": + LOGGER.error(f"Machine type of {self.ip} is {machine_type}, not aarch64. Please replace this machine.") + raise NotMatchedMachineTypeException() + + def install_component(self, component_name): + try: + self.install_component_handler(component_name) + except (FileNotFoundError, PermissionError, NotADirectoryError, OSError, IOError) as e: + LOGGER.error(f"Local machine {self.ip} occur Error: {str(e)}") + + def install_component_handler(self, component_name): + result = subprocess.run(f"mkdir -p /tmp/{constant.DEPENDENCY_DIR}".split(' '), + capture_output=False, shell=False, stderr=subprocess.STDOUT) + if result.returncode == 0: + LOGGER.debug(f"Local machine {self.ip} mkdir -p /tmp/{constant.DEPENDENCY_DIR} result: success") + else: + LOGGER.error(f"Local machine {self.ip} mkdir -p /tmp/{constant.DEPENDENCY_DIR} result: failed") + raise NotADirectoryError(f"Local machine {self.ip} " + f"directory {os.path.join('/tmp/', constant.DEPENDENCY_DIR)} not exist.") + + # 上传 组件压缩包和校验文件 + LOGGER.info(f"Install component in local machine {self.ip}: {component_name}") + remote_file_list = [] + shell_dict = component_collection_map.get(component_name) + for shell_cmd in shell_dict: + url_and_save_path = shell_dict.get(shell_cmd) + component = url_and_save_path.get("save_path") + LOGGER.debug(f"Copy component file to local machine {self.ip}: {component}") + remote_file = os.path.abspath(os.path.join('/tmp', component)) + remote_file_list.append(remote_file) + subprocess.run(f"/bin/cp -rf {component} {remote_file}".split(' '), + capture_output=False, shell=False, stderr=subprocess.STDOUT) + + # 上传并执行 安装脚本, 校验安装结果脚本 + shell_file_list = ["install.sh", "check_install_result.sh"] + install_result = "" + for shell_file in shell_file_list: + execute_output, sh_file_remote_path = ( + self.transport_shell_file_and_execute(component_name, shell_file)) + remote_file_list.append(sh_file_remote_path) + if shell_file == shell_file_list[1]: + install_result = execute_output + + if install_result == "true": + LOGGER.info(f"Local machine {self.ip} install {component_name} success.") + else: + LOGGER.error(f"Local machine {self.ip} install {component_name} failed.") + # 清理tmp临时文件 + self.clear_tmp_file_at_local_machine(remote_file_list) + + def transport_shell_file_and_execute(self, component_name, shell_file): + sh_file_local_path = os.path.join(base_path("component"), component_name, shell_file) + if not os.path.exists(sh_file_local_path): + LOGGER.error(f"{sh_file_local_path} not exists.") + raise FileNotFoundError(f"local file {sh_file_local_path} not exists.") + sh_file_remote_path = os.path.join("/tmp/", constant.DEPENDENCY_DIR, component_name + shell_file) + LOGGER.debug(f"Copy local_file: {sh_file_local_path} to local machine {self.ip} " + f"remote_file: {sh_file_remote_path}") + subprocess.run(f"/bin/cp -rf {sh_file_local_path} {sh_file_remote_path}".split(' '), + capture_output=False, shell=False, stderr=subprocess.STDOUT) + result = subprocess.run(f"bash {sh_file_remote_path}".split(' '), + capture_output=True, shell=False) + output = result.stdout.decode().strip() + LOGGER.info(f"Local machine {self.ip} bash {component_name}{shell_file} file output: {output}") + return output, sh_file_remote_path + + def clear_tmp_file_at_local_machine(self, remote_file_list): + LOGGER.debug(f"Clear tmp file at local machine {self.ip}") + for remote_file in remote_file_list: + LOGGER.debug(f"Delete tmp file at local machine {self.ip}: {remote_file}") + subprocess.run(f"rm -f {remote_file}".split(' '), + capture_output=False, shell=False, stderr=subprocess.STDOUT) -- Gitee