#!/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 </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 '.*' ! -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:" local i for i in "${!available[@]}"; do printf ' %d) %s\n' "$((i + 1))" "${available[$i]}" done local answer read -rp " Which keys? (numbers, first one becomes id_ed25519, empty = none): " answer local num n name target n=0 for num in $answer; do if [[ ! $num =~ ^[0-9]+$ ]] || (( num < 1 || num > ${#available[@]} )); then die "Not a valid key number: $num" fi name="${available[$((num - 1))]}" n=$((n + 1)) # The first chosen key becomes the standard default identity, # anything after that keeps its own name. if (( n == 1 )); then target="id_ed25519" else target="$name" fi if [[ -e "$HOME/.ssh/$target" ]]; then warn "$target already exists in ~/.ssh - skipping" continue fi install -m 600 "$WORK/plain/ssh/$name" "$HOME/.ssh/$target" if [[ -f "$WORK/plain/ssh/$name.pub" ]]; then install -m 644 "$WORK/plain/ssh/$name.pub" "$HOME/.ssh/$target.pub" fi CHOSEN_KEYS+=("$name") info "Installed $name as ~/.ssh/$target" 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 [[ ! -f "$HOME/.ssh/id_ed25519" ]]; then warn "No default key (~/.ssh/id_ed25519) - cannot clone." return fi mkdir -p "$(dirname "$ENGRAM_DIR")" if ! git clone "$ENGRAM_REPO" "$ENGRAM_DIR"; then rm -rf "$ENGRAM_DIR" die "Clone failed - is the deploy key set up on gitea?" fi info "Cloned into $ENGRAM_DIR" } # --------------------------------------------------------------------------- # Done # --------------------------------------------------------------------------- finish() { step "That is it - the machine is online" cat <