SelfHosting/VMs/ForgejoRunner/launch_forgejo_runner_vm.sh
2025-12-25 00:29:30 +01:00

104 lines
2.5 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
########################################
# Configuration
########################################
VM_NAME="forgejo-runner-vm"
IMAGE="images:debian/trixie"
MEMORY="2GiB"
CPUS="4"
########################################
# Helper functions
########################################
log() {
echo "[+] $*"
}
vm_exists() {
incus info "$VM_NAME" >/dev/null 2>&1
}
########################################
# Create VM if needed
########################################
if vm_exists; then
log "VM '$VM_NAME' already exists, skipping creation"
else
log "Creating VM '$VM_NAME'"
incus launch "$IMAGE" "$VM_NAME" \
--vm \
-c limits.memory="$MEMORY" \
-c limits.cpu="$CPUS"
fi
########################################
# Wait for VM to be ready
########################################
log "Waiting for VM to become ready"
incus exec "$VM_NAME" -- cloud-init status --wait >/dev/null 2>&1 || true
log "Waiting for Incus Agent to start"
# This loop tries a simple command until it succeeds or times out
RETRIES=0
MAX_RETRIES=30
until incus exec "$VM_NAME" -- uptime >/dev/null 2>&1; do
RETRIES=$((RETRIES + 1))
if [ $RETRIES -ge $MAX_RETRIES ]; then
echo "Error: Timeout waiting for VM agent to start."
exit 1
fi
echo "$RETRIES retries"
sleep 1
done
log "Agent is responsive. Proceeding..."
########################################
# Install Docker inside the VM
########################################
log "Installing Docker inside VM"
incus exec "$VM_NAME" -- bash -eux <<'EOF'
export DEBIAN_FRONTEND=noninteractive
# Base packages
apt-get update
apt-get install -y \
ca-certificates \
curl \
gnupg
# Docker GPG key
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg \
-o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
# Docker repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
> /etc/apt/sources.list.d/docker.list
# Install Docker
apt-get update
apt-get install -y \
docker-ce \
docker-ce-cli \
containerd.io \
docker-buildx-plugin \
docker-compose-plugin
# Enable Docker
systemctl enable docker
systemctl start docker
EOF
########################################
# Done
########################################
log "Docker successfully installed in VM '$VM_NAME'"
log "You can access it with:"
echo " incus exec $VM_NAME -- bash"