diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index a9f736d..86cdc37 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -186,11 +186,52 @@ sudo systemctl reset-failed kubelet 2>/dev/null || true"; then start_remote_kubernetes_services() { local target="$1" - echo "--> Starting Kubernetes services on remote worker ${target}..." - ssh -o ConnectTimeout=5 "${target}" \ + echo "--> Starting Kubernetes services on ${target}..." + ssh -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new "${target}" \ "sudo systemctl start containerd && sudo systemctl start kubelet" } +wait_for_ssh_target() { + local target="$1" + local timeout_seconds="${2:-180}" + local elapsed=0 + + echo "--> Waiting for SSH on ${target} for up to ${timeout_seconds}s..." + until ssh -o BatchMode=yes -o ConnectTimeout=5 -o StrictHostKeyChecking=accept-new "${target}" "true" >/dev/null 2>&1; do + if ((elapsed >= timeout_seconds)); then + echo "Timed out waiting for SSH on ${target} after ${timeout_seconds}s." >&2 + return 1 + fi + sleep 5 + elapsed=$((elapsed + 5)) + done +} + +wait_for_cluster_ssh_targets() { + local timeout_seconds="${LAB_CLUSTER_VM_SSH_WAIT_SECONDS:-180}" + local target + local failures=0 + + if ! [[ "${timeout_seconds}" =~ ^[0-9]+$ ]]; then + echo "LAB_CLUSTER_VM_SSH_WAIT_SECONDS must be an integer, got '${timeout_seconds}'." >&2 + return 1 + fi + + if [[ "${LAB_CLUSTER_PROFILE:-debian}" == "pimox" ]]; then + local cp_host="${LAB_CLUSTER_CONTROL_PLANE_LAN_IP:-}" + local cp_user="${LAB_CLUSTER_CONTROL_PLANE_USER:-${LAB_DEBIAN_USER:-jv}}" + if [[ -n "${cp_host}" ]]; then + wait_for_ssh_target "${cp_user}@${cp_host}" "${timeout_seconds}" || failures=$((failures + 1)) + fi + fi + + for target in "${CLUSTER_WORKER_TARGETS[@]}"; do + wait_for_ssh_target "${target}" "${timeout_seconds}" || failures=$((failures + 1)) + done + + ((failures == 0)) +} + pimox_worker_count_default() { local spec_file="${REPO_ROOT}/.lab/pimox-workers.tsv" local var_file="${REPO_ROOT}/.lab/cluster-workers.auto.tfvars.json" @@ -438,6 +479,39 @@ sudo '${qm_bin}' start '${vmid}'" done } +start_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:-}}" + + 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 start 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 + + echo "--> Starting 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: running'; then + echo 'Pimox control-plane VM ${vmid} is already running.' + exit 0 +fi +echo 'Starting Pimox control-plane VM ${vmid}...' +sudo '${qm_bin}' start '${vmid}'" +} + stop_cluster() { local target local failures=0 @@ -468,17 +542,33 @@ stop_cluster() { start_cluster() { local target + local control_plane_target="" declare -a CLUSTER_WORKER_TARGETS=() require_debian_server "start-cluster" cluster_worker_targets echo "Starting Kubernetes cluster runtime without rebuilding state..." - start_local_kubernetes_services - start_pimox_worker_vms - for target in "${CLUSTER_WORKER_TARGETS[@]}"; do - start_remote_kubernetes_services "${target}" - done + if [[ "${LAB_CLUSTER_PROFILE:-debian}" == "pimox" ]]; then + start_pimox_control_plane_vm + start_pimox_worker_vms + wait_for_cluster_ssh_targets + + if [[ -n "${LAB_CLUSTER_CONTROL_PLANE_LAN_IP:-}" ]]; then + control_plane_target="${LAB_CLUSTER_CONTROL_PLANE_USER:-${LAB_DEBIAN_USER:-jv}}@${LAB_CLUSTER_CONTROL_PLANE_LAN_IP}" + start_remote_kubernetes_services "${control_plane_target}" + fi + for target in "${CLUSTER_WORKER_TARGETS[@]}"; do + start_remote_kubernetes_services "${target}" + done + wait_for_kubernetes_api "${LAB_CLUSTER_START_WAIT_SECONDS:-360}" + else + start_local_kubernetes_services + start_pimox_worker_vms + for target in "${CLUSTER_WORKER_TARGETS[@]}"; do + start_remote_kubernetes_services "${target}" + done + fi echo "Kubernetes runtime start requested. Use 'kubectl get nodes -o wide' to watch readiness." }