#!/bin/bash # Foxhunt Runpod Deployment Script # Usage: ./deploy.sh [init|plan|apply|destroy|output|ssh|help] set -e # Exit on error SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Helper functions log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; } log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; } # Check if terraform.tfvars exists check_tfvars() { if [ ! -f "terraform.tfvars" ]; then log_error "terraform.tfvars not found!" log_info "Please copy terraform.tfvars.example and fill in the required values:" echo "" echo " cp terraform.tfvars.example terraform.tfvars" echo " vim terraform.tfvars" echo "" exit 1 fi } # Check if OpenTofu or Terraform is installed check_terraform() { if command -v tofu &> /dev/null; then TF_CMD="tofu" log_info "Using OpenTofu" elif command -v terraform &> /dev/null; then TF_CMD="terraform" log_info "Using Terraform" else log_error "Neither OpenTofu nor Terraform is installed!" log_info "Install OpenTofu: https://opentofu.org/docs/intro/install/" log_info "OR install Terraform: https://www.terraform.io/downloads" exit 1 fi } # Initialize Terraform cmd_init() { log_info "Initializing Terraform..." $TF_CMD init log_success "Initialization complete!" } # Plan deployment cmd_plan() { check_tfvars log_info "Planning deployment..." $TF_CMD plan log_success "Plan complete! Review the changes above." } # Apply deployment cmd_apply() { check_tfvars log_warning "This will deploy infrastructure to Runpod and incur costs." log_info "Estimated cost: $0.44/hr (Tesla V100) or $0.69/hr (RTX 4090)" echo "" read -p "Do you want to proceed? (yes/no): " confirm if [ "$confirm" != "yes" ]; then log_info "Deployment cancelled." exit 0 fi log_info "Deploying to Runpod..." $TF_CMD apply log_success "Deployment complete!" echo "" log_info "Connection details:" $TF_CMD output connection_summary } # Destroy deployment cmd_destroy() { log_warning "This will DELETE the pod and volume. ALL DATA WILL BE LOST!" echo "" read -p "Are you ABSOLUTELY SURE? (type 'yes' to confirm): " confirm if [ "$confirm" != "yes" ]; then log_info "Destruction cancelled." exit 0 fi log_info "Destroying infrastructure..." $TF_CMD destroy log_success "Infrastructure destroyed." } # Show outputs cmd_output() { log_info "Terraform outputs:" if [ -n "$1" ]; then $TF_CMD output "$1" else $TF_CMD output fi } # SSH into pod cmd_ssh() { check_tfvars log_info "Getting SSH connection details..." SSH_CMD=$($TF_CMD output -raw pod_ssh_command 2>/dev/null || echo "") if [ -z "$SSH_CMD" ] || [ "$SSH_CMD" == "Public IP not enabled - cannot SSH" ]; then log_error "Public IP is not enabled or pod is not running." log_info "Enable public IP in terraform.tfvars: enable_public_ip = true" exit 1 fi log_info "Connecting to pod..." eval "$SSH_CMD" } # Show help cmd_help() { cat << EOF Foxhunt Runpod Deployment Script Usage: $0 [command] Commands: init Initialize Terraform (download providers) plan Preview deployment changes (dry-run) apply Deploy infrastructure to Runpod destroy Destroy infrastructure (WARNING: data loss!) output Show deployment outputs (e.g., IP, endpoints) ssh SSH into the deployed pod help Show this help message Examples: # First-time setup $0 init $0 plan $0 apply # Check deployment $0 output connection_summary $0 ssh # Teardown $0 destroy Documentation: - README.md (this directory) - RUNPOD_DEPLOYMENT_GUIDE.md (project root) - terraform.tfvars.example (configuration template) Cost Estimates: - Tesla V100: \$0.44/hr = \$316.80/month (24/7) - RTX 4090: \$0.69/hr = \$496.80/month (24/7) - Volume: \$0.10/GB/month EOF } # Main command dispatcher main() { check_terraform case "${1:-help}" in init) cmd_init ;; plan) cmd_plan ;; apply) cmd_apply ;; destroy) cmd_destroy ;; output) cmd_output "$2" ;; ssh) cmd_ssh ;; help|--help|-h) cmd_help ;; *) log_error "Unknown command: $1" echo "" cmd_help exit 1 ;; esac } main "$@"