-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·279 lines (246 loc) · 7.71 KB
/
deploy.sh
File metadata and controls
executable file
·279 lines (246 loc) · 7.71 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
#!/bin/bash
# Variables
PREFIX='local'
SUFFIX='test'
RESOURCE_GROUP='rg-pgflex'
SERVER_NAME='pgflex-sample'
LOCATION='westeurope'
ADMIN_USER='pgadmin'
ADMIN_PASSWORD='P@ssw0rd12345!'
PG_VERSION='16'
STORAGE_SIZE=32
PRIMARY_DB='sampledb'
SECONDARY_DB='analyticsdb'
APP_SERVICE_PLAN_NAME="${PREFIX}-pgflex-plan-${SUFFIX}"
APP_SERVICE_PLAN_SKU="S1"
WEB_APP_NAME="${PREFIX}-pgflex-webapp-${SUFFIX}"
RUNTIME="python"
RUNTIME_VERSION="3.13"
ZIPFILE="notes_app.zip"
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
SAMPLE_DIR="$(cd "$CURRENT_DIR/.." && pwd)"
ENVIRONMENT=$(az account show --query environmentName --output tsv)
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Choose the appropriate CLI based on the environment
if [[ $ENVIRONMENT == "LocalStack" ]]; then
echo "Using azlocal for LocalStack emulator environment."
AZ="azlocal"
else
echo "Using standard az for AzureCloud environment."
AZ="az"
fi
# Create resource group
echo "Creating resource group [$RESOURCE_GROUP]..."
$AZ group create \
--name "$RESOURCE_GROUP" \
--location "$LOCATION" \
--output table
if [[ $? != 0 ]]; then
echo "Failed to create resource group. Exiting."
exit 1
fi
# Create PostgreSQL Flexible Server
# Note: We use 'az rest' to call the management API directly because the
# 'az postgres flexible-server create' CLI command performs a SKU availability
# pre-check that fails on LocalStack (capabilities endpoint returns no SKUs).
# Using the REST API bypasses this client-side validation.
SUBSCRIPTION_ID=$($AZ account show --query id --output tsv)
echo "Creating PostgreSQL Flexible Server [$SERVER_NAME]..."
$AZ rest \
--method PUT \
--url "https://management.azure.com/subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}/providers/Microsoft.DBforPostgreSQL/flexibleServers/${SERVER_NAME}?api-version=2024-08-01" \
--body "{
\"location\": \"${LOCATION}\",
\"properties\": {
\"version\": \"${PG_VERSION}\",
\"administratorLogin\": \"${ADMIN_USER}\",
\"administratorLoginPassword\": \"${ADMIN_PASSWORD}\",
\"storage\": { \"storageSizeGB\": ${STORAGE_SIZE} },
\"createMode\": \"Default\"
},
\"sku\": {
\"name\": \"Standard_B1ms\",
\"tier\": \"Burstable\"
}
}" \
--output table
if [[ $? != 0 ]]; then
echo "Failed to create PostgreSQL Flexible Server. Exiting."
exit 1
fi
echo "PostgreSQL Flexible Server [$SERVER_NAME] created successfully."
# Wait for the server to be fully ready.
# The REST API returns immediately but the PostgreSQL container may still
# be initializing (admin role creation, etc.).
echo "Waiting for server to be fully provisioned..."
for i in $(seq 1 30); do
STATE=$($AZ postgres flexible-server show \
--name "$SERVER_NAME" \
--resource-group "$RESOURCE_GROUP" \
--query "state" \
--output tsv 2>/dev/null)
if [[ "$STATE" == "Ready" ]]; then
echo "Server is ready."
break
fi
echo " Server state: ${STATE:-unknown} (attempt $i/30)"
sleep 5
done
# Create primary database
echo "Creating database [$PRIMARY_DB]..."
$AZ postgres flexible-server db create \
--server-name "$SERVER_NAME" \
--resource-group "$RESOURCE_GROUP" \
--database-name "$PRIMARY_DB" \
--charset "UTF8" \
--collation "en_US.utf8" \
--output table
if [[ $? != 0 ]]; then
echo "Failed to create database [$PRIMARY_DB]. Exiting."
exit 1
fi
# Create secondary database
echo "Creating database [$SECONDARY_DB]..."
$AZ postgres flexible-server db create \
--server-name "$SERVER_NAME" \
--resource-group "$RESOURCE_GROUP" \
--database-name "$SECONDARY_DB" \
--charset "UTF8" \
--collation "en_US.utf8" \
--output table
if [[ $? != 0 ]]; then
echo "Failed to create database [$SECONDARY_DB]. Exiting."
exit 1
fi
# Create firewall rules
# Note: The firewall-rule subcommand uses --name/-n for the server name
# and --rule-name/-r for the rule name (unlike db create which uses --server-name).
echo "Creating firewall rule [allow-all]..."
$AZ postgres flexible-server firewall-rule create \
--rule-name "allow-all" \
--name "$SERVER_NAME" \
--resource-group "$RESOURCE_GROUP" \
--start-ip-address "0.0.0.0" \
--end-ip-address "255.255.255.255" \
--output table
echo "Creating firewall rule [corporate-network]..."
$AZ postgres flexible-server firewall-rule create \
--rule-name "corporate-network" \
--name "$SERVER_NAME" \
--resource-group "$RESOURCE_GROUP" \
--start-ip-address "10.0.0.1" \
--end-ip-address "10.0.255.255" \
--output table
echo "Creating firewall rule [vpn-access]..."
$AZ postgres flexible-server firewall-rule create \
--rule-name "vpn-access" \
--name "$SERVER_NAME" \
--resource-group "$RESOURCE_GROUP" \
--start-ip-address "192.168.100.1" \
--end-ip-address "192.168.100.254" \
--output table
# Retrieve the FQDN of the server
echo "Retrieving the FQDN of the [$SERVER_NAME] PostgreSQL Flexible Server..."
SERVER_FQDN=$($AZ postgres flexible-server show \
--name "$SERVER_NAME" \
--resource-group "$RESOURCE_GROUP" \
--query "fullyQualifiedDomainName" \
--output tsv)
if [ -z "$SERVER_FQDN" ]; then
echo "Failed to retrieve the FQDN of the PostgreSQL Flexible Server"
exit 1
fi
echo "Server FQDN: $SERVER_FQDN"
# Create App Service Plan
echo "Creating App Service Plan [$APP_SERVICE_PLAN_NAME]..."
$AZ appservice plan create \
--resource-group "$RESOURCE_GROUP" \
--name "$APP_SERVICE_PLAN_NAME" \
--location "$LOCATION" \
--sku "$APP_SERVICE_PLAN_SKU" \
--is-linux \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "App Service Plan [$APP_SERVICE_PLAN_NAME] created successfully."
else
echo "Failed to create App Service Plan [$APP_SERVICE_PLAN_NAME]."
exit 1
fi
# Create the web app
echo "Creating web app [$WEB_APP_NAME]..."
$AZ webapp create \
--resource-group "$RESOURCE_GROUP" \
--plan "$APP_SERVICE_PLAN_NAME" \
--name "$WEB_APP_NAME" \
--runtime "$RUNTIME:$RUNTIME_VERSION" \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Web app [$WEB_APP_NAME] created successfully."
else
echo "Failed to create web app [$WEB_APP_NAME]."
exit 1
fi
# Set web app settings with PostgreSQL connection details
echo "Setting web app settings for [$WEB_APP_NAME]..."
$AZ webapp config appsettings set \
--name "$WEB_APP_NAME" \
--resource-group "$RESOURCE_GROUP" \
--settings \
SCM_DO_BUILD_DURING_DEPLOYMENT='true' \
ENABLE_ORYX_BUILD='true' \
PG_HOST="$SERVER_FQDN" \
PG_USER="$ADMIN_USER" \
PG_PASSWORD="$ADMIN_PASSWORD" \
PG_DATABASE="$PRIMARY_DB" \
PG_PORT="5432" \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Web app settings for [$WEB_APP_NAME] set successfully."
else
echo "Failed to set web app settings for [$WEB_APP_NAME]."
exit 1
fi
# Change to the notes-app source directory
cd "$SAMPLE_DIR/src/notes-app" || exit
# Remove any existing zip package
if [ -f "$ZIPFILE" ]; then
rm "$ZIPFILE"
fi
# Create the zip package of the web app
echo "Creating zip package of the notes app..."
zip -r "$ZIPFILE" app.py requirements.txt
# Deploy the web app
echo "Deploying web app [$WEB_APP_NAME] with zip file [$ZIPFILE]..."
$AZ webapp deploy \
--resource-group "$RESOURCE_GROUP" \
--name "$WEB_APP_NAME" \
--src-path "$ZIPFILE" \
--type zip \
--async true 1>/dev/null
if [ $? -eq 0 ]; then
echo "Web app [$WEB_APP_NAME] deployed successfully."
else
echo "Failed to deploy web app [$WEB_APP_NAME]."
exit 1
fi
# Clean up zip file
if [ -f "$ZIPFILE" ]; then
rm "$ZIPFILE"
fi
# Get web app URL
WEB_APP_URL=$($AZ webapp show \
--name "$WEB_APP_NAME" \
--resource-group "$RESOURCE_GROUP" \
--query "defaultHostName" \
--output tsv)
echo ""
echo "=== Deployment Complete ==="
echo "Resource Group: $RESOURCE_GROUP"
echo "Server Name: $SERVER_NAME"
echo "Server FQDN: $SERVER_FQDN"
echo "Primary DB: $PRIMARY_DB"
echo "Secondary DB: $SECONDARY_DB"
echo "Web App Name: $WEB_APP_NAME"
echo "Web App URL: https://$WEB_APP_URL"
echo ""