underpost 3.2.10 → 3.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/.vscode/extensions.json +9 -9
  2. package/.vscode/settings.json +12 -1
  3. package/CHANGELOG.md +92 -1
  4. package/CLI-HELP.md +80 -26
  5. package/README.md +6 -10
  6. package/bin/build.js +9 -6
  7. package/bin/build.template.js +187 -0
  8. package/bin/deploy.js +29 -18
  9. package/conf.js +1 -4
  10. package/manifests/cronjobs/dd-cron/dd-cron-backup.yaml +1 -1
  11. package/manifests/cronjobs/dd-cron/dd-cron-dns.yaml +1 -1
  12. package/manifests/deployment/dd-default-development/deployment.yaml +2 -2
  13. package/manifests/deployment/dd-test-development/deployment.yaml +2 -2
  14. package/manifests/lxd/lxd-admin-profile.yaml +12 -3
  15. package/manifests/mongodb-4.4/headless-service.yaml +10 -0
  16. package/manifests/mongodb-4.4/kustomization.yaml +3 -1
  17. package/manifests/mongodb-4.4/mongodb-nodeport.yaml +17 -0
  18. package/manifests/mongodb-4.4/pv-pvc.yaml +10 -14
  19. package/manifests/mongodb-4.4/statefulset.yaml +79 -0
  20. package/manifests/mongodb-4.4/storage-class.yaml +9 -0
  21. package/manifests/valkey/statefulset.yaml +1 -1
  22. package/manifests/valkey/valkey-nodeport.yaml +17 -0
  23. package/package.json +3 -3
  24. package/scripts/ipxe-setup.sh +52 -49
  25. package/scripts/k3s-node-setup.sh +84 -68
  26. package/scripts/lxd-vm-setup.sh +193 -8
  27. package/scripts/maas-nat-firewalld.sh +145 -0
  28. package/src/cli/baremetal.js +115 -93
  29. package/src/cli/cluster.js +548 -221
  30. package/src/cli/deploy.js +131 -166
  31. package/src/cli/fs.js +11 -3
  32. package/src/cli/index.js +75 -17
  33. package/src/cli/lxd.js +1034 -240
  34. package/src/cli/monitor.js +9 -3
  35. package/src/cli/release.js +72 -36
  36. package/src/cli/repository.js +10 -16
  37. package/src/cli/run.js +72 -55
  38. package/src/cli/secrets.js +11 -2
  39. package/src/client/components/core/Auth.js +4 -3
  40. package/src/client/components/core/ClientEvents.js +76 -0
  41. package/src/client/components/core/EventBus.js +4 -0
  42. package/src/client/components/core/Modal.js +82 -41
  43. package/src/db/DataBaseProvider.js +9 -9
  44. package/src/db/mariadb/MariaDB.js +2 -1
  45. package/src/db/mongo/MongoBootstrap.js +592 -522
  46. package/src/db/mongo/MongooseDB.js +19 -15
  47. package/src/index.js +1 -1
  48. package/src/server/conf.js +67 -19
  49. package/src/server/proxy.js +9 -2
  50. package/src/server/start.js +8 -4
  51. package/src/server/valkey.js +2 -0
  52. package/bin/file.js +0 -220
  53. package/bin/vs.js +0 -74
  54. package/bin/zed.js +0 -84
@@ -1,46 +1,70 @@
1
1
  #!/bin/bash
2
- set -e
2
+ set -euo pipefail
3
3
 
4
4
  # ---------------------------------------------------------------------------
5
5
  # Underpost K3s Node Setup
6
- # Usage:
7
- # --control Initialize as K3s control plane node (default)
8
- # --worker Initialize as K3s worker node
9
- # --control-ip=<ip> Control plane IP (required for --worker)
10
- # --token=<token> K3s node token (required for --worker)
11
6
  #
