diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index bcc1515..62a1b8a 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -2309,6 +2309,228 @@ sudo '${qm_bin}' set '${vmid}' --onboot 1" printf '%s\t%s\t%s\t%s\t%s\n' "${worker_key}" "${guest_ip}" "${worker_user}" "${node_name}" "${worker_key_path}" >>"${spec_file}" } +ensure_pimox_control_plane_node() { + local pimox_host="$1" + local pimox_user="$2" + local pimox_key="$3" + local template_vmid="$4" + local bridge="$5" + local vmid="$6" + local vm_name="$7" + local node_ip="$8" + local guest_user="$9" + local guest_key_path="${10}" + local ip_prefix="${11}" + local timeout_seconds="${12}" + local qm_bin="${13}" + local storage="${14}" + local replace_existing="${15}" + local cores="${16}" + local memory="${17}" + local cpu_affinity="${18}" + local kvm_cpuset="${19}" + local network_mode="${LAB_PIMOX_CONTROL_PLANE_NETWORK_MODE:-${LAB_PIMOX_WORKER_NETWORK_MODE:-static}}" + local gateway="${LAB_PIMOX_CONTROL_PLANE_GATEWAY:-${LAB_PIMOX_WORKER_GATEWAY:-192.168.100.1}}" + local dns_servers="${LAB_PIMOX_CONTROL_PLANE_DNS_SERVERS:-${LAB_PIMOX_WORKER_DNS_SERVERS:-192.168.100.89 1.1.1.1}}" + local interface_name="${LAB_PIMOX_CONTROL_PLANE_INTERFACE:-${LAB_PIMOX_WORKER_INTERFACE:-enp0s18}}" + local net0_config + local guest_ip + local guest_cpu_count + + if ! net0_config="$(pimox_worker_net0_config "${vmid}" "${bridge}")"; then + exit 1 + fi + + if pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "sudo '${qm_bin}' status '${vmid}' >/dev/null 2>&1"; then + if pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "sudo '${qm_bin}' config '${vmid}' | grep -q '^template: 1$'"; then + echo "VM ${vmid} exists as a template; refusing to reuse it as control-plane ${vm_name}." >&2 + exit 1 + fi + if truthy "${replace_existing}"; then + echo "Replacing existing Pimox control-plane VM ${vmid} (${vm_name}) before cloning from template ${template_vmid}..." + pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "set -eu +sudo '${qm_bin}' stop '${vmid}' >/dev/null 2>&1 || true +elapsed=0 +while [ \"\$elapsed\" -lt 300 ]; do + if sudo '${qm_bin}' status '${vmid}' | grep -q 'status: stopped'; then + break + fi + sleep 5 + elapsed=\$((elapsed + 5)) +done +sudo '${qm_bin}' destroy '${vmid}' --purge 1 >/dev/null 2>&1 || sudo '${qm_bin}' destroy '${vmid}'" + else + pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "set -eu +was_running=false +had_legacy_args=false +if sudo '${qm_bin}' status '${vmid}' | grep -q 'status: running'; then + was_running=true +fi +if sudo '${qm_bin}' config '${vmid}' | grep -q '^args:'; then + had_legacy_args=true +fi +sudo '${qm_bin}' set '${vmid}' \ + --agent enabled=1 \ + --bios ovmf \ + --boot 'order=scsi0;net0' \ + --cores '${cores}' \ + --memory '${memory}' \ + --net0 '${net0_config}' \ + --numa 0 \ + --ostype l26 \ + --scsihw virtio-scsi-pci \ + --sockets 1 \ + --vga virtio +if [ \"\$had_legacy_args\" = true ]; then + sudo '${qm_bin}' set '${vmid}' --delete args +fi +if [ -n '${cpu_affinity}' ]; then + affinity_output=\"\$(sudo '${qm_bin}' set '${vmid}' --affinity '${cpu_affinity}' 2>&1)\" || { + case \"\$affinity_output\" in + *'Unknown option: affinity'*) + echo 'Pimox qm does not support --affinity; skipping CPU affinity ${cpu_affinity} for VM ${vmid}.' + ;; + *) + printf '%s\n' \"\$affinity_output\" >&2 + exit 1 + ;; + esac + } +fi +if [ \"\$had_legacy_args\" = true ] && [ \"\$was_running\" = true ]; then + echo 'Restarting VM ${vmid} to apply removal of legacy qm args.' + sudo '${qm_bin}' shutdown '${vmid}' --timeout 120 || true + elapsed=0 + while [ \"\$elapsed\" -lt 300 ]; do + if sudo '${qm_bin}' status '${vmid}' | grep -q 'status: stopped'; then + break + fi + sleep 5 + elapsed=\$((elapsed + 5)) + done + if ! sudo '${qm_bin}' status '${vmid}' | grep -q 'status: stopped'; then + echo 'VM ${vmid} did not stop after removing legacy qm args.' >&2 + exit 1 + fi +fi" + fi + fi + + if ! pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "sudo '${qm_bin}' status '${vmid}' >/dev/null 2>&1"; then + echo "Creating Pimox control-plane VM ${vmid} (${vm_name}) from template ${template_vmid}..." + pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "set -eu +if ! ip link show '${bridge}' >/dev/null 2>&1; then + echo 'Pimox bridge ${bridge} does not exist. Refusing to change Orange Pi networking.' >&2 + exit 1 +fi +pvesm_cmd=\"\$(command -v pvesm 2>/dev/null || true)\" +if [ -z \"\$pvesm_cmd\" ] && [ -x /usr/sbin/pvesm ]; then + pvesm_cmd=/usr/sbin/pvesm +fi +if [ -z \"\$pvesm_cmd\" ]; then + echo 'pvesm was not found; cannot validate Pimox control-plane storage ${storage}' >&2 + exit 1 +fi +if ! sudo \"\$pvesm_cmd\" status | awk -v storage='${storage}' 'NR > 1 && \$1 == storage { found = 1 } END { exit found ? 0 : 1 }'; then + echo 'Pimox control-plane storage ${storage} was not found. Refusing to create ${vm_name}.' >&2 + exit 1 +fi +sudo '${qm_bin}' clone '${template_vmid}' '${vmid}' --name '${vm_name}' --full 1 --storage '${storage}' +sudo '${qm_bin}' set '${vmid}' \ + --agent enabled=1 \ + --bios ovmf \ + --boot 'order=scsi0;net0' \ + --cores '${cores}' \ + --memory '${memory}' \ + --net0 '${net0_config}' \ + --numa 0 \ + --ostype l26 \ + --scsihw virtio-scsi-pci \ + --sockets 1 \ + --vga virtio +if sudo '${qm_bin}' config '${vmid}' | grep -q '^args:'; then + sudo '${qm_bin}' set '${vmid}' --delete args +fi +if [ -n '${cpu_affinity}' ]; then + affinity_output=\"\$(sudo '${qm_bin}' set '${vmid}' --affinity '${cpu_affinity}' 2>&1)\" || { + case \"\$affinity_output\" in + *'Unknown option: affinity'*) + echo 'Pimox qm does not support --affinity; skipping CPU affinity ${cpu_affinity} for VM ${vmid}.' + ;; + *) + printf '%s\n' \"\$affinity_output\" >&2 + exit 1 + ;; + esac + } +fi +sudo '${qm_bin}' set '${vmid}' --onboot 1" + fi + + if pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "sudo '${qm_bin}' status '${vmid}' | grep -q 'status: stopped'"; then + pimox_start_vm_with_cpuset "${pimox_host}" "${pimox_user}" "${pimox_key}" "${qm_bin}" "${vmid}" "${kvm_cpuset}" + elif ! pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "sudo '${qm_bin}' status '${vmid}' | grep -q 'status: running'"; then + echo "Pimox control-plane VM ${vmid} (${vm_name}) is neither stopped nor running after configuration." >&2 + exit 1 + fi + + case "${network_mode}" in + static) + if [[ -z "${node_ip}" ]]; then + echo "LAB_PIMOX_CONTROL_PLANE_NETWORK_MODE=static requires LAB_CLUSTER_CONTROL_PLANE_LAN_IP." >&2 + exit 1 + fi + echo "Configuring Pimox control-plane VM ${vmid} static network ${node_ip} via qemu-guest-agent..." + configure_pimox_worker_forwarding \ + "${pimox_host}" \ + "${pimox_user}" \ + "${pimox_key}" \ + "${node_ip}" + if ! configure_pimox_worker_guest_network \ + "${pimox_host}" \ + "${pimox_user}" \ + "${pimox_key}" \ + "${vmid}" \ + "${qm_bin}" \ + "${node_ip}" \ + "${gateway}" \ + "${dns_servers}" \ + "${interface_name}"; then + pimox_worker_vm_debug "${pimox_host}" "${pimox_user}" "${pimox_key}" "${vmid}" "${qm_bin}" + exit 1 + fi + ;; + dhcp) + ;; + *) + echo "LAB_PIMOX_CONTROL_PLANE_NETWORK_MODE must be 'static' or 'dhcp'." >&2 + exit 1 + ;; + esac + + if ! guest_ip="$(wait_for_pimox_guest_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "${vmid}" "${guest_user}" "${guest_key_path}" "${ip_prefix}" "${timeout_seconds}" "${qm_bin}")"; then + echo "Timed out waiting for control-plane VM ${vmid} (${vm_name}) to report a reachable guest IP." >&2 + exit 1 + fi + + guest_cpu_count="$(pimox_guest_cpu_count "${guest_ip}" "${guest_user}" "${guest_key_path}" 2>/dev/null || true)" + if ! [[ "${guest_cpu_count}" =~ ^[0-9]+$ ]] || ((guest_cpu_count < cores)); then + echo "Pimox control-plane VM ${vmid} (${vm_name}) reports ${guest_cpu_count:-unknown} CPU(s); restarting through the pinned KVM launch path." >&2 + pimox_shutdown_vm_gracefully "${pimox_host}" "${pimox_user}" "${pimox_key}" "${qm_bin}" "${vmid}" + pimox_start_vm_with_cpuset "${pimox_host}" "${pimox_user}" "${pimox_key}" "${qm_bin}" "${vmid}" "${kvm_cpuset}" + if ! guest_ip="$(wait_for_pimox_guest_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "${vmid}" "${guest_user}" "${guest_key_path}" "${ip_prefix}" "${timeout_seconds}" "${qm_bin}")"; then + echo "Timed out waiting for control-plane VM ${vmid} (${vm_name}) after pinned KVM restart." >&2 + exit 1 + fi + guest_cpu_count="$(pimox_guest_cpu_count "${guest_ip}" "${guest_user}" "${guest_key_path}" 2>/dev/null || true)" + fi + if ! [[ "${guest_cpu_count}" =~ ^[0-9]+$ ]] || ((guest_cpu_count < cores)); then + echo "Pimox control-plane VM ${vmid} (${vm_name}) reports ${guest_cpu_count:-unknown} CPU(s); expected at least ${cores}." >&2 + echo "On Pimox 7 ARM this means KVM was not launched on a compatible host CPU set. Check LAB_PIMOX_CONTROL_PLANE_KVM_CPUSET (default: LAB_PIMOX_KVM_CPUSET)." >&2 + exit 1 + fi +} + write_cluster_worker_var_file() { local var_file="$1" local default_platform_labels_json='{"node-role.kubernetes.io/worker":"worker","homelab.dev/node-role":"app","homelab.dev/storage":"ssd","homelab.dev/workload-class":"platform"}' @@ -2837,6 +3059,18 @@ run_pimox_pipeline() { local pimox_kvm_cpuset="${LAB_PIMOX_KVM_CPUSET:-4-7}" local template_kvm_cpuset="${LAB_PIMOX_TEMPLATE_KVM_CPUSET:-${pimox_kvm_cpuset}}" local worker_kvm_cpuset="${LAB_PIMOX_WORKER_KVM_CPUSET:-${pimox_kvm_cpuset}}" + local control_plane_enabled="${LAB_PIMOX_CONTROL_PLANE_ENABLED:-auto}" + local control_plane_vmid="${LAB_PIMOX_CONTROL_PLANE_VMID:-${LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID:-}}" + local control_plane_name="${LAB_PIMOX_CONTROL_PLANE_NAME:-${LAB_CLUSTER_CONTROL_PLANE_NODE_NAME:-pimox-cp-01}}" + local control_plane_ip="${LAB_PIMOX_CONTROL_PLANE_IP:-${LAB_CLUSTER_CONTROL_PLANE_LAN_IP:-}}" + local control_plane_user="${LAB_PIMOX_CONTROL_PLANE_USER:-${LAB_CLUSTER_CONTROL_PLANE_USER:-${LAB_PIMOX_WORKER_USER:-jv}}}" + local control_plane_key_path="${LAB_PIMOX_CONTROL_PLANE_SSH_KEY_PATH:-${LAB_CLUSTER_CONTROL_PLANE_SSH_KEY_PATH:-${LAB_PIMOX_WORKER_SSH_KEY_PATH:-/home/jv/.ssh/id_ed25519}}}" + local control_plane_cores="${LAB_PIMOX_CONTROL_PLANE_CORES:-2}" + local control_plane_memory="${LAB_PIMOX_CONTROL_PLANE_MEMORY:-8192}" + local control_plane_storage="${LAB_PIMOX_CONTROL_PLANE_STORAGE:-${LAB_CLUSTER_PIMOX_STORAGE:-${LAB_PIMOX_WORKER_STORAGE:-${TF_VAR_pimox_worker_storage:-opi5_ssd}}}}" + local control_plane_replace_existing="${LAB_PIMOX_CONTROL_PLANE_REPLACE_EXISTING:-false}" + local control_plane_cpu_affinity="${LAB_PIMOX_CONTROL_PLANE_CPU_AFFINITY:-}" + local control_plane_kvm_cpuset="${LAB_PIMOX_CONTROL_PLANE_KVM_CPUSET:-${pimox_kvm_cpuset}}" local worker_replace_existing="${LAB_PIMOX_WORKER_REPLACE_EXISTING:-false}" local worker_storage="${LAB_PIMOX_WORKER_STORAGE:-${TF_VAR_pimox_worker_storage:-opi5_ssd}}" local worker_user="${LAB_PIMOX_WORKER_USER:-jv}" @@ -2862,24 +3096,31 @@ run_pimox_pipeline() { if [[ "${mode}" == "auto" && -n "${LAB_PIMOX_WORKER_COUNT+x}" ]]; then mode="true" fi + if [[ "${control_plane_enabled}" == "auto" ]]; then + if [[ -n "${control_plane_vmid}" && -n "${control_plane_ip}" ]]; then + control_plane_enabled="true" + else + control_plane_enabled="false" + fi + fi if ! [[ "${worker_count}" =~ ^[0-9]+$ ]]; then echo "LAB_PIMOX_WORKER_COUNT must be a non-negative integer." >&2 exit 1 fi - if ! [[ "${template_cores}" =~ ^[0-9]+$ && "${worker_cores}" =~ ^[0-9]+$ ]]; then - echo "LAB_PIMOX_TEMPLATE_CORES and LAB_PIMOX_WORKER_CORES must be positive integers." >&2 + if ! [[ "${template_cores}" =~ ^[0-9]+$ && "${worker_cores}" =~ ^[0-9]+$ && "${control_plane_cores}" =~ ^[0-9]+$ ]]; then + echo "LAB_PIMOX_TEMPLATE_CORES, LAB_PIMOX_WORKER_CORES, and LAB_PIMOX_CONTROL_PLANE_CORES must be positive integers." >&2 exit 1 fi - if ! [[ "${template_memory}" =~ ^[0-9]+$ && "${worker_memory}" =~ ^[0-9]+$ ]]; then - echo "LAB_PIMOX_TEMPLATE_MEMORY and LAB_PIMOX_WORKER_MEMORY must be positive integer MiB values." >&2 + if ! [[ "${template_memory}" =~ ^[0-9]+$ && "${worker_memory}" =~ ^[0-9]+$ && "${control_plane_memory}" =~ ^[0-9]+$ ]]; then + echo "LAB_PIMOX_TEMPLATE_MEMORY, LAB_PIMOX_WORKER_MEMORY, and LAB_PIMOX_CONTROL_PLANE_MEMORY must be positive integer MiB values." >&2 exit 1 fi - if ((template_cores == 0 || worker_cores == 0 || template_memory == 0 || worker_memory == 0)); then - echo "Pimox template and worker CPU and memory values must be greater than zero." >&2 + if ((template_cores == 0 || worker_cores == 0 || control_plane_cores == 0 || template_memory == 0 || worker_memory == 0 || control_plane_memory == 0)); then + echo "Pimox template, control-plane, and worker CPU and memory values must be greater than zero." >&2 exit 1 fi - for cpuset_name in template_kvm_cpuset worker_kvm_cpuset; do + for cpuset_name in template_kvm_cpuset control_plane_kvm_cpuset worker_kvm_cpuset; do cpuset_value="${!cpuset_name}" if [[ -z "${cpuset_value}" ]]; then continue @@ -2891,6 +3132,8 @@ run_pimox_pipeline() { required_cores="${template_cores}" if [[ "${cpuset_name}" == "worker_kvm_cpuset" ]]; then required_cores="${worker_cores}" + elif [[ "${cpuset_name}" == "control_plane_kvm_cpuset" ]]; then + required_cores="${control_plane_cores}" fi if ((cpuset_cpu_total < required_cores)); then echo "${cpuset_name}='${cpuset_value}' has ${cpuset_cpu_total} CPU(s), but ${required_cores} are required." >&2 @@ -2909,6 +3152,40 @@ run_pimox_pipeline() { echo "LAB_PIMOX_WORKER_REPLACE_EXISTING must be true or false." >&2 exit 1 fi + if ! truthy "${control_plane_enabled}" && ! disabled_value "${control_plane_enabled}"; then + echo "LAB_PIMOX_CONTROL_PLANE_ENABLED must be true, false, or auto." >&2 + exit 1 + fi + if truthy "${control_plane_enabled}"; then + if [[ -z "${control_plane_vmid}" ]]; then + echo "LAB_PIMOX_CONTROL_PLANE_ENABLED=true requires LAB_CLUSTER_PIMOX_CONTROL_PLANE_VMID or LAB_PIMOX_CONTROL_PLANE_VMID." >&2 + exit 1 + fi + if ! [[ "${control_plane_vmid}" =~ ^[0-9]+$ ]]; then + echo "LAB_PIMOX_CONTROL_PLANE_VMID must be an integer." >&2 + exit 1 + fi + if [[ "${control_plane_vmid}" == "${template_vmid}" ]]; then + echo "LAB_PIMOX_CONTROL_PLANE_VMID cannot match LAB_PIMOX_TEMPLATE_VMID (${template_vmid})." >&2 + exit 1 + fi + if ((worker_count > 0 && control_plane_vmid >= worker_base_vmid && control_plane_vmid < worker_base_vmid + worker_count)); then + echo "LAB_PIMOX_CONTROL_PLANE_VMID ${control_plane_vmid} overlaps the worker VMID range ${worker_base_vmid}-$((worker_base_vmid + worker_count - 1))." >&2 + exit 1 + fi + if ! truthy "${control_plane_replace_existing}" && ! disabled_value "${control_plane_replace_existing}"; then + echo "LAB_PIMOX_CONTROL_PLANE_REPLACE_EXISTING must be true or false." >&2 + exit 1 + fi + if ! [[ "${control_plane_storage}" =~ ^[A-Za-z0-9_.:-]+$ ]]; then + echo "LAB_PIMOX_CONTROL_PLANE_STORAGE must be a valid Pimox storage identifier." >&2 + exit 1 + fi + if [[ "${control_plane_storage}" == "local" ]]; then + echo "LAB_PIMOX_CONTROL_PLANE_STORAGE cannot be local; only the Pimox template VM should live on local storage." >&2 + exit 1 + fi + fi if ! [[ "${worker_storage}" =~ ^[A-Za-z0-9_.:-]+$ ]]; then echo "LAB_PIMOX_WORKER_STORAGE must be a valid Pimox storage identifier." >&2 exit 1 @@ -2983,10 +3260,6 @@ fi" 2>&1)" echo "Preparing Pimox provisioning and Debian worker template on ${pimox_host} without changing Orange Pi host networking..." run_tofu_stack "bootstrap/provisioning" - if ((worker_count == 0)); then - return 0 - fi - if ! pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "sudo '${qm_bin}' config '${template_vmid}' | grep -q '^template: 1$'"; then echo "Template VM ${template_vmid} is not available as a Pimox template after provisioning." >&2 exit 1 @@ -2994,6 +3267,34 @@ fi" 2>&1)" pimox_ssh "${pimox_host}" "${pimox_user}" "${pimox_key}" "sudo '${qm_bin}' set '${template_vmid}' --agent enabled=1" echo "Worker VM clones will be created on Pimox storage ${worker_storage}; template VM ${template_vmid} stays on its configured template storage." + if truthy "${control_plane_enabled}"; then + echo "Ensuring Pimox control-plane VM ${control_plane_vmid} (${control_plane_name}) on storage ${control_plane_storage}..." + ensure_pimox_control_plane_node \ + "${pimox_host}" \ + "${pimox_user}" \ + "${pimox_key}" \ + "${template_vmid}" \ + "${bridge}" \ + "${control_plane_vmid}" \ + "${control_plane_name}" \ + "${control_plane_ip}" \ + "${control_plane_user}" \ + "${control_plane_key_path}" \ + "${ip_prefix}" \ + "${timeout_seconds}" \ + "${qm_bin}" \ + "${control_plane_storage}" \ + "${control_plane_replace_existing}" \ + "${control_plane_cores}" \ + "${control_plane_memory}" \ + "${control_plane_cpu_affinity}" \ + "${control_plane_kvm_cpuset}" + fi + + if ((worker_count == 0)); then + return 0 + fi + mkdir -p "${REPO_ROOT}/.lab" : >"${spec_file}" for ((index = 1; index <= worker_count; index++)); do