#!/bin/bash # Builds the Linux bridge that carries Spinifex's external datapath over the # secondary OCI VNIC. Discovers that VNIC from the instance metadata service, # because Terraform attaches it after the instance is already RUNNING and its # MAC and address are therefore unknown when user-data is written. set -euo pipefail BRIDGE="${BRIDGE:-br-wan}" MTU="${MTU:-9000}" IMDS="${IMDS:-http://169.254.169.254/opc/v2}" # A file of our own. 50-cloud-init.yaml belongs to cloud-init, so anything added # there is lost when it re-renders. netplan merges every file in the directory, # which is also why strip_cloud_init_secondary below has work to do. NETPLAN_FILE="${NETPLAN_FILE:-/etc/netplan/60-spinifex-wan.yaml}" log() { printf '[spinifex-wan-bridge] %s\n' "$*"; } die() { log "ERROR: $*" exit 1 } # The VNIC holding the default route is the host plane and must be left alone. primary_mac() { local dev dev="$(ip -4 route show default | awk '{for (i=1;i "$NETPLAN_FILE" < /etc/cloud/cloud.cfg.d/99-spinifex-network.cfg chmod 0644 /etc/cloud/cloud.cfg.d/99-spinifex-network.cfg } main() { wait_for_secondary log "bridging $WAN_IFACE into $BRIDGE" strip_cloud_init_secondary local previous="" [ -f "$NETPLAN_FILE" ] && previous="$(cat "$NETPLAN_FILE")" write_netplan "$WAN_IFACE" if [ "$STRIPPED" = 0 ] && [ "$previous" = "$(cat "$NETPLAN_FILE")" ] && ip link show "$BRIDGE" >/dev/null 2>&1; then log "$BRIDGE is already configured and unchanged" return 0 fi netplan apply for _ in $(seq 1 30); do ip -4 addr show "$BRIDGE" 2>/dev/null | grep -q "inet ${WAN_IP}/" && break sleep 2 done ip -br addr show "$BRIDGE" || die "$BRIDGE did not come up" ip -4 addr show "$BRIDGE" | grep -q "inet ${WAN_IP}/" \ || die "$BRIDGE is up but does not hold ${WAN_IP}" log "table 200: $(ip route show table 200 | tr '\n' ';')" } main "$@"