From 15bd87b11a64b5deb92478fa7d1f240968cd306d Mon Sep 17 00:00:00 2001 From: Seaswimmer Date: Wed, 13 Mar 2024 03:06:44 +0000 Subject: [PATCH] Add spin-up-runner.sh --- spin-up-runner.sh | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 spin-up-runner.sh diff --git a/spin-up-runner.sh b/spin-up-runner.sh new file mode 100644 index 0000000..41ed7e7 --- /dev/null +++ b/spin-up-runner.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +# Check if the script is run with sudo +if [[ $EUID -ne 0 ]]; then + echo "Please run this script with sudo." + exit 1 +fi + +# Change directory to /var/lib/forgejo-runner +cd /var/lib/forgejo-runner + +# Request runner directory name from the user +read -p "Enter the runner directory name: " RUNNER_DIR_NAME + +# Create the directory with the user's input +mkdir "$RUNNER_DIR_NAME" + +# Copy the ./forgejo-runner file into the created directory +cp ./forgejo-runner "$RUNNER_DIR_NAME/" + +# Change directory to the created directory +cd "$RUNNER_DIR_NAME" + +# Request runner name from the user +read -p "Enter the runner name: " RUNNER_NAME + +# Request runner target domain from the user +while true; do + read -p "Enter the URL of the Forgejo instance you're registering this runner to: " URL + if [ -n "$URL" ]; then + # Check if the URL includes the http(s) scheme + if [[ ! "$URL" =~ ^https?:// ]]; then + # Prepend https:// to the entered URL + URL="https://$URL" + fi + + # Append /api/v1/forgejo/version to the entered URL + CHECK_URL="${URL%/}/api/v1/version" + + # Use curl to check if the URL is valid + if curl -s -o /dev/null -w "%{http_code}" "$CHECK_URL" > /dev/null 2>&1; then + break + else + echo "Invalid URL or unable to reach the version endpoint. Please try again." + fi + else + echo "URL cannot be empty. Please try again." + fi +done + +# Request runner registration token from the user +while true; do + read -p "Enter the runner registration token: " TOKEN + if [ -n "$TOKEN" ]; then + break + else + echo "Token cannot be empty. Please try again." + fi +done + +# Register the runner with the provided information +./forgejo-runner register --no-interactive --token "$TOKEN" --name "$RUNNER_NAME" --instance "$URL" --labels docker:docker://node:16-bullseye,self-hosted + +# Enable and start the systemd service with the runner directory name +sudo systemctl enable --now "forgejo-runner@$RUNNER_DIR_NAME" + +# Completion message +echo "Runner setup for $RUNNER_NAME in $RUNNER_DIR_NAME completed." + +# Exit the script +exit 0