main
1#!/usr/bin/env bash
2# Deploy an Alpine appliance image to a host.
3# Usage: ./alpine/deploy.sh <hostname> [update|flash]
4set -euo pipefail
5
6SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
7HOST="${1:?Usage: $0 <hostname> [update|flash]}"
8ACTION="${2:-update}"
9DOMAIN="${DEPLOY_DOMAIN:-.vpn}"
10TARGET="root@${HOST}${DOMAIN}"
11IMAGE_DIR="$SCRIPT_DIR/images/$HOST"
12
13# SSH identity (hardware key). Override with DEPLOY_SSH_KEY. A shared
14# connection avoids one security-key touch per ssh/rsync invocation.
15SSH_KEY="${DEPLOY_SSH_KEY:-$HOME/.ssh/id_homelab_sk}"
16SSH_OPTS=(-i "$SSH_KEY" -o IdentitiesOnly=yes
17 -o ControlMaster=auto -o "ControlPath=/tmp/deploy-%C" -o ControlPersist=120)
18ssh() { command ssh "${SSH_OPTS[@]}" "$@"; }
19rsync() { command rsync -e "ssh ${SSH_OPTS[*]}" "$@"; }
20
21if [ ! -d "$IMAGE_DIR" ]; then
22 echo "Error: no image found for $HOST. Run build.sh first."
23 exit 1
24fi
25
26case "$ACTION" in
27 update)
28 echo "==> Deploying to $HOST via rsync"
29 echo "WARNING: This will update the boot partition. The host will need a reboot."
30 read -p "Continue? [y/N] " -n 1 -r
31 echo
32 [[ $REPLY =~ ^[Yy]$ ]] || exit 0
33
34 # Mount boot partition (diskless Alpine doesn't mount it by default)
35 ssh "${TARGET}" "mkdir -p /boot && mount /dev/mmcblk0p1 /boot 2>/dev/null || true"
36
37 # Backup current files (A/B rollback) — move instead of copy to
38 # free space before uploading the new image
39 ssh "${TARGET}" "mv /boot/initramfs /boot/initramfs.prev 2>/dev/null || true"
40 ssh "${TARGET}" "mv /boot/vmlinuz-rpi /boot/vmlinuz-rpi.prev 2>/dev/null || true"
41
42 # Deploy new files
43 rsync -Pv "$IMAGE_DIR/boot/initramfs" "${TARGET}:/boot/initramfs"
44 rsync -Pv "$IMAGE_DIR/boot/vmlinuz-rpi" "${TARGET}:/boot/vmlinuz-rpi" 2>/dev/null || true
45
46 # Unmount boot partition
47 ssh "${TARGET}" "umount /boot"
48
49 echo "==> Deployed. Reboot $HOST to activate:"
50 echo " ssh ${TARGET} reboot"
51 echo ""
52 echo " To rollback: ssh ${TARGET}"
53 echo " mount -o remount,rw /boot"
54 echo " mv /boot/initramfs.prev /boot/initramfs"
55 echo " reboot"
56 ;;
57 flash)
58 echo "==> SD card flashing"
59 echo "Manual steps:"
60 echo " 1. Partition SD: 256MB FAT32 (boot, label=ALPINE) + 512MB ext4 (label=persist)"
61 echo " 2. Copy boot files from $IMAGE_DIR/ to FAT32 partition"
62 echo " 3. Copy SSH host key to persist: persist/ssh/ssh_host_ed25519_key"
63 echo " 4. Copy WireGuard key to persist: persist/wireguard/private.key"
64 ;;
65 *)
66 echo "Unknown action: $ACTION"
67 echo "Usage: $0 <hostname> [update|flash]"
68 exit 1
69 ;;
70esac