auto-update-daily-20260202
1#!/usr/bin/env bash
2# Start microvm on demand and connect via SSH
3# Usage: microvm-ssh <vm-name> <host> <port>
4#
5# This script is used as SSH ProxyCommand to:
6# 1. Start the microvm if not running (via aomi)
7# 2. Wait for SSH to become available
8# 3. Connect using nc/socat
9
10set -euo pipefail
11
12VM_NAME="${1:-}"
13HOST="${2:-}"
14PORT="${3:-22}"
15JUMP_HOST="${MICROVM_JUMP_HOST:-aomi.sbr.pm}"
16
17if [[ -z "$VM_NAME" ]] || [[ -z "$HOST" ]]; then
18 echo "Usage: microvm-ssh <vm-name> <host> [port]" >&2
19 exit 1
20fi
21
22# Check if VM is running, start if not
23# This runs on the jump host (aomi)
24ssh -q "$JUMP_HOST" "
25 if ! systemctl is-active --quiet microvm@${VM_NAME}; then
26 echo 'Starting microvm@${VM_NAME}...' >&2
27 sudo systemctl start microvm@${VM_NAME}
28 # Wait for VM to boot and SSH to be available
29 for i in {1..30}; do
30 if nc -z ${HOST} ${PORT} 2>/dev/null; then
31 break
32 fi
33 sleep 1
34 done
35 fi
36"
37
38# Connect through the jump host
39exec ssh -q -W "${HOST}:${PORT}" "$JUMP_HOST"