#!/bin/bash # Bootstrap the golden image by booting it, running sparrowdo --bootstrap, then shutting down # This should be run ONCE after creating the golden image GOLDEN_IMAGE="$1" SSH_PRIVATE_KEY="${2:-$HOME/.ssh/id_rsa}" if [ -z "$GOLDEN_IMAGE" ]; then echo "Usage: $0 [ssh_private_key]" exit 1 fi if [ ! -f "$GOLDEN_IMAGE" ]; then echo "ERROR: Golden image not found: $GOLDEN_IMAGE" exit 1 fi BOOTSTRAP_VM="bootstrap-golden-$(date +%s)" SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" echo "==========================================" echo "Bootstrapping Golden Image" echo "==========================================" echo "Golden Image: $GOLDEN_IMAGE" echo "Bootstrap VM: $BOOTSTRAP_VM" echo "SSH Key: $SSH_PRIVATE_KEY" echo "" # Provision temporary VM from golden image echo "[1/4] Provisioning temporary VM..." VM_IP=$("${SCRIPT_DIR}/provision_vm.sh" "$BOOTSTRAP_VM" "$GOLDEN_IMAGE" 60) if [ "$VM_IP" = "ERROR" ] || [ -z "$VM_IP" ]; then echo "ERROR: Failed to provision bootstrap VM" exit 1 fi VM_IP=$(echo "$VM_IP" | tail -1) echo "VM IP: $VM_IP" # Wait for SSH echo "" echo "[2/4] Waiting for SSH to be ready..." for i in {1..30}; do if ssh -i "$SSH_PRIVATE_KEY" \ -o StrictHostKeyChecking=no \ -o ConnectTimeout=5 \ -o UserKnownHostsFile=/dev/null \ rocky@${VM_IP} 'echo "SSH ready"' 2>/dev/null; then echo "SSH connection established" break fi sleep 2 done # Run sparrowdo bootstrap echo "" echo "[3/4] Running Sparrowdo bootstrap..." if sparrowdo \ --host="${VM_IP}" \ --ssh_user=rocky \ --ssh_private_key="${SSH_PRIVATE_KEY}" \ --ssh_args="-o StrictHostKeyChecking=no -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null" \ --bootstrap \ --color; then echo "Bootstrap completed successfully!" else echo "WARNING: Bootstrap may have failed" fi # Shutdown the VM cleanly to save changes echo "" echo "[4/4] Shutting down VM to save changes..." ssh -i "$SSH_PRIVATE_KEY" \ -o StrictHostKeyChecking=no \ -o UserKnownHostsFile=/dev/null \ rocky@${VM_IP} 'sudo shutdown -h now' 2>/dev/null || true # Wait for VM to shut down echo "Waiting for VM to shut down..." sleep 10 for i in {1..30}; do if ! sudo virsh -c qemu:///system list --name 2>/dev/null | grep -q "$BOOTSTRAP_VM"; then echo "VM has shut down" break fi sleep 2 done # Clean up VM definition (disk contains the bootstrapped state) echo "Cleaning up VM definition..." sudo virsh -c qemu:///system undefine "$BOOTSTRAP_VM" 2>/dev/null || true echo "" echo "==========================================" echo "Bootstrap Complete" echo "==========================================" echo "Golden image has been bootstrapped with Sparrowdo" echo "The image now contains:" echo " - Raku/Rakudo runtime" echo " - Sparrowdo and dependencies" echo " - All required testing tools" echo "" echo "This image can now be used for testing without" echo "running bootstrap on each test VM." echo "=========================================="