Files
workflows/actions/orchestrating.sh

103 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# Record start time
START_TIME=$(date +%s)
# Function to check list of required environment variables
check_env_vars() {
local missing_vars=()
for var in "$@"; do
if [ -z "${!var}" ]; then
missing_vars+=("$var")
fi
done
if [ ${#missing_vars[@]} -ne 0 ]; then
echo "Error: The following environment variables are not set: ${missing_vars[*]}"
exit 1
fi
}
# Function to check if doctl is available
check_doctl() {
if ! command -v doctl &> /dev/null; then
echo "Error: doctl is not installed or not in PATH"
echo "Please install doctl: https://docs.digitalocean.com/reference/doctl/how-to/install/"
exit 1
fi
}
# Function to setup SSH private key
setup_ssh_key() {
if [ -z "$SSH_PRIVATE_KEY" ]; then
echo "Error: SSH_PRIVATE_KEY environment variable is not set"
exit 1
fi
# Create SSH directory if it doesn't exist
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Write private key to file (decode if base64)
if echo "$SSH_PRIVATE_KEY" | base64 -d > /dev/null 2>&1; then
echo "$SSH_PRIVATE_KEY" | base64 -d > ~/.ssh/id_rsa
else
echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
fi
chmod 600 ~/.ssh/id_rsa
echo "SSH private key has been set up successfully"
}
# Function to create droplet
create_droplet() {
local name="${1:-runner-${RUNNER_ID}}"
local size="${2:-s-1vcpu-1gb}"
local image="${3:-ubuntu-22-04-x64}"
local region="${4:-sgp1}"
echo "Creating droplet: $name"
echo "Size: $size, Image: $image, Region: $region"
# Authenticate doctl
doctl auth init --access-token "$DO_TOKEN"
# Create droplet
local droplet_id=$(doctl compute droplet create "$name" \
--size "$size" \
--image "$image" \
--region "$region" \
--ssh-keys $(doctl compute ssh-key list --format ID --no-header | tr '\n' ',' | sed 's/,$//' || echo "") \
--format ID \
--no-header \
--wait)
if [ -n "$droplet_id" ]; then
echo "Droplet created successfully with ID: $droplet_id"
echo "Droplet name: $name"
# Get droplet IP
local droplet_ip=$(doctl compute droplet get "$droplet_id" --format PublicIPv4 --no-header)
echo "Droplet IP: $droplet_ip"
return 0
else
echo "Error: Failed to create droplet"
return 1
fi
}
# Check required environment variables
check_env_vars "SPEC" "RUNNER_ID" "DO_TOKEN" "SSH_PRIVATE_KEY"
# Check if doctl is available
check_doctl
# Setup SSH private key
setup_ssh_key
create_droplet
# Calculate and display execution time
END_TIME=$(date +%s)
EXECUTION_TIME=$((END_TIME - START_TIME))
echo "Script execution time: ${EXECUTION_TIME} seconds"