77 lines
2 KiB
Bash
77 lines
2 KiB
Bash
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
[ -r ./env-fip ] && . ./env-fip
|
|
|
|
if [ -z "${IFACE:-}" ]; then
|
|
echo "ERROR: IFACE not set"
|
|
exit 1
|
|
fi
|
|
|
|
# Support both FLOATING_IPS (space-separated, new) and FLOATING_IP (legacy singular)
|
|
if [ -n "${FLOATING_IPS:-}" ]; then
|
|
IPS=($FLOATING_IPS)
|
|
elif [ -n "${FLOATING_IP:-}" ]; then
|
|
IPS=($FLOATING_IP)
|
|
else
|
|
echo "ERROR: neither FLOATING_IPS nor FLOATING_IP set — check taskserv params"
|
|
exit 1
|
|
fi
|
|
|
|
IFACES_FILE="/etc/network/interfaces.d/60-floating-ip"
|
|
|
|
# Build expected file content for all IPs
|
|
build_content() {
|
|
echo "# Floating IPs managed by provisioning — do not edit manually"
|
|
local idx=0
|
|
for ip in "${IPS[@]}"; do
|
|
echo ""
|
|
echo "auto ${IFACE}:fip${idx}"
|
|
echo "iface ${IFACE}:fip${idx} inet static"
|
|
echo " address ${ip}"
|
|
echo " netmask 255.255.255.255"
|
|
idx=$((idx + 1))
|
|
done
|
|
}
|
|
|
|
EXPECTED=$(build_content)
|
|
|
|
# Check if all IPs are already configured and active
|
|
all_configured=true
|
|
for ip in "${IPS[@]}"; do
|
|
if ! grep -qF "address ${ip}" "$IFACES_FILE" 2>/dev/null; then
|
|
all_configured=false
|
|
break
|
|
fi
|
|
done
|
|
|
|
if $all_configured; then
|
|
echo "All floating IPs already configured in ${IFACES_FILE}"
|
|
# Ensure each is active (re-apply after reboot if missing)
|
|
idx=0
|
|
for ip in "${IPS[@]}"; do
|
|
if ! ip addr show "$IFACE" | grep -qF "${ip}"; then
|
|
ip addr add "${ip}/32" dev "$IFACE" 2>/dev/null || true
|
|
echo "Re-applied ${ip} on ${IFACE}:fip${idx}"
|
|
fi
|
|
idx=$((idx + 1))
|
|
done
|
|
exit 0
|
|
fi
|
|
|
|
echo "$EXPECTED" > "$IFACES_FILE"
|
|
echo "Written ${IFACES_FILE}"
|
|
|
|
# Activate all IPs immediately
|
|
idx=0
|
|
for ip in "${IPS[@]}"; do
|
|
ip addr add "${ip}/32" dev "$IFACE" 2>/dev/null || true
|
|
sleep 0.5
|
|
if ip addr show "$IFACE" | grep -qF "${ip}"; then
|
|
echo "Floating IP ${ip} active on ${IFACE}:fip${idx}"
|
|
else
|
|
echo "ERROR: ${ip} not visible on ${IFACE} after ip addr add"
|
|
exit 1
|
|
fi
|
|
idx=$((idx + 1))
|
|
done
|