-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdeploy.sh
221 lines (183 loc) · 7 KB
/
deploy.sh
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
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
#set -o xtrace # For debugging
#####################
# PARAMETERS
# Azure parameters
SUBSCRIPTION_ID=<your-subscription-id>
# Resource group parameters
RG_NAME=mlflow-rg
RG_LOCATION=westeurope
# Container registry parameters
ACR_NAME=mlflowcontainerregistry
# Docker image parameters
DOCKER_IMAGE_NAME=mlflowimage
DOCKER_IMAGE_TAG=latest
# App service plan parameters
ASP_NAME=mlflowappserviceplan
# Web app parameters
WEB_APP_NAME=mlflow
# MLFlow settings
MLFLOW_HOST=0.0.0.0
MLFLOW_PORT=5000
MLFLOW_WORKERS=1
MLFLOW_FILESTORE=/mlruns/mlruns
# Storage parameters
STORAGE_ACCOUNT_NAME=storage$RANDOM
STORAGE_CONTAINER_NAME=mlflow
STORAGE_MOUNT_POINT=/mlruns
STORAGE_FILE_SHARE_NAME=mlflow
STORAGE_FILE_SHARE_SIZE=2
#####################
# LOGIN
echo "Logging into Azure"
az login
echo "Setting default subscription: $SUBSCRIPTION_ID"
az account set \
--subscription $SUBSCRIPTION_ID
#####################
# DEPLOYMENT
echo "Creating resource group: $RG_NAME"
az group create \
--name $RG_NAME \
--location $RG_LOCATION
echo "Creating storage account: $STORAGE_ACCOUNT_NAME"
az storage account create \
--resource-group $RG_NAME \
--location $RG_LOCATION \
--name $STORAGE_ACCOUNT_NAME \
--sku Standard_LRS
echo "Creating storage container for MLflow artifacts: $STORAGE_CONTAINER_NAME"
az storage container create \
--name $STORAGE_CONTAINER_NAME \
--account-name $STORAGE_ACCOUNT_NAME
echo "Creating file share for mounting to MLflow container: $STORAGE_CONTAINER_NAME"
az storage share create \
--name $STORAGE_FILE_SHARE_NAME \
--account-name $STORAGE_ACCOUNT_NAME \
--quota $STORAGE_FILE_SHARE_SIZE
echo "Exporting storage keys: $STORAGE_ACCOUNT_NAME"
export STORAGE_ACCESS_KEY=$(az storage account keys list --resource-group $RG_NAME --account-name $STORAGE_ACCOUNT_NAME --query "[0].value" --output tsv)
export STORAGE_CONNECTION_STRING=`az storage account show-connection-string --resource-group $RG_NAME --name $STORAGE_ACCOUNT_NAME --output tsv`
echo "Creating Azure container registry: $ACR_NAME"
az acr create \
--name $ACR_NAME \
--resource-group $RG_NAME \
--sku Basic \
--admin-enabled true
echo "Getting Azure container registry credentials: $ACR_NAME"
export ACR_USERNAME=$(az acr credential show --name $ACR_NAME --query "username" --output tsv)
export ACR_PASSWORD=$(az acr credential show --name $ACR_NAME --query "passwords[0].value" --output tsv)
echo "Logging into Azure container registry"
docker login $ACR_NAME.azurecr.io \
--username "$ACR_USERNAME" \
--password "$ACR_PASSWORD"
echo "Building Docker image from file: $DOCKER_IMAGE_NAME"
cd docker
docker build \
--tag $DOCKER_IMAGE_NAME \
--file Dockerfile . \
--no-cache
cd ..
echo "Pushing image to Azure container registry: $ACR_NAME"
docker tag $DOCKER_IMAGE_NAME $ACR_NAME.azurecr.io/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG
docker push $ACR_NAME.azurecr.io/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG
echo "Showing pushed images"
az acr repository list \
--name $ACR_NAME
echo "Creating app service plan: $ASP_NAME"
az appservice plan create \
--name $ASP_NAME \
--resource-group $RG_NAME \
--sku S1 \
--is-linux
echo "Creating web app: $WEB_APP_NAME"
az webapp create \
--resource-group $RG_NAME \
--plan $ASP_NAME \
--name $WEB_APP_NAME \
--deployment-container-image-name $ACR_NAME.azurecr.io/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG
echo "Configuring registry credentials in web app"
az webapp config container set \
--name $WEB_APP_NAME \
--resource-group $RG_NAME \
--docker-custom-image-name $ACR_NAME.azurecr.io/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG \
--docker-registry-server-url https://$ACR_NAME.azurecr.io \
--docker-registry-server-user $ACR_USERNAME \
--docker-registry-server-password $ACR_PASSWORD \
--enable-app-service-storage true
# unfixed bug according to: https://github.com/Azure/azure-cli/issues/7261 and https://github.com/MicrosoftDocs/azure-docs/issues/34240
#echo "Enable continuous deployment for web app"
#az webapp deployment container config \
# --name $WEB_APP_NAME \
# --resource-group $RG_NAME \
# --enable-cd true
echo "Setting Azure container registry credentials"
az webapp config appsettings set \
--resource-group $RG_NAME \
--name $WEB_APP_NAME \
--settings WEBSITES_PORT=$MLFLOW_PORT
echo "Enabling access to logs generated from inside the container"
az webapp log config \
--name $WEB_APP_NAME \
--resource-group $RG_NAME \
--docker-container-logging filesystem
echo "Setting environment variables"
az webapp config appsettings set \
--resource-group $RG_NAME \
--name $WEB_APP_NAME \
--settings AZURE_STORAGE_CONNECTION_STRING=$STORAGE_CONNECTION_STRING
az webapp config appsettings set \
--resource-group $RG_NAME \
--name $WEB_APP_NAME \
--settings MLFLOW_SERVER_DEFAULT_ARTIFACT_ROOT=wasbs://$STORAGE_CONTAINER_NAME@$STORAGE_ACCOUNT_NAME.blob.core.windows.net/mlartefacts
az webapp config appsettings set \
--resource-group $RG_NAME \
--name $WEB_APP_NAME \
--settings MLFLOW_SERVER_WORKERS=$MLFLOW_WORKERS
az webapp config appsettings set \
--resource-group $RG_NAME \
--name $WEB_APP_NAME \
--settings MLFLOW_SERVER_PORT=$MLFLOW_PORT
az webapp config appsettings set \
--resource-group $RG_NAME \
--name $WEB_APP_NAME \
--settings MLFLOW_SERVER_HOST=$MLFLOW_HOST
az webapp config appsettings set \
--resource-group $RG_NAME \
--name $WEB_APP_NAME \
--settings MLFLOW_SERVER_FILE_STORE=$MLFLOW_FILESTORE
echo "Linking storage account to web app"
az webapp config storage-account add \
--resource-group $RG_NAME \
--name $WEB_APP_NAME \
--custom-id $STORAGE_ACCOUNT_NAME \
--storage-type AzureFiles \
--share-name $STORAGE_CONTAINER_NAME \
--account-name $STORAGE_ACCOUNT_NAME \
--access-key $STORAGE_ACCESS_KEY \
--mount-path $STORAGE_MOUNT_POINT
echo "Verify linked storage account: $STORAGE_ACCOUNT_NAME"
az webapp config storage-account list \
--resource-group $RG_NAME \
--name $WEB_APP_NAME
#####################
# AZURE AD AUTHENTICATION AUTOMATION
# Azure Active Directory and Service Principal parameters
#AAD_ISSUER_URL=<your-aad-issuer-url> # e.g. https://login.microsoftonline.com/AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE/
#SP_NAME=mlflowserviceprincipal
#echo "Creating Azure Active Directory Service Principal"
#export SP_CLIENT_SECRET=$(az ad sp create-for-rbac --name $SP_NAME --query "password" --output tsv)
#export SP_CLIENT_ID=$(az ad sp list --show-mine --query "[?appDisplayName=='$SP_NAME'].appId" -o tsv)
#echo "Enable Azure Active Directory Authentication"
#az webapp auth update \
# --resource-group $RG_NAME \
# --name $WEB_APP_NAME \
# --enabled true \
# --action LoginWithAzureActiveDirectory \
# --aad-allowed-token-audiences https://$WEB_APP_NAME.azurewebsites.net/.auth/login/aad/callback \
# --aad-client-id $SP_CLIENT_ID \
# --aad-client-secret $SP_CLIENT_SECRET \
# --aad-token-issuer-url $AAD_ISSUER_URL