Add adaptive router VM/interface scaling and local delivery integrity tests

Terraform now provisions router_count IaaS Router VMs (default 2, no
longer hardcoded to router1/router2), each with 1 public +
private_interface_count isolated private interfaces (no shared LAN or
VRRP between routers). Both counts scale via Terraform variables and
TF_VAR_* environment variables. The post-install script became a
Terraform template that matches interfaces to their expected subnet by
CIDR instead of a fragile "first private IP" heuristic.

Added an offline pytest suite (terraform/tests/) that checks the
delivery's internal consistency and runs real terraform init/validate
against the actual vkcs provider schema via a project-local filesystem
mirror (provider binary fetched from its GitHub releases, bypassing the
region-blocked HashiCorp registry) - no cloud credentials or API calls
involved. terraform/versions.tf now declares the previously-missing
required_providers block.

Ansible (inventory.ini, base/frr_router/keepalived roles) still assumes
the old 2-router/2-NIC/VRRP topology and is not yet adapted - documented
as a follow-up, not addressed here.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hXR2ftXZZhJ4Y3XuSoR8r
This commit is contained in:
ayurishchevandClaude Sonnet 5 committed 2026-09-03 16:03:12 +03:00
1 parent e711faca42
commit 5979a9a58b
20 files changed
+1181 -139

No files matched your search

