feat: add homelab-wide shutdown command

This commit is contained in:
juvenal.diaz 2026-10-26 09:48:13 -06:00
parent fbf4060946
commit 866089b346
4 changed files with 173 additions and 9 deletions

View File

@ -29,9 +29,10 @@ var allowedCommands = map[string]bool{
"help": true, "up": true, "plan": true,
"cluster-profile": true, "rebuild-cluster": true, "stop-cluster": true,
"start-cluster": true, "cleanup-debian-k8s": true,
"status": true, "capacity": true, "capacity-advisor": true,
"shutdown": true,
"status": true, "capacity": true, "capacity-advisor": true,
"capacity-limits": true, "recover-plan": true, "gitops-status": true,
"gitops": true,
"gitops": true,
"storage": true,
"mesh": true,
"platform": true, "edge": true, "edge-cache-flush": true,
@ -61,14 +62,14 @@ var allowedCommands = map[string]bool{
"doctor-rpi": true, "doctor-cluster": true, "doctor-k8s": true,
"preflight": true, "doctor-preapply": true, "inventory-check": true,
"state-backup": true, "fix-debian-docker-root": true,
"state": true,
"pimox": true,
"email-vm": true,
"state": true,
"pimox": true,
"email-vm": true,
"oci-foundation": true,
"oci-auth": true,
"oci": true,
"secrets-init": true, "secrets-check": true, "tailnet-policy-check": true,
"secrets": true,
"oci-auth": true,
"oci": true,
"secrets-init": true, "secrets-check": true, "tailnet-policy-check": true,
"secrets": true,
"ai-index": true, "ai-check": true, "ask": true, "ai-evals": true,
"ai-scheduler": true, "ai-memory": true, "model-observe": true,
"impact": true, "review-last-change": true,

View File

@ -148,6 +148,12 @@ func TestEmailVMCommandIsRemotelyEnabled(t *testing.T) {
}
}
func TestShutdownCommandIsRemotelyEnabled(t *testing.T) {
if !allowedCommands["shutdown"] {
t.Fatal("shutdown must be exposed through the remote API")
}
}
func TestOCIFoundationCommandIsRemotelyEnabled(t *testing.T) {
if !allowedCommands["oci-foundation"] {
t.Fatal("oci-foundation must be exposed through the remote API")

View File

@ -80,6 +80,7 @@ Cluster Lifecycle
rebuild-cluster Recreate the cluster through the guarded path.
stop-cluster Stop Kubernetes and worker VMs without destroy.
start-cluster Start Kubernetes and desired worker VMs.
shutdown {plan|apply --yes} Stop homelab services on every managed host; keep Jeannie API.
cleanup-debian-k8s Remove old Debian kubeadm runtime after Pimox offload.
workers <list|tailnet|start|stop|restart|drain|uncordon|recreate-plan|rebalance|disk-status|disk-cleanup>
Manage Pimox/Kubernetes workers.
@ -240,6 +241,10 @@ case "${1:-}" in
record_change_journal "stop-cluster" "$@"
stop_cluster
;;
shutdown)
record_change_journal "shutdown" "$@"
homelab_shutdown "${@:2}"
;;
start-cluster)
record_change_journal "start-cluster" "$@"
start_cluster

View File

@ -737,6 +737,158 @@ stop_cluster() {
echo "Kubernetes runtime stopped. OpenTofu state, kubeadm files, PV data, and VM disks were left intact."
}
shutdown_remote_compose_stack() {
local label="$1"
local target="$2"
local install_dir="$3"
local key_path="${4:-}"
local ssh_args=(-o BatchMode=yes -o ConnectTimeout=15 -o StrictHostKeyChecking=accept-new)
[[ -n "${key_path}" ]] && ssh_args+=(-i "${key_path}")
echo "--> Stopping ${label} services on ${target} (${install_dir})..."
ssh "${ssh_args[@]}" "${target}" "
set -eu
if [ -f '${install_dir}/docker-compose.yml' ] || [ -f '${install_dir}/compose.yml' ]; then
cd '${install_dir}'
if sudo docker compose version >/dev/null 2>&1; then
sudo docker compose stop || true
elif command -v docker-compose >/dev/null 2>&1; then
sudo docker-compose stop || true
else
echo 'Docker Compose is unavailable; leaving the stack unchanged.' >&2
exit 1
fi
else
echo '${install_dir}/docker-compose.yml not found; leaving the stack unchanged.' >&2
fi"
}
shutdown_rpi_services() {
local rpi_host="${LAB_RPI_HOST:-${LAB_RASPBERRY_HOST:-192.168.100.89}}"
local rpi_user="${LAB_RPI_USER:-${LAB_RASPBERRY_USER:-jv}}"
local rpi_key="${LAB_RPI_SSH_KEY_PATH:-${LAB_RASPBERRY_SSH_KEY_PATH:-${HOME}/.ssh/id_ed25519}}"
local install_dir="${LAB_RPI_SERVICES_INSTALL_DIR:-/opt/homelab-rpi-services}"
shutdown_remote_compose_stack "RPi4" "${rpi_user}@${rpi_host}" "${install_dir}" "${rpi_key}"
}
shutdown_oci_edge_services() {
local edge_host="${LAB_EDGE_HOST:-${LAB_OCI_EDGE_PUBLIC_IP:-}}"
local edge_user="${LAB_EDGE_USER:-ubuntu}"
local edge_key="${LAB_EDGE_SSH_KEY_PATH:-${LAB_OCI_SSH_PRIVATE_KEY:-}}"
local install_dir="${LAB_EDGE_INSTALL_DIR:-/opt/homelab-edge}"
[[ -n "${edge_host}" ]] || {
echo "OCI edge host is not configured; set LAB_EDGE_HOST or LAB_OCI_EDGE_PUBLIC_IP." >&2
return 1
}
shutdown_remote_compose_stack "OCI edge" "${edge_user}@${edge_host}" "${install_dir}" "${edge_key}"
}
shutdown_oci_email_services() {
local email_host="${LAB_EMAIL_PUBLIC_IP:-${LAB_OCI_EMAIL_PUBLIC_IP:-}}"
local email_user="${LAB_EMAIL_SSH_USER:-ubuntu}"
local email_key="${LAB_EMAIL_SSH_PRIVATE_KEY:-${HOME}/.ssh/id_ed25519}"
if [[ -z "${email_host}" ]]; then
email_host="$(tofu -chdir="${REPO_ROOT}/bootstrap/oci" output -raw email_public_ip 2>/dev/null || true)"
fi
[[ -n "${email_host}" && "${email_host}" != "null" ]] || {
echo "OCI email host is not configured; skipping mail VM." >&2
return 0
}
echo "--> Stopping email services on ${email_user}@${email_host}..."
ssh -i "${email_key}" -o BatchMode=yes -o ConnectTimeout=15 -o StrictHostKeyChecking=accept-new \
"${email_user}@${email_host}" \
"sudo systemctl stop postfix dovecot fail2ban prometheus-node-exporter 2>/dev/null || true"
}
shutdown_debian_services() {
echo "--> Stopping Debian homelab services (jeannie-server is preserved)..."
sudo systemctl stop \
jeannie-eventd.service \
jeannie-temporal-worker.service \
homelab-edge-traefik-route.service \
homelab-pimox-registry-forward.service \
homelab-gitea-runner.service \
homelab-gitea-backup.service \
homelab-gitea-restore-drill.service \
homelab-profile-backup.service \
2>/dev/null || true
if command -v docker >/dev/null 2>&1; then
local containers
containers="$(sudo docker ps -q 2>/dev/null || true)"
if [[ -n "${containers}" ]]; then
echo "Stopping ${containers//$'\n'/ } Debian Docker container(s)..."
# Jeannie API is a host process/systemd service, not a container.
printf '%s\n' "${containers}" | xargs -r sudo docker stop >/dev/null
fi
fi
}
homelab_shutdown() {
local action="${1:-plan}"
local failures=0
case "${action}" in
--help|help)
echo "Usage: ./jeannie shutdown {plan|apply --yes}"
return 0
;;
plan)
cat <<'EOF'
Homelab shutdown plan
=====================
The apply action stops managed services on Debian, RPi4, OCI edge, and OCI email,
then stops the Kubernetes/Pimox runtime. Hosts, disks, SSH, and jeannie-server
remain running so the API can receive the next command. Docker volumes and VM
disks are not deleted.
Targets:
- Debian Docker containers and managed homelab systemd units
- RPi4 Docker Compose services
- OCI edge Docker Compose services
- OCI Postfix, Dovecot, fail2ban, and node-exporter
- Kubernetes services and Pimox control-plane/worker VMs
Run ./jeannie shutdown apply --yes to execute this plan.
EOF
;;
apply)
if [[ "${2:-}" != "--yes" ]]; then
echo "Refusing homelab-wide shutdown without --yes." >&2
echo "Run ./jeannie shutdown plan first, then ./jeannie shutdown apply --yes." >&2
return 2
fi
require_debian_server "shutdown"
echo "Shutting down managed homelab services; jeannie-server will remain running."
shutdown_rpi_services || failures=$((failures + 1))
shutdown_oci_edge_services || failures=$((failures + 1))
shutdown_oci_email_services || failures=$((failures + 1))
stop_cluster || failures=$((failures + 1))
shutdown_debian_services || failures=$((failures + 1))
if systemctl is-active --quiet jeannie-server.service 2>/dev/null; then
echo "PASS jeannie-server.service remains active."
else
echo "FAIL jeannie-server.service is not active; the API is unavailable." >&2
failures=$((failures + 1))
fi
if ((failures > 0)); then
echo "Homelab shutdown finished with ${failures} failure(s)." >&2
return 1
fi
echo "Homelab services stopped. jeannie-server and host SSH remain available."
;;
*)
echo "Usage: ./jeannie shutdown {plan|apply --yes}" >&2
return 2
;;
esac
}
start_cluster() {
local target
local control_plane_target=""