-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
299 lines (264 loc) · 8.93 KB
/
deploy.sh
File metadata and controls
299 lines (264 loc) · 8.93 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/bash
# Pill Reminder - Deployment Script
# Deploys Cloud Functions, sets up Cloud Scheduler, and configures GCS bucket
set -e
# ============ CONFIGURATION ============
# These can be set as environment variables or edited here
PROJECT_ID="${GCP_PROJECT_ID:-}"
REGION="${GCP_REGION:-us-central1}"
BUCKET_NAME="${AUDIO_BUCKET_NAME:-}" # e.g., "my-pill-reminder-audios"
# Timezone for Cloud Scheduler (IANA format)
TIMEZONE="${TIMEZONE:-America/New_York}"
# ============ VALIDATION ============
echo "=== Pill Reminder Deployment ==="
echo ""
# Check required environment variables
MISSING=""
if [ -z "$PROJECT_ID" ]; then
MISSING="$MISSING GCP_PROJECT_ID"
fi
if [ -z "$TWILIO_SID" ]; then
MISSING="$MISSING TWILIO_SID"
fi
if [ -z "$TWILIO_TOKEN" ]; then
MISSING="$MISSING TWILIO_TOKEN"
fi
if [ -z "$CALLER_NUMBER" ]; then
MISSING="$MISSING CALLER_NUMBER"
fi
if [ -z "$RECIPIENT_NUMBER" ]; then
MISSING="$MISSING RECIPIENT_NUMBER"
fi
if [ -z "$BUCKET_NAME" ]; then
MISSING="$MISSING AUDIO_BUCKET_NAME"
fi
if [ -n "$MISSING" ]; then
echo "ERROR: Missing required environment variables:"
echo " $MISSING"
echo ""
echo "Copy .env.example to .env, fill in your values, then run:"
echo " source .env && ./deploy.sh"
exit 1
fi
# Optional variables with defaults
ALERT_NUMBER="${ALERT_NUMBER:-$CALLER_NUMBER}"
SHEET_ID="${SHEET_ID:-}"
QUEUE_NAME="${QUEUE_NAME:-pill-retries}"
# ============ DEPLOYMENT ============
# Set project
echo "1. Setting project to $PROJECT_ID..."
gcloud config set project $PROJECT_ID
# Enable required APIs
echo ""
echo "2. Enabling required APIs..."
gcloud services enable \
cloudfunctions.googleapis.com \
cloudscheduler.googleapis.com \
cloudtasks.googleapis.com \
secretmanager.googleapis.com \
storage.googleapis.com \
--quiet
# Create storage bucket for audios
echo ""
echo "3. Creating storage bucket for audio files..."
gsutil mb -l $REGION gs://$BUCKET_NAME 2>/dev/null || echo " Bucket already exists"
gsutil iam ch allUsers:objectViewer gs://$BUCKET_NAME
# Upload audio files if they exist
echo ""
echo "4. Uploading audio files (if any)..."
if ls audios/*.mp3 1> /dev/null 2>&1; then
for file in audios/*.mp3; do
gsutil cp "$file" gs://$BUCKET_NAME/
echo " Uploaded: $file"
done
else
echo " No .mp3 files found in audios/ - will use text-to-speech"
fi
AUDIO_BASE_URL="https://storage.googleapis.com/$BUCKET_NAME"
# Create Cloud Tasks queue
echo ""
echo "5. Creating Cloud Tasks queue..."
gcloud tasks queues create $QUEUE_NAME --location=$REGION 2>/dev/null || echo " Queue already exists"
# Get function URL base
FUNCTION_BASE_URL="https://$REGION-$PROJECT_ID.cloudfunctions.net"
# Build env vars string
ENV_VARS="TWILIO_SID=$TWILIO_SID"
ENV_VARS="$ENV_VARS,TWILIO_TOKEN=$TWILIO_TOKEN"
ENV_VARS="$ENV_VARS,CALLER_NUMBER=$CALLER_NUMBER"
ENV_VARS="$ENV_VARS,RECIPIENT_NUMBER=$RECIPIENT_NUMBER"
ENV_VARS="$ENV_VARS,ALERT_NUMBER=$ALERT_NUMBER"
ENV_VARS="$ENV_VARS,AUDIO_BASE_URL=$AUDIO_BASE_URL"
ENV_VARS="$ENV_VARS,GCP_PROJECT_ID=$PROJECT_ID"
ENV_VARS="$ENV_VARS,GCP_REGION=$REGION"
ENV_VARS="$ENV_VARS,QUEUE_NAME=$QUEUE_NAME"
ENV_VARS="$ENV_VARS,FUNCTION_BASE_URL=$FUNCTION_BASE_URL"
ENV_VARS="$ENV_VARS,TIMEZONE=$TIMEZONE"
if [ -n "$SHEET_ID" ]; then
ENV_VARS="$ENV_VARS,SHEET_ID=$SHEET_ID"
fi
# Deploy Cloud Functions
echo ""
echo "6. Deploying Cloud Functions..."
cd functions
# Main reminder function
echo " Deploying make-reminder-call..."
# WARNING: Production deployments should remove --allow-unauthenticated and use IAM auth
gcloud functions deploy make-reminder-call \
--gen2 \
--runtime=python311 \
--region=$REGION \
--source=. \
--entry-point=make_reminder_call \
--trigger-http \
--allow-unauthenticated \
--set-env-vars="$ENV_VARS" \
--quiet
# Confirmation call function
echo " Deploying make-confirmation-call..."
# WARNING: Production deployments should remove --allow-unauthenticated and use IAM auth
gcloud functions deploy make-confirmation-call \
--gen2 \
--runtime=python311 \
--region=$REGION \
--source=. \
--entry-point=make_confirmation_call \
--trigger-http \
--allow-unauthenticated \
--set-env-vars="$ENV_VARS" \
--quiet
# Handle response function
echo " Deploying handle-response..."
# WARNING: Production deployments should remove --allow-unauthenticated and use IAM auth
gcloud functions deploy handle-response \
--gen2 \
--runtime=python311 \
--region=$REGION \
--source=. \
--entry-point=handle_response \
--trigger-http \
--allow-unauthenticated \
--set-env-vars="$ENV_VARS" \
--quiet
# Handle no response function
echo " Deploying handle-no-response..."
# WARNING: Production deployments should remove --allow-unauthenticated and use IAM auth
gcloud functions deploy handle-no-response \
--gen2 \
--runtime=python311 \
--region=$REGION \
--source=. \
--entry-point=handle_no_response \
--trigger-http \
--allow-unauthenticated \
--set-env-vars="$ENV_VARS" \
--quiet
# Voice reminder TwiML endpoint
echo " Deploying voice-reminder..."
# WARNING: Production deployments should remove --allow-unauthenticated and use IAM auth
gcloud functions deploy voice-reminder \
--gen2 \
--runtime=python311 \
--region=$REGION \
--source=. \
--entry-point=voice_reminder \
--trigger-http \
--allow-unauthenticated \
--set-env-vars="AUDIO_BASE_URL=$AUDIO_BASE_URL" \
--quiet
# Voice confirmation TwiML endpoint
echo " Deploying voice-confirmation..."
# WARNING: Production deployments should remove --allow-unauthenticated and use IAM auth
gcloud functions deploy voice-confirmation \
--gen2 \
--runtime=python311 \
--region=$REGION \
--source=. \
--entry-point=voice_confirmation \
--trigger-http \
--allow-unauthenticated \
--set-env-vars="AUDIO_BASE_URL=$AUDIO_BASE_URL" \
--quiet
cd ..
# Get deployed function URL
REMINDER_URL="$FUNCTION_BASE_URL/make-reminder-call"
CONFIRMATION_URL="$FUNCTION_BASE_URL/make-confirmation-call"
# ============ CLOUD SCHEDULER ============
echo ""
echo "7. Setting up Cloud Scheduler jobs..."
# Morning reminder - customize time as needed
echo " Creating morning reminder (8:30 AM)..."
gcloud scheduler jobs create http pill-morning-reminder \
--location=$REGION \
--schedule="30 8 * * *" \
--time-zone="$TIMEZONE" \
--uri="${REMINDER_URL}?schedule=morning" \
--http-method=GET \
--attempt-deadline=300s \
2>/dev/null || gcloud scheduler jobs update http pill-morning-reminder \
--location=$REGION \
--schedule="30 8 * * *" \
--time-zone="$TIMEZONE" \
--uri="${REMINDER_URL}?schedule=morning"
# Morning confirmation - 5 minutes after reminder
echo " Creating morning confirmation (8:35 AM)..."
gcloud scheduler jobs create http pill-morning-confirmation \
--location=$REGION \
--schedule="35 8 * * *" \
--time-zone="$TIMEZONE" \
--uri="${CONFIRMATION_URL}?schedule=morning&retry=0" \
--http-method=GET \
--attempt-deadline=300s \
2>/dev/null || gcloud scheduler jobs update http pill-morning-confirmation \
--location=$REGION \
--schedule="35 8 * * *" \
--time-zone="$TIMEZONE" \
--uri="${CONFIRMATION_URL}?schedule=morning&retry=0"
# Noon reminder - customize time as needed
echo " Creating noon reminder (12:00 PM)..."
gcloud scheduler jobs create http pill-noon-reminder \
--location=$REGION \
--schedule="0 12 * * *" \
--time-zone="$TIMEZONE" \
--uri="${REMINDER_URL}?schedule=noon" \
--http-method=GET \
--attempt-deadline=300s \
2>/dev/null || gcloud scheduler jobs update http pill-noon-reminder \
--location=$REGION \
--schedule="0 12 * * *" \
--time-zone="$TIMEZONE" \
--uri="${REMINDER_URL}?schedule=noon"
# Noon confirmation - 5 minutes after reminder
echo " Creating noon confirmation (12:05 PM)..."
gcloud scheduler jobs create http pill-noon-confirmation \
--location=$REGION \
--schedule="5 12 * * *" \
--time-zone="$TIMEZONE" \
--uri="${CONFIRMATION_URL}?schedule=noon&retry=0" \
--http-method=GET \
--attempt-deadline=300s \
2>/dev/null || gcloud scheduler jobs update http pill-noon-confirmation \
--location=$REGION \
--schedule="5 12 * * *" \
--time-zone="$TIMEZONE" \
--uri="${CONFIRMATION_URL}?schedule=noon&retry=0"
# ============ SUMMARY ============
echo ""
echo "=== DEPLOYMENT COMPLETE ==="
echo ""
echo "Function URLs:"
echo " Reminder: $REMINDER_URL"
echo " Confirmation: $CONFIRMATION_URL"
echo ""
echo "Audio files: https://storage.googleapis.com/$BUCKET_NAME/"
echo ""
echo "Scheduled jobs (timezone: $TIMEZONE):"
echo " - Morning reminder: 8:30 AM"
echo " - Morning confirmation: 8:35 AM"
echo " - Noon reminder: 12:00 PM"
echo " - Noon confirmation: 12:05 PM"
echo ""
echo "To test manually:"
echo " curl '${REMINDER_URL}?schedule=morning&target=test'"
echo ""
echo "To view logs:"
echo " gcloud functions logs read make-reminder-call --region=$REGION"