12
- # DESIGN NOTES:
13
- # - This script installs the underpost CLI globally via npm.
14
- # - It then uses the global CLI binary for configuration and K3s setup.
15
- # - Engine source replication (e.g. via --bootstrap-engine from the host)
16
- # is a SEPARATE step. This script does NOT depend on /home/dd/engine
17
- # being pre-pushed. Use --bootstrap-engine after --init-vm completes.
7
+ # This script runs INSIDE an LXD VM. It assumes the host has already mirrored
8
+ # the project source into $ENGINE_ROOT (default /home/dd/engine) via the
9
+ # `underpost lxd [vm-id] --vm-init` flow in `src/cli/lxd.js`. There is no
10
+ # fallback to a globally installed `underpost`: every operational command
11
+ # resolves from the local project so the latest local changes are always what
12
+ # runs in the VM.
13
+ #
14
+ # Usage:
15
+ # --engine-root=<path> Path to the mirrored engine source (default: /home/dd/engine)
16
+ # --control Initialize as K3s control plane node (default)
17
+ # --worker Initialize as K3s worker node
18
+ # --control-ip=<ip> Control plane IP (required for --worker)
19
+ # --token=<token> K3s node token (required for --worker)
18
20
  # ---------------------------------------------------------------------------
19
21
 
20
22
  ROLE="control"
21
23
  CONTROL_IP=""
22
24
  K3S_TOKEN=""
25
+ ENGINE_ROOT="/home/dd/engine"
23
26
 
24
27
  for arg in "$@"; do
25
- case $arg in
26
- --worker) ROLE="worker" ;;
27
- --control) ROLE="control" ;;
28
- --control-ip=*) CONTROL_IP="${arg#*=}" ;;
29
- --token=*) K3S_TOKEN="${arg#*=}" ;;
30
- esac
28
+ case $arg in
29
+ --worker) ROLE="worker" ;;
30
+ --control) ROLE="control" ;;
31
+ --control-ip=*) CONTROL_IP="${arg#*=}" ;;
32
+ --token=*) K3S_TOKEN="${arg#*=}" ;;
33
+ --engine-root=*) ENGINE_ROOT="${arg#*=}" ;;
34
+ esac
31
35
  done
36
+
37
+ # Fail fast if the bootstrap step did not run / left the directory empty.
38
+ # Split into two checks so `ls -A` only runs against a path that exists; this
39
+ # avoids needing an error-swallowing redirect.
40
+ if [ ! -d "$ENGINE_ROOT" ]; then
41
+ echo "ERROR: engine source directory $ENGINE_ROOT does not exist."
42
+ echo "The LXD [vm-id] --vm-init flow must mirror the project here before running this script."
43
+ exit 1
44
+ fi
45
+ if [ -z "$(ls -A "$ENGINE_ROOT")" ]; then
46
+ echo "ERROR: engine source directory $ENGINE_ROOT is empty."
47
+ echo "The LXD [vm-id] --vm-init flow must mirror the project here before running this script."
48
+ exit 1
49
+ fi
50
+
32
51
  # ---------------------------------------------------------------------------
33
- # NVM and Node.js
52
+ # NVM and Node.js — required for `node bin ...` entrypoints
34
53
  # ---------------------------------------------------------------------------
35
- echo "Installing NVM and Node.js v24.10.0..."
54
+ echo "Installing NVM and Node.js v24.15.0..."
36
55
 
37
56
  curl -o- https://cdn.jsdelivr.net/gh/nvm-sh/nvm@v0.40.1/install.sh | bash
38
57
 
39
58
  export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
