deploy fix bug

This commit is contained in:
ZuoZuo 2026-04-07 18:16:32 +08:00
parent b8905eaa3a
commit 5d16f18188

View File

@ -8,6 +8,7 @@ import json
import shlex
import sys
import time
import textwrap
import urllib.request
from pathlib import Path
from typing import Any
@ -104,6 +105,7 @@ class CloudOperator:
def deploy(self) -> None:
self.ensure_agent_online()
self.ensure_systemd_unit_installed()
package_url, sha256 = self.resolve_release_package()
if self.service_name == "gateway":
@ -119,6 +121,7 @@ class CloudOperator:
def restart(self) -> None:
self.ensure_agent_online()
self.ensure_systemd_unit_installed()
unit_name = str(self.service_cfg["unit_name"])
health_url = str(self.service_cfg["health_url"])
command = " && ".join(
@ -189,6 +192,59 @@ class CloudOperator:
self.ensure_task_success(task, f"deploy {self.service_name}")
log(f"deploy completed for {self.service_name}@{self.instance_id} -> {self.release_id}")
def ensure_systemd_unit_installed(self) -> None:
unit_name = str(self.service_cfg["unit_name"])
unit_path = f"/etc/systemd/system/{unit_name}"
unit_content = self.systemd_unit_content().rstrip() + "\n"
command = textwrap.dedent(
f"""\
set -Eeuo pipefail
unit_path={shlex.quote(unit_path)}
tmp_file="$(mktemp)"
trap 'rm -f "$tmp_file"' EXIT
cat >"$tmp_file" <<'UNIT_EOF'
{unit_content}UNIT_EOF
if [[ ! -f "$unit_path" ]] || ! cmp -s "$tmp_file" "$unit_path"; then
install -D -m 0644 "$tmp_file" "$unit_path"
/usr/bin/systemctl daemon-reload
fi
/usr/bin/systemctl enable {shlex.quote(unit_name)} >/dev/null
"""
)
invocation_id = self.run_tat_command(command, f"ensure-unit-{self.service_name}")
task = self.wait_for_tat(invocation_id)
self.ensure_task_success(task, f"ensure systemd unit {unit_name}")
def systemd_unit_content(self) -> str:
deploy_root = str(self.service_cfg["deploy_root"]).rstrip("/")
binary_path = f"{deploy_root}/current/bin/{self.service_name}"
config_path = f"{deploy_root}/current/config/prod.yaml"
description = f"ChatApp {self.service_name.capitalize()} Service"
working_directory = f"{deploy_root}/current"
return textwrap.dedent(
f"""\
[Unit]
Description={description}
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory={working_directory}
ExecStart={binary_path} -config {config_path}
Restart=always
RestartSec=3
KillSignal=SIGTERM
TimeoutStopSec=30
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
"""
)
def run_tat_command(self, command: str, command_name: str) -> str:
payload: dict[str, Any] = {
"CommandName": command_name,