Skip to content

Commit

Permalink
Modify command_checker and setup_logger
Browse files Browse the repository at this point in the history
- 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.

Resolves: #17
  • Loading branch information
ajiankexx committed Aug 2, 2024
1 parent 5f76799 commit a502666
Showing 1 changed file with 33 additions and 20 deletions.
53 changes: 33 additions & 20 deletions script/deploy_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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
Expand All @@ -99,14 +99,14 @@ def deploy_on_ubuntu(config):
return -1
if config.runTest:
res = os.system('mvn test')
command_checker(res, "mvn test failed.", 0)
command_checker(res, "mvn test")
if config.deploy:
res = subprocess.run('bash script/get_jar_position.sh', shell=True,
capture_output=True, text=True)
command_checker(res.returncode, "script/get_jar_position.sh failed.", 0)
command_checker(res.returncode, "script/get_jar_position.sh")
package_path = res.stdout.strip()
res = os.system('mvn package')
command_checker(res, "mvn package failed.", 0)
command_checker(res, "mvn package")

if os.system(f"cat /etc/passwd | grep -w -E '^{config.serviceUser}'") != 0:
os.system(f'sudo useradd {config.serviceUser}')
Expand All @@ -123,51 +123,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',
Expand All @@ -177,7 +176,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':
Expand Down

0 comments on commit a502666

Please sign in to comment.