#!/usr/bin/env bash # name: estate-bootstrap # summary: First-boot tailnet join, driven by a per-VM credential # Runs once at first boot (from cloud-init vendor data). Carries no secrets: it # resolves a credential injected per-VM, then hands off to the published # installer at install.xaritomi.xyz/tailscale. # # Delegating rather than reimplementing means Tailscale gets INSTALLED if the # base image lacks it (zerene's template did, midgard's did not), and that # credential handling lives in exactly one place. Improvements to the installer # reach every future VM without rebuilding a template. set -uo pipefail INSTALLER="${TS_INSTALLER:-https://install.xaritomi.xyz/tailscale}" log() { printf '[estate-bootstrap] %s\n' "$*"; } if command -v tailscale >/dev/null 2>&1 && tailscale status >/dev/null 2>&1; then log "already on the tailnet"; exit 0 fi key=""; src="" if [ -r /etc/op-service-account.env ]; then set -a; . /etc/op-service-account.env; set +a if command -v op >/dev/null 2>&1; then log "reading the tailnet key from 1Password" key=$(op read "${TS_AUTHKEY_OP:-op://mimir/tailscale/authkey}" /dev/null || true) [ -n "$key" ] && src=op fi fi if [ -z "$key" ] && [ -r /etc/ts-authkey ]; then log "using the injected auth key" key=$(tr -d '\r\n' < /etc/ts-authkey); src=file fi # a placeholder is not a credential case "$key" in REPLACE_ME*|"") [ -n "$key" ] && log "credential is still a placeholder" ;; esac [ "${key:-}" = "REPLACE_ME" ] && key="" if [ -z "$key" ]; then log "no tailnet credential present; skipping join (run 'tailscale up' by hand)" exit 0 fi log "installing tailscale and joining via ${INSTALLER}" if curl -fsSL "$INSTALLER" | TS_AUTHKEY="$key" TS_HOSTNAME="$(hostname)" bash; then log "joined the tailnet as $(hostname)" [ "$src" = file ] && shred -u /etc/ts-authkey 2>/dev/null exit 0 fi # Deliberately keep the key on failure: shredding it would make a retry # impossible, and a transient network fault is the likeliest cause. log "tailnet join FAILED; the credential has been left in place for a retry" exit 1