-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·238 lines (201 loc) · 8.77 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·238 lines (201 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env bash
# =============================================================================
# Development Process Navigator — One-Click Deployment Script
#
# Usage:
# ./deploy.sh # deploy to default AWS profile / us-east-1
# AWS_PROFILE=myprofile ./deploy.sh
# AWS_REGION=us-west-2 ./deploy.sh
#
# Works in AWS CloudShell (no local tooling needed) or any machine with:
# - AWS CLI v2
# - Node.js 18+
# - Python 3.12+
# =============================================================================
set -euo pipefail
# ---- Configuration ----------------------------------------------------------
PROFILE="${AWS_PROFILE:-}"
REGION="${AWS_REGION:-us-east-1}"
STACK_NAME="ProcessCanvasStack"
SES_EMAIL_DOMAIN="${SES_EMAIL_DOMAIN:-wpcarey.asu.edu}"
# Build profile flag (empty if no profile set — works for CloudShell)
PROFILE_FLAG=""
if [ -n "$PROFILE" ]; then
PROFILE_FLAG="--profile $PROFILE"
fi
# Colours
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; NC='\033[0m'
log() { echo -e "${CYAN}[deploy]${NC} $1"; }
ok() { echo -e "${GREEN}[ ok ]${NC} $1"; }
warn() { echo -e "${YELLOW}[ warn ]${NC} $1"; }
fail() { echo -e "${RED}[ fail ]${NC} $1"; exit 1; }
# ---- Pre-flight checks -------------------------------------------------------
log "Checking prerequisites..."
command -v aws >/dev/null 2>&1 || fail "AWS CLI not found. Install from https://aws.amazon.com/cli/"
command -v node >/dev/null 2>&1 || fail "Node.js not found. Install v18+ from https://nodejs.org"
command -v npm >/dev/null 2>&1 || fail "npm not found."
command -v python3 >/dev/null 2>&1 || fail "Python 3 not found."
NODE_VER=$(node --version | sed 's/v//' | cut -d. -f1)
if [ "$NODE_VER" -lt 18 ]; then
fail "Node.js 18+ required (found v$NODE_VER)"
fi
AWS_ACCOUNT=$(aws sts get-caller-identity $PROFILE_FLAG --query Account --output text 2>/dev/null) \
|| AWS_ACCOUNT=$(aws sts get-caller-identity --query Account --output text 2>/dev/null) \
|| fail "AWS credentials not configured. Run: aws configure"
ok "AWS account: $AWS_ACCOUNT | region: $REGION | profile: ${PROFILE:-default}"
# ---- Install CDK CLI ---------------------------------------------------------
log "Installing/updating AWS CDK CLI..."
# Install locally to avoid permission issues in CloudShell
npx --yes aws-cdk@latest --version >/dev/null 2>&1
ok "CDK available via npx"
# ---- Bootstrap CDK (idempotent) ----------------------------------------------
log "Bootstrapping CDK in $AWS_ACCOUNT/$REGION (safe to re-run)..."
cd "$(dirname "$0")/infrastructure"
npm install --quiet
npx aws-cdk@latest bootstrap "aws://$AWS_ACCOUNT/$REGION" $PROFILE_FLAG --quiet || \
warn "Bootstrap returned non-zero (may already be bootstrapped — continuing)"
# ---- Deploy CDK stack --------------------------------------------------------
log "Deploying CDK stack: $STACK_NAME..."
npx aws-cdk@latest deploy "$STACK_NAME" \
$PROFILE_FLAG \
--require-approval never \
--context sesEmailDomain="$SES_EMAIL_DOMAIN" \
--outputs-file /tmp/cdk-outputs.json
ok "CDK stack deployed."
# ---- Extract stack outputs ---------------------------------------------------
REST_API_URL=$(python3 -c "
import json
outputs = json.load(open('/tmp/cdk-outputs.json'))
stack = outputs.get('$STACK_NAME', {})
for k, v in stack.items():
if 'RestApiUrl' in k or 'RestApi' in k:
print(v.rstrip('/'))
break
" 2>/dev/null || echo "")
AMPLIFY_APP_ID=$(python3 -c "
import json
outputs = json.load(open('/tmp/cdk-outputs.json'))
stack = outputs.get('$STACK_NAME', {})
for k, v in stack.items():
if 'AmplifyAppId' in k:
print(v)
break
" 2>/dev/null || echo "")
WS_API_URL=$(python3 -c "
import json
outputs = json.load(open('/tmp/cdk-outputs.json'))
stack = outputs.get('$STACK_NAME', {})
for k, v in stack.items():
if 'WsApiUrl' in k:
print(v)
break
" 2>/dev/null || echo "")
if [ -z "$REST_API_URL" ] || [ -z "$AMPLIFY_APP_ID" ]; then
warn "Could not parse stack outputs from /tmp/cdk-outputs.json"
warn "You can find the outputs in the AWS CloudFormation console under stack: $STACK_NAME"
REST_API_URL="<RestApiUrl from CloudFormation outputs>"
AMPLIFY_APP_ID="<AmplifyAppId from CloudFormation outputs>"
fi
ok "REST API URL : $REST_API_URL"
ok "Amplify App : $AMPLIFY_APP_ID"
# ---- Set APP_URL on the Lambda (for invite emails) ---------------------------
if [ -n "$AMPLIFY_APP_ID" ] && [ "$AMPLIFY_APP_ID" != "<AmplifyAppId from CloudFormation outputs>" ]; then
FRONTEND_URL="https://main.${AMPLIFY_APP_ID}.amplifyapp.com"
LAMBDA_NAME=$(aws lambda list-functions $PROFILE_FLAG --region "$REGION" \
--query "Functions[?contains(FunctionName,'ProcessCanvas') && contains(FunctionName,'ApiFn')].FunctionName" \
--output text 2>/dev/null || echo "")
if [ -n "$LAMBDA_NAME" ]; then
aws lambda update-function-configuration \
--function-name "$LAMBDA_NAME" \
--environment "Variables={$(aws lambda get-function-configuration --function-name "$LAMBDA_NAME" $PROFILE_FLAG --region "$REGION" --query 'Environment.Variables' --output json | python3 -c "import sys,json; d=json.load(sys.stdin); d['APP_URL']='$FRONTEND_URL'; print(','.join(f'{k}={v}' for k,v in d.items()))")}" \
$PROFILE_FLAG --region "$REGION" >/dev/null 2>&1 && \
ok "Set APP_URL=$FRONTEND_URL on Lambda" || \
warn "Could not set APP_URL on Lambda — set it manually in the Lambda console"
fi
fi
# ---- Build frontend ----------------------------------------------------------
log "Building Next.js frontend..."
cd ../frontend
# Free up disk space (CDK node_modules no longer needed after deploy)
rm -rf ../infrastructure/node_modules
npm install --quiet
# Write env vars for the static build
cat > .env.production << EOF
NEXT_PUBLIC_API_URL=$REST_API_URL
NEXT_PUBLIC_WS_URL=$WS_API_URL
NEXT_PUBLIC_REGION=$REGION
EOF
npm run build
ok "Frontend built → frontend/out/"
# ---- Deploy frontend to Amplify ----------------------------------------------
log "Deploying frontend to Amplify (app: $AMPLIFY_APP_ID)..."
# Zip from inside out/ so index.html is at the root of the zip
cd out
zip -r ../deploy.zip . -q
cd ..
DEPLOY_OUT=$(aws amplify create-deployment \
--app-id "$AMPLIFY_APP_ID" \
--branch-name main \
$PROFILE_FLAG \
--region "$REGION" \
--output json)
JOB_ID=$(echo "$DEPLOY_OUT" | python3 -c 'import sys,json;print(json.load(sys.stdin)["jobId"])')
UPLOAD_URL=$(echo "$DEPLOY_OUT" | python3 -c 'import sys,json;print(json.load(sys.stdin)["zipUploadUrl"])')
log "Uploading frontend artifact (job: $JOB_ID)..."
curl -s -X PUT "$UPLOAD_URL" --upload-file deploy.zip
echo ""
aws amplify start-deployment \
--app-id "$AMPLIFY_APP_ID" \
--branch-name main \
--job-id "$JOB_ID" \
$PROFILE_FLAG \
--region "$REGION" >/dev/null
log "Waiting for Amplify deployment to complete..."
for i in $(seq 1 40); do
STATUS=$(aws amplify get-job \
--app-id "$AMPLIFY_APP_ID" \
--branch-name main \
--job-id "$JOB_ID" \
$PROFILE_FLAG \
--region "$REGION" \
--query "job.summary.status" \
--output text 2>/dev/null || echo "UNKNOWN")
if [ "$STATUS" = "SUCCEED" ]; then
ok "Amplify deployment succeeded."
break
elif [ "$STATUS" = "FAILED" ] || [ "$STATUS" = "CANCELLED" ]; then
fail "Amplify deployment $STATUS. Check the Amplify console for details."
fi
echo -ne "\r Status: $STATUS (${i}/40)..."
sleep 5
done
echo ""
# ---- Cleanup -----------------------------------------------------------------
rm -f .env.production deploy.zip
# ---- Print summary -----------------------------------------------------------
FRONTEND_URL="https://main.${AMPLIFY_APP_ID}.amplifyapp.com"
echo ""
echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN} Deployment Complete!${NC}"
echo -e "${GREEN}============================================${NC}"
echo -e " Frontend URL : ${CYAN}${FRONTEND_URL}${NC}"
echo -e " REST API : ${CYAN}${REST_API_URL}${NC}"
if [ -n "$WS_API_URL" ]; then
echo -e " WebSocket : ${CYAN}${WS_API_URL}${NC}"
fi
echo -e " AWS Account : ${CYAN}${AWS_ACCOUNT}${NC}"
echo -e " Region : ${CYAN}${REGION}${NC}"
echo -e "${GREEN}============================================${NC}"
echo ""
echo " Instructor Access Code: Set in infrastructure/lib/processcanvas-stack.ts (INSTRUCTOR_ACCESS_CODE)"
echo " (Change in infrastructure/lib/processcanvas-stack.ts → INSTRUCTOR_ACCESS_CODE)"
echo ""
echo -e "${GREEN}Open the app:${NC} $FRONTEND_URL"
echo ""
echo -e "${YELLOW}IMPORTANT — Email Setup:${NC}"
echo " To enable instructor invite emails, verify the email domain in SES:"
echo " 1. Go to SES Console → Verified Identities"
echo " 2. Find 'asu.edu' (or your domain) → Authentication tab"
echo " 3. Add the 3 DKIM CNAME records to your DNS"
echo " 4. Once verified, instructors can send invites from their @asu.edu email"
echo ""