#!/usr/bin/env bash # Run this locally to provision a staging EC2 instance for staging.musehub.ai. # Mirrors the prod setup (same AMI, SG, key pair) with a separate instance + EIP. # # Prerequisites: # brew install awscli # aws configure (Access Key ID, Secret, region=us-east-1, output=json) # ~/.ssh/musehub-key.pem must exist (created by aws-provision.sh) # # Usage: # chmod +x deploy/aws-provision-staging.sh # ./deploy/aws-provision-staging.sh set -euo pipefail export AWS_PAGER="" REGION="us-east-1" AMI_ID="ami-0c7217cdde317cfec" # Ubuntu 22.04 LTS (us-east-1) INSTANCE_TYPE="t3.small" KEY_NAME="musehub-key" SG_ID="sg-05815872537fcfe76" # musehub-sg — shared with prod INSTANCE_NAME="musehub-staging" echo "==> [1/5] Checking key pair..." if [ ! -f ~/.ssh/${KEY_NAME}.pem ]; then echo "ERROR: ~/.ssh/${KEY_NAME}.pem not found." echo " Run deploy/aws-provision.sh first to create the key pair." exit 1 fi echo " Key found: ~/.ssh/${KEY_NAME}.pem" # ── SSH rule for current IP ──────────────────────────────────────────────────── echo "==> [2/5] Adding SSH inbound rule for your current IP..." MY_IP=$(curl -s https://checkip.amazonaws.com) echo " Your IP: $MY_IP" aws ec2 authorize-security-group-ingress \ --region "$REGION" \ --group-id "$SG_ID" \ --protocol tcp --port 22 --cidr "${MY_IP}/32" \ --output text > /dev/null 2>&1 \ && echo " SSH rule added" \ || echo " SSH rule already exists, skipping" # ── EC2 instance ────────────────────────────────────────────────────────────── echo "==> [3/5] Checking for existing staging instance..." EXISTING=$(aws ec2 describe-instances \ --region "$REGION" \ --filters "Name=tag:Name,Values=$INSTANCE_NAME" "Name=instance-state-name,Values=pending,running,stopped" \ --query 'Reservations[0].Instances[0].InstanceId' \ --output text) if [ "$EXISTING" != "None" ] && [ -n "$EXISTING" ]; then echo " Staging instance already exists: $EXISTING" INSTANCE_ID="$EXISTING" # Start it if stopped STATE=$(aws ec2 describe-instances \ --region "$REGION" \ --instance-ids "$INSTANCE_ID" \ --query 'Reservations[0].Instances[0].State.Name' \ --output text) if [ "$STATE" = "stopped" ]; then echo " Instance is stopped — starting it..." aws ec2 start-instances --region "$REGION" --instance-ids "$INSTANCE_ID" --output text > /dev/null fi else echo " Launching staging EC2 instance..." INSTANCE_ID=$(aws ec2 run-instances \ --region "$REGION" \ --image-id "$AMI_ID" \ --instance-type "$INSTANCE_TYPE" \ --key-name "$KEY_NAME" \ --security-group-ids "$SG_ID" \ --block-device-mappings '[{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":20,"VolumeType":"gp3","DeleteOnTermination":true}}]' \ --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$INSTANCE_NAME},{Key=Environment,Value=staging}]" \ --query 'Instances[0].InstanceId' \ --output text) echo " Instance ID: $INSTANCE_ID" fi echo "==> Waiting for instance to reach running state..." aws ec2 wait instance-running --region "$REGION" --instance-ids "$INSTANCE_ID" echo " Instance is running" # ── Elastic IP ──────────────────────────────────────────────────────────────── echo "==> [4/5] Checking Elastic IP..." EXISTING_EIP=$(aws ec2 describe-addresses \ --region "$REGION" \ --filters "Name=instance-id,Values=$INSTANCE_ID" \ --query 'Addresses[0].PublicIp' \ --output text) if [ "$EXISTING_EIP" != "None" ] && [ -n "$EXISTING_EIP" ]; then echo " Elastic IP already associated: $EXISTING_EIP" PUBLIC_IP="$EXISTING_EIP" else echo " Allocating new Elastic IP..." ALLOC_ID=$(aws ec2 allocate-address \ --region "$REGION" \ --domain vpc \ --tag-specifications "ResourceType=elastic-ip,Tags=[{Key=Name,Value=musehub-staging-eip},{Key=Environment,Value=staging}]" \ --query 'AllocationId' \ --output text) echo " Allocation ID: $ALLOC_ID" aws ec2 associate-address \ --region "$REGION" \ --instance-id "$INSTANCE_ID" \ --allocation-id "$ALLOC_ID" \ --output text > /dev/null PUBLIC_IP=$(aws ec2 describe-addresses \ --region "$REGION" \ --allocation-ids "$ALLOC_ID" \ --query 'Addresses[0].PublicIp' \ --output text) fi # ── Summary ──────────────────────────────────────────────────────────────────── echo "" echo "============================================================" echo " STAGING INSTANCE READY" echo "============================================================" echo " Instance ID : $INSTANCE_ID" echo " Elastic IP : $PUBLIC_IP" echo " Key file : ~/.ssh/${KEY_NAME}.pem" echo "" echo " Next steps:" echo "" echo " 1. Add DNS record on Namecheap (musehub.ai, Advanced DNS):" echo " Type: A Record | Host: staging | Value: $PUBLIC_IP | TTL: Automatic" echo "" echo " 2. Wait for DNS propagation:" echo " watch -n 10 'dig staging.musehub.ai +short'" echo "" echo " 3. Copy deploy scripts to the instance:" echo " scp -i ~/.ssh/${KEY_NAME}.pem -r deploy/ ubuntu@${PUBLIC_IP}:/home/ubuntu/" echo "" echo " 4. SSH in and run the setup script:" echo " ssh -i ~/.ssh/${KEY_NAME}.pem ubuntu@${PUBLIC_IP}" echo " chmod +x ~/deploy/setup-ec2-staging.sh && ~/deploy/setup-ec2-staging.sh" echo "" echo " 5. Sync code to staging:" echo " rsync -az --exclude='.env' --exclude='node_modules/' --exclude='__pycache__/' \\" echo " --exclude='.muse/' --exclude='*.pyc' \\" echo " -e 'ssh -i ~/.ssh/${KEY_NAME}.pem' \\" echo " ~/musehub/ ubuntu@${PUBLIC_IP}:/opt/musehub/" echo "" echo " 6. On the instance: rebuild and start" echo " ssh -i ~/.ssh/${KEY_NAME}.pem ubuntu@${PUBLIC_IP} \\" echo " 'cd /opt/musehub && sudo docker compose up -d --build'" echo "" echo " Update docs/infrastructure.md with the staging IP: $PUBLIC_IP" echo "============================================================"