59
+ # shellcheck disable=SC1090
40
60
  [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
41
61
 
42
62
  nvm install 24.15.0
43
63
  nvm use 24.15.0
64
+ nvm alias default 24.15.0
65
+ ln -sf "$(command -v node)" /usr/local/bin/node
66
+ ln -sf "$(command -v npm)" /usr/local/bin/npm
67
+ ln -sf "$(command -v npx)" /usr/local/bin/npx
44
68
 
45
69
  echo "
46
70
  ██╗░░░██╗███╗░░██╗██████╗░███████╗██████╗░██████╗░░█████╗░░██████╗████████╗
@@ -50,59 +74,51 @@ echo "
50
74
  ╚██████╔╝██║░╚███║██████╔╝███████╗██║░░██║██║░░░░░╚█████╔╝██████╔╝░░░██║░░░
51
75
  ░╚═════╝░╚═╝░░╚══╝╚═════╝░╚══════╝╚═╝░░╚═╝╚═╝░░░░░░╚════╝░╚═════╝░░░░╚═╝░░░
52
76
 
53
- Installing underpost VM node...
77
+ Bringing up underpost VM node from $ENGINE_ROOT (role=$ROLE)
54
78
  "
55
79
 
56
- npm install -g underpost
80
+ cd "$ENGINE_ROOT"
57
81
 
58
- echo "Applying host configuration..."
59
-
60
- # Use the globally installed underpost CLI
61
- # NOTE: --dev is used to match the host dev context
62
- underpost cluster --dev --config
63
-
64
- # Check if /home/dd/engine exists locally for engine-specific operations.
65
- # This directory is populated by the host running --bootstrap-engine.
66
- # If absent, the global underpost CLI handles all operations.
67
- if [ -d /home/dd/engine ]; then
68
- echo "Local engine source found at /home/dd/engine. Using local underpost."
69
- cd /home/dd/engine
70
- npm install
71
- node bin run secret
72
- else
73
- echo "No local engine source at /home/dd/engine. Using global underpost CLI."
74
- echo "Engine source can be replicated later with --bootstrap-engine from the host."
75
- # The globally installed underpost handles secret generation via its own config
76
- underpost secret underpost --init
77
- fi
82
+ # Install JS deps and generate secrets using the local engine entrypoint only.
83
+ npm install
84
+ node bin run secret
78
85
 
79
86
  if [ "$ROLE" = "control" ]; then
80
- echo "Initializing K3s control plane..."
81
- # Use global underpost CLI for K3s setup
82
- underpost cluster --dev --k3s
83
-
84
- echo ""
85
- echo "K3s control plane is ready."
86
- echo "Node token (share with workers to join this cluster):"
87
- sudo cat /var/lib/rancher/k3s/server/node-token
88
- echo ""
89
- echo "Control plane IP addresses:"
90
- ip -4 addr show scope global | grep inet | awk '{print $2}' | cut -d/ -f1
91
-
92
- elif [ "$ROLE" = "worker" ]; then
93
- if [ -z "$CONTROL_IP" ] || [ -z "$K3S_TOKEN" ]; then
94
- echo "ERROR: --control-ip and --token are required for worker role."
95
- echo "Usage: bash k3s-node-setup.sh --worker --control-ip=<ip> --token=<token>"
96
- exit 1
97
- fi
98
-
99
- echo "Joining K3s cluster at https://${CONTROL_IP}:6443..."
100
- curl -sfL https://get.k3s.io | \
87
+ echo "Installing underpost CLI..."
88
+ npm install -g underpost
89
+ underpost --version
90
+ echo "Initializing K3s control plane via local engine..."
91
+ node bin cluster --dev --k3s
92
+ ln -s /usr/local/bin/k3s /bin/k3s
93
+ ln -s /usr/local/bin/kubectl /bin/kubectl
94
+
95
+ echo ""
96
+ echo "K3s control plane is ready."
97
+ echo "Node token (share with workers to join this cluster):"
98
+ sudo cat /var/lib/rancher/k3s/server/node-token
99
+ echo ""
100
+ echo "Control plane IP addresses:"
101
+ ip -4 addr show scope global | grep inet | awk '{print $2}' | cut -d/ -f1
102
+
103
+ elif [ "$ROLE" = "worker" ]; then
104
+ if [ -z "$CONTROL_IP" ] || [ -z "$K3S_TOKEN" ]; then
105
+ echo "ERROR: --control-ip and --token are required for worker role."
106
+ echo "Usage: bash k3s-node-setup.sh --worker --control-ip=<ip> --token=<token>"
107
+ exit 1
108
+ fi
109
+
110
+ # Worker nodes still need the minimal K3s host prep even though they join via
111
+ # the upstream installer rather than `node bin cluster --k3s`.
112
+ echo "Applying minimal K3s host configuration via local engine..."
113
+ node bin cluster --dev --config --k3s
114
+
115
+ echo "Joining K3s cluster at https://${CONTROL_IP}:6443..."
116
+ curl -sfL https://get.k3s.io | \
101
117
  K3S_URL="https://${CONTROL_IP}:6443" \
102
118
  K3S_TOKEN="${K3S_TOKEN}" \
103
119
  sh -s - agent
104
-
105
- echo ""
106
- echo "K3s worker node joined the cluster at https://${CONTROL_IP}:6443 successfully."
107
- sudo systemctl status k3s-agent --no-pager
108
- fi
120
+
121
+ echo ""
122
+ echo "K3s worker joined https://${CONTROL_IP}:6443 successfully."
123
+ sudo systemctl status k3s-agent --no-pager
124
+ fi
@@ -1,23 +1,208 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
1
3
 
2
- echo "Expanding /dev/sda2 and resizing filesystem..."
4
+ # ---------------------------------------------------------------------------
5
+ # LXD VM OS base setup. Runs inside the VM via `lxc exec`. Idempotent.
6
+ # ---------------------------------------------------------------------------
3
7
 
4
- if ! command -v parted &>/dev/null; then
5
- sudo dnf install -y parted
8
+ # lxdbr0 is a plain L2 bridge: LXD runs no DHCP/DNS on it (MAAS owns
9
+ # provisioning), so VMs configure their NIC statically and deterministically.
10
+ # The host pins the gateway (10.250.250.1) on the bridge and provides NAT; no
11
+ # resolver listens there, so DNS must use public/MAAS resolvers, not the gateway.
12
+ # LXD_NET_MODE=dhcp opts back into DHCP-first for VMs on a MAAS-served segment.
13
+ LXD_NET_MODE="${LXD_NET_MODE:-static}"
14
+ LXD_FALLBACK_IPV4_CIDR="${LXD_FALLBACK_IPV4_CIDR:-10.250.250.100/24}"
15
+ LXD_FALLBACK_GATEWAY="${LXD_FALLBACK_GATEWAY:-10.250.250.1}"
16
+ LXD_FALLBACK_DNS="${LXD_FALLBACK_DNS:-1.1.1.1 8.8.8.8}"
17
+ ROCKY_MIRROR_HOST="${ROCKY_MIRROR_HOST:-mirrors.rockylinux.org}"
18
+
19
+ current_ipv4() {
20
+ ip -4 addr show "$IFACE" | awk '/inet /{print $2; exit}'
21
+ }
22
+
23
+ wait_for_ipv4() {
24
+ local current_ip=""
25
+
26
+ for _ in $(seq 1 10); do
27
+ current_ip="$(current_ipv4)"
28
+ if [ -n "$current_ip" ]; then
29
+ echo "$current_ip"
30
+ return 0
31
+ fi
32
+ sleep 1
33
+ done
34
+
35
+ return 1
36
+ }
37
+
38
+ refresh_resolver_state() {
39
+ if command -v resolvectl >/dev/null 2>&1; then
40
+ sudo resolvectl flush-caches || true
41
+ fi
42
+
43
+ if [ -f /run/NetworkManager/resolv.conf ]; then
44
+ sudo ln -sf /run/NetworkManager/resolv.conf /etc/resolv.conf || true
45
+ fi
46
+ }
47
+
48
+ verify_name_resolution() {
49
+ getent ahostsv4 "$ROCKY_MIRROR_HOST" >/dev/null 2>&1
50
+ }
51
+
52
+ retry_dnf() {
53
+ local attempt=1
54
+ local max_attempts=3
55
+
56
+ until "$@"; do
57
+ if [ "$attempt" -ge "$max_attempts" ]; then
58
+ return 1
59
+ fi
60
+
61
+ echo "DNF command failed (attempt $attempt/$max_attempts): $*"
62
+ echo "Refreshing resolver state before retry..."
63
+ refresh_resolver_state
64
+ verify_name_resolution || true
65
+ attempt=$((attempt + 1))
66
+ done
67
+ }
68
+
69
+ # k3s derives the node name from the system hostname. The Rocky image keeps
70
+ # "localhost.localdomain" for every VM, so without this every node would try to
71
+ # register under the same name — the second one is rejected ("Node password
72
+ # rejected, duplicate hostname"). Pin the hostname to the LXD instance name so
73
+ # node names are unique and deterministic (control plane labeling relies on it).
74
+ if [ -n "${LXD_NODE_NAME:-}" ] && [ "$(hostname)" != "$LXD_NODE_NAME" ]; then
75
+ echo "--- Hostname ---"
76
+ echo "Setting hostname to ${LXD_NODE_NAME} (k3s node name)..."
77
+ sudo hostnamectl set-hostname "$LXD_NODE_NAME" 2>/dev/null || sudo hostname "$LXD_NODE_NAME"
78
+ fi
79
+
80
+ echo "--- Network Configuration ---"
81
+
82
+ # 1. Detect primary non-loopback interface
83
+ IFACE=$(ip -o link show up | awk -F': ' '$2!="lo"{print $2; exit}')
84
+ echo "Using network interface: ${IFACE:-none}"
85
+
86
+ if [ -z "$IFACE" ]; then
87
+ echo "CRITICAL ERROR: No network interface detected."
88
+ exit 1
89
+ fi
90
+
91
+ # 2. Force NetworkManager initialization if interface lacks an IP address
92
+ CURRENT_IP="$(current_ipv4 || true)"
93
+
94
+ if command -v nmcli >/dev/null 2>&1 && [ -z "$CURRENT_IP" ]; then
95
+ echo "Inspecting NetworkManager profiles..."
96
+
97
+ # Check if a profile is tracking this device
98
+ NM_CON=$(nmcli -t -f NAME,DEVICE connection show | awk -F: -v iface="$IFACE" '$2==iface{print $1; exit}')
99
+
100
+ if [ -z "$NM_CON" ]; then
101
+ echo "No connection profile matches $IFACE. Forcing generation of profile 'k3s-net'..."
102
+ nmcli connection add type ethernet con-name k3s-net ifname "$IFACE"
103
+ NM_CON="k3s-net"
104
+ fi
105
+
106
+ apply_static_ipv4() {
107
+ echo "Applying static LXD bridge address ${LXD_FALLBACK_IPV4_CIDR} (gw ${LXD_FALLBACK_GATEWAY}, dns ${LXD_FALLBACK_DNS})..."
108
+ nmcli connection modify "$NM_CON" \
109
+ connection.autoconnect yes \
110
+ connection.interface-name "$IFACE" \
111
+ ipv4.method manual \
112
+ ipv4.addresses "$LXD_FALLBACK_IPV4_CIDR" \
113
+ ipv4.gateway "$LXD_FALLBACK_GATEWAY" \
114
+ ipv4.dns "$LXD_FALLBACK_DNS" \
115
+ ipv4.ignore-auto-dns yes \
116
+ ipv6.method ignore
117
+ nmcli connection up "$NM_CON"
118
+ CURRENT_IP="$(wait_for_ipv4 || true)"
119
+ }
120
+
121
+ # Static-first: lxdbr0 has no DHCP, so a DHCP attempt only adds a ~45s
122
+ # activation timeout per boot. DHCP is opt-in for VMs on a MAAS-served
123
+ # segment (LXD_NET_MODE=dhcp), with a static fallback if no lease appears.
124
+ if [ "$LXD_NET_MODE" = "dhcp" ]; then
125
+ echo "Configuring DHCP-first NetworkManager profile '$NM_CON'..."
126
+ nmcli connection modify "$NM_CON" \
127
+ connection.autoconnect yes \
128
+ connection.interface-name "$IFACE" \
129
+ ipv4.method auto \
130
+ ipv4.dhcp-client-id mac \
131
+ ipv4.ignore-auto-dns no \
132
+ ipv6.method ignore
133
+ echo "Bringing network interface up with DHCP..."
134
+ nmcli connection up "$NM_CON" || echo "DHCP activation failed; static fallback will be applied."
135
+ CURRENT_IP="$(wait_for_ipv4 || true)"
136
+ if [ -z "$CURRENT_IP" ]; then
137
+ echo "No DHCP lease on $IFACE."
138
+ apply_static_ipv4
139
+ fi
140
+ else
141
+ echo "Configuring static NetworkManager profile '$NM_CON' for the plain LXD bridge..."
142
+ apply_static_ipv4
143
+ fi
144
+ fi
145
+
146
+ # 3. Give the network interface a short window to settle and lease an IP address
147
+ echo "Waiting for IP address allocation..."
148
+ CURRENT_IP="${CURRENT_IP:-$(wait_for_ipv4 || true)}"
149
+ if [ -n "$CURRENT_IP" ]; then
150
+ echo "Interface $IFACE successfully initialized with IP: $CURRENT_IP"
151
+ fi
152
+
153
+ # 4. Verify DNS and outbound connectivity before proceeding (Fail Closed)
154
+ echo "Verifying internet and DNS connectivity..."
155
+ refresh_resolver_state
156
+ if ! verify_name_resolution; then
157
+ echo "CRITICAL ERROR: DNS lookup failed for $ROCKY_MIRROR_HOST. Diagnostic snapshot:"
158
+ echo "=== /etc/resolv.conf ==="
159
+ cat /etc/resolv.conf || true
160
+ echo "=== Interface State ==="
161
+ ip -4 addr show "$IFACE" || true
162
+ echo "=== Routing Table ==="
163
+ ip route show || true
164
+ echo "=== NetworkManager Status ==="
165
+ nmcli device status || true
166
+ exit 1
167
+ fi
168
+
169
+ if ! timeout 15 curl -fsSL --max-time 10 "https://${ROCKY_MIRROR_HOST}" -o /dev/null; then
170
+ echo "CRITICAL ERROR: Network/DNS remains unreachable. Diagnostic snapshot:"
171
+ echo "=== /etc/resolv.conf ==="
172
+ cat /etc/resolv.conf || true
173
+ echo "=== Interface State ==="
174
+ ip -4 addr show "$IFACE" || true
175
+ echo "=== Routing Table ==="
176
+ ip route show || true
177
+ echo "=== NetworkManager Status ==="
178
+ nmcli device status || true
179
+ exit 1
6
180
  fi
7
181
 
182
+ echo "--- Disk Resizing ---"
183
+ if ! command -v parted >/dev/null 2>&1; then
184
+ sudo dnf install -y parted
185
+ fi
186
+
187
+ set +e
8
188
  sudo parted /dev/sda ---pretend-input-tty <<EOF
9
189
  unit s
10
190
  resizepart 2 100%
11
191
  Yes
12
192
  quit
13
193
  EOF
194
+ set -e
14
195
 
15
196
  sudo resize2fs /dev/sda2
16
- echo "Disk resized."
197
+ echo "Disk partition and filesystem resized successfully."
17
198
 
18
- echo "Installing essential packages..."
19
- sudo dnf install -y tar bzip2 git curl jq epel-release
20
- sudo dnf -y update
199
+ echo "--- Package Installation ---"
200
+ retry_dnf sudo dnf install -y epel-release
201
+ retry_dnf sudo dnf install -y tar bzip2 git curl jq
202
+ retry_dnf sudo dnf -y update
21
203
 
22
- echo "Loading br_netfilter module..."
204
+ echo "--- Kernel Modules for K3s ---"
23
205
  sudo modprobe br_netfilter
206
+ echo "br_netfilter" | sudo tee /etc/modules-load.d/k3s-br_netfilter.conf > /dev/null
207
+
208
+ echo "Setup complete. System is ready for k3s-node-setup.sh"
@@ -0,0 +1,145 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # MAAS NAT + firewall helper
5
+ # - Enables IPv4 forwarding and masquerading for the MAAS/provisioning subnet
6
+ # - Opens the MAAS API on the uplink zone when requested
7
+ # - Opens provisioning and optional NFS services on the zone bound to MAAS_LAN_CIDR
8
+ #
9
+ # Usage examples:
10
+ # sudo MAAS_LAN_CIDR="192.168.1.0/24" PUBLIC_ZONE="public" ./maas-nat-firewalld.sh
11
+ # sudo NFS_MODE="v3" CONFIGURE_NFS_V3_PORTS="true" ./maas-nat-firewalld.sh
12
+
13
+ MAAS_LAN_CIDR="${MAAS_LAN_CIDR:-192.168.1.0/24}"
14
+ PUBLIC_ZONE="${PUBLIC_ZONE:-public}"
15
+ TRUSTED_ZONE="${TRUSTED_ZONE:-trusted}"
16
+ NFS_MODE="${NFS_MODE:-v4}" # v4 or v3
17
+ CONFIGURE_NFS_V3_PORTS="${CONFIGURE_NFS_V3_PORTS:-false}"
18
+ EXPOSE_MAAS_API_PUBLIC="${EXPOSE_MAAS_API_PUBLIC:-true}"
19
+
20
+ # Fixed NFSv3 ports (only used when CONFIGURE_NFS_V3_PORTS=true)
21
+ NFS_MOUNTD_PORT="${NFS_MOUNTD_PORT:-20048}"
22
+ NFS_STATD_PORT="${NFS_STATD_PORT:-32765}"
23
+ # Outgoing port must differ from the listen port; rpc.statd exits 255 if they match.
24
+ NFS_STATD_OUTGOING_PORT="${NFS_STATD_OUTGOING_PORT:-32766}"
25
+ NFS_LOCKD_PORT="${NFS_LOCKD_PORT:-32803}"
26
+ NFS_LOCKD_UDP_PORT="${NFS_LOCKD_UDP_PORT:-${NFS_LOCKD_PORT}}"
27
+ NFS_CONF_DROPIN="/etc/nfs.conf.d/99-maas-nfs-v3-ports.conf"
28
+
29
+ case "${NFS_MODE}" in
30
+ v3|v4) ;;
31
+ *)
32
+ echo "[ERROR] NFS_MODE must be 'v3' or 'v4'." >&2
33
+ exit 1
34
+ ;;
35
+ esac
36
+
37
+ # Ensure the kernel NFS server is enabled and actually running. `exportfs -rav` only populates the
38
+ # export table; it does not start nfsd, and `systemctl try-restart` is a no-op when the unit is
39
+ # stopped. Without this, clients hang on mount because nothing listens on 2049/mountd.
40
+ ensure_nfs_server_running() {
41
+ if [[ "${NFS_MODE}" == "v3" ]]; then
42
+ # NFSv3 depends on the portmapper and the status monitor.
43
+ sudo systemctl enable --now rpcbind 2>/dev/null || true
44
+ sudo systemctl enable --now rpc-statd 2>/dev/null || true
45
+ fi
46
+
47
+ local unit=""
48
+ for candidate in nfs-server nfs-kernel-server; do
49
+ if systemctl cat "${candidate}.service" >/dev/null 2>&1; then
50
+ unit="${candidate}"
51
+ break
52
+ fi
53
+ done
54
+ if [[ -z "${unit}" ]]; then
55
+ echo "[ERROR] No NFS server unit found (nfs-server/nfs-kernel-server). Install nfs-utils or nfs-kernel-server." >&2
56
+ return 1
57
+ fi
58
+
59
+ echo "[INFO] Enabling and (re)starting ${unit} to apply exports and fixed ports..."
60
+ sudo systemctl enable "${unit}" 2>/dev/null || true
61
+ sudo systemctl restart "${unit}"
62
+ }
63
+
64
+ echo "[INFO] Enabling firewalld..."
65
+ sudo systemctl enable --now firewalld
66
+
67
+ echo "[INFO] Enabling kernel forwarding..."
68
+ sudo tee /etc/sysctl.d/99-maas-nat.conf >/dev/null <<'EOF'
69
+ net.ipv4.ip_forward = 1
70
+ EOF
71
+
72
+ sudo sysctl --system >/dev/null
73
+
74
+ echo "[INFO] Allowing NAT for the MAAS LAN..."
75
+ sudo firewall-cmd --permanent --zone="${PUBLIC_ZONE}" --add-masquerade
76
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-source="${MAAS_LAN_CIDR}"
77
+
78
+ echo "[INFO] Opening MAAS services on the MAAS LAN zone..."
79
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-port=5240/tcp
80
+ for service in dns dhcp tftp; do
81
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-service="${service}"
82
+ done
83
+
84
+ if [[ "${EXPOSE_MAAS_API_PUBLIC}" == "true" ]]; then
85
+ echo "[INFO] Opening MAAS API on the uplink zone..."
86
+ sudo firewall-cmd --permanent --zone="${PUBLIC_ZONE}" --add-port=5240/tcp
87
+ fi
88
+
89
+ echo "[INFO] Opening NFS ports on the MAAS LAN zone..."
90
+
91
+ if [[ "${NFS_MODE}" == "v4" ]]; then
92
+ # NFSv4 normally needs only TCP 2049.
93
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-port=2049/tcp
94
+ else
95
+ # NFSv3 side-band daemons need fixed ports for deterministic firewall rules.
96
+ if [[ "${CONFIGURE_NFS_V3_PORTS}" != "true" ]]; then
97
+ echo "[ERROR] NFSv3 requires CONFIGURE_NFS_V3_PORTS=true for consistent firewalld rules." >&2
98
+ echo "[ERROR] Re-run with CONFIGURE_NFS_V3_PORTS=true or switch to NFS_MODE=v4." >&2
99
+ exit 1
100
+ fi
101
+
102
+ echo "[INFO] Writing fixed NFSv3 port configuration..."
103
+ sudo install -d -m 0755 /etc/nfs.conf.d
104
+ sudo tee "${NFS_CONF_DROPIN}" >/dev/null <<EOF
105
+ [nfsd]
106
+ vers3=y
107
+
108
+ [mountd]
109
+ port=${NFS_MOUNTD_PORT}
110
+
111
+ [statd]
112
+ port=${NFS_STATD_PORT}
113
+ outgoing-port=${NFS_STATD_OUTGOING_PORT}
114
+
115
+ [lockd]
116
+ port=${NFS_LOCKD_PORT}
117
+ udp-port=${NFS_LOCKD_UDP_PORT}
118
+ EOF
119
+
120
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-service=rpc-bind
121
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-port=2049/tcp
122
+ for proto in tcp udp; do
123
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-port="${NFS_MOUNTD_PORT}/${proto}"
124
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-port="${NFS_STATD_PORT}/${proto}"
125
+ # Outgoing port is the fixed source for SM_NOTIFY; peers need to reach it.
126
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-port="${NFS_STATD_OUTGOING_PORT}/${proto}"
127
+ done
128
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-port="${NFS_LOCKD_PORT}/tcp"
129
+ sudo firewall-cmd --permanent --zone="${TRUSTED_ZONE}" --add-port="${NFS_LOCKD_UDP_PORT}/udp"
130
+ fi
131
+
132
+ ensure_nfs_server_running
133
+
134
+ echo "[INFO] Reloading firewall..."
135
+ sudo firewall-cmd --reload
136
+
137
+ echo
138
+ echo "[INFO] Firewall status:"
139
+ sudo firewall-cmd --zone="${PUBLIC_ZONE}" --list-all
140
+ echo
141
+ echo "[INFO] MAAS LAN zone (${TRUSTED_ZONE}):"
142
+ sudo firewall-cmd --zone="${TRUSTED_ZONE}" --list-all
143
+
144
+ echo
145
+ echo "[INFO] MAAS NAT + firewall configuration completed."