#!/usr/bin/env bash # ╔══════════════════════════════════════════════════════════════════╗ # ║ arch-dev roaming :: add_new_machine ║ # ║ Onboard a new host into the roaming fleet: ║ # ║ 1. push your SSH key (ssh-copy-id) ║ # ║ 2. register it in ~/.config/roaming/hosts ║ # ║ 3. optionally test the connection + mount ║ # ╚══════════════════════════════════════════════════════════════════╝ set -euo pipefail ROAMING_DIR="${ROAMING_DIR:-$HOME/.config/roaming}" ROAMING_HOSTS="${ROAMING_HOSTS:-$ROAMING_DIR/hosts}" ROAMING_DEFAULT_USER="${ROAMING_DEFAULT_USER:-$USER}" usage() { cat < [user] [mount] name short alias for the host (e.g. mail) fqdn reachable hostname (e.g. mail.tailnet.ts) user ssh user (default: $ROAMING_DEFAULT_USER) mount remote path to expose via sshfs (optional, e.g. / or /srv) Examples: add_new_machine.sh mail mail.tailnet.ts root / add_new_machine.sh proxy proxy.tailnet.ts admin /etc/caddy add_new_machine.sh dev dev-server.tailnet.ts wayne EOF exit 1 } [[ $# -lt 2 ]] && usage NAME="$1" FQDN="$2" USER_ARG="${3:-$ROAMING_DEFAULT_USER}" MOUNT="${4:-}" mkdir -p "$ROAMING_DIR" touch "$ROAMING_HOSTS" # ── Guard: already registered? ──────────────────────────────────────────────── if grep -qE "^\s*${NAME}\s" "$ROAMING_HOSTS" 2>/dev/null; then echo "✗ '$NAME' is already in $ROAMING_HOSTS" echo " edit the file directly to change it" exit 1 fi # ── Step 1: SSH key ─────────────────────────────────────────────────────────── echo "── Step 1: pushing SSH key to ${USER_ARG}@${FQDN} ──" echo " (you'll be prompted for the remote password once)" if ssh-copy-id "${USER_ARG}@${FQDN}"; then echo " ✓ key installed" else echo " ✗ ssh-copy-id failed — aborting, nothing registered" exit 1 fi # ── Step 2: register ────────────────────────────────────────────────────────── echo "── Step 2: registering in $ROAMING_HOSTS ──" printf "%-10s %-26s %-8s %s\n" "$NAME" "$FQDN" "$USER_ARG" "$MOUNT" >> "$ROAMING_HOSTS" echo " ✓ registered" # ── Step 3: verify ──────────────────────────────────────────────────────────── echo "── Step 3: verifying connection ──" if ssh -o BatchMode=yes -o ConnectTimeout=8 "${USER_ARG}@${FQDN}" 'echo ok' >/dev/null 2>&1; then echo " ✓ key-based SSH works" else echo " ⚠ key-based SSH test failed (host may still be fine — check manually)" fi echo "" echo "✓ Done. In a new shell (or after 'roaming-reload'):" echo " $NAME # interactive shell" echo " $NAME sudo systemctl status # run a command" [[ -n "$MOUNT" ]] && echo " mount_host $NAME # sshfs ${MOUNT} → /workspace/$NAME"