Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·289 lines (253 loc) · 9.41 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·289 lines (253 loc) · 9.41 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
#!/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}"
LOCATION='westeurope'
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
# Intialize Terraform
echo "Initializing Terraform..."
terraform init -upgrade
# Run terraform plan and check for errors
echo "Planning Terraform deployment..."
terraform plan -out=tfplan \
-var "prefix=$PREFIX" \
-var "suffix=$SUFFIX" \
-var "location=$LOCATION" \
-var "mysql_admin_login=$MYSQL_ADMIN_USER" \
-var "mysql_admin_password=$MYSQL_ADMIN_PASSWORD"
if [[ $? != 0 ]]; then
echo "Terraform plan failed. Exiting."
exit 1
fi
# Apply the Terraform configuration
echo "Applying Terraform configuration..."
terraform apply -auto-approve tfplan
if [[ $? != 0 ]]; then
echo "Terraform apply failed. Exiting."
exit 1
fi
# Get the output values
RESOURCE_GROUP_NAME=$(terraform output -raw resource_group_name)
WEB_APP_NAME=$(terraform output -raw web_app_name)
MYSQL_SERVER_NAME=$(terraform output -raw mysql_server_name)
MYSQL_FQDN_FULL=$(terraform output -raw mysql_fqdn)
DATABASE_NAME=$(terraform output -raw mysql_database_name)
if [[ -z "$RESOURCE_GROUP_NAME" || -z "$WEB_APP_NAME" || -z "$MYSQL_SERVER_NAME" ]]; then
echo "Resource Group Name, 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
echo "Waiting for the [$MYSQL_SERVER_NAME] MySQL flexible server to accept connections..."
MYSQL_READY=0
for attempt in $(seq 1 30); do
if MYSQL_PWD="$MYSQL_ADMIN_PASSWORD" mysql \
--host="$MYSQL_FQDN" \
--port="$MYSQL_PORT" \
--user="$MYSQL_ADMIN_USER" \
--protocol=TCP \
--connect-timeout=5 \
-e "SELECT 1;" &>/dev/null; then
MYSQL_READY=1
echo "MySQL flexible server is accepting connections (attempt $attempt/30)"
break
fi
echo "MySQL flexible server not ready yet (attempt $attempt/30)..."
sleep 2
done
if [ "$MYSQL_READY" -ne 1 ]; then
echo "MySQL flexible server did not become reachable after 30 attempts. Exiting."
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