Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

6.9 KB
Raw Blame
#!/bin/bash
# Drive the e2e-local GCP VM from GitHub Actions as the controller service
# account (ci-gha-e2e-ctl).
#
# Usage: gcp-e2e-vm.sh start-pool|start-primary|start|stop|clear-token
#   start-pool   try the primary once, then the fallback after rejection;
#                write the attempted instance and zone to GITHUB_OUTPUT
#   start-primary  start with one attempt, for fast pool fallback
#   start        hand the VM a fresh runner registration token and boot it;
#                retry rejection up to six times, or reuse an online runner
#   stop         request a stop without waiting
#   clear-token  remove the registration token from the VM's metadata
#
# The controller holds only compute.instances.get/start/stop/setMetadata on
# each configured VM. It lacks compute.zoneOperations.get, so nothing here waits on
# a gcloud operation: start/stop use --async, status is polled, and metadata
# is written through the setMetadata REST call.
#
# Env: GCP_PROJECT, GCP_ZONE, GCP_INSTANCE, RUNNER_LABEL, REPO, and GH_TOKEN
# (a GitHub App token with administration:write).
# start-pool also requires GCP_FALLBACK_ZONE, GCP_FALLBACK_INSTANCE, GITHUB_OUTPUT.
set -euo pipefail

: "${GCP_PROJECT:?}" "${GCP_ZONE:?}" "${GCP_INSTANCE:?}" "${RUNNER_LABEL:?}" "${REPO:?}"

TOKEN_KEY=runner-reg-token
# A zone out of capacity (ZONE_RESOURCE_POOL_EXHAUSTED) rejects a start and
# leaves the VM TERMINATED; capacity often returns within minutes.
START_RETRY_DELAY=60

vm() {
    gcloud compute instances "$1" "$GCP_INSTANCE" \
        --project "$GCP_PROJECT" --zone "$GCP_ZONE" "${@:2}"
}

vm_status() {
    vm describe --format='value(status)'
}

wait_status() {
    for _ in $(seq 60); do
        [ "$(vm_status)" = "$1" ] && return 0
        sleep 5
    done
    echo "::error::$GCP_INSTANCE did not reach $1 within 5 minutes" >&2
    return 1
}

capacity_error() {
    local code='(^|[^[:alnum:]_])ZONE_RESOURCE_POOL_EXHAUSTED(_WITH_DETAILS)?([^[:alnum:]_]|$)'
    local message="The zone '[^']+' does not have[[:space:]]+enough resources available to fulfill the request"
    [[ "$1" =~ $code || "$1" =~ $message ]]
}

# boot_vm ATTEMPTS: retry only recognized capacity errors or observed rejection.
boot_vm() {
    local attempts=$1 attempt output result
    for attempt in $(seq "$attempts"); do
        if output=$(vm start --async 2>&1); then
            printf '%s\n' "$output"
            if wait_booted; then
                return 0
            fi
        else
            result=$?
            printf '%s\n' "$output" >&2
            capacity_error "$output" || return "$result"
        fi
        echo "::warning::$GCP_INSTANCE start attempt $attempt/$attempts was rejected; $GCP_ZONE may lack capacity" >&2
        [ "$attempt" = "$attempts" ] || sleep "$START_RETRY_DELAY"
    done
    echo "::error::could not start $GCP_INSTANCE after $attempts attempts" >&2
    return 75
}

# wait_booted: succeed once RUNNING; fail when the VM sits TERMINATED for a
# minute, which means the start was rejected. Without
# compute.zoneOperations.get the status is the only signal.
wait_booted() {
    local status terminated=0
    for _ in $(seq 60); do
        status=$(vm_status)
        [ "$status" = RUNNING ] && return 0
        if [ "$status" = TERMINATED ]; then
            terminated=$((terminated + 1))
            [ "$terminated" -lt 12 ] || return 1
        else
            terminated=0
        fi
        sleep 5
    done
    echo "::error::$GCP_INSTANCE did not reach RUNNING within 5 minutes" >&2
    exit 1
}

