gabriel / musehub public
aws-provision-staging.sh bash
156 lines 6.4 KB
Raw
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2 feat: add repair-commit wire endpoint (API parity with repa… Opus 4.8 minor ⚠ breaking 1 day ago
1 #!/usr/bin/env bash
2 # Run this locally to provision a staging EC2 instance for staging.musehub.ai.
3 # Mirrors the prod setup (same AMI, SG, key pair) with a separate instance + EIP.
4 #
5 # Prerequisites:
6 # brew install awscli
7 # aws configure (Access Key ID, Secret, region=us-east-1, output=json)
8 # ~/.ssh/musehub-key.pem must exist (created by aws-provision.sh)
9 #
10 # Usage:
11 # chmod +x deploy/aws-provision-staging.sh
12 # ./deploy/aws-provision-staging.sh
13
14 set -euo pipefail
15
16 export AWS_PAGER=""
17
18 REGION="us-east-1"
19 AMI_ID="ami-0c7217cdde317cfec" # Ubuntu 22.04 LTS (us-east-1)
20 INSTANCE_TYPE="t3.small"
21 KEY_NAME="musehub-key"
22 SG_ID="sg-05815872537fcfe76" # musehub-sg — shared with prod
23 INSTANCE_NAME="musehub-staging"
24
25 echo "==> [1/5] Checking key pair..."
26 if [ ! -f ~/.ssh/${KEY_NAME}.pem ]; then
27 echo "ERROR: ~/.ssh/${KEY_NAME}.pem not found."
28 echo " Run deploy/aws-provision.sh first to create the key pair."
29 exit 1
30 fi
31 echo " Key found: ~/.ssh/${KEY_NAME}.pem"
32
33 # ── SSH rule for current IP ────────────────────────────────────────────────────
34 echo "==> [2/5] Adding SSH inbound rule for your current IP..."
35 MY_IP=$(curl -s https://checkip.amazonaws.com)
36 echo " Your IP: $MY_IP"
37
38 aws ec2 authorize-security-group-ingress \
39 --region "$REGION" \
40 --group-id "$SG_ID" \
41 --protocol tcp --port 22 --cidr "${MY_IP}/32" \
42 --output text > /dev/null 2>&1 \
43 && echo " SSH rule added" \
44 || echo " SSH rule already exists, skipping"
45
46 # ── EC2 instance ──────────────────────────────────────────────────────────────
47 echo "==> [3/5] Checking for existing staging instance..."
48 EXISTING=$(aws ec2 describe-instances \
49 --region "$REGION" \
50 --filters "Name=tag:Name,Values=$INSTANCE_NAME" "Name=instance-state-name,Values=pending,running,stopped" \
51 --query 'Reservations[0].Instances[0].InstanceId' \
52 --output text)
53
54 if [ "$EXISTING" != "None" ] && [ -n "$EXISTING" ]; then
55 echo " Staging instance already exists: $EXISTING"
56 INSTANCE_ID="$EXISTING"
57
58 # Start it if stopped
59 STATE=$(aws ec2 describe-instances \
60 --region "$REGION" \
61 --instance-ids "$INSTANCE_ID" \
62 --query 'Reservations[0].Instances[0].State.Name' \
63 --output text)
64 if [ "$STATE" = "stopped" ]; then
65 echo " Instance is stopped — starting it..."
66 aws ec2 start-instances --region "$REGION" --instance-ids "$INSTANCE_ID" --output text > /dev/null
67 fi
68 else
69 echo " Launching staging EC2 instance..."
70 INSTANCE_ID=$(aws ec2 run-instances \
71 --region "$REGION" \
72 --image-id "$AMI_ID" \
73 --instance-type "$INSTANCE_TYPE" \
74 --key-name "$KEY_NAME" \
75 --security-group-ids "$SG_ID" \
76 --block-device-mappings '[{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":20,"VolumeType":"gp3","DeleteOnTermination":true}}]' \
77 --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$INSTANCE_NAME},{Key=Environment,Value=staging}]" \
78 --query 'Instances[0].InstanceId' \
79 --output text)
80 echo " Instance ID: $INSTANCE_ID"
81 fi
82
83 echo "==> Waiting for instance to reach running state..."
84 aws ec2 wait instance-running --region "$REGION" --instance-ids "$INSTANCE_ID"
85 echo " Instance is running"
86
87 # ── Elastic IP ────────────────────────────────────────────────────────────────
88 echo "==> [4/5] Checking Elastic IP..."
89 EXISTING_EIP=$(aws ec2 describe-addresses \
90 --region "$REGION" \
91 --filters "Name=instance-id,Values=$INSTANCE_ID" \
92 --query 'Addresses[0].PublicIp' \
93 --output text)
94
95 if [ "$EXISTING_EIP" != "None" ] && [ -n "$EXISTING_EIP" ]; then
96 echo " Elastic IP already associated: $EXISTING_EIP"
97 PUBLIC_IP="$EXISTING_EIP"
98 else
99 echo " Allocating new Elastic IP..."
100 ALLOC_ID=$(aws ec2 allocate-address \
101 --region "$REGION" \
102 --domain vpc \
103 --tag-specifications "ResourceType=elastic-ip,Tags=[{Key=Name,Value=musehub-staging-eip},{Key=Environment,Value=staging}]" \
104 --query 'AllocationId' \
105 --output text)
106 echo " Allocation ID: $ALLOC_ID"
107
108 aws ec2 associate-address \
109 --region "$REGION" \
110 --instance-id "$INSTANCE_ID" \
111 --allocation-id "$ALLOC_ID" \
112 --output text > /dev/null
113
114 PUBLIC_IP=$(aws ec2 describe-addresses \
115 --region "$REGION" \
116 --allocation-ids "$ALLOC_ID" \
117 --query 'Addresses[0].PublicIp' \
118 --output text)
119 fi
120
121 # ── Summary ────────────────────────────────────────────────────────────────────
122 echo ""
123 echo "============================================================"
124 echo " STAGING INSTANCE READY"
125 echo "============================================================"
126 echo " Instance ID : $INSTANCE_ID"
127 echo " Elastic IP : $PUBLIC_IP"
128 echo " Key file : ~/.ssh/${KEY_NAME}.pem"
129 echo ""
130 echo " Next steps:"
131 echo ""
132 echo " 1. Add DNS record on Namecheap (musehub.ai, Advanced DNS):"
133 echo " Type: A Record | Host: staging | Value: $PUBLIC_IP | TTL: Automatic"
134 echo ""
135 echo " 2. Wait for DNS propagation:"
136 echo " watch -n 10 'dig staging.musehub.ai +short'"
137 echo ""
138 echo " 3. Copy deploy scripts to the instance:"
139 echo " scp -i ~/.ssh/${KEY_NAME}.pem -r deploy/ ubuntu@${PUBLIC_IP}:/home/ubuntu/"
140 echo ""
141 echo " 4. SSH in and run the setup script:"
142 echo " ssh -i ~/.ssh/${KEY_NAME}.pem ubuntu@${PUBLIC_IP}"
143 echo " chmod +x ~/deploy/setup-ec2-staging.sh && ~/deploy/setup-ec2-staging.sh"
144 echo ""
145 echo " 5. Sync code to staging:"
146 echo " rsync -az --exclude='.env' --exclude='node_modules/' --exclude='__pycache__/' \\"
147 echo " --exclude='.muse/' --exclude='*.pyc' \\"
148 echo " -e 'ssh -i ~/.ssh/${KEY_NAME}.pem' \\"
149 echo " ~/musehub/ ubuntu@${PUBLIC_IP}:/opt/musehub/"
150 echo ""
151 echo " 6. On the instance: rebuild and start"
152 echo " ssh -i ~/.ssh/${KEY_NAME}.pem ubuntu@${PUBLIC_IP} \\"
153 echo " 'cd /opt/musehub && sudo docker compose up -d --build'"
154 echo ""
155 echo " Update docs/infrastructure.md with the staging IP: $PUBLIC_IP"
156 echo "============================================================"
File History 1 commit
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2 feat: add repair-commit wire endpoint (API parity with repa… Opus 4.8 minor 1 day ago