Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop improvements #22

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 56 additions & 23 deletions host/launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ REGISTRY_IMAGE_NAME=runner

LOGFILE=runner.log

SCHEDULE_SHUTDOWN=false

function log_output {
echo "$(date "+%Y/%m/%d %H:%M:%S") $1"
echo "$(date "+%Y/%m/%d %H:%M:%S") [${RUN_ID:-PREPARING}] $1" >>$LOGFILE
Expand All @@ -27,36 +29,33 @@ function stream_output {
done
}

# Load .env file
if [ -f .env ]; then
# shellcheck disable=SC2046
export $(xargs <.env)
fi

# Configure Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"
function reload_env {
if [ -f .env ]; then
# shellcheck disable=SC2046
export $(xargs <.env)
fi
}

# Show a shutdown message when closing the script
trap "log_output \"[HOST] 🚦 Stopping runner script\"; exit 1" SIGINT
function cleanup {
log_output "[HOST] 🚦 Stopping runner script"
exit 0
}

# Select image
if [ -n "${REGISTRY_URL}" ]; then
REGISTRY_PATH="$REGISTRY_URL/$REGISTRY_IMAGE_NAME"
else
REGISTRY_PATH="$REGISTRY_IMAGE_NAME"
fi
function pull_image {
log_output "[HOST] ⬇️ Downloading from remote registry"
TART_REGISTRY_USERNAME=$REGISTRY_USERNAME TART_REGISTRY_PASSWORD=$REGISTRY_PASSWORD tart pull "$REGISTRY_PATH"
}

# Main loop
while :; do
function run_loop {
RUN_ID="$RANDOM$RANDOM"

log_output "[HOST] 🎫 Creating registration token"
REGISTRATION_TOKEN=$(curl -s -XPOST -H "Authorization: bearer $GITHUB_API_TOKEN" -H "Accept: application/vnd.github.v3+json" "$GITHUB_REGISTRATION_ENDPOINT" | grep "token" | sed "s/..\"token\":.\"//" | sed "s/\",$//")

log_output "[HOST] 💻 Launching macOS VM"
INSTANCE_NAME=runner_"$RUNNER_NAME"_"$RUN_ID"
TART_REGISTRY_USERNAME=$REGISTRY_USERNAME TART_REGISTRY_PASSWORD=$REGISTRY_PASSWORD tart clone $REGISTRY_PATH $INSTANCE_NAME
trap 'log_output "[HOST] 🪓 Killing the VM"; tart delete $INSTANCE_NAME; log_output "[HOST] 🚦 Stopping runner script"; exit 1' SIGINT
tart clone "$REGISTRY_PATH" "$INSTANCE_NAME"
trap 'log_output "[HOST] 🪓 Killing the VM"; tart delete $INSTANCE_NAME; cleanup' SIGINT SIGTERM
tart run --no-graphics $INSTANCE_NAME >/dev/null 2>&1 &

log_output "[HOST] 💤 Waiting for VM to boot"
Expand All @@ -72,10 +71,10 @@ while :; do
done

log_output "[HOST] 🔨 Configuring runner on VM"
ssh -q "$VM_USERNAME@$IP_ADDRESS" "./actions-runner/config.sh --url $RUNNER_URL --token $REGISTRATION_TOKEN --ephemeral --name $RUNNER_NAME --labels $RUNNER_LABELS --unattended --replace" >/dev/null
ssh -q -o StrictHostKeyChecking=no "$VM_USERNAME@$IP_ADDRESS" "./actions-runner/config.sh --url $RUNNER_URL --token $REGISTRATION_TOKEN --ephemeral --name $RUNNER_NAME --labels $RUNNER_LABELS --unattended --replace" >/dev/null

log_output "[HOST] 🏃 Starting runner on VM"
ssh -q "$VM_USERNAME@$IP_ADDRESS" "source ~/.zprofile && ./actions-runner/run.sh" 2>&1 | sed -nru 's/^(.+)$/[GUEST] 📀 \1/p' | stream_output
ssh -q -o StrictHostKeyChecking=no "$VM_USERNAME@$IP_ADDRESS" "source ~/.zprofile && ./actions-runner/run.sh" 2>&1 | sed -nru 's/^(.+)$/[GUEST] 📀 \1/p' | stream_output

log_output "[HOST] ✋ Stop the VM"
tart stop "$INSTANCE_NAME"
Expand All @@ -84,5 +83,39 @@ while :; do
tart delete "$INSTANCE_NAME"

RUN_ID=""
trap 'log_output "[HOST] 🚦 Stopping runner script"; exit 1' SIGINT
trap cleanup SIGINT SIGTERM
}

# Configure Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"

# Show a shutdown message when closing the script
trap cleanup SIGINT SIGTERM

# Main loop
while :; do
reload_env

if [[ "$SCHEDULE_SHUTDOWN" == "true" ]]; then
log_output "[HOST] ⏰ Scheduled for shutdown"
cleanup
fi

# Select image
if [ -n "${REGISTRY_URL}" ]; then
REGISTRY_PATH="$REGISTRY_URL/$REGISTRY_IMAGE_NAME"
else
REGISTRY_PATH="$REGISTRY_IMAGE_NAME"
fi

if tart list | grep $REGISTRY_PATH; then
run_loop
else
log_output "[HOST] 🔎 Target image not found"
if [ -n "${REGISTRY_URL}" ]; then
pull_image
else
cleanup
fi
fi
done
Loading