From 793b63697b77a8ffbf1ea88465c156267fff5417 Mon Sep 17 00:00:00 2001 From: Tuan Nie Date: Wed, 31 Jul 2024 17:24:33 +0800 Subject: [PATCH 1/7] Add functions in `deploy_helper.py` In script/deploy_helper.py, add two functions named command_checker and setup_logger. And substitute some code with command_checker.Below are their functions: - Add command_checker function to check if a command execute successfully. If executing failed, command_checker will output error log and status code. If executing successfully, nothing will happen. - Add setup_logger function to configure the logging system. It will create a logger object and set the log level to INFO. Also, user can set the log level using --log-level option. - Replace some code like 'res=...if res!=0' with command_checker. See: #17 --- script/deploy_helper.py | 69 +++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/script/deploy_helper.py b/script/deploy_helper.py index 3a697d8..f0ab6a4 100644 --- a/script/deploy_helper.py +++ b/script/deploy_helper.py @@ -10,6 +10,31 @@ from types import SimpleNamespace import os import subprocess +import logging + + +def setup_logger(log_level=logging.INFO): + """ + 配置全局日志系统。 + + :param log_level: 设置日志记录级别,默认为 INFO 级别。 + """ + logging.basicConfig(level=log_level, + format='%(asctime)s - %(levelname)s - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S') + + +def command_checker(status_code: int, message: str, expected_code: int = 0): + """ + 检查命令执行的状态码是否符合预期。 + + :param status_code: 实际命令执行的状态码 + :param message: 要记录的日志信息 + :param expected_code: 预期的状态码,默认为0 + """ + if status_code != expected_code: + logging.error(f"Command failed: {message} Expected status code {expected_code}, got status code {status_code}.") + exit(status_code) # open config_path and default_config_path @@ -65,8 +90,7 @@ def create_systemd_service(config): """ res = os.system( f'echo "{gcs_file_content}" | sudo tee {service_full_path}') - if res != 0: - exit(res) + command_checker(res, f"echo \"{gcs_file_content}\" | sudo tee {service_full_path} failed", 0) # TODO: add checker to check @@ -101,44 +125,51 @@ def deploy_on_ubuntu(config): if not os.path.exists(os.path.dirname(config.serviceStartJarFile)): os.system(f'sudo mkdir -p {os.path.dirname(config.serviceStartJarFile)}') res = os.system(f'sudo cp {package_path} {config.serviceStartJarFile}') - if res != 0: - return res + command_checker(res, f"sudo cp {package_path} failed", 0) create_systemd_service(config) if config.serviceEnable: res = os.system(f'sudo systemctl enable {config.serviceName}') - if res != 0: - return res + command_checker(res, f"sudo systemctl enable {config.serviceName} failed", 0) else: res = os.system(f'sudo systemctl disable {config.serviceName}') - if res != 0: - return res + command_checker(res, f"sudo systemctl disable {config.serviceName} failed", 0) res = os.system(f'sudo systemctl start {config.serviceName}') - if res != 0: - return res + command_checker(res, f"sudo systemctl start {config.serviceName} failed", 0) # TODO: finish deploy on docker # TODO: add checker to check def clean(config): - os.system(f'sudo systemctl disable {config.serviceName}') - os.system(f'sudo systemctl stop {config.serviceName}') + res = os.system(f'sudo systemctl disable {config.serviceName}') + command_checker(res, f"sudo systemctl disable {config.serviceName} failed", 0) + res = os.system(f'sudo systemctl stop {config.serviceName}') + command_checker(res, f"sudo systemctl stop {config.serviceName} failed", 0) if os.path.exists(f'/etc/systemd/system/{config.serviceName}.{config.serviceSuffix}'): - os.system( + res = os.system( f'sudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} && ' f'sudo systemctl daemon-reload') - os.system(f'sudo systemctl reset-failed {config.serviceName}') + command_checker(res, f"sudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} && \ + sudo systemctl daemon-reload failed", 0) + res = os.system(f'sudo systemctl reset-failed {config.serviceName}') + command_checker(res, f"sudo systemctl reset-failed {config.serviceName} failed", 0) if os.path.exists(f'{config.serviceWorkingDirectory}'): - os.system(f'sudo rm -rf {config.serviceWorkingDirectory}') + res = os.system(f'sudo rm -rf {config.serviceWorkingDirectory}') + command_checker(res, f"sudo rm -rf {config.serviceWorkingDirectory} failed", 0) if os.path.exists(f'{config.serviceStartJarFile}'): - os.system(f'sudo rm -rf {config.serviceStartJarFile}') + res = os.system(f'sudo rm -rf {config.serviceStartJarFile}') + command_checker(res, f"sudo rm -rf {config.serviceStartJarFile} failed", 0) if os.path.exists(f'{config.servicePIDFile}'): - os.system(f'sudo rm -rf {config.servicePIDFile}') + res = os.system(f'sudo rm -rf {config.servicePIDFile}') + command_checker(res, f"sudo rm -rf {config.servicePIDFile} failed", 0) if os.system(f"cat /etc/passwd | grep -w -E '^{config.serviceUser}'") == 0: - os.system(f'sudo userdel {config.serviceUser}') - os.system(f'mvn clean') + res = os.system(f'sudo userdel {config.serviceUser}') + command_checker(res, f"sudo userdel {config.serviceUser} failed", 0) + res = os.system(f'mvn clean') + command_checker(res, f"mvn clean failed", 0) if __name__ == "__main__": + setup_logger() parser = argparse.ArgumentParser( description="Deploy the project when the environment is ready.") parser.add_argument('--config-path', nargs='?', default='../config.json', From dd4dbf534e1a2a64624a77498b2e67bafcb1e63c Mon Sep 17 00:00:00 2001 From: Tuan Nie Date: Wed, 31 Jul 2024 20:35:56 +0800 Subject: [PATCH 2/7] Modify comments in `deploy_helper.py` Translate original Chinese comments in command_checker and setup_logger in script/deploy_helper.py into English. See: #17 --- script/deploy_helper.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/script/deploy_helper.py b/script/deploy_helper.py index f0ab6a4..92abe5f 100644 --- a/script/deploy_helper.py +++ b/script/deploy_helper.py @@ -15,9 +15,9 @@ def setup_logger(log_level=logging.INFO): """ - 配置全局日志系统。 + Configure the global logging system. - :param log_level: 设置日志记录级别,默认为 INFO 级别。 + :param log_level: Set the logging level, defaulting to INFO. """ logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s - %(message)s', @@ -26,11 +26,11 @@ def setup_logger(log_level=logging.INFO): def command_checker(status_code: int, message: str, expected_code: int = 0): """ - 检查命令执行的状态码是否符合预期。 + Check if the command execution status code meets the expected value. - :param status_code: 实际命令执行的状态码 - :param message: 要记录的日志信息 - :param expected_code: 预期的状态码,默认为0 + :param status_code: The actual status code of the command execution. + :param message: The log message to be recorded. + :param expected_code: The expected status code, defaulting to 0. """ if status_code != expected_code: logging.error(f"Command failed: {message} Expected status code {expected_code}, got status code {status_code}.") From 4ca40bcfa4ad72bd897e8e3dca1b2bb6f2f2ca76 Mon Sep 17 00:00:00 2001 From: Tuan Nie Date: Thu, 1 Aug 2024 19:53:12 +0800 Subject: [PATCH 3/7] Modify `command_checker` and `setup_logger` - Modify the format of the log message in command_checker to improve readability. - Delete parameter 0 in each call of command_checker when the parameter expected status code is 0. - Add support for setting log level of setup_logger when executing deploy_helper.py in shell. See: #17 --- script/deploy_helper.py | 47 ++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/script/deploy_helper.py b/script/deploy_helper.py index 92abe5f..40fd526 100644 --- a/script/deploy_helper.py +++ b/script/deploy_helper.py @@ -33,7 +33,7 @@ def command_checker(status_code: int, message: str, expected_code: int = 0): :param expected_code: The expected status code, defaulting to 0. """ if status_code != expected_code: - logging.error(f"Command failed: {message} Expected status code {expected_code}, got status code {status_code}.") + logging.error(f"The command below failed:\n\t {message} \nExpected status code {expected_code}, got status code {status_code}.") exit(status_code) @@ -90,7 +90,7 @@ def create_systemd_service(config): """ res = os.system( f'echo "{gcs_file_content}" | sudo tee {service_full_path}') - command_checker(res, f"echo \"{gcs_file_content}\" | sudo tee {service_full_path} failed", 0) + command_checker(res, f"echo \"{gcs_file_content}\" | sudo tee {service_full_path}") # TODO: add checker to check @@ -125,51 +125,50 @@ def deploy_on_ubuntu(config): if not os.path.exists(os.path.dirname(config.serviceStartJarFile)): os.system(f'sudo mkdir -p {os.path.dirname(config.serviceStartJarFile)}') res = os.system(f'sudo cp {package_path} {config.serviceStartJarFile}') - command_checker(res, f"sudo cp {package_path} failed", 0) + command_checker(res, f"sudo cp {package_path}") create_systemd_service(config) if config.serviceEnable: res = os.system(f'sudo systemctl enable {config.serviceName}') - command_checker(res, f"sudo systemctl enable {config.serviceName} failed", 0) + command_checker(res, f"sudo systemctl enable {config.serviceName}") else: res = os.system(f'sudo systemctl disable {config.serviceName}') - command_checker(res, f"sudo systemctl disable {config.serviceName} failed", 0) + command_checker(res, f"sudo systemctl disable {config.serviceName}") res = os.system(f'sudo systemctl start {config.serviceName}') - command_checker(res, f"sudo systemctl start {config.serviceName} failed", 0) + command_checker(res, f"sudo systemctl start {config.serviceName}") # TODO: finish deploy on docker # TODO: add checker to check def clean(config): res = os.system(f'sudo systemctl disable {config.serviceName}') - command_checker(res, f"sudo systemctl disable {config.serviceName} failed", 0) + command_checker(res, f"sudo systemctl disable {config.serviceName}") res = os.system(f'sudo systemctl stop {config.serviceName}') - command_checker(res, f"sudo systemctl stop {config.serviceName} failed", 0) + command_checker(res, f"sudo systemctl stop {config.serviceName}") if os.path.exists(f'/etc/systemd/system/{config.serviceName}.{config.serviceSuffix}'): res = os.system( f'sudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} && ' f'sudo systemctl daemon-reload') - command_checker(res, f"sudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} && \ - sudo systemctl daemon-reload failed", 0) + command_checker(res, f"sudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} &&\n" + f"\tsudo systemctl daemon-reload") res = os.system(f'sudo systemctl reset-failed {config.serviceName}') - command_checker(res, f"sudo systemctl reset-failed {config.serviceName} failed", 0) + command_checker(res, f"sudo systemctl reset-failed {config.serviceName}") if os.path.exists(f'{config.serviceWorkingDirectory}'): res = os.system(f'sudo rm -rf {config.serviceWorkingDirectory}') - command_checker(res, f"sudo rm -rf {config.serviceWorkingDirectory} failed", 0) + command_checker(res, f"sudo rm -rf {config.serviceWorkingDirectory}") if os.path.exists(f'{config.serviceStartJarFile}'): res = os.system(f'sudo rm -rf {config.serviceStartJarFile}') - command_checker(res, f"sudo rm -rf {config.serviceStartJarFile} failed", 0) + command_checker(res, f"sudo rm -rf {config.serviceStartJarFile}") if os.path.exists(f'{config.servicePIDFile}'): res = os.system(f'sudo rm -rf {config.servicePIDFile}') - command_checker(res, f"sudo rm -rf {config.servicePIDFile} failed", 0) + command_checker(res, f"sudo rm -rf {config.servicePIDFile}") if os.system(f"cat /etc/passwd | grep -w -E '^{config.serviceUser}'") == 0: res = os.system(f'sudo userdel {config.serviceUser}') - command_checker(res, f"sudo userdel {config.serviceUser} failed", 0) + command_checker(res, f"sudo userdel {config.serviceUser}") res = os.system(f'mvn clean') - command_checker(res, f"mvn clean failed", 0) + command_checker(res, f"mvn clean") if __name__ == "__main__": - setup_logger() parser = argparse.ArgumentParser( description="Deploy the project when the environment is ready.") parser.add_argument('--config-path', nargs='?', default='../config.json', @@ -179,7 +178,21 @@ def clean(config): parser.add_argument('--default-config-path', nargs='?', default='../config_default.json', type=str, help="Linux distribution") parser.add_argument('--clean', action='store_true', help="Clean up the project") + parser.add_argument('--log-level', nargs='?', default='INFO', + type=str, help=( + "Set the logging level. Possible values are: " + "'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'. " + "Default is 'INFO'.\n" + "- DEBUG: Detailed information, typically of interest only when diagnosing problems.\n" + "- INFO: Confirmation that things are working as expected.\n" + "- WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g., 'disk space low'). The software is still working as expected.\n" + "- ERROR: Due to a more serious problem, the software has not been able to perform some function.\n" + "- CRITICAL: A very serious error, indicating that the program itself may be unable to continue running." + )) args = parser.parse_args() + if args.log_level.upper() not in logging._nameToLevel: + raise ValueError(f"Invalid log level: {args.log_level}") + setup_logger(getattr(logging, args.log_level.upper())) if args.clean: clean(load_config_file_as_obj(args.config_path, args.default_config_path)) elif args.distro == 'ubuntu': From a6b8fc06ad060f508d40e82e6aa91e723cfc5956 Mon Sep 17 00:00:00 2001 From: Tuan Nie Date: Thu, 1 Aug 2024 20:20:45 +0800 Subject: [PATCH 4/7] Add developer information - Add information of developer ajiankexx to pom.xml See: #12 --- pom.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pom.xml b/pom.xml index c296dd5..1c46c8f 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,17 @@ https://github.com/CMIPT 8 + + ajiankexx + ajianke2@gmail.com + https://github.com/ajiankexx + + Developer + + CMIPT + https://github.com/CMIPT + 8 + From 27b7cd4132c52481d451fa7c928071c08a116d5c Mon Sep 17 00:00:00 2001 From: ajianke Date: Fri, 2 Aug 2024 20:43:56 +0800 Subject: [PATCH 5/7] Modify how command_checker outputs log - Add the current file name in the output log. - Modify the behavior of command_checker. Now it will only output the content in parameter of message. - Add command_checker for each call of os.system(). - Use """ """ instead of " " to output log. See: #17 --- script/deploy_helper.py | 96 ++++++++++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 21 deletions(-) diff --git a/script/deploy_helper.py b/script/deploy_helper.py index 40fd526..f13ddd5 100644 --- a/script/deploy_helper.py +++ b/script/deploy_helper.py @@ -20,7 +20,7 @@ def setup_logger(log_level=logging.INFO): :param log_level: Set the logging level, defaulting to INFO. """ logging.basicConfig(level=log_level, - format='%(asctime)s - %(levelname)s - %(message)s', + format='%(asctime)s -%(levelname)s- in %(filename)s:%(message)s', datefmt='%Y-%m-%d %H:%M:%S') @@ -33,7 +33,7 @@ def command_checker(status_code: int, message: str, expected_code: int = 0): :param expected_code: The expected status code, defaulting to 0. """ if status_code != expected_code: - logging.error(f"The command below failed:\n\t {message} \nExpected status code {expected_code}, got status code {status_code}.") + logging.error(message) exit(status_code) @@ -90,7 +90,10 @@ def create_systemd_service(config): """ res = os.system( f'echo "{gcs_file_content}" | sudo tee {service_full_path}') - command_checker(res, f"echo \"{gcs_file_content}\" | sudo tee {service_full_path}") + command_checker(res, f""" +The command below failed: +\techo \"{gcs_file_content}\" | sudo tee {service_full_path} +Expected status code 0, got status code {res}.""") # TODO: add checker to check @@ -111,9 +114,17 @@ def deploy_on_ubuntu(config): if config.deploy: if os.system(f"cat /etc/passwd | grep -w -E '^{config.serviceUser}'") != 0: - os.system(f'sudo useradd {config.serviceUser}') + res = os.system(f'sudo useradd {config.serviceUser}') + command_checker(res, f""" +The command below failed: +\tsudo useradd {config.serviceUser} +Expected status code 0, got status code {res}.""") if config.serviceUserPassword == None or config.serviceUserPassword == "": - os.system(f'sudo passwd -d {config.serviceUser}') + res = os.system(f'sudo passwd -d {config.serviceUser}') + command_checker(res, f""" +The command below failed: +\tsudo passwd -d {config.serviceUser} +Expected status code 0, got status code {res}.""") else: process = subprocess.Popen(['sudo', 'chpasswd'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) @@ -123,49 +134,92 @@ def deploy_on_ubuntu(config): process.communicate() if not os.path.exists(os.path.dirname(config.serviceStartJarFile)): - os.system(f'sudo mkdir -p {os.path.dirname(config.serviceStartJarFile)}') + res = os.system(f'sudo mkdir -p {os.path.dirname(config.serviceStartJarFile)}') + command_checker(res, f""" +The command below failed: +\tsudo mkdir -p {os.path.dirname(config.serviceStartJarFile)} +Expected status code 0, got status code {res}.""") res = os.system(f'sudo cp {package_path} {config.serviceStartJarFile}') - command_checker(res, f"sudo cp {package_path}") + command_checker(res, f""" +The command below failed: +\tsudo cp {package_path} {config.serviceStartJarFile} +Expected status code 0, got status code {res}.""") create_systemd_service(config) if config.serviceEnable: res = os.system(f'sudo systemctl enable {config.serviceName}') - command_checker(res, f"sudo systemctl enable {config.serviceName}") + command_checker(res, f""" +The command below failed: +\tsudo systemctl enable {config.serviceName} +Expected status code 0, got status code {res}.""") else: res = os.system(f'sudo systemctl disable {config.serviceName}') - command_checker(res, f"sudo systemctl disable {config.serviceName}") + command_checker(res, f""" +The command below failed: +\tsudo systemctl disable {config.serviceName} +Expected status code 0, got status code {res}.""") res = os.system(f'sudo systemctl start {config.serviceName}') - command_checker(res, f"sudo systemctl start {config.serviceName}") + command_checker(res, f""" +The command below failed: +\tsudo systemctl start {config.serviceName} +Expected status code 0, got status code {res}.""") # TODO: finish deploy on docker # TODO: add checker to check def clean(config): res = os.system(f'sudo systemctl disable {config.serviceName}') - command_checker(res, f"sudo systemctl disable {config.serviceName}") + command_checker(res, f""" +The command below failed: +\tsudo systemctl disable {config.serviceName} +Expected status code 0, got status code {res}.""") res = os.system(f'sudo systemctl stop {config.serviceName}') - command_checker(res, f"sudo systemctl stop {config.serviceName}") + command_checker(res, f""" +The command below failed: +\tsudo systemctl stop {config.serviceName} +Expected status code 0, got status code {res}.""") if os.path.exists(f'/etc/systemd/system/{config.serviceName}.{config.serviceSuffix}'): res = os.system( f'sudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} && ' f'sudo systemctl daemon-reload') - command_checker(res, f"sudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} &&\n" - f"\tsudo systemctl daemon-reload") + command_checker(res, f""" +The command below failed: +\tsudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} && \ +\tsudo systemctl daemon-reload +Expected status code 0, got status code {res}.""") res = os.system(f'sudo systemctl reset-failed {config.serviceName}') - command_checker(res, f"sudo systemctl reset-failed {config.serviceName}") + command_checker(res, f""" +The command below failed: +\tsudo systemctl reset-failed {config.serviceName} +Expected status code 0, got status code {res}.""") if os.path.exists(f'{config.serviceWorkingDirectory}'): res = os.system(f'sudo rm -rf {config.serviceWorkingDirectory}') - command_checker(res, f"sudo rm -rf {config.serviceWorkingDirectory}") + command_checker(res, f""" +The command below failed: +\tsudo rm -rf {config.serviceWorkingDirectory} +Expected status code 0, got status code {res}.""") if os.path.exists(f'{config.serviceStartJarFile}'): res = os.system(f'sudo rm -rf {config.serviceStartJarFile}') - command_checker(res, f"sudo rm -rf {config.serviceStartJarFile}") + command_checker(res, f""" +The command below failed: +\tsudo rm -rf {config.serviceStartJarFile} +Expected status code 0, got status code {res}.""") if os.path.exists(f'{config.servicePIDFile}'): res = os.system(f'sudo rm -rf {config.servicePIDFile}') - command_checker(res, f"sudo rm -rf {config.servicePIDFile}") + command_checker(res, f""" +The command below failed: +\tsudo rm -rf {config.servicePIDFile} +Expected status code 0, got status code {res}.""") if os.system(f"cat /etc/passwd | grep -w -E '^{config.serviceUser}'") == 0: res = os.system(f'sudo userdel {config.serviceUser}') - command_checker(res, f"sudo userdel {config.serviceUser}") + command_checker(res, f""" +The command below failed: +\tsudo userdel {config.serviceUser} +Expected status code 0, got status code {res}.""") res = os.system(f'mvn clean') - command_checker(res, f"mvn clean") + command_checker(res, f""" +The command below failed: +\tmvn clean +Expected status code 0, got status code {res}.""") if __name__ == "__main__": @@ -185,7 +239,7 @@ def clean(config): "Default is 'INFO'.\n" "- DEBUG: Detailed information, typically of interest only when diagnosing problems.\n" "- INFO: Confirmation that things are working as expected.\n" - "- WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g., 'disk space low'). The software is still working as expected.\n" + "- WARNING: An indication that something unexpected happened, or indicative of some problem in the near future. The software is still working as expected.\n" "- ERROR: Due to a more serious problem, the software has not been able to perform some function.\n" "- CRITICAL: A very serious error, indicating that the program itself may be unable to continue running." )) From ce1b5866c98edd87a392dc421927f1d039a1d43f Mon Sep 17 00:00:00 2001 From: ajianke Date: Thu, 8 Aug 2024 09:57:30 +0800 Subject: [PATCH 6/7] Show line number and improve readability of code - Add support for showing the line number of the command_checker to help the user to find which command failed quickly. - Modify the format how the log is printed. - Add two vars message_tmp and command to store the template of the message and the command to be executed respectively. It will make codes more readable and easier to understand. Resolves: #17 --- script/deploy_helper.py | 176 +++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 94 deletions(-) diff --git a/script/deploy_helper.py b/script/deploy_helper.py index f13ddd5..9f6fea8 100644 --- a/script/deploy_helper.py +++ b/script/deploy_helper.py @@ -11,6 +11,7 @@ import os import subprocess import logging +import inspect def setup_logger(log_level=logging.INFO): @@ -20,7 +21,7 @@ def setup_logger(log_level=logging.INFO): :param log_level: Set the logging level, defaulting to INFO. """ logging.basicConfig(level=log_level, - format='%(asctime)s -%(levelname)s- in %(filename)s:%(message)s', + format='%(asctime)s -%(levelname)s- in %(filename)s:%(caller_lineno)d %(message)s', datefmt='%Y-%m-%d %H:%M:%S') @@ -33,7 +34,8 @@ def command_checker(status_code: int, message: str, expected_code: int = 0): :param expected_code: The expected status code, defaulting to 0. """ if status_code != expected_code: - logging.error(message) + caller_frame = inspect.currentframe().f_back + logging.error(message, extra={'caller_lineno': caller_frame.f_lineno}) exit(status_code) @@ -88,12 +90,10 @@ def create_systemd_service(config): [Install] WantedBy={wanted_by} """ - res = os.system( - f'echo "{gcs_file_content}" | sudo tee {service_full_path}') - command_checker(res, f""" -The command below failed: -\techo \"{gcs_file_content}\" | sudo tee {service_full_path} -Expected status code 0, got status code {res}.""") + command = f'echo "{gcs_file_content}" | sudo tee {service_full_path}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) # TODO: add checker to check @@ -108,23 +108,22 @@ def deploy_on_ubuntu(config): if res.returncode != 0: return res.returncode package_path = res.stdout.strip() - res = os.system(f'mvn package {skip_test}') - if res != 0: - return res + command = f'mvn package {skip_test}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) if config.deploy: if os.system(f"cat /etc/passwd | grep -w -E '^{config.serviceUser}'") != 0: - res = os.system(f'sudo useradd {config.serviceUser}') - command_checker(res, f""" -The command below failed: -\tsudo useradd {config.serviceUser} -Expected status code 0, got status code {res}.""") + command = f'sudo useradd {config.serviceUser}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) if config.serviceUserPassword == None or config.serviceUserPassword == "": - res = os.system(f'sudo passwd -d {config.serviceUser}') - command_checker(res, f""" -The command below failed: -\tsudo passwd -d {config.serviceUser} -Expected status code 0, got status code {res}.""") + command =f'sudo passwd -d {config.serviceUser}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) else: process = subprocess.Popen(['sudo', 'chpasswd'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) @@ -134,95 +133,84 @@ def deploy_on_ubuntu(config): process.communicate() if not os.path.exists(os.path.dirname(config.serviceStartJarFile)): - res = os.system(f'sudo mkdir -p {os.path.dirname(config.serviceStartJarFile)}') - command_checker(res, f""" -The command below failed: -\tsudo mkdir -p {os.path.dirname(config.serviceStartJarFile)} -Expected status code 0, got status code {res}.""") - res = os.system(f'sudo cp {package_path} {config.serviceStartJarFile}') - command_checker(res, f""" -The command below failed: -\tsudo cp {package_path} {config.serviceStartJarFile} -Expected status code 0, got status code {res}.""") + command = f'sudo mkdir -p {os.path.dirname(config.serviceStartJarFile)}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) + command = f'sudo cp {package_path} {config.serviceStartJarFile}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) create_systemd_service(config) if config.serviceEnable: - res = os.system(f'sudo systemctl enable {config.serviceName}') - command_checker(res, f""" -The command below failed: -\tsudo systemctl enable {config.serviceName} -Expected status code 0, got status code {res}.""") + command = f'sudo systemctl enable {config.serviceName}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) else: - res = os.system(f'sudo systemctl disable {config.serviceName}') - command_checker(res, f""" -The command below failed: -\tsudo systemctl disable {config.serviceName} -Expected status code 0, got status code {res}.""") - res = os.system(f'sudo systemctl start {config.serviceName}') - command_checker(res, f""" -The command below failed: -\tsudo systemctl start {config.serviceName} -Expected status code 0, got status code {res}.""") + command = f'sudo systemctl disable {config.serviceName}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) + command = f'sudo systemctl start {config.serviceName}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) # TODO: finish deploy on docker # TODO: add checker to check def clean(config): - res = os.system(f'sudo systemctl disable {config.serviceName}') - command_checker(res, f""" -The command below failed: -\tsudo systemctl disable {config.serviceName} -Expected status code 0, got status code {res}.""") - res = os.system(f'sudo systemctl stop {config.serviceName}') - command_checker(res, f""" -The command below failed: -\tsudo systemctl stop {config.serviceName} -Expected status code 0, got status code {res}.""") + command = f'sudo systemctl disable {config.serviceName}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) + command = f'sudo systemctl stop {config.serviceName}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) if os.path.exists(f'/etc/systemd/system/{config.serviceName}.{config.serviceSuffix}'): - res = os.system( - f'sudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} && ' - f'sudo systemctl daemon-reload') - command_checker(res, f""" -The command below failed: -\tsudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} && \ -\tsudo systemctl daemon-reload -Expected status code 0, got status code {res}.""") - res = os.system(f'sudo systemctl reset-failed {config.serviceName}') - command_checker(res, f""" -The command below failed: -\tsudo systemctl reset-failed {config.serviceName} -Expected status code 0, got status code {res}.""") + command = f'''sudo rm -rf /etc/systemd/system/{config.serviceName}.{config.serviceSuffix} && \\ + sudo systemctl daemon-reload''' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) + command = f'sudo systemctl reset-failed {config.serviceName}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) if os.path.exists(f'{config.serviceWorkingDirectory}'): - res = os.system(f'sudo rm -rf {config.serviceWorkingDirectory}') - command_checker(res, f""" -The command below failed: -\tsudo rm -rf {config.serviceWorkingDirectory} -Expected status code 0, got status code {res}.""") + command = f'sudo rm -rf {config.serviceWorkingDirectory}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) if os.path.exists(f'{config.serviceStartJarFile}'): - res = os.system(f'sudo rm -rf {config.serviceStartJarFile}') - command_checker(res, f""" -The command below failed: -\tsudo rm -rf {config.serviceStartJarFile} -Expected status code 0, got status code {res}.""") + command = f'sudo rm -rf {config.serviceStartJarFile}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) if os.path.exists(f'{config.servicePIDFile}'): - res = os.system(f'sudo rm -rf {config.servicePIDFile}') - command_checker(res, f""" -The command below failed: -\tsudo rm -rf {config.servicePIDFile} -Expected status code 0, got status code {res}.""") + command = f'sudo rm -rf {config.servicePIDFile}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) if os.system(f"cat /etc/passwd | grep -w -E '^{config.serviceUser}'") == 0: - res = os.system(f'sudo userdel {config.serviceUser}') - command_checker(res, f""" -The command below failed: -\tsudo userdel {config.serviceUser} -Expected status code 0, got status code {res}.""") - res = os.system(f'mvn clean') - command_checker(res, f""" -The command below failed: -\tmvn clean -Expected status code 0, got status code {res}.""") + command = f'sudo userdel {config.serviceUser}' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) + command = f'mvn clean' + res = os.system(command) + message = message_tmp.format(command, res) + command_checker(res, message) if __name__ == "__main__": + message_tmp = '''\ +The command below failed: + {0} +Expected status code 0, got status code {1} +''' parser = argparse.ArgumentParser( description="Deploy the project when the environment is ready.") parser.add_argument('--config-path', nargs='?', default='../config.json', From 9b260c4c1c15793c9d9cb071a5f0bc8dff1505cd Mon Sep 17 00:00:00 2001 From: ajianke Date: Thu, 8 Aug 2024 12:06:54 +0800 Subject: [PATCH 7/7] Replace filename with pathname in log message --- script/deploy_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/deploy_helper.py b/script/deploy_helper.py index 9f6fea8..4d05ed6 100644 --- a/script/deploy_helper.py +++ b/script/deploy_helper.py @@ -21,7 +21,7 @@ def setup_logger(log_level=logging.INFO): :param log_level: Set the logging level, defaulting to INFO. """ logging.basicConfig(level=log_level, - format='%(asctime)s -%(levelname)s- in %(filename)s:%(caller_lineno)d %(message)s', + format='%(asctime)s -%(levelname)s- in %(pathname)s:%(caller_lineno)d: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')