Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·336 lines (300 loc) · 11.2 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·336 lines (300 loc) · 11.2 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/bash
# Where curl discards a response body. Git Bash and MSYS2 shells hand a Windows curl /dev/null
# unchanged when MSYS_NO_PATHCONV=1 is set, as the README asks, and it cannot open that path, so it
# gets the NUL device instead; MSYS2's own curl, under /usr/bin, opens /dev/null as on Linux and macOS.
null_device() {
if command -v cygpath >/dev/null 2>&1 && [[ "$(command -v curl)" != /usr/bin/* ]]; then
echo NUL
else
echo /dev/null
fi
}
# Variables
PREFIX="${PREFIX:-local}"
SUFFIX="${SUFFIX:-test}"
TEMPLATE="main.bicep"
PARAMETERS="main.bicepparam"
RESOURCE_GROUP_NAME="${PREFIX}-rg"
LOCATION="westeurope"
VALIDATE_TEMPLATE=1
USE_WHAT_IF=0
SUBSCRIPTION_NAME=$(az account show --query name --output tsv)
MYSQL_ADMIN_USER="${MYSQL_ADMIN_USER:-myadmin}"
MYSQL_ADMIN_PASSWORD="${MYSQL_ADMIN_PASSWORD:-P@ssw0rd1234!}"
MYSQL_APP_USER="${MYSQL_APP_USER:-testuser}"
MYSQL_APP_PASSWORD="${MYSQL_APP_PASSWORD:-TestP@ssw0rd123}"
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
ZIPFILE="planner_website.zip"
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Validates if the resource group exists in the subscription, if not creates it
echo "Checking if resource group [$RESOURCE_GROUP_NAME] exists in the subscription [$SUBSCRIPTION_NAME]..."
az group show --name $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No resource group [$RESOURCE_GROUP_NAME] exists in the subscription [$SUBSCRIPTION_NAME]"
echo "Creating resource group [$RESOURCE_GROUP_NAME] in the subscription [$SUBSCRIPTION_NAME]..."
# Create the resource group
az group create \
--name $RESOURCE_GROUP_NAME \
--location $LOCATION \
--only-show-errors 1> /dev/null
if [[ $? == 0 ]]; then
echo "Resource group [$RESOURCE_GROUP_NAME] successfully created in the subscription [$SUBSCRIPTION_NAME]"
else
echo "Failed to create resource group [$RESOURCE_GROUP_NAME] in the subscription [$SUBSCRIPTION_NAME]"
exit 1
fi
else
echo "Resource group [$RESOURCE_GROUP_NAME] already exists in the subscription [$SUBSCRIPTION_NAME]"
fi
# Validates the Bicep template
if [[ $VALIDATE_TEMPLATE == 1 ]]; then
if [[ $USE_WHAT_IF == 1 ]]; then
# Execute a deployment What-If operation at resource group scope.
echo "Previewing changes deployed by Bicep template [$TEMPLATE]..."
az deployment group what-if \
--resource-group $RESOURCE_GROUP_NAME \
--template-file $TEMPLATE \
--parameters $PARAMETERS \
--parameters location=$LOCATION \
prefix=$PREFIX \
suffix=$SUFFIX \
mysqlAdminPassword="$MYSQL_ADMIN_PASSWORD" \
--only-show-errors
if [[ $? == 0 ]]; then
echo "Bicep template [$TEMPLATE] validation succeeded"
else
echo "Failed to validate Bicep template [$TEMPLATE]"
exit 1
fi
else
# Validate the Bicep template
echo "Validating Bicep template [$TEMPLATE]..."
output=$(az deployment group validate \
--resource-group $RESOURCE_GROUP_NAME \
--template-file $TEMPLATE \
--parameters $PARAMETERS \
--parameters location=$LOCATION \
prefix=$PREFIX \
suffix=$SUFFIX \
mysqlAdminPassword="$MYSQL_ADMIN_PASSWORD" \
--only-show-errors)
if [[ $? == 0 ]]; then
echo "Bicep template [$TEMPLATE] validation succeeded"
else
echo "Failed to validate Bicep template [$TEMPLATE]"
echo "$output"
exit 1
fi
fi
fi
# Deploy the Bicep template
echo "Deploying Bicep template [$TEMPLATE]..."
if DEPLOYMENT_OUTPUTS=$(az deployment group create \
--resource-group $RESOURCE_GROUP_NAME \
--only-show-errors \
--template-file $TEMPLATE \
--parameters $PARAMETERS \
--parameters location=$LOCATION \
prefix=$PREFIX \
suffix=$SUFFIX \
mysqlAdminPassword="$MYSQL_ADMIN_PASSWORD" \
--query 'properties.outputs' -o json); then
# Extract only the JSON portion (everything from first { to the end)
DEPLOYMENT_JSON=$(echo "$DEPLOYMENT_OUTPUTS" | sed -n '/{/,$ p')
echo "Bicep template [$TEMPLATE] deployed successfully. Outputs:"
echo "$DEPLOYMENT_JSON" | jq .
WEB_APP_NAME=$(echo "$DEPLOYMENT_JSON" | jq -r '.webAppName.value')
MYSQL_SERVER_NAME=$(echo "$DEPLOYMENT_JSON" | jq -r '.mysqlServerName.value')
MYSQL_FQDN_FULL=$(echo "$DEPLOYMENT_JSON" | jq -r '.mysqlFqdn.value')
DATABASE_NAME=$(echo "$DEPLOYMENT_JSON" | jq -r '.databaseName.value')
echo "Deployment details:"
echo "Web App Name: $WEB_APP_NAME"
echo "MySQL Server Name: $MYSQL_SERVER_NAME"
echo "MySQL FQDN: $MYSQL_FQDN_FULL"
echo "Database Name: $DATABASE_NAME"
else
echo "Failed to deploy Bicep template [$TEMPLATE]"
exit 1
fi
if [[ -z "$WEB_APP_NAME" || -z "$MYSQL_SERVER_NAME" ]]; then
echo "Web App Name or MySQL Server Name is empty. Exiting."
exit 1
fi
# Split host:port — the LocalStack emulator embeds the dynamically allocated TCP-proxy port
# directly in fullyQualifiedDomainName, mirroring the storage / container registry emulators.
# Real Azure returns just the bare host so MYSQL_PORT defaults to 3306.
MYSQL_FQDN="${MYSQL_FQDN_FULL%%:*}"
if [[ "$MYSQL_FQDN_FULL" == *:* ]]; then
MYSQL_PORT="${MYSQL_FQDN_FULL##*:}"
else
MYSQL_PORT=3306
fi
echo "MySQL host = $MYSQL_FQDN, port = $MYSQL_PORT"
# Check if the MySQL CLI is installed
MYSQL_CHECK=$(command -v mysql)
if [[ -z "$MYSQL_CHECK" ]]; then
echo "[mysql] CLI is not installed. Install it with: sudo apt install -y mysql-client" >&2
exit 1
fi
# Create application user [$MYSQL_APP_USER] on the MySQL flexible server
echo "Creating login [$MYSQL_APP_USER] on the [$MYSQL_SERVER_NAME] MySQL flexible server..."
MYSQL_PWD="$MYSQL_ADMIN_PASSWORD" mysql \
--host="$MYSQL_FQDN" \
--port="$MYSQL_PORT" \
--user="$MYSQL_ADMIN_USER" \
--protocol=TCP \
-e "CREATE USER IF NOT EXISTS '$MYSQL_APP_USER'@'%' IDENTIFIED BY '$MYSQL_APP_PASSWORD';
GRANT ALL PRIVILEGES ON \`$DATABASE_NAME\`.* TO '$MYSQL_APP_USER'@'%';
FLUSH PRIVILEGES;"
if [ $? -eq 0 ]; then
echo "Login [$MYSQL_APP_USER] created successfully"
else
echo "Failed to create login [$MYSQL_APP_USER]"
exit 1
fi
# Test connection
echo "Testing connection with user [$MYSQL_APP_USER]..."
MYSQL_PWD="$MYSQL_APP_PASSWORD" mysql \
--host="$MYSQL_FQDN" \
--port="$MYSQL_PORT" \
--user="$MYSQL_APP_USER" \
--protocol=TCP \
--database="$DATABASE_NAME" \
-e "SELECT CURRENT_USER() AS user_name, DATABASE() AS db_name, NOW() AS server_time;"
if [ $? -eq 0 ]; then
echo "Connection test successful with user [$MYSQL_APP_USER]"
else
echo "Connection test failed with user [$MYSQL_APP_USER]"
exit 1
fi
# Create [activities] table
echo "Creating [activities] table in the [$DATABASE_NAME] database..."
MYSQL_PWD="$MYSQL_APP_PASSWORD" mysql \
--host="$MYSQL_FQDN" \
--port="$MYSQL_PORT" \
--user="$MYSQL_APP_USER" \
--protocol=TCP \
--database="$DATABASE_NAME" \
-e "CREATE TABLE IF NOT EXISTS activities (
id VARCHAR(32) NOT NULL,
username VARCHAR(255) NOT NULL,
activity TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
INDEX idx_activities_username (username),
INDEX idx_activities_created_at (created_at DESC)
);"
if [ $? -eq 0 ]; then
echo "[activities] table created successfully"
else
echo "Failed to create [activities] table"
exit 1
fi
# Insert sample data
echo "Inserting sample data into [activities] table..."
MYSQL_PWD="$MYSQL_APP_PASSWORD" mysql \
--host="$MYSQL_FQDN" \
--port="$MYSQL_PORT" \
--user="$MYSQL_APP_USER" \
--protocol=TCP \
--database="$DATABASE_NAME" \
-e "INSERT IGNORE INTO activities (id, username, activity) VALUES
(MD5('paolo_pisa_seed'), 'paolo', 'Visit the Leaning Tower in Pisa'),
(MD5('paolo_volterra_seed'), 'paolo', 'Explore Etruscan walls in Volterra'),
(MD5('paolo_san_gimignano_seed'), 'paolo', 'Climb Torre Grossa in San Gimignano'),
(MD5('paolo_siena_seed'), 'paolo', 'Walk across Piazza del Campo in Siena'),
(MD5('paolo_montalcino_seed'), 'paolo', 'Taste Brunello wine in Montalcino'),
(MD5('paolo_pienza_seed'), 'paolo', 'Sample Pecorino cheese in Pienza'),
(MD5('paolo_florence_seed'), 'paolo', 'Admire Michelangelo''s David in Florence'),
(MD5('paolo_viareggio_beach_seed'), 'paolo', 'Relax by the beach in Viareggio'),
(MD5('paolo_viareggio_promenade_seed'), 'paolo', 'Stroll along the Viareggio promenade');"
if [ $? -eq 0 ]; then
echo "Sample data inserted successfully into [activities] table"
else
echo "Failed to insert sample data into [activities] table"
exit 1
fi
# Query sample data
echo "Querying sample data from [activities] table..."
MYSQL_PWD="$MYSQL_APP_PASSWORD" mysql \
--host="$MYSQL_FQDN" \
--port="$MYSQL_PORT" \
--user="$MYSQL_APP_USER" \
--protocol=TCP \
--database="$DATABASE_NAME" \
-e "SELECT * FROM activities;"
if [ $? -eq 0 ]; then
echo "Sample data queried successfully from [activities] table"
else
echo "Failed to query sample data from [activities] table"
exit 1
fi
# Set MYSQL_USER + MYSQL_PASSWORD on the web app to point at the application user
echo "Setting MYSQL_USER=[$MYSQL_APP_USER] and MYSQL_PASSWORD on the [$WEB_APP_NAME] web app..."
az webapp config appsettings set \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$WEB_APP_NAME" \
--settings MYSQL_USER="$MYSQL_APP_USER" MYSQL_PASSWORD="$MYSQL_APP_PASSWORD" \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "MYSQL_USER and MYSQL_PASSWORD set successfully on the [$WEB_APP_NAME] web app"
else
echo "Failed to set MYSQL_USER and MYSQL_PASSWORD on the [$WEB_APP_NAME] web app"
exit 1
fi
# Print the application settings of the web app
echo "Retrieving application settings for web app [$WEB_APP_NAME]..."
az webapp config appsettings list \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$WEB_APP_NAME"
# Change current directory to source folder
cd "../src" || exit
# Remove any existing zip package of the web app
if [ -f "$ZIPFILE" ]; then
rm "$ZIPFILE"
fi
# Create the zip package of the web app
echo "Creating zip package of the web app..."
zip -r "$ZIPFILE" app.py database.py gunicorn.conf.py static templates requirements.txt
# Deploy the web app
# Deploy the web app
echo "Deploying web app [$WEB_APP_NAME] with zip file [$ZIPFILE]..."
az webapp deploy \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$WEB_APP_NAME" \
--src-path "$ZIPFILE" \
--type zip \
--async true 1>/dev/null
# Remove the zip package of the web app
if [ -f "$ZIPFILE" ]; then
rm "$ZIPFILE"
fi
# Print the list of resources in the resource group
echo "Listing resources in resource group [$RESOURCE_GROUP_NAME]..."
az resource list --resource-group "$RESOURCE_GROUP_NAME" --output table
# Ping the web app to confirm the deployment is reachable
echo "Getting the default hostname of the [$WEB_APP_NAME] web app..."
WEB_APP_HOSTNAME=$(az webapp show \
--name "$WEB_APP_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query defaultHostName \
--output tsv \
--only-show-errors)
WEB_APP_URL="http://${WEB_APP_HOSTNAME}"
echo "You can visit your app at: $WEB_APP_URL"
echo "Pinging [$WEB_APP_URL] to verify the web app responds..."
HTTP_CODE="000"
for attempt in $(seq 1 12); do
HTTP_CODE=$(curl -s -o "$(null_device)" -w '%{http_code}' --max-time 10 "$WEB_APP_URL" || echo "000")
if [ "$HTTP_CODE" = "200" ]; then
echo "Web app responded with HTTP 200. Deployment verified successfully."
break
fi
echo "Web app not ready yet (attempt $attempt/12, HTTP $HTTP_CODE)..."
sleep 5
done
if [ "$HTTP_CODE" != "200" ]; then
echo "Web app did not return HTTP 200 after 12 attempts (last code: $HTTP_CODE). Deployment verification failed."
exit 1
fi