gabriel / musehub public
bootstrap-instance.sh bash
94 lines 3.4 KB
Raw
sha256:a10adeeb7a0169cb9900f9806ed7a973047258abb6283724fe55e8eb68ff3f0a init: musehub initial commit Human 71 days ago
1 #!/usr/bin/env bash
2 # One-time bootstrap for a MuseHub EC2 instance.
3 #
4 # Installs AWS CLI v2 on the instance via SSM so that deploy.sh can
5 # authenticate with ECR (aws ecr get-login-password).
6 #
7 # Run this once per instance — staging and prod each need it.
8 # After this, all deploys go through deploy/push.sh.
9 #
10 # Usage:
11 # bash deploy/bootstrap-instance.sh staging
12 # bash deploy/bootstrap-instance.sh prod
13
14 set -euo pipefail
15
16 REGION="us-east-1"
17 STAGING_INSTANCE="i-07547cd20bee2dea5"
18 PROD_INSTANCE="i-0855d6efe7fa1a49d"
19
20 case "${1:-}" in
21 staging) INSTANCE_ID="$STAGING_INSTANCE"; ENV="staging" ;;
22 prod) INSTANCE_ID="$PROD_INSTANCE"; ENV="prod" ;;
23 *)
24 echo "Usage: bash deploy/bootstrap-instance.sh [staging|prod]"
25 exit 1
26 ;;
27 esac
28
29 log() { echo "[bootstrap] $*"; }
30 die() { echo "[bootstrap] ERROR: $*" >&2; exit 1; }
31
32 log "Bootstrapping $ENV ($INSTANCE_ID)..."
33
34 CMD_ID=$(aws ssm send-command \
35 --region "$REGION" \
36 --instance-ids "$INSTANCE_ID" \
37 --document-name "AWS-RunShellScript" \
38 --parameters 'commands=[
39 "if command -v aws >/dev/null 2>&1; then echo \"[bootstrap] AWS CLI already installed: $(aws --version)\"; aws ecr get-login-password --region us-east-1 | sudo docker login --username AWS --password-stdin 992382692655.dkr.ecr.us-east-1.amazonaws.com && echo \"[bootstrap] ECR login OK.\"; exit 0; fi",
40 "echo \"[bootstrap] Installing dependencies...\"",
41 "sudo apt-get install -y unzip curl 2>&1 | tail -2",
42 "echo \"[bootstrap] Installing AWS CLI v2...\"",
43 "cd /tmp && curl -fsSL https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip && unzip -q awscliv2.zip && sudo ./aws/install && rm -rf awscliv2.zip aws/",
44 "echo \"[bootstrap] Installed: $(aws --version)\"",
45 "echo \"[bootstrap] Verifying ECR access...\"",
46 "aws ecr get-login-password --region us-east-1 | sudo docker login --username AWS --password-stdin 992382692655.dkr.ecr.us-east-1.amazonaws.com && echo \"[bootstrap] ECR login OK.\"",
47 "echo \"[bootstrap] Done.\""
48 ]' \
49 --comment "musehub bootstrap: install AWS CLI" \
50 --timeout-seconds 300 \
51 --query "Command.CommandId" \
52 --output text)
53
54 log "SSM command ID: $CMD_ID"
55 log "Waiting for bootstrap to complete..."
56
57 for i in $(seq 1 60); do
58 sleep 5
59 STATUS=$(aws ssm get-command-invocation \
60 --region "$REGION" \
61 --command-id "$CMD_ID" \
62 --instance-id "$INSTANCE_ID" \
63 --query "Status" \
64 --output text 2>/dev/null || echo "Pending")
65
66 case "$STATUS" in
67 Success)
68 log "✅ Bootstrap complete."
69 echo ""
70 aws ssm get-command-invocation \
71 --region "$REGION" \
72 --command-id "$CMD_ID" \
73 --instance-id "$INSTANCE_ID" \
74 --query "StandardOutputContent" \
75 --output text 2>/dev/null || true
76 exit 0
77 ;;
78 Failed|Cancelled|TimedOut)
79 log "❌ Bootstrap FAILED (status: $STATUS)"
80 aws ssm get-command-invocation \
81 --region "$REGION" \
82 --command-id "$CMD_ID" \
83 --instance-id "$INSTANCE_ID" \
84 --query "[StandardOutputContent, StandardErrorContent]" \
85 --output text 2>/dev/null || true
86 exit 1
87 ;;
88 *)
89 printf "."
90 ;;
91 esac
92 done
93
94 die "Bootstrap timed out. Check SSM command: $CMD_ID"
File History 1 commit
sha256:a10adeeb7a0169cb9900f9806ed7a973047258abb6283724fe55e8eb68ff3f0a init: musehub initial commit Human 71 days ago