runner_online() {
    gh api "repos/$REPO/actions/runners" --jq "any(.runners[];
        .name == \"$GCP_INSTANCE\" and .status == \"online\"
        and any(.labels[]; .name == \"$RUNNER_LABEL\"))" | grep -qx true
}

# set_token VALUE: store VALUE under TOKEN_KEY, or drop the key when VALUE is empty.
set_token() {
    vm describe --format=json |
        jq -c --arg key "$TOKEN_KEY" --arg value "$1" '{
            fingerprint: .metadata.fingerprint,
            items: ([(.metadata.items // [])[] | select(.key != $key)]
                + (if $value == "" then [] else [{key: $key, value: $value}] end))
        }' |
        curl -fsS -o /dev/null -X POST \
            -H "Authorization: Bearer $(gcloud auth print-access-token)" \
            -H 'Content-Type: application/json' --data-binary @- \
            "https://compute.googleapis.com/compute/v1/projects/$GCP_PROJECT/zones/$GCP_ZONE/instances/$GCP_INSTANCE/setMetadata"
}

# stored_token: print the stored token, or "" when absent; fails when metadata cannot be read.
stored_token() {
    local instance
    instance=$(vm describe --format=json) || return 1
    jq -er --arg key "$TOKEN_KEY" \
        '[.metadata.items[]? | select(.key == $key) | .value] | first // ""' <<<"$instance"
}

# wait_token VALUE: setMetadata is asynchronous; wait until the stored token is
# VALUE ("" = absent). A failed read never counts as a match.
wait_token() {
    local current
    for _ in $(seq 30); do
        if current=$(stored_token) && [ "$current" = "$1" ]; then
            return 0
        fi
        sleep 2
    done
    echo "::error::the registration token in VM metadata did not reach the expected state" >&2
    return 1
}

start() {
    local status token
    status=$(vm_status)
    if [ "$status" = RUNNING ] && runner_online; then
        echo "$GCP_INSTANCE is running with its runner online"
        return 0
    fi
    if [ "$status" != TERMINATED ]; then
        echo "$GCP_INSTANCE is $status without an online runner; stopping it first"
        [ "$status" = STOPPING ] || vm stop --async
        wait_status TERMINATED
    fi

    token=$(gh api -X POST "repos/$REPO/actions/runners/registration-token" --jq .token)
    echo "::add-mask::$token"
    set_token "$token"
    # Boot only once the startup script can read this token, not a stale one.
    wait_token "$token"

    boot_vm "$1"
}

start_pool() {
    : "${GCP_FALLBACK_ZONE:?}" "${GCP_FALLBACK_INSTANCE:?}" "${GITHUB_OUTPUT:?}"
    local result
    # Publish before starting so failure/cancellation cleanup has an identity.
    printf 'instance=%s\nzone=%s\n' "$GCP_INSTANCE" "$GCP_ZONE" >> "$GITHUB_OUTPUT"
    # A separate Bash process preserves errexit inside start and its helpers.
    if bash "$0" start-primary; then
        return 0
    else
        result=$?
    fi
    [ "$result" -eq 75 ] || return "$result"

    bash "$0" clear-token
    wait_status TERMINATED
    echo "::notice::$GCP_ZONE rejected the primary start; trying $GCP_FALLBACK_ZONE"
    export GCP_INSTANCE="$GCP_FALLBACK_INSTANCE" GCP_ZONE="$GCP_FALLBACK_ZONE"
    printf 'instance=%s\nzone=%s\n' "$GCP_INSTANCE" "$GCP_ZONE" >> "$GITHUB_OUTPUT"
    bash "$0" start
}

case "${1:-}" in
    start-pool) start_pool ;;
    start-primary) start 1 ;;
    start) start 6 ;;
    stop) vm stop --async ;;
    # e2e-tests runs PR code on the VM next, so confirm the token is gone.
    clear-token) set_token "" && wait_token "" ;;
    *) echo "usage: $0 start-pool|start-primary|start|stop|clear-token" >&2; exit 2 ;;
esac