Skip to content

Commit

Permalink
Loop improvements (#22)
Browse files Browse the repository at this point in the history
## πŸ“– Description

This PR improves the run loop by checking for changes in the `.env`
which allows us to change stuff on the fly.

- The `.env` is reloaded at the beginning of the loop
- Before starting the runner, the script will now validate that the
image exist, and attempt to pull it if it doesn't.
- The script will check for a `SCHEDULE_SHUTDOWN` flag in the `.env` and
will shutdown if it is set to true. This allows us to turn off the
runner when it has completed its current job.
- `SIGTERM` events will now be intercepted by the script to allow the
cleanup to work when it is running as a service
  • Loading branch information
randy-sab-roy authored Feb 14, 2024
1 parent aa3b986 commit a76e073
Showing 1 changed file with 56 additions and 23 deletions.
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

0 comments on commit a76e073

Please sign in to comment.