spawn/fly/claude.sh
A 0ebaadb345
refactor: replace inline Claude config with setup_claude_code_config in fly/koyeb/railway (#711)
Replace 40 lines of duplicated inline Claude Code configuration in each
of fly/claude.sh, koyeb/claude.sh, and railway/claude.sh with the shared
setup_claude_code_config helper from shared/common.sh. This eliminates
~120 lines of copy-pasted config generation (mktemp, heredoc, upload, rm)
and uses json_escape for the API key, matching the pattern already used
by 30+ other claude.sh scripts.

Agent: complexity-hunter

Co-authored-by: A <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-12 15:02:25 -08:00

71 lines
2 KiB
Bash

#!/bin/bash
set -eo pipefail
# Source common functions - try local file first, fall back to remote
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd)"
if [[ -f "$SCRIPT_DIR/lib/common.sh" ]]; then
source "$SCRIPT_DIR/lib/common.sh"
else
eval "$(curl -fsSL https://raw.githubusercontent.com/OpenRouterTeam/spawn/main/fly/lib/common.sh)"
fi
log_info "Claude Code on Fly.io"
echo ""
# 1. Ensure flyctl CLI and API token
ensure_fly_cli
ensure_fly_token
# 2. Get app name and create machine
SERVER_NAME=$(get_server_name)
create_server "$SERVER_NAME"
# 3. Install base tools
wait_for_cloud_init
# 4. Install Claude Code
log_step "Installing Claude Code..."
run_server "curl -fsSL https://claude.ai/install.sh | bash"
# Verify installation
if ! run_server "export PATH=\$HOME/.local/bin:\$PATH && command -v claude" >/dev/null 2>&1; then
log_error "Claude Code installation failed"
exit 1
fi
log_info "Claude Code installed"
# 5. Get OpenRouter API key
echo ""
if [[ -n "${OPENROUTER_API_KEY:-}" ]]; then
log_info "Using OpenRouter API key from environment"
else
OPENROUTER_API_KEY=$(get_openrouter_api_key_oauth 5180)
fi
# 6. Inject environment variables into ~/.zshrc
log_step "Setting up environment variables..."
inject_env_vars_fly \
"OPENROUTER_API_KEY=${OPENROUTER_API_KEY}" \
"ANTHROPIC_BASE_URL=https://openrouter.ai/api" \
"ANTHROPIC_AUTH_TOKEN=${OPENROUTER_API_KEY}" \
"ANTHROPIC_API_KEY=" \
"CLAUDE_CODE_SKIP_ONBOARDING=1" \
"CLAUDE_CODE_ENABLE_TELEMETRY=0" \
"PATH=\$HOME/.claude/local/bin:\$HOME/.bun/bin:\$PATH"
# 7. Configure Claude Code settings
setup_claude_code_config "${OPENROUTER_API_KEY}" \
"upload_file" \
"run_server"
echo ""
log_info "Fly.io machine setup completed successfully!"
log_info "App: $SERVER_NAME (Machine ID: $FLY_MACHINE_ID)"
echo ""
# 8. Start Claude Code interactively
log_step "Starting Claude Code..."
sleep 1
clear
interactive_session "export PATH=\$HOME/.local/bin:\$PATH && source ~/.bashrc && claude"