Skip to content

Commit

Permalink
Merge pull request #64 from CMIPT/kaiser-doc
Browse files Browse the repository at this point in the history
Fix a bug related with Sys-Init-V.
  • Loading branch information
Kaiser-Yang authored Oct 9, 2024
2 parents 16ed0cc + 71b7382 commit 2c96eb2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 60 deletions.
3 changes: 2 additions & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
| `dockerPortMapping` | `list` | `["8080:8080"]` | `Docker` 端口映射。 |
| `dockerWithGpu` | `bool` | `false` | `Docker` 是否使用 `GPU`|
| `dockerSrcPath` | `string` | `"/opt/gcs-back-end-src"` | `Docker` 中源码路径。源码会被拷贝到该路径进行编译。 |
| `serviceEnable` | `bool` | `true` | 是否启用 `systemd` 服务。 |
| `serviceType` | `string` | `"systemd"` | 部署的服务类型,可选值为 `"systemd"``"sys-init-v"` |
| `serviceName` | `string` | `"gcs"` | 服务名称。 |
| `serviceDescription` | `string` | `"Git server center back-end service"` | 服务描述。 |
| `servicePIDFile` | `string` | `"/var/run/gcs.pid"` | 服务 `PID` 文件。 |
Expand All @@ -30,6 +30,7 @@
| `serviceStartJavaCommand` | `string` | `"/usr/bin/java"` | 服务启动的 `Java` 命令。 |
| `serviceStartJavaArgs` | `list` | `["-jar"]` | 服务启动的 `Java` 参数。 |
| `serviceStartJarFile` | `string` | `"/opt/gcs/gcs.jar"` | 服务启动的 `Jar` 文件。脚本会将 `maven` 打包出来的文件拷贝到该位置。 |
| `serviceEnable` | `bool` | `true` | 是否启用 `systemd` 服务。 |
| `serviceSuffix` | `string` | `".service"` | `systemd` 服务文件后缀。 |
| `serviceWorkingDirectory` | `string` | `"/opt/gcs"` | `systemd` 服务工作目录。 |
| `serviceRestartPolicy` | `string` | `"always"` | `systemd` 服务重启策略。 |
Expand Down
3 changes: 1 addition & 2 deletions config_debug.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"createGitUser": false,
"deployWithDocker": false,
"repositoryDirectory": "~/.local/gcs/repository",
"skipTest": false,
"deployLogLevel": "debug",
"profiles": [
"dev"
],
"serviceType": "systemd",
"postgresqlUserName": "gcs_debug",
"postgresqlUserPassword": "gcs_debug",
"postgresqlDatabaseName": "gcs_debug",
Expand Down
2 changes: 0 additions & 2 deletions config_debug_docker.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"createGitUser": false,
"repositoryDirectory": "~/.local/gcs/repository",
"skipTest": false,
"deployLogLevel": "debug",
"profiles": [
Expand Down
3 changes: 2 additions & 1 deletion config_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"dockerWithGpu": false,
"dockerSrcPath": "/opt/gcs-back-end-src",
"serviceEnable": true,
"serviceType": "systemd",
"serviceName": "gcs",
"serviceDescription": "Git server center back-end service",
"servicePIDFile": "/var/run/gcs.pid",
Expand All @@ -31,6 +31,7 @@
"-jar"
],
"serviceStartJarFile": "/opt/gcs/gcs.jar",
"serviceEnable": true,
"serviceSuffix": ".service",
"serviceWorkingDirectory": "/opt/gcs",
"serviceRestartPolicy": "always",
Expand Down
93 changes: 52 additions & 41 deletions script/deploy_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import inspect