@@ -69,15 +69,15 @@ cidr_to_netmask() {
for ((i=0; i<4; i++)); do
if [ "$i" -lt "$full_octets" ]; then
netmask="${netmask}255"
netmask="$${netmask}255"
elif [ "$i" -eq "$full_octets" ] && [ "$remaining_bits" -gt 0 ]; then
netmask="${netmask}${partial_octet}"
netmask="$${netmask}$${partial_octet}"
else
netmask="${netmask}0"
netmask="$${netmask}0"
fi
if [ "$i" -lt 3 ]; then
netmask="${netmask}."
netmask="$${netmask}."
fi
done
@@ -91,8 +91,8 @@ netmask_to_cidr() {
# Use -a for array, not -o
IFS='.' read -ra octets <<< "$netmask"
for octet in "${octets[@]}"; do
for octet in "$${octets[@]}"; do
case $octet in
255) cidr=$((cidr + 8)) ;;
254) cidr=$((cidr + 7)) ;;
@@ -207,12 +207,48 @@ has_private_ip() {
return 1 # No private IP
}
# Identify LAN and WAN interfaces with enhanced logic
LAN_IFACE=""
LAN_MAC=""
LAN_IFACE_IPv4=""
LAN_NETMASK=""
LAN_CIDR=""
# Convert a dotted IPv4 address to a 32-bit integer
ip_to_int() {
local ip="$1"
local a b c d
IFS='.' read -r a b c d <<< "$ip"
echo $(( (a << 24) | (b << 16) | (c << 8) | d ))
}
# Check whether IPv4 address $1 falls inside CIDR network $2 (e.g. 10.90.0.8/29)
ip_in_cidr() {
local ip="$1"
local cidr="$2"
local net="$${cidr%/*}"
local prefix="$${cidr#*/}"
local ip_int net_int mask
ip_int=$(ip_to_int "$ip")
net_int=$(ip_to_int "$net")
if [ "$prefix" -eq 0 ]; then
mask=0
else
mask=$(( (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF ))
fi
[ $(( ip_int & mask )) -eq $(( net_int & mask )) ]
}
# Expected private interfaces, injected by Terraform: one CIDR + target
# ethX name per entry. Populated below via a template directive.
PRIV_TARGETS=()
%{ for pi in private_interfaces ~}
PRIV_TARGETS+=("${pi.cidr}|${pi.name}")
%{ endfor ~}
# Identify private and WAN interfaces
declare -A PRIV_IFACE # target name -> interface
declare -A PRIV_MAC # target name -> MAC
declare -A PRIV_IPv4 # target name -> IP
declare -A PRIV_NETMASK # target name -> netmask
declare -A PRIV_CIDR # target name -> CIDR prefix length
MATCHED_PRIV_IFACES=""
WAN_IFACE=""
WAN_MAC=""
WAN_IFACE_IPv4=""
@@ -220,33 +256,45 @@ WAN_NETMASK=""
WAN_CIDR=""
WAN_GW_IPv4=""
# First pass: Look for interfaces with private IPs (LAN candidates)
# First pass: match each interface with a private IP against the expected
# private target CIDRs (each router interface lives in its own unique
# micro-subnet, so matching by CIDR membership is unambiguous).
for iface in $(ls /sys/class/net/ | grep -v lo); do
if has_private_ip "$iface"; then
if [ -z "$LAN_IFACE" ]; then
LAN_IFACE="$iface"
LAN_MAC=$(cat "/sys/class/net/$iface/address" 2>/dev/null)
# Get IP, netmask, and CIDR
lan_ip_netmask_cidr=$(get_private_ip_netmask_cidr "$iface")
if [ -n "$lan_ip_netmask_cidr" ]; then
LAN_IFACE_IPv4=$(echo "$lan_ip_netmask_cidr" | cut -d',' -f1)
LAN_NETMASK=$(echo "$lan_ip_netmask_cidr" | cut -d',' -f2)
LAN_CIDR=$(echo "$lan_ip_netmask_cidr" | cut -d',' -f3)
priv_ip_netmask_cidr=$(get_private_ip_netmask_cidr "$iface")
[ -z "$priv_ip_netmask_cidr" ] && continue
priv_ip=$(echo "$priv_ip_netmask_cidr" | cut -d',' -f1)
for target in "$${PRIV_TARGETS[@]}"; do
target_cidr="$${target%%|*}"
target_name="$${target##*|}"
if ip_in_cidr "$priv_ip" "$target_cidr"; then
if [ -n "$${PRIV_IFACE[$target_name]:-}" ]; then
log "Multiple interfaces matched target $target_name ($target_cidr): $${PRIV_IFACE[$target_name]} and $iface"
continue
fi
PRIV_IFACE[$target_name]="$iface"
PRIV_MAC[$target_name]=$(cat "/sys/class/net/$iface/address" 2>/dev/null)
PRIV_IPv4[$target_name]=$priv_ip
PRIV_NETMASK[$target_name]=$(echo "$priv_ip_netmask_cidr" | cut -d',' -f2)
PRIV_CIDR[$target_name]=$(echo "$priv_ip_netmask_cidr" | cut -d',' -f3)
MATCHED_PRIV_IFACES="$MATCHED_PRIV_IFACES $iface"
log "Identified private interface $target_name: $iface (MAC: $${PRIV_MAC[$target_name]}) with IP: $priv_ip, CIDR: /$${PRIV_CIDR[$target_name]} (target $target_cidr)"
fi
log "Identified LAN interface: $iface (MAC: $LAN_MAC) with private IP: $LAN_IFACE_IPv4, Netmask: $LAN_NETMASK, CIDR: /$LAN_CIDR"
else
log "Multiple LAN interface candidates found: $LAN_IFACE and $iface"
fi
done
fi
done
# Second pass: Look for WAN interface
for iface in $(ls /sys/class/net/ | grep -v lo); do
# Skip if this is already identified as LAN
[ "$iface" = "$LAN_IFACE" ] && continue
# Skip interfaces already matched to a private target
case " $MATCHED_PRIV_IFACES " in
*" $iface "*) continue ;;
esac
ip_addrs=$(ip addr show "$iface" 2>/dev/null | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}')
if [ -n "$ip_addrs" ]; then
@@ -286,33 +334,38 @@ for iface in $(ls /sys/class/net/ | grep -v lo); do
fi
done
# If no WAN found but we have LAN, pick first non-LAN interface
if [ -z "$WAN_IFACE" ] && [ -n "$LAN_IFACE" ]; then
# If no WAN found but we matched at least one private interface, pick the
# first still-unmatched interface as a fallback WAN candidate
if [ -z "$WAN_IFACE" ] && [ -n "$MATCHED_PRIV_IFACES" ]; then
for iface in $(ls /sys/class/net/ | grep -v lo); do
if [ "$iface" != "$LAN_IFACE" ]; then
WAN_IFACE="$iface"
WAN_MAC=$(cat "/sys/class/net/$iface/address" 2>/dev/null)
WAN_GW_IPv4=$(get_interface_gateway "$iface")
log "Assumed WAN interface: $iface (MAC: $WAN_MAC) - default selection, Gateway: $WAN_GW_IPv4"
break
fi
case " $MATCHED_PRIV_IFACES " in
*" $iface "*) continue ;;
esac
WAN_IFACE="$iface"
WAN_MAC=$(cat "/sys/class/net/$iface/address" 2>/dev/null)
WAN_GW_IPv4=$(get_interface_gateway "$iface")
log "Assumed WAN interface: $iface (MAC: $WAN_MAC) - default selection, Gateway: $WAN_GW_IPv4"
break
done
fi
# Final assignment and logging
log "Final interface assignment:"
log " LAN Interface: $LAN_IFACE (MAC: $LAN_MAC)"
if [ -n "$LAN_IFACE_IPv4" ]; then
log " LAN IPv4: $LAN_IFACE_IPv4"
log " LAN Netmask: $LAN_NETMASK"
log " LAN CIDR: /$LAN_CIDR"
else
log " LAN IPv4: Not assigned"
LAN_IFACE_IPv4="" # Ensure it's empty if no IP found
LAN_NETMASK="" # Ensure netmask is also empty
LAN_CIDR="" # Ensure CIDR is also empty
fi
MISSING_PRIV_TARGETS=""
for target in "$${PRIV_TARGETS[@]}"; do
target_cidr="$${target%%|*}"
target_name="$${target##*|}"
if [ -n "$${PRIV_IFACE[$target_name]:-}" ]; then
log " $target_name Interface: $${PRIV_IFACE[$target_name]} (MAC: $${PRIV_MAC[$target_name]})"
log " $target_name IPv4: $${PRIV_IPv4[$target_name]}"
log " $target_name CIDR: /$${PRIV_CIDR[$target_name]} (expected network: $target_cidr)"
else
log " $target_name Interface: Not detected (expected network: $target_cidr)"
MISSING_PRIV_TARGETS="$MISSING_PRIV_TARGETS $target_name"
fi
done
if [ -n "$WAN_IFACE" ]; then
# Only set WAN IP, Netmask, CIDR and Gateway if WAN interface is detected
@@ -347,16 +400,14 @@ else
fi
# Validate that we have the required information before proceeding
if [ -z "$WAN_MAC" ] || [ -z "$WAN_IFACE_IPv4" ] || [ -z "$WAN_CIDR" ] || [ -z "$WAN_GW_IPv4" ] || [ -z "$LAN_MAC" ] || [ -z "$LAN_IFACE_IPv4" ] || [ -z "$LAN_CIDR" ]; then
if [ -z "$WAN_MAC" ] || [ -z "$WAN_IFACE_IPv4" ] || [ -z "$WAN_CIDR" ] || [ -z "$WAN_GW_IPv4" ] || [ -n "$MISSING_PRIV_TARGETS" ]; then
log "ERROR: Required network information is missing. Cannot proceed with network configuration."
log "Missing information:"
[ -z "$WAN_MAC" ] && log " - WAN MAC address"
[ -z "$WAN_IFACE_IPv4" ] && log " - WAN IP address"
[ -z "$WAN_CIDR" ] && log " - WAN CIDR"
[ -z "$WAN_GW_IPv4" ] && log " - WAN Gateway"
[ -z "$LAN_MAC" ] && log " - LAN MAC address"
[ -z "$LAN_IFACE_IPv4" ] && log " - LAN IP address"
[ -z "$LAN_CIDR" ] && log " - LAN CIDR"
[ -n "$MISSING_PRIV_TARGETS" ] && log " - Unmatched private interfaces:$MISSING_PRIV_TARGETS"
exit 1
fi
@@ -375,7 +426,9 @@ EOF
log "Creating network config file"
# Creating netplan config with proper validation
# Creating netplan config with proper validation: WAN interface first, then
# one ethernets entry per matched private interface (no default route on
# private interfaces - each is an isolated point-to-point micro-subnet).
cat > /etc/netplan/50-cloud-init.yaml << EOF
network:
version: 2
@@ -392,14 +445,24 @@ network:
via: $WAN_GW_IPv4
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
lan-iface:
EOF
for target in "$${PRIV_TARGETS[@]}"; do
target_name="$${target##*|}"
priv_mac="$${PRIV_MAC[$target_name]}"
priv_ip4="$${PRIV_IPv4[$target_name]}"
priv_cidr="$${PRIV_CIDR[$target_name]}"
cat >> /etc/netplan/50-cloud-init.yaml << EOF
$target_name-iface:
match:
macaddress: "$LAN_MAC"
set-name: eth1
macaddress: "$priv_mac"
set-name: $target_name
dhcp4: false
addresses:
- $LAN_IFACE_IPv4/$LAN_CIDR
- $priv_ip4/$priv_cidr
EOF
done
log "Network config file has been created"