From de58a03a219fe2e93da8fef32ba3484149befb0f Mon Sep 17 00:00:00 2001 From: yyamashita Date: Sun, 1 Nov 2026 04:18:03 +0900 Subject: hetzner-admin: stop/start code-server (systemd) from the admin panel Co-Authored-By: Claude Opus 5.5 --- hetzner-admin/app.py | 2 +- hetzner-admin/run-command.sh | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'hetzner-admin') diff --git a/hetzner-admin/app.py b/hetzner-admin/app.py index ac3178c..a9377a8 100644 --- a/hetzner-admin/app.py +++ b/hetzner-admin/app.py @@ -66,7 +66,7 @@ COMMANDS = [ COMMAND_MAP = {label: cmd for label, cmd in COMMANDS} # 個別に停止・起動できるサービス(run-command.sh の SERVICES と status/targets.txt の名前に合わせる) -SERVICES = ["golive", "whoisband", "blog", "todo", "git", "vpn", "ntfy", "mail"] +SERVICES = ["golive", "whoisband", "blog", "todo", "git", "vpn", "ntfy", "mail", "code"] # ブラウザ側: JSON ⇔ WebAuthn の変換はブラウザ組み込みの # parseCreationOptionsFromJSON / parseRequestOptionsFromJSON / toJSON に任せる diff --git a/hetzner-admin/run-command.sh b/hetzner-admin/run-command.sh index c5b8ea5..d1f68f8 100755 --- a/hetzner-admin/run-command.sh +++ b/hetzner-admin/run-command.sh @@ -4,7 +4,14 @@ set -e # 管理画面から停止・起動できるサービス(名前は status/targets.txt の名前に合わせる) # Caddy / hetzner-admin / status は止めると管理画面に戻れなくなるので対象外 -SERVICES="golive whoisband blog todo git vpn ntfy mail" +SERVICES="golive whoisband blog todo git vpn ntfy mail code" +# docker compose ではなく systemd で動くサービス +service_unit() { + case "$1" in + code) echo code-server@yyamashita.service ;; + *) return 1 ;; + esac +} service_compose() { case "$1" in golive) echo /app/tokyo-livehouse-events/docker-compose.yml ;; @@ -26,22 +33,27 @@ case "$SSH_ORIGINAL_COMMAND" in "services") touch "$STOPPED_FILE" for name in $SERVICES; do - running=$(docker compose -f "$(service_compose "$name")" ps -q --status running 2>/dev/null | wc -l) - if [ "$running" -gt 0 ]; then state=running; else state=stopped; fi + if unit=$(service_unit "$name"); then + if systemctl is-active --quiet "$unit"; then state=running; else state=stopped; fi + else + running=$(docker compose -f "$(service_compose "$name")" ps -q --status running 2>/dev/null | wc -l) + if [ "$running" -gt 0 ]; then state=running; else state=stopped; fi + fi echo "$name $state" done ;; "stop "*|"start "*) action="${SSH_ORIGINAL_COMMAND%% *}" name="${SSH_ORIGINAL_COMMAND#* }" - compose=$(service_compose "$name") || { echo "Unknown service: $name" >&2; exit 1; } + unit=$(service_unit "$name") || compose=$(service_compose "$name") || { echo "Unknown service: $name" >&2; exit 1; } touch "$STOPPED_FILE" if [ "$action" = stop ]; then grep -qx "$name" "$STOPPED_FILE" || echo "$name" >> "$STOPPED_FILE" - docker compose -f "$compose" stop 2>&1 + # systemd は disable もして、再起動後も停止したままにする(docker の unless-stopped と同じ扱い) + if [ -n "$unit" ]; then systemctl disable --now "$unit" 2>&1; else docker compose -f "$compose" stop 2>&1; fi else sed -i "/^${name}\$/d" "$STOPPED_FILE" - docker compose -f "$compose" up -d 2>&1 + if [ -n "$unit" ]; then systemctl enable --now "$unit" 2>&1; else docker compose -f "$compose" up -d 2>&1; fi fi ;; "docker ps") -- cgit v1.3