engram-boot/bootstrap.sh

272 lines
7.2 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# engram // bootstrap.sh
#
# The second half of a fresh machine: installs the tools, unlocks the
# encrypted secrets bundle, puts the SSH keys where they belong, imports
# the GPG key and clones the private engram repo. Dotfiles and Ansible
# come later - this script only gets the machine online.
#
# Usage (on the fresh system, as your user):
#
# wget -O bootstrap.sh https://gitea.nevim42.cz/nevim42/engram-boot/raw/branch/main/bootstrap.sh
# less bootstrap.sh
# bash bootstrap.sh
#
set -euo pipefail
trap 'printf "\n[x] bootstrap stopped on line %s - check the output above\n" "$LINENO" >&2' ERR
# ---------------------------------------------------------------------------
# Output helpers - same neon as install.sh, smaller banner.
# ---------------------------------------------------------------------------
if [[ -t 1 ]]; then
C_RESET=$'\033[0m'
C_CYAN=$'\033[36m'
C_YELLOW=$'\033[33m'
C_RED=$'\033[31m'
C_DIM=$'\033[2m'
else
C_RESET=""
C_CYAN=""
C_YELLOW=""
C_RED=""
C_DIM=""
fi
info() { printf '%s[+]%s %s\n' "$C_CYAN" "$C_RESET" "$*"; }
warn() { printf '%s[!]%s %s\n' "$C_YELLOW" "$C_RESET" "$*"; }
die() { printf '%s[x]%s %s\n' "$C_RED" "$C_RESET" "$*" >&2; exit 1; }
step() { printf '\n%s==>%s %s\n' "$C_CYAN" "$C_RESET" "$*"; }
banner() {
cat <<BANNER
${C_CYAN}
── engram // bootstrap ──────────────────────────────────────
${C_RESET}${C_DIM} getting this machine online: keys first, dotfiles later
${C_RESET}
BANNER
}
# ---------------------------------------------------------------------------
# The recipe - same spirit as install.sh.
# ---------------------------------------------------------------------------
readonly RAW_BASE="https://gitea.nevim42.cz/nevim42/engram-boot/raw/branch/main"
readonly ENGRAM_REPO="git@gitea.nevim42.cz:nevim42/engram.git"
readonly ENGRAM_DIR="$HOME/Repositories/engram"
readonly KNOWN_HOST="gitea.nevim42.cz"
readonly DEPS=(age git gnupg)
WORK=""
CHOSEN_KEYS=()
cleanup() {
if [[ -n $WORK && -d $WORK ]]; then
rm -rf "$WORK"
fi
}
trap cleanup EXIT
# ---------------------------------------------------------------------------
# Preflight - user, network.
# ---------------------------------------------------------------------------
preflight() {
step "Preflight checks"
if [[ $EUID -eq 0 ]]; then
die "Run me as your user, not root - sudo is used where needed."
fi
if ! ping -c1 -W3 "$KNOWN_HOST" >/dev/null 2>&1; then
die "No network (cannot reach $KNOWN_HOST)."
fi
info "User, network - all good."
}
# ---------------------------------------------------------------------------
# Tools
# ---------------------------------------------------------------------------
install_deps() {
step "Installing tools (${DEPS[*]})"
sudo pacman -S --needed "${DEPS[@]}"
}
# ---------------------------------------------------------------------------
# Fetch and unlock the secrets bundle.
# ---------------------------------------------------------------------------
fetch() {
if command -v wget >/dev/null; then
wget -qO- "$1"
else
curl -fsSL "$1"
fi
}
unlock_secrets() {
step "Unlocking the secrets bundle"
WORK="$(mktemp -d)"
mkdir -p "$WORK/plain"
fetch "$RAW_BASE/secrets/secrets.age" > "$WORK/secrets.age" \
|| die "Could not fetch secrets.age from $RAW_BASE"
info "age will ask for the bundle passphrase"
if ! age -d "$WORK/secrets.age" | tar xz -C "$WORK/plain"; then
die "Unlock failed - wrong passphrase, or a broken bundle?"
fi
}
# ---------------------------------------------------------------------------
# SSH keys - which ones live here is your call.
# ---------------------------------------------------------------------------
install_keys() {
step "SSH keys"
mkdir -p "$HOME/.ssh"
chmod 700 "$HOME/.ssh"
local available=()
mapfile -t available < <(find "$WORK/plain/ssh" -maxdepth 1 -type f \
! -name '*.pub' ! -name 'known_hosts*' -printf '%f\n' | sort)
if (( ${#available[@]} == 0 )); then
die "No SSH keys found in the bundle."
fi
info "Available keys: ${available[*]}"
local answer
read -rp " Install which keys? (space separated, empty = none): " answer
read -ra CHOSEN_KEYS <<< "$answer"
local name
for name in "${CHOSEN_KEYS[@]}"; do
if [[ ! -f "$WORK/plain/ssh/$name" ]]; then
die "Unknown key: $name"
fi
if [[ -e "$HOME/.ssh/$name" ]]; then
warn "$name already exists in ~/.ssh - skipping"
continue
fi
install -m 600 "$WORK/plain/ssh/$name" "$HOME/.ssh/$name"
if [[ -f "$WORK/plain/ssh/$name.pub" ]]; then
install -m 644 "$WORK/plain/ssh/$name.pub" "$HOME/.ssh/$name.pub"
fi
info "Installed $HOME/.ssh/$name"
done
}
# ---------------------------------------------------------------------------
# Pinned gitea host key + GPG import.
# ---------------------------------------------------------------------------
pin_known_host() {
step "Pinning the $KNOWN_HOST host key"
local pin="$WORK/plain/ssh/known_hosts.gitea"
if [[ ! -s $pin ]]; then
warn "No pinned host key in the bundle - SSH may ask on first connect."
return
fi
touch "$HOME/.ssh/known_hosts"
if grep -q "$KNOWN_HOST" "$HOME/.ssh/known_hosts"; then
info "Already pinned."
else
cat "$pin" >> "$HOME/.ssh/known_hosts"
info "Pinned."
fi
}
import_gpg() {
step "GPG key"
local found=0 f
for f in "$WORK"/plain/gpg/*.asc; do
[[ -e $f ]] || continue
found=1
if [[ -s $f ]]; then
gpg --import-options restore --import "$f"
else
warn "$(basename "$f") is empty (placeholder?) - skipping"
fi
done
if (( found == 0 )); then
warn "No GPG export in the bundle - skipping."
fi
}
# ---------------------------------------------------------------------------
# The private repo - this is what the keys were for.
# ---------------------------------------------------------------------------
clone_engram() {
step "Cloning engram"
if [[ -d $ENGRAM_DIR ]]; then
warn "$ENGRAM_DIR already exists - skipping the clone."
return
fi
if (( ${#CHOSEN_KEYS[@]} == 0 )); then
warn "No SSH key was installed - cannot clone."
return
fi
mkdir -p "$(dirname "$ENGRAM_DIR")"
local key
for key in "${CHOSEN_KEYS[@]}"; do
info "Trying key: $key"
if GIT_SSH_COMMAND="ssh -o IdentitiesOnly=yes -i $HOME/.ssh/$key" \
git clone "$ENGRAM_REPO" "$ENGRAM_DIR"; then
info "Cloned into $ENGRAM_DIR (key: $key)"
return
fi
rm -rf "$ENGRAM_DIR"
done
die "Clone failed with every key - check the deploy key setup on gitea."
}
# ---------------------------------------------------------------------------
# Done
# ---------------------------------------------------------------------------
finish() {
step "That is it - the machine is online"
cat <<SUMMARY
user : $(whoami)
keys : ${CHOSEN_KEYS[*]:-none}
repo : $ENGRAM_DIR
SUMMARY
info "Next step, when ready: dotfiles (df) and Ansible."
}
# ---------------------------------------------------------------------------
# Main flow
# ---------------------------------------------------------------------------
main() {
banner
preflight
install_deps
unlock_secrets
install_keys
pin_known_host
import_gpg
clone_engram
finish
}
main "$@"