Each non-hubris client needs HTTPS auth against gitea for the initial context clone (chicken-and-egg: a PAT stored in SOPS can't be fetched until after the clone exists). Adds a --gitea-token flag that writes credentials to /etc/homelab-context/git-credentials and points git's credential.helper at it, so the clone and all future pulls succeed without prompting. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
294 lines
10 KiB
Bash
Executable File
294 lines
10 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# bootstrap.sh — enroll a new client into the homelab context system.
|
|
#
|
|
# Usage:
|
|
# curl -fsSL https://git.hubris.network/dtoro/Homelab-Docs/raw/main/bootstrap.sh \
|
|
# | sudo bash
|
|
# curl ... | sudo bash -s -- --with-mcp # also wire Claude's .mcp.json
|
|
# curl ... | sudo bash -s -- --dry-run # show what would happen
|
|
# curl ... | sudo bash -s -- --no-secrets # skip age-key issuance
|
|
#
|
|
# Prerequisites the script verifies:
|
|
# - running as root
|
|
# - OS is Linux or macOS
|
|
# - git, age, sops are installed
|
|
# - at least one mesh (netbird OR tailscale) is connected
|
|
# - this host has an `hosts/<hostname>.yaml` entry in the repo (or refuses)
|
|
|
|
set -euo pipefail
|
|
|
|
# -------- defaults --------
|
|
REPO_HTTPS="${HOMELAB_REPO_URL:-https://git.hubris.network/dtoro/Homelab-Docs.git}"
|
|
CLONE_DIR="${HOMELAB_CONTEXT_DIR:-/opt/homelab-context}"
|
|
ISSUANCE_URL_NETBIRD="${HOMELAB_ISSUANCE_NETBIRD:-http://apps.netbird.selfhosted:9820/issue}"
|
|
ISSUANCE_URL_TAILSCALE="${HOMELAB_ISSUANCE_TAILSCALE:-http://apps.ts:9820/issue}"
|
|
MCP_URL="${HOMELAB_MCP_URL:-http://apps.netbird.selfhosted:9810/sse}"
|
|
|
|
WITH_MCP=0
|
|
DRY_RUN=0
|
|
NO_SECRETS=0
|
|
GITEA_TOKEN="${HOMELAB_GITEA_TOKEN:-}"
|
|
GITEA_USER="${HOMELAB_GITEA_USER:-dtoro}"
|
|
|
|
# -------- flag parsing --------
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--with-mcp) WITH_MCP=1; shift ;;
|
|
--dry-run) DRY_RUN=1; shift ;;
|
|
--no-secrets) NO_SECRETS=1; shift ;;
|
|
--gitea-token) GITEA_TOKEN="$2"; shift 2 ;;
|
|
--gitea-user) GITEA_USER="$2"; shift 2 ;;
|
|
--help|-h)
|
|
sed -n '2,11p' "$0" | sed 's/^# *//'
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "unknown flag: $1" >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
|
|
# If a gitea token is provided, write it to the standard credential store
|
|
# BEFORE the clone happens. The HTTPS REPO_HTTPS will then pick it up via
|
|
# git's credential helper.
|
|
configure_gitea_creds() {
|
|
if [ -z "$GITEA_TOKEN" ]; then return 0; fi
|
|
local creds_dir=/etc/homelab-context
|
|
local creds_file=$creds_dir/git-credentials
|
|
mkdir -p "$creds_dir"
|
|
chmod 700 "$creds_dir"
|
|
# Format the credential URL: protocol://user:token@host
|
|
# Extract host from REPO_HTTPS for the credential line.
|
|
local host
|
|
host=$(echo "$REPO_HTTPS" | sed -E 's|^https?://([^/]+).*|\1|')
|
|
printf 'https://%s:%s@%s\n' "$GITEA_USER" "$GITEA_TOKEN" "$host" > "$creds_file"
|
|
chmod 600 "$creds_file"
|
|
# Point git at this store globally for root (so future pulls work too).
|
|
git config --global credential.helper "store --file=$creds_file"
|
|
echo "[bootstrap] wrote gitea credentials to $creds_file"
|
|
}
|
|
|
|
run() {
|
|
if [ "$DRY_RUN" -eq 1 ]; then
|
|
printf '+ %s\n' "$*"
|
|
else
|
|
eval "$*"
|
|
fi
|
|
}
|
|
|
|
# -------- preflight --------
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
echo "bootstrap.sh must run as root (use sudo)." >&2
|
|
exit 1
|
|
fi
|
|
|
|
OS="$(uname -s)"
|
|
case "$OS" in
|
|
Linux|Darwin) ;;
|
|
*) echo "unsupported OS: $OS" >&2; exit 1 ;;
|
|
esac
|
|
|
|
# Resolve hostname; on macOS prefer LocalHostName if set.
|
|
if [ "$OS" = "Darwin" ]; then
|
|
HNAME="$(scutil --get LocalHostName 2>/dev/null || hostname -s)"
|
|
HNAME_ALT="$(hostname -s)"
|
|
if [ "$HNAME" != "$HNAME_ALT" ]; then
|
|
echo "note: scutil LocalHostName=$HNAME differs from hostname=$HNAME_ALT"
|
|
echo " using LocalHostName for inventory lookup."
|
|
fi
|
|
else
|
|
HNAME="$(hostname -s)"
|
|
fi
|
|
echo "[bootstrap] hostname: $HNAME"
|
|
|
|
# Check dependencies.
|
|
missing=()
|
|
for cmd in git; do command -v "$cmd" >/dev/null || missing+=("$cmd"); done
|
|
if [ "$NO_SECRETS" -eq 0 ]; then
|
|
for cmd in age sops; do command -v "$cmd" >/dev/null || missing+=("$cmd"); done
|
|
fi
|
|
if [ "${#missing[@]}" -gt 0 ]; then
|
|
echo "missing required tools: ${missing[*]}" >&2
|
|
if [ "$OS" = "Darwin" ]; then
|
|
echo " brew install ${missing[*]}"
|
|
else
|
|
echo " apt install -y ${missing[*]} (or platform equivalent)"
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
# Mesh check — accept either Netbird OR Tailscale.
|
|
MESH_CONNECTED=""
|
|
if command -v netbird >/dev/null && netbird status 2>/dev/null | grep -q "Management: Connected"; then
|
|
MESH_CONNECTED="netbird"
|
|
elif command -v tailscale >/dev/null && tailscale status >/dev/null 2>&1; then
|
|
MESH_CONNECTED="tailscale"
|
|
fi
|
|
if [ -z "$MESH_CONNECTED" ] && [ "$NO_SECRETS" -eq 0 ]; then
|
|
echo "neither netbird nor tailscale is connected; cannot bootstrap." >&2
|
|
echo "either bring up the mesh first, or pass --no-secrets to skip issuance." >&2
|
|
exit 1
|
|
fi
|
|
echo "[bootstrap] mesh: ${MESH_CONNECTED:-none (skipped, --no-secrets)}"
|
|
|
|
# -------- gitea creds (if provided) --------
|
|
configure_gitea_creds
|
|
|
|
# -------- clone --------
|
|
if [ -d "$CLONE_DIR/.git" ]; then
|
|
existing_remote="$(git -C "$CLONE_DIR" remote get-url origin 2>/dev/null || true)"
|
|
if [ -n "$existing_remote" ] && [ "$existing_remote" != "$REPO_HTTPS" ]; then
|
|
echo "$CLONE_DIR already exists with a different remote ($existing_remote);" >&2
|
|
echo "refusing to overwrite. Move it aside or set HOMELAB_REPO_URL." >&2
|
|
exit 1
|
|
fi
|
|
echo "[bootstrap] clone exists; pulling"
|
|
run "git -C '$CLONE_DIR' pull --ff-only --quiet"
|
|
else
|
|
echo "[bootstrap] cloning to $CLONE_DIR"
|
|
run "git clone --quiet '$REPO_HTTPS' '$CLONE_DIR'"
|
|
fi
|
|
|
|
# -------- identity check --------
|
|
HOST_YAML="$CLONE_DIR/hosts/$HNAME.yaml"
|
|
if [ ! -f "$HOST_YAML" ]; then
|
|
cat >&2 <<EOF
|
|
[bootstrap] no hosts/$HNAME.yaml in the repo.
|
|
This client has not been enrolled yet. From any existing client, run:
|
|
|
|
homelab client add $HNAME
|
|
|
|
then re-run this bootstrap. (If the hostname here is wrong, fix it first:
|
|
'sudo hostnamectl set-hostname <name>' on Linux, or System Preferences →
|
|
Sharing on macOS.)
|
|
EOF
|
|
exit 1
|
|
fi
|
|
|
|
# -------- secrets issuance --------
|
|
if [ "$NO_SECRETS" -eq 0 ]; then
|
|
if [ "$MESH_CONNECTED" = "netbird" ]; then
|
|
URL="$ISSUANCE_URL_NETBIRD"
|
|
else
|
|
URL="$ISSUANCE_URL_TAILSCALE"
|
|
fi
|
|
KEY_FILE=/etc/age/key.txt
|
|
run "mkdir -p /etc/age && chmod 0700 /etc/age"
|
|
|
|
if [ -f "$KEY_FILE" ]; then
|
|
echo "[bootstrap] age key already exists at $KEY_FILE — verifying with issuance"
|
|
fi
|
|
echo "[bootstrap] requesting age key from $URL"
|
|
if [ "$DRY_RUN" -eq 0 ]; then
|
|
# The issuance endpoint identifies us by source mesh IP. No body needed.
|
|
HTTP_CODE=$(curl -sS -o /tmp/homelab-age-key -w '%{http_code}' \
|
|
-X POST -H "Content-Type: application/json" \
|
|
-d "{\"hostname\":\"$HNAME\"}" \
|
|
"$URL") || HTTP_CODE=000
|
|
case "$HTTP_CODE" in
|
|
200)
|
|
mv /tmp/homelab-age-key "$KEY_FILE"
|
|
chmod 0600 "$KEY_FILE"
|
|
echo "[bootstrap] age key installed at $KEY_FILE"
|
|
# Capture the PR snippet (if returned in a sidecar header) so the
|
|
# operator knows the pubkey to add to inventory.yaml. The server
|
|
# includes it in the JSON response when generating a new key.
|
|
if grep -q '"pubkey"' "$KEY_FILE" 2>/dev/null; then
|
|
# Shouldn't happen — server should return raw key, not JSON.
|
|
echo "[bootstrap] unexpected: key file contains JSON, please inspect" >&2
|
|
fi
|
|
;;
|
|
403)
|
|
echo "[bootstrap] issuance returned 403 — caller not recognized" >&2
|
|
echo "is this peer in the Netbird/Tailscale console? is the hostname" >&2
|
|
echo "above ('$HNAME') matching the inventory entry?" >&2
|
|
exit 1
|
|
;;
|
|
*)
|
|
echo "[bootstrap] issuance failed (HTTP $HTTP_CODE)" >&2
|
|
cat /tmp/homelab-age-key >&2 || true
|
|
exit 1
|
|
;;
|
|
esac
|
|
fi
|
|
fi
|
|
|
|
# -------- install sync timer / launchd plist --------
|
|
echo "[bootstrap] installing sync mechanism for $OS"
|
|
run "bash '$CLONE_DIR/scripts/sync/install.sh'"
|
|
|
|
# -------- install homelab CLI --------
|
|
echo "[bootstrap] installing homelab CLI to /usr/local/bin/homelab"
|
|
run "install -m 0755 '$CLONE_DIR/bin/homelab' /usr/local/bin/homelab"
|
|
|
|
# -------- AGENTS.md symlink --------
|
|
case "$OS" in
|
|
Linux)
|
|
AGENTS_LINK=/root/AGENTS.md
|
|
;;
|
|
Darwin)
|
|
AGENTS_LINK=/etc/AGENTS.md
|
|
;;
|
|
esac
|
|
run "ln -sfn '$CLONE_DIR/AGENTS.md' '$AGENTS_LINK'"
|
|
echo "[bootstrap] linked AGENTS.md → $AGENTS_LINK"
|
|
|
|
# -------- MCP wiring --------
|
|
if [ "$WITH_MCP" -eq 1 ]; then
|
|
# Pick the right user's home — when invoked via sudo, SUDO_USER is set.
|
|
if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then
|
|
USER_HOME=$(eval echo "~$SUDO_USER")
|
|
else
|
|
USER_HOME="$HOME"
|
|
fi
|
|
MCP_CONFIG="$USER_HOME/.claude/.mcp.json"
|
|
run "mkdir -p '$USER_HOME/.claude'"
|
|
|
|
# Merge endpoint into existing config (or create new). Use python for the merge
|
|
# because shell JSON juggling is error-prone.
|
|
PY_MERGE=$(cat <<PYEOF
|
|
import json, os, sys
|
|
path = "$MCP_CONFIG"
|
|
url = "$MCP_URL"
|
|
cfg = {}
|
|
if os.path.exists(path):
|
|
with open(path) as f:
|
|
try:
|
|
cfg = json.load(f)
|
|
except Exception:
|
|
cfg = {}
|
|
cfg.setdefault("mcpServers", {})
|
|
cfg["mcpServers"]["homelab"] = {"type": "sse", "url": url}
|
|
with open(path, "w") as f:
|
|
json.dump(cfg, f, indent=2)
|
|
print("[bootstrap] merged MCP server 'homelab' into", path)
|
|
PYEOF
|
|
)
|
|
if [ "$DRY_RUN" -eq 1 ]; then
|
|
echo "+ would merge homelab MCP server into $MCP_CONFIG"
|
|
else
|
|
python3 -c "$PY_MERGE"
|
|
if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then
|
|
chown "$SUDO_USER" "$MCP_CONFIG"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# -------- done --------
|
|
cat <<EOF
|
|
|
|
[bootstrap] done.
|
|
|
|
Identity: $CLONE_DIR/hosts/$HNAME.yaml
|
|
Sync: 5-minute interval ($([ "$OS" = "Darwin" ] && echo launchd || echo systemd))
|
|
Manual pull: homelab sync (or 'systemctl start homelab-context-sync' / 'launchctl kickstart')
|
|
CLI: /usr/local/bin/homelab (try 'homelab whoami')
|
|
AGENTS.md: $AGENTS_LINK
|
|
EOF
|
|
|
|
if [ "$NO_SECRETS" -eq 0 ]; then
|
|
echo "Secrets: sops -d $CLONE_DIR/secrets/<name>.yaml (key at $KEY_FILE)"
|
|
fi
|
|
if [ "$WITH_MCP" -eq 1 ]; then
|
|
echo "MCP: merged into $MCP_CONFIG"
|
|
fi
|