From be3af6f4a043ddfa55807ae8a6ab9073796cf355 Mon Sep 17 00:00:00 2001 From: Randy Sab-Roy Date: Wed, 14 Feb 2024 12:18:26 -0500 Subject: [PATCH 1/5] Improve loop --- host/launch.sh | 68 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/host/launch.sh b/host/launch.sh index 6bb9eca..0a8b31b 100755 --- a/host/launch.sh +++ b/host/launch.sh @@ -27,27 +27,24 @@ 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" @@ -55,8 +52,8 @@ while :; do 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; shutdown' SIGINT SIGTERM tart run --no-graphics $INSTANCE_NAME >/dev/null 2>&1 & log_output "[HOST] 💤 Waiting for VM to boot" @@ -84,5 +81,34 @@ 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 + + # 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 From be226696b4a3e8d5bd67f67d24ca493182a3282c Mon Sep 17 00:00:00 2001 From: Randy Sab-Roy Date: Wed, 14 Feb 2024 12:22:55 -0500 Subject: [PATCH 2/5] Allow to schedule for shutdown --- host/launch.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/host/launch.sh b/host/launch.sh index 0a8b31b..4aac207 100755 --- a/host/launch.sh +++ b/host/launch.sh @@ -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 @@ -94,6 +96,10 @@ trap cleanup SIGINT SIGTERM while :; do reload_env + if [[ "${SCHEDULE_SHUTDOWN,,}" == "true" ]]; then + cleanup + fi + # Select image if [ -n "${REGISTRY_URL}" ]; then REGISTRY_PATH="$REGISTRY_URL/$REGISTRY_IMAGE_NAME" From 2d3b77b443883433155a9b286761d3982deae68b Mon Sep 17 00:00:00 2001 From: Randy Sab-Roy Date: Wed, 14 Feb 2024 12:37:37 -0500 Subject: [PATCH 3/5] Fix script --- host/launch.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/host/launch.sh b/host/launch.sh index 4aac207..9978849 100755 --- a/host/launch.sh +++ b/host/launch.sh @@ -55,7 +55,7 @@ function run_loop { log_output "[HOST] 💻 Launching macOS VM" INSTANCE_NAME=runner_"$RUNNER_NAME"_"$RUN_ID" tart clone $REGISTRY_PATH $INSTANCE_NAME - trap 'log_output "[HOST] 🪓 Killing the VM"; tart delete $INSTANCE_NAME; shutdown' SIGINT SIGTERM + 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" @@ -96,7 +96,8 @@ trap cleanup SIGINT SIGTERM while :; do reload_env - if [[ "${SCHEDULE_SHUTDOWN,,}" == "true" ]]; then + if [[ "$SCHEDULE_SHUTDOWN" == "true" ]]; then + log_output "[HOST] ⏰ Scheduled for shutdown" cleanup fi From 51171888d4c15d092a5fde6b85a157c97fe0676f Mon Sep 17 00:00:00 2001 From: Randy Sab-Roy Date: Wed, 14 Feb 2024 12:39:33 -0500 Subject: [PATCH 4/5] Disable strict host key checking --- host/launch.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/host/launch.sh b/host/launch.sh index 9978849..c8b2015 100755 --- a/host/launch.sh +++ b/host/launch.sh @@ -71,10 +71,10 @@ function run_loop { 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" From 357023f4c10edaa2eabf4a97c5e6c6f59cef3b5b Mon Sep 17 00:00:00 2001 From: Randy Sab-Roy Date: Wed, 14 Feb 2024 12:54:57 -0500 Subject: [PATCH 5/5] Fix linting --- host/launch.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/host/launch.sh b/host/launch.sh index c8b2015..584585d 100755 --- a/host/launch.sh +++ b/host/launch.sh @@ -43,7 +43,7 @@ function cleanup { function pull_image { log_output "[HOST] ⬇️ Downloading from remote registry" - TART_REGISTRY_USERNAME=$REGISTRY_USERNAME TART_REGISTRY_PASSWORD=$REGISTRY_PASSWORD tart pull $REGISTRY_PATH + TART_REGISTRY_USERNAME=$REGISTRY_USERNAME TART_REGISTRY_PASSWORD=$REGISTRY_PASSWORD tart pull "$REGISTRY_PATH" } function run_loop { @@ -54,7 +54,7 @@ function run_loop { log_output "[HOST] 💻 Launching macOS VM" INSTANCE_NAME=runner_"$RUNNER_NAME"_"$RUN_ID" - tart clone $REGISTRY_PATH $INSTANCE_NAME + 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 &