essential_packages = ['python-is-python3', 'postgresql postgresql-client',
'openjdk-17-jdk-headless', 'maven', 'systemd', 'sudo']
'openjdk-17-jdk-headless', 'maven', 'systemd', 'sudo', 'git',
'openssh-server']
sudo_cmd = os.popen('command -v sudo').read().strip()
apt_updated = False
message_tmp = '''\
Expand Down Expand Up @@ -132,28 +133,25 @@ def create_sys_v_init_service(config):
service_content = header + service_content
log_debug(f"service_content:\n {service_content}")

res = os.system(f"echo '{service_content}' | "
f"{sudo_cmd} tee {config.serviceSysVInitDirectory}/{config.serviceName}")
command_checker(res, f"Failed to create {config.serviceSysVInitDirectory}/{config.serviceName}")
try:
with open(f'{config.serviceSysVInitDirectory}/{config.serviceName}', 'w') as f:
f.write(service_content)
except Exception as e:
command_checker(1, f"Error: {e}")
return
res = os.system(f'{sudo_cmd} chmod +x {config.serviceSysVInitDirectory}/{config.serviceName}')
command_checker(
res, f"Failed to chmod +x {config.serviceSysVInitDirectory}/{config.serviceName}")

if logging.getLogger().level == logging.DEBUG:
try:
with open(f'{config.serviceSysVInitDirectory}/{config.serviceName}', 'r') as f:
log_debug(f"Service content:\n {f.read()}")
except Exception as e:
command_checker(1, f"Error: {e}")
return


def apt_install_package(name):
global apt_updated
global apt_updated, sudo_cmd
if not apt_updated:
res = os.system(f'{sudo_cmd} apt update')
command_checker(res, 'Failed to update apt')
apt_updated = True
if 'sudo' in name:
sudo_cmd = os.popen('command -v sudo').read().strip()
res = os.system(f'{sudo_cmd} apt install -y {name}')
command_checker(res, f'Failed to install {name}')

Expand Down Expand Up @@ -360,10 +358,21 @@ def write_other_config(config):

def deploy_on_ubuntu(config):
assert(config != None)
if config.inDocker:
if config.serviceType != 'systemd':
essential_packages.remove('systemd')
apt_install_package(parse_iterable_into_str(essential_packages))
init_database(config)
create_or_update_user(config.gitUserName, config.gitUserPassword)
if not os.path.exists(f'{config.gitHomeDirectory}/.ssh'):
os.system(f"{sudo_cmd} -u {config.gitUserName} mkdir -p {config.gitHomeDirectory}/.ssh")
os.system(f"{sudo_cmd} -u {config.gitUserName} chmod 700 {config.gitHomeDirectory}/.ssh")
create_or_update_user(config.serviceUser, config.serviceUserPassword)
# let the service user can use git, rm and tee commands as the git user without password
sudoers_entry = f"{config.serviceUser} ALL=(git) NOPASSWD: /usr/bin/git, /usr/bin/tee, /usr/bin/rm"
res = subprocess.run(f"echo '{sudoers_entry}' | {sudo_cmd} tee /etc/sudoers.d/{config.serviceUser}", shell=True);
command_checker(res.returncode, f"Failed to create /etc/sudoers.d/{config.serviceUser}")
res = subprocess.run(f"{sudo_cmd} chmod 440 /etc/sudoers.d/{config.serviceUser}", shell=True)
command_checker(res.returncode, f"Failed to chmod 440 /etc/sudoers.d/{config.serviceUser}")
activate_profile(config)
write_other_config(config)
skip_test = ""
Expand All @@ -377,14 +386,6 @@ def deploy_on_ubuntu(config):
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
create_or_update_user(config.gitUserName, config.gitUserPassword)
create_or_update_user(config.serviceUser, config.serviceUserPassword)
# let the service user can use git, rm and tee commands as the git user without password
sudoers_entry = f"{config.serviceUser} ALL=(git) NOPASSWD: /usr/bin/git, /usr/bin/tee, /usr/bin/rm"
res = subprocess.run(f"echo '{sudoers_entry}' | {sudo_cmd} tee /etc/sudoers.d/{config.serviceUser}", shell=True);
command_checker(res.returncode, f"Failed to create /etc/sudoers.d/{config.serviceUser}")
res = subprocess.run(f"{sudo_cmd} chmod 440 /etc/sudoers.d/{config.serviceUser}", shell=True)
command_checker(res.returncode, f"Failed to chmod 440 /etc/sudoers.d/{config.serviceUser}")

if config.deploy:
if not os.path.exists(os.path.dirname(config.serviceStartJarFile)):
Expand All @@ -396,10 +397,12 @@ def deploy_on_ubuntu(config):
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
if config.inDocker:
if config.serviceType == 'sys-init-v':
deploy_with_sys_v_init(config)
else:
elif config.serviceType == 'systemd':
deploy_with_systemd(config)
else:
raise ValueError(f"Invalid service type: {config.serviceType}")


def delete_user(username):
Expand All @@ -418,24 +421,32 @@ def clean(config):
res = os.system(f"docker rm {config.dockerName}")
command_checker(res, f"Failed to remove {config.dockerName}")
return
command = f'{sudo_cmd} systemctl disable {config.serviceName}'
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
command = f'{sudo_cmd} systemctl stop {config.serviceName}'
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
if os.path.exists(f'{config.serviceSystemdDirectory}/{config.serviceName}{config.serviceSuffix}'):
command = f'''{sudo_cmd} rm -rf {config.serviceSystemdDirectory}/{config.serviceName}{config.serviceSuffix} && \\
{sudo_cmd} systemctl daemon-reload'''
if config.serviceType == 'systemd':
command = f'{sudo_cmd} systemctl disable {config.serviceName}'
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
command = f'{sudo_cmd} systemctl reset-failed {config.serviceName}'
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
command = f'{sudo_cmd} systemctl stop {config.serviceName}'
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
if os.path.exists(f'{config.serviceSystemdDirectory}/{config.serviceName}{config.serviceSuffix}'):
command = f'''{sudo_cmd} rm -rf {config.serviceSystemdDirectory}/{config.serviceName}{config.serviceSuffix} && \\
{sudo_cmd} systemctl daemon-reload'''
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
command = f'{sudo_cmd} systemctl reset-failed {config.serviceName}'
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
elif config.serviceType == 'sys-init-v':
command = f'{sudo_cmd} service {config.serviceName} uninstall'
res = os.system(command)
message = message_tmp.format(command, res)
command_checker(res, message)
else:
raise ValueError(f"Invalid service type: {config.serviceType}")
if os.path.exists(f'{config.serviceWorkingDirectory}'):
command = f'{sudo_cmd} rm -rf {config.serviceWorkingDirectory}'
res = os.system(command)
Expand Down Expand Up @@ -503,7 +514,7 @@ def get_cli_args():
return parser.parse_args()


def deploy_in_docker(config):
def deploy_with_docker(config):
res = os.system(f'cd 3rdparty/docker-script && '
f'bash create_docker.sh -n {config.dockerName} -d {config.dockerImage} '
f'-w {int(config.dockerWithGpu)} '
Expand Down Expand Up @@ -537,7 +548,7 @@ def main():
if args.clean:
clean(config)
elif not args.in_docker and config.deployWithDocker:
deploy_in_docker(config)
deploy_with_docker(config)
elif args.distro == 'ubuntu':
deploy_on_ubuntu(config)

Expand Down
21 changes: 8 additions & 13 deletions script/service_tmp.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
PIDDIR=$(dirname "$PIDFILE")
LOGDIR=$(dirname "$LOGFILE")
start() {
if [ -f "$PIDDIR/$PIDNAME" ] && kill -0 "$(cat "$PIDDIR/$PIDNAME")"; then
if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")"; then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
su -c "mkdir -p ""$PIDDIR" "$RUNAS"
su -c "mkdir -p ""$LOGDIR" "$RUNAS"
su -c "$CMD" "$RUNAS" > "$PIDFILE"
su -c "$SCRIPT & echo \$!" "$RUNAS" > "$LOGFILE"
head -1 "$LOGFILE" > "$PIDFILE"
echo 'Service started' >&2
}

Expand All @@ -24,16 +24,11 @@ stop() {
}

uninstall() {
echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
local SURE
read SURE
if [ "$SURE" = "yes" ]; then
stop
rm -f "$PIDFILE"
echo "Notice: log file is not be removed: '$LOGFILE'" >&2
update-rc.d -f "$NAME" remove
rm -fv "$0"
fi
stop
rm -f "$PIDFILE"
echo "Notice: log file is not be removed: '$LOGFILE'" >&2
update-rc.d -f "$NAME" remove
rm -fv "$0"
}

case "$1" in
Expand Down

0 comments on commit 2c96eb2

Please sign in to comment.