diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 154aca9..5a43f80 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -528,6 +528,79 @@ echo 'Starting Pimox control-plane VM ${vmid}...' sudo '${qm_bin}' start '${vmid}'" } +stop_pimox_control_plane_vm() { + local pimox_host="${LAB_PIMOX_HOST:-${TF_VAR_pimox_host:-192.168.100.80}}" + local pimox_user="${LAB_PIMOX_USER:-${TF_VAR_pimox_user:-jv}}" + local pimox_key="${LAB_PIMOX_SSH_KEY_PATH:-${TF_VAR_pimox_ssh_key_path:-/home/jv/.ssh/id_ed25519}}" + local qm_bin="${LAB_PIMOX_QM_BIN:-${TF_VAR_pimox_qm_bin:-/usr/sbin/qm}}" + local vmid="${LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID:-${LAB_PIMOX_CONTROL_PLANE_VMID:-}}" + local shutdown_timeout="${LAB_CLUSTER_STOP_VM_TIMEOUT_SECONDS:-120}" + local force_stop="${LAB_CLUSTER_STOP_FORCE:-true}" + + if [[ "${LAB_CLUSTER_PROFILE:-debian}" != "pimox" ]]; then + return 0 + fi + if [[ -z "${vmid}" ]]; then + echo "LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID is not configured; cannot stop Pimox control-plane VM." >&2 + return 1 + fi + if ! [[ "${vmid}" =~ ^[0-9]+$ ]]; then + echo "LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID must be an integer, got '${vmid}'." >&2 + return 1 + fi + if ! [[ "${shutdown_timeout}" =~ ^[0-9]+$ ]]; then + echo "LAB_CLUSTER_STOP_VM_TIMEOUT_SECONDS must be an integer, got '${shutdown_timeout}'." >&2 + return 1 + fi + + echo "--> Stopping Pimox control-plane VM ${vmid} on ${pimox_host}..." + pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "set -eu +if ! sudo '${qm_bin}' status '${vmid}' >/dev/null 2>&1; then + echo 'Pimox control-plane VM ${vmid} does not exist.' >&2 + exit 1 +fi +if sudo '${qm_bin}' status '${vmid}' | grep -q 'status: stopped'; then + echo 'Pimox control-plane VM ${vmid} is already stopped.' + exit 0 +fi +echo 'Gracefully shutting down Pimox control-plane VM ${vmid}...' +if sudo '${qm_bin}' shutdown '${vmid}' --timeout '${shutdown_timeout}'; then + exit 0 +fi +if [ '${force_stop}' = 'true' ]; then + echo 'Graceful shutdown timed out; forcing stop for Pimox control-plane VM ${vmid}.' + sudo '${qm_bin}' stop '${vmid}' +else + echo 'Graceful shutdown timed out for Pimox control-plane VM ${vmid}. Set LAB_CLUSTER_STOP_FORCE=true to force stop.' >&2 + exit 1 +fi" +} + +verify_pimox_control_plane_vm_stopped() { + local pimox_host="${LAB_PIMOX_HOST:-${TF_VAR_pimox_host:-192.168.100.80}}" + local pimox_user="${LAB_PIMOX_USER:-${TF_VAR_pimox_user:-jv}}" + local pimox_key="${LAB_PIMOX_SSH_KEY_PATH:-${TF_VAR_pimox_ssh_key_path:-/home/jv/.ssh/id_ed25519}}" + local qm_bin="${LAB_PIMOX_QM_BIN:-${TF_VAR_pimox_qm_bin:-/usr/sbin/qm}}" + local vmid="${LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID:-${LAB_PIMOX_CONTROL_PLANE_VMID:-}}" + + if [[ "${LAB_CLUSTER_PROFILE:-debian}" != "pimox" ]]; then + return 0 + fi + if [[ -z "${vmid}" ]]; then + echo "LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID is not configured; cannot verify Pimox control-plane VM." >&2 + return 1 + fi + if ! [[ "${vmid}" =~ ^[0-9]+$ ]]; then + echo "LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID must be an integer, got '${vmid}'." >&2 + return 1 + fi + + if ! pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "sudo '${qm_bin}' status '${vmid}' 2>/dev/null | grep -q 'status: stopped'"; then + echo "Pimox control-plane VM ${vmid} is not stopped." >&2 + return 1 + fi +} + stop_cluster() { local target local failures=0 @@ -537,15 +610,30 @@ stop_cluster() { cluster_worker_targets echo "Stopping Kubernetes cluster runtime without destroying state..." - for target in "${CLUSTER_WORKER_TARGETS[@]}"; do - stop_remote_kubernetes_services "${target}" - done - stop_pimox_worker_vms - stop_local_kubernetes_services + if [[ "${LAB_CLUSTER_PROFILE:-debian}" == "pimox" ]]; then + echo "--> Pimox profile detected; stopping VMs through the Pimox host instead of SSHing into guest nodes." + stop_pimox_worker_vms + stop_pimox_control_plane_vm || failures=$((failures + 1)) + if truthy "${LAB_CLUSTER_STOP_DEBIAN_K8S_IN_PIMOX_PROFILE:-false}"; then + stop_local_kubernetes_services + else + echo "--> Skipping local Debian Kubernetes service stop because active cluster profile is Pimox." + echo " Set LAB_CLUSTER_STOP_DEBIAN_K8S_IN_PIMOX_PROFILE=true to stop leftover local Debian kubelet/containerd tasks too." + fi + else + for target in "${CLUSTER_WORKER_TARGETS[@]}"; do + stop_remote_kubernetes_services "${target}" + done + stop_pimox_worker_vms + stop_local_kubernetes_services + fi if kubernetes_api_reachable; then echo "Kubernetes API is still reachable after stop; control-plane containers may still be running." >&2 failures=$((failures + 1)) fi + if ! verify_pimox_control_plane_vm_stopped; then + failures=$((failures + 1)) + fi if ! verify_pimox_worker_vms_stopped; then failures=$((failures + 1)) fi