-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-assigned.sh
More file actions
executable file
·356 lines (310 loc) · 12 KB
/
user-assigned.sh
File metadata and controls
executable file
·356 lines (310 loc) · 12 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/bin/bash
# Variables
PREFIX='local'
SUFFIX='test'
LOCATION='westeurope'
STORAGE_ACCOUNT_NAME="${PREFIX}storage${SUFFIX}"
APP_SERVICE_PLAN_NAME="${PREFIX}-app-service-plan-${SUFFIX}"
APP_SERVICE_PLAN_SKU="B1"
MANAGED_IDENTITY_NAME="${PREFIX}-identity-${SUFFIX}"
WEB_APP_NAME="${PREFIX}-webapp-${SUFFIX}"
RESOURCE_GROUP_NAME="${PREFIX}-web-app-rg"
RUNTIME="python"
RUNTIME_VERSION="3.13"
CONTAINER_NAME='activities'
ZIPFILE="webapp_app.zip"
SUBSCRIPTION_NAME=$(az account show --query name --output tsv)
SUBSCRIPTION_ID=$(az account show --query id --output tsv)
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
RETRY_COUNT=3
SLEEP=5
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Create a resource group
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
fi
else
echo "Resource group [$RESOURCE_GROUP_NAME] already exists in the subscription [$SUBSCRIPTION_NAME]"
fi
# Create a storage account
echo "Checking if storage account [$STORAGE_ACCOUNT_NAME] exists in the resource group [$RESOURCE_GROUP_NAME]..."
az storage account show \
--name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No storage account [$STORAGE_ACCOUNT_NAME] exists in the [$RESOURCE_GROUP_NAME] resource group."
echo "Creating storage account [$STORAGE_ACCOUNT_NAME] in the [$RESOURCE_GROUP_NAME] resource group..."
az storage account create \
--name $STORAGE_ACCOUNT_NAME \
--location "$LOCATION" \
--resource-group $RESOURCE_GROUP_NAME \
--sku Standard_LRS 1>/dev/null
if [ $? -eq 0 ]; then
echo "Storage account [$STORAGE_ACCOUNT_NAME] created successfully in the [$RESOURCE_GROUP_NAME] resource group."
else
echo "Failed to create storage account [$STORAGE_ACCOUNT_NAME] in the [$RESOURCE_GROUP_NAME] resource group."
exit 1
fi
else
echo "Storage account [$STORAGE_ACCOUNT_NAME] already exists in the [$RESOURCE_GROUP_NAME] resource group."
fi
# Get the storage account key
echo "Getting storage account key for [$STORAGE_ACCOUNT_NAME]..."
STORAGE_ACCOUNT_KEY=$(az storage account keys list \
--account-name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--query "[0].value" \
--output tsv)
if [ -n "$STORAGE_ACCOUNT_KEY" ]; then
echo "Storage account key retrieved successfully: [$STORAGE_ACCOUNT_KEY]"
else
echo "Failed to retrieve storage account key."
exit 1
fi
# Get the storage account resource ID
STORAGE_ACCOUNT_RESOURCE_ID=$(az storage account show \
--name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--query "id" \
--output tsv \
--only-show-errors)
if [ -n "$STORAGE_ACCOUNT_RESOURCE_ID" ]; then
echo "Storage account resource ID retrieved successfully: $STORAGE_ACCOUNT_RESOURCE_ID"
else
echo "Failed to retrieve storage account resource ID."
exit 1
fi
# Get the storage account blob primary endpoint
AZURE_STORAGE_ACCOUNT_URL=$(az storage account show \
--name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--query "primaryEndpoints.blob" \
--output tsv \
--only-show-errors)
if [ -n "$AZURE_STORAGE_ACCOUNT_URL" ]; then
echo "Storage account blob primary endpoint retrieved successfully: $AZURE_STORAGE_ACCOUNT_URL"
else
echo "Failed to retrieve storage account blob primary endpoint."
exit 1
fi
# Create blob container
echo "Creating blob container [$CONTAINER_NAME] in the [$STORAGE_ACCOUNT_NAME] storage account..."
az storage container create \
--name $CONTAINER_NAME \
--account-name $STORAGE_ACCOUNT_NAME \
--account-key "$STORAGE_ACCOUNT_KEY" \
--public-access blob 1>/dev/null
if [ $? -eq 0 ]; then
echo "Blob container [$CONTAINER_NAME] created successfully in the [$STORAGE_ACCOUNT_NAME] storage account."
else
echo "Failed to create blob container [$CONTAINER_NAME] in the [$STORAGE_ACCOUNT_NAME] storage account."
exit 1
fi
# Check if the App Service Plan already exists
echo "Checking if App Service Plan [$APP_SERVICE_PLAN_NAME] exists in the resource group [$RESOURCE_GROUP_NAME]..."
az appservice plan show \
--name $APP_SERVICE_PLAN_NAME \
--resource-group $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No App Service Plan [$APP_SERVICE_PLAN_NAME] exists in the [$RESOURCE_GROUP_NAME] resource group."
# Create App Service Plan
echo "Creating App Service Plan [$APP_SERVICE_PLAN_NAME]..."
az appservice plan create \
--resource-group "$RESOURCE_GROUP_NAME" \
--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
else
echo "App Service Plan [$APP_SERVICE_PLAN_NAME] already exists in the [$RESOURCE_GROUP_NAME] resource group."
fi
# Check if the user-assigned managed identity already exists
echo "Checking if [$MANAGED_IDENTITY_NAME] user-assigned managed identity actually exists in the [$RESOURCE_GROUP_NAME] resource group..."
az identity show \
--name"$MANAGED_IDENTITY_NAME" \
--resource-group $"$RESOURCE_GROUP_NAME" &>/dev/null
if [[ $? != 0 ]]; then
echo "No [$MANAGED_IDENTITY_NAME] user-assigned managed identity actually exists in the [$RESOURCE_GROUP_NAME] resource group"
echo "Creating [$MANAGED_IDENTITY_NAME] user-assigned managed identity in the [$RESOURCE_GROUP_NAME] resource group..."
# Create the user-assigned managed identity
az identity create \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--subscription "$SUBSCRIPTION_ID" 1>/dev/null
if [[ $? == 0 ]]; then
echo "[$MANAGED_IDENTITY_NAME] user-assigned managed identity successfully created in the [$RESOURCE_GROUP_NAME] resource group"
else
echo "Failed to create [$MANAGED_IDENTITY_NAME] user-assigned managed identity in the [$RESOURCE_GROUP_NAME] resource group"
exit 1
fi
else
echo "[$MANAGED_IDENTITY_NAME] user-assigned managed identity already exists in the [$RESOURCE_GROUP_NAME] resource group"
fi
# Retrieve the clientId of the user-assigned managed identity
echo "Retrieving clientId for [$MANAGED_IDENTITY_NAME] managed identity..."
CLIENT_ID=$(az identity show \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query clientId \
--output tsv)
if [[ -n $CLIENT_ID ]]; then
echo "[$CLIENT_ID] clientId for the [$MANAGED_IDENTITY_NAME] managed identity successfully retrieved"
else
echo "Failed to retrieve clientId for the [$MANAGED_IDENTITY_NAME] managed identity"
exit 1
fi
# Retrieve the principalId of the user-assigned managed identity
echo "Retrieving principalId for [$MANAGED_IDENTITY_NAME] managed identity..."
PRINCIPAL_ID=$(az identity show \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query principalId \
--output tsv)
if [[ -n $PRINCIPAL_ID ]]; then
echo "[$PRINCIPAL_ID] principalId for the [$MANAGED_IDENTITY_NAME] managed identity successfully retrieved"
else
echo "Failed to retrieve principalId for the [$MANAGED_IDENTITY_NAME] managed identity"
exit 1
fi
# Retrieve the resource id of the user-assigned managed identity
echo "Retrieving resource id for the [$MANAGED_IDENTITY_NAME] managed identity..."
IDENTITY_ID=$(az identity show \
--name "$MANAGED_IDENTITY_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query id \
--output tsv)
if [[ -n $IDENTITY_ID ]]; then
echo "Resource id for the [$MANAGED_IDENTITY_NAME] managed identity successfully retrieved"
else
echo "Failed to retrieve the resource id for the [$MANAGED_IDENTITY_NAME] managed identity"
exit 1
fi
# Check if the web app already exists
echo "Checking if web app [$WEB_APP_NAME] exists in the resource group [$RESOURCE_GROUP_NAME]..."
az webapp show \
--name $WEB_APP_NAME \
--resource-group $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No web app [$WEB_APP_NAME] exists in the [$RESOURCE_GROUP_NAME] resource group."
# Create the web app
echo "Creating web app [$WEB_APP_NAME]..."
az webapp create \
--resource-group "$RESOURCE_GROUP_NAME" \
--plan "$APP_SERVICE_PLAN_NAME" \
--name "$WEB_APP_NAME" \
--runtime "$RUNTIME:$RUNTIME_VERSION" \
--assign-identity "${IDENTITY_ID}" \
--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
else
echo "Web app [$WEB_APP_NAME] already exists in the [$RESOURCE_GROUP_NAME] resource group."
fi
# Assign the Storage Blob Data Contributor role to the managed identity with the storage account as scope
ROLE="Storage Blob Data Contributor"
echo "Checking if the managed identity with principal ID [$PRINCIPAL_ID] has the [$ROLE] role assignment on storage account [$STORAGE_ACCOUNT_NAME]..."
current=$(az role assignment list \
--assignee "$PRINCIPAL_ID" \
--scope "$STORAGE_ACCOUNT_RESOURCE_ID" \
--query "[?roleDefinitionName=='$ROLE'].roleDefinitionName" \
--output tsv 2>/dev/null)
if [[ $current == $ROLE ]]; then
echo "Managed identity already has the [$ROLE] role assignment on storage account [$STORAGE_ACCOUNT_NAME]"
else
echo "Managed identity does not have the [$ROLE] role assignment on storage account [$STORAGE_ACCOUNT_NAME]"
echo "Creating role assignment: assigning [$ROLE] role to managed identity on storage account [$STORAGE_ACCOUNT_NAME]..."
ATTEMPT=1
while [ $ATTEMPT -le $RETRY_COUNT ]; do
echo "Attempt $ATTEMPT of $RETRY_COUNT to assign role..."
az role assignment create \
--assignee "$PRINCIPAL_ID" \
--role "$ROLE" \
--scope "$STORAGE_ACCOUNT_RESOURCE_ID" 1>/dev/null
if [[ $? == 0 ]]; then
break
else
if [ $ATTEMPT -lt $RETRY_COUNT ]; then
echo "Role assignment failed. Waiting [$SLEEP] seconds before retry..."
sleep $SLEEP
fi
ATTEMPT=$((ATTEMPT + 1))
fi
done
if [[ $? == 0 ]]; then
echo "Successfully assigned [$ROLE] role to managed identity on storage account [$STORAGE_ACCOUNT_NAME]"
else
echo "Failed to assign [$ROLE] role to managed identity on storage account [$STORAGE_ACCOUNT_NAME]"
exit
fi
fi
# Set web app settings
echo "Setting web app settings for [$WEB_APP_NAME]..."
az webapp config appsettings set \
--name $WEB_APP_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--settings \
SCM_DO_BUILD_DURING_DEPLOYMENT='true' \
ENABLE_ORYX_BUILD='true' \
AZURE_CLIENT_ID="$CLIENT_ID" \
AZURE_STORAGE_ACCOUNT_URL="$AZURE_STORAGE_ACCOUNT_URL" \
CONTAINER_NAME="$CONTAINER_NAME" \
--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
# CD into the web app directory
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 requirements.txt static templates
# 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
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
# Remove the zip package of the web app
if [ -f "$ZIPFILE" ]; then
rm "$ZIPFILE"
fi