Skip to content

Commit

Permalink
Merge pull request #17 from CMIPT/nt-dev
Browse files Browse the repository at this point in the history
Add command_checker and setup_logger to script/deploy_helper.py.
  • Loading branch information
luckygalaxy666 authored Aug 8, 2024
2 parents 33396f7 + 9b260c4 commit 6ed74c4
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 33 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
<organizationUrl>https://github.com/CMIPT</organizationUrl>
<timezone>8</timezone>
</developer>
<developer>
<name>ajiankexx</name>
<email>[email protected]</email>
<url>https://github.com/ajiankexx</url>
<roles>
<role>Developer</role>
</roles>
<organization>CMIPT</organization>
<organizationUrl>https://github.com/CMIPT</organizationUrl>
<timezone>8</timezone>
</developer>
</developers>
<scm>
<connection/>
Expand Down
152 changes: 119 additions & 33 deletions script/deploy_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@
from types import SimpleNamespace
import os
import subprocess
import logging
import inspect


def setup_logger(log_level=logging.INFO):
"""
Configure the global logging system.
:param log_level: Set the logging level, defaulting to INFO.
"""
logging.basicConfig(level=log_level,
format='%(asctime)s -%(levelname)s- in %(pathname)s:%(caller_lineno)d: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')


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: 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:
caller_frame = inspect.currentframe().f_back
logging.error(message, extra={'caller_lineno': caller_frame.f_lineno})
exit(status_code)


# open config_path and default_config_path
Expand Down Expand Up @@ -63,10 +90,10 @@ def create_systemd_service(config):
[Install]
WantedBy={wanted_by}
"""
res = os.system(
f'echo "{gcs_file_content}" | sudo tee {service_full_path}')
if res != 0:
exit(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
Expand All @@ -81,15 +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:
os.system(f'sudo useradd {config.serviceUser}')
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 == "":
os.system(f'sudo passwd -d {config.serviceUser}')
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)
Expand All @@ -99,46 +133,84 @@ 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 cp {package_path} {config.serviceStartJarFile}')
if res != 0:
return 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}')
if res != 0:
return 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}')
if res != 0:
return res
res = os.system(f'sudo systemctl start {config.serviceName}')
if res != 0:
return 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):
os.system(f'sudo systemctl disable {config.serviceName}')
os.system(f'sudo systemctl stop {config.serviceName}')
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}'):
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 = 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}'):
os.system(f'sudo rm -rf {config.serviceWorkingDirectory}')
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}'):
os.system(f'sudo rm -rf {config.serviceStartJarFile}')
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}'):
os.system(f'sudo rm -rf {config.servicePIDFile}')
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:
os.system(f'sudo userdel {config.serviceUser}')
os.system(f'mvn clean')
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',
Expand All @@ -148,7 +220,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. 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 6ed74c4

Please sign in to comment.