spawn/sprite/lib/common.sh
Sprite cabdbc37ba refactor: add pipefail to error handling flags
Changed 65 agent scripts from `set -e` to `set -eo pipefail` to ensure
errors in piped commands are properly caught. This prevents silent
failures when commands like `curl | bash` fail in the middle.

Files updated across all cloud providers:
- aws-lightsail: 10 scripts
- digitalocean: 3 scripts
- e2b: 10 scripts
- gcp: 10 scripts
- hetzner: 3 scripts
- lambda: 10 scripts
- linode: 3 scripts
- modal: 10 scripts
- sprite: 3 scripts
- vultr: 3 scripts

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-08 02:34:45 +00:00

149 lines
4.8 KiB
Bash

#!/bin/bash
set -eo pipefail
# Common bash functions shared between spawn scripts
# Source shared provider-agnostic functions (local or remote fallback)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd)"
if [[ -n "${SCRIPT_DIR}" && -f "${SCRIPT_DIR}/../../shared/common.sh" ]]; then
source "${SCRIPT_DIR}/../../shared/common.sh"
else
eval "$(curl -fsSL https://raw.githubusercontent.com/OpenRouterTeam/spawn/main/shared/common.sh)"
fi
# Check if sprite CLI is installed, install if not
ensure_sprite_installed() {
if ! command -v sprite &> /dev/null; then
log_warn "Installing sprite CLI..."
curl -fsSL https://sprites.dev/install.sh | bash
export PATH="${HOME}/.local/bin:${PATH}"
fi
}
# Check if already authenticated with sprite
ensure_sprite_authenticated() {
if ! sprite org list &> /dev/null; then
log_warn "Logging in to sprite..."
sprite login || true
fi
}
# Prompt for sprite name
get_sprite_name() {
local sprite_name
sprite_name=$(get_resource_name "SPRITE_NAME" "Enter sprite name: ") || return 1
if ! validate_server_name "${sprite_name}"; then
return 1
fi
echo "${sprite_name}"
}
# Check if sprite exists, create if not
ensure_sprite_exists() {
local sprite_name=${1}
local sleep_time=${2:-3}
if sprite list 2>/dev/null | grep -qE "^${sprite_name}( |$)"; then
log_info "Sprite '${sprite_name}' already exists"
else
log_warn "Creating sprite '${sprite_name}'..."
sprite create -skip-console "${sprite_name}" || true
log_warn "Waiting for sprite to be ready..."
sleep "${sleep_time}"
fi
}
# Verify sprite is accessible (retry up to max_attempts)
verify_sprite_connectivity() {
local sprite_name=${1}
local max_attempts=${2:-6}
local attempt=1
log_warn "Verifying sprite connectivity..."
while [[ "${attempt}" -le "${max_attempts}" ]]; do
if sprite exec -s "${sprite_name}" -- echo "ok" >/dev/null 2>&1; then
log_info "Sprite '${sprite_name}' is ready"
return 0
fi
log_warn "Sprite not ready, retrying (${attempt}/${max_attempts})..."
sleep 5
((attempt++))
done
log_error "Sprite '${sprite_name}' failed to respond after ${max_attempts} attempts"
return 1
}
# Helper function to run commands on sprite
run_sprite() {
local sprite_name=${1}
local command=${2}
sprite exec -s "${sprite_name}" -- bash -c "${command}"
}
# Configure shell environment (PATH, zsh setup)
setup_shell_environment() {
local sprite_name=${1}
log_warn "Configuring shell environment..."
# Create temp file with path config
local path_temp
path_temp=$(mktemp)
cat > "${path_temp}" << 'EOF'
# [spawn:path]
export PATH="${HOME}/.bun/bin:/.sprite/languages/bun/bin:${PATH}"
EOF
# Upload and append to shell configs
sprite exec -s "${sprite_name}" -file "${path_temp}:/tmp/path_config" -- bash -c "cat /tmp/path_config >> ~/.zprofile && cat /tmp/path_config >> ~/.zshrc && rm /tmp/path_config"
rm "${path_temp}"
# Switch bash to zsh
local bash_temp
bash_temp=$(mktemp)
cat > "${bash_temp}" << 'EOF'
# [spawn:bash]
exec /usr/bin/zsh -l
EOF
sprite exec -s "${sprite_name}" -file "${bash_temp}:/tmp/bash_config" -- bash -c "cat /tmp/bash_config > ~/.bash_profile && cat /tmp/bash_config > ~/.bashrc && rm /tmp/bash_config"
rm "${bash_temp}"
}
# Inject environment variables into sprite's shell config
# Usage: inject_env_vars_sprite SPRITE_NAME KEY1=val1 KEY2=val2 ...
# Example: inject_env_vars_sprite "$SPRITE_NAME" \
# "OPENROUTER_API_KEY=$OPENROUTER_API_KEY" \
# "ANTHROPIC_BASE_URL=https://openrouter.ai/api"
inject_env_vars_sprite() {
local sprite_name="${1}"
shift
local env_temp
env_temp=$(mktemp)
chmod 600 "${env_temp}"
generate_env_config "$@" > "${env_temp}"
# Upload and append to .zshrc using sprite exec with -file flag
sprite exec -s "${sprite_name}" -file "${env_temp}:/tmp/env_config" -- bash -c "cat /tmp/env_config >> ~/.zshrc && rm /tmp/env_config"
rm "${env_temp}"
}
# Upload file to sprite (for use with setup_claude_code_config callback)
# Usage: upload_file_sprite SPRITE_NAME LOCAL_PATH REMOTE_PATH
# Example: upload_file_sprite "$SPRITE_NAME" "/tmp/settings.json" "/root/.claude/settings.json"
upload_file_sprite() {
local sprite_name="${1}"
local local_path="${2}"
local remote_path="${3}"
# Generate a unique temp path to avoid collisions
local temp_remote="/tmp/sprite_upload_$(basename "${remote_path}")_$$"
sprite exec -s "${sprite_name}" -file "${local_path}:${temp_remote}" -- bash -c "mkdir -p \$(dirname '${remote_path}') && mv '${temp_remote}' '${remote_path}'"
}
# Note: Provider-agnostic functions (nc_listen, open_browser, OAuth helpers, validate_model_id) are now in shared/common.sh