gabriel / musehub public
aws-provision-staging.sh bash
156 lines 6.4 KB
Raw
sha256:34035d72cef530c1ab9d6a6f53be18d803dde705fc3157617d70352a96d0747b Merge branch 'fix/wire-push-external-parent-manifest' into dev Human 8 days 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 14 commits
sha256:34035d72cef530c1ab9d6a6f53be18d803dde705fc3157617d70352a96d0747b Merge branch 'fix/wire-push-external-parent-manifest' into dev Human 8 days ago
sha256:fc04e4cae9e1774d6a21b65c45daeed0e6787eb581d13aa1b03bfe9384a34226 Merge branch 'fix/two-column-scroll-layout' into dev Human 8 days ago
sha256:408916fc5973ba59c6e4eebaa80ebdcc801c0a63205651e25009d11548f79454 chore: bump version to 0.2.0.dev2 — nightly.2, matching muse Sonnet 4.6 patch 11 days ago
sha256:d035733f21ccff27735fddebfbbe0ed24565a32a22db8de5885402262671ecd2 chore: bump version to 0.2.0rc15 for musehub#113 fix release Sonnet 4.6 patch 14 days ago
sha256:0032d6cfa33bc3c8367436ad768e7dd0e339b4332153160247da8266cb5fa352 Merge branch 'task/version-tags-phase3-server' into dev Human 17 days ago
sha256:4669620efda9ff41c55bdefd1f7bfe1c239d468428744c84ead9957e5a003a53 merge: rescue snapshot-recovery hardening (c00aa21d) into d… Opus 4.8 minor 29 days ago
sha256:a59da49c4611b970fc4b6ae48678ce4943261c213a07ddbd73ce9201df869b4a fix: remove false-positive proposal_comments index drop fro… Sonnet 4.6 patch 33 days ago
sha256:0a240d6dbff234f07d98a28a4a9a68db702f3f9ff9260196f24219bdb1c0b6f3 feat: render markdown mists as HTML with heading anchor links Sonnet 4.6 patch 34 days ago
sha256:24a7d47486ebc4ebd1832830580e177ec6f877b48dced8c000e198cdec4ce9d6 Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump … Human 35 days ago
sha256:b9ff931d147e0114a1f17060f415b89ed551c170a91ff226c70437aa5c85f9ee Merge 'task/bump-version-rc12' into 'dev' — proposal: Bump … Human 35 days ago
sha256:d1122d21e73471879b460037b22c0b50fded7c423444a176f248428f75dac39c Merge 'task/fix-issue-pagination-cursor' into 'dev' — propo… Human 35 days ago
sha256:01e18975e73d2b3cd5b6db7929c895bef9aa6e0d4391dc5b2adfc548b41318dd Merge 'feat/adding-debug-logs-to-staging' into 'dev' — prop… Human 35 days ago
sha256:6b1949fc2797ca4c1936a637a4cbfec828ef56cf52398a2e74ca3c4f494e728f fix: use wire_bytes not mpack_bytes_raw in compute_object_b… Sonnet 4.6 patch 48 days ago
sha256:b99f2455dc346966d040133f5203297e6e3ef5803a93728a2c30568d0a0f7583 rename: delta_add → delta_upsert across wire format, models… Sonnet 4.6 patch 50 days ago