-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.ps1
175 lines (156 loc) · 7.51 KB
/
deploy.ps1
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
# Deploys the R webservice defined in folder webservice to Azure Machine Learning.
# --------------------------------------------------------------------------------
# Requirements:
# - an Azure ML workspace
# - Azure CLI incl. ml extension installed (v2).
# - a local Docker instance (for dev/test before deploying to Azure)
# - a proper configuration, see webservice/config.psd1 file
Param(
[Parameter(Mandatory = $False)]
[switch]$AcceptConfiguration = $False,
[Parameter(Mandatory = $False)]
[switch]$DeployToAzure = $False
)
$ErrorActionPreference = "Stop"
# -- load configuration
Write-Host ">> Loading configuration..." -ForegroundColor DarkCyan
$config = Import-PowerShellDataFile -Path .\webservice\config.psd1
if (![string]::IsNullOrWhiteSpace($config.HTTP_PROXY)) {
$env:HTTP_PROXY = $config.HTTP_PROXY
}
if (![string]::IsNullOrWhiteSpace($config.HTTPS_PROXY)) {
$env:HTTP_PROXY = $config.HTTPS_PROXY
}
$config.GetEnumerator() | Sort-Object -Property key
Write-Host
if (!$AcceptConfiguration -and `
(!((Read-Host -Prompt "Do you want to continue with the configuration shown above? [y/n]") -match "[yY]"))) {
exit 1
}
Write-Host
# -- compute several required variable values
Write-Host ">> Computing several required variables..." -ForegroundColor DarkCyan
$local_model_path = "$PWD/webservice"
$acr_name = $(az ml workspace show -g $($config.RESOURCE_GROUP) -n $($config.WORKSPACE) `
--query container_registry -o tsv).split("/")[-1]
$latest_model_version = 0
try {
$latest_model_version = $(az ml model list -n $($config.MODEL_NAME) `
-g $($config.RESOURCE_GROUP) -w $($config.WORKSPACE) --query 'reverse(sort_by([], &version))[0].version' -o tsv)
}
finally {
$next_model_version = [int] $latest_model_version + 1
Write-Host "Next model version: $next_model_version"
}
try {
$latest_environment_version = $(az ml environment list -n $($config.MODEL_NAME) `
-g $($config.RESOURCE_GROUP) -w $($config.WORKSPACE) --query 'reverse(sort_by([], &version))[0].version' -o tsv)
}
finally {
$next_environment_version = [int] $latest_environment_version + 1
Write-Host "Next environment version: $next_environment_version"
}
$image_tag = "$acr_name.azurecr.io/$($config.APP_NAME):$next_environment_version"
Write-Host "Image Tag: $image_tag"
## LOCAL CONTAINER BUILD FOR TESTING
Write-Host "`nBuild and run local container..." -ForegroundColor Cyan
# -- locally build the docker image
Write-Host ">> Build the local docker image..." -ForegroundColor DarkCyan
$azureml_model_dir = "/var/azureml-app/azureml-models/$($config.MODEL_NAME)/$next_model_version/webservice"
Push-Location
Set-Location webservice
(Get-Content -Path Dockerfile.template) -replace "#PATH_TO_PLUMBER_R#", `
"""$azureml_model_dir/plumber.R""" `
| Set-Content -Path Dockerfile.temp
docker build -f Dockerfile.temp -t $image_tag --build-arg HTTP_PROXY=$($env:HTTP_PROXY) `
--build-arg HTTPS_PROXY=$($env:HTTPS_PROXY) .
Remove-Item Dockerfile.temp
Pop-Location
Write-Host
# -- start the image locally
# notes: - if the image does not start, checking the logs of the container might help.
# to see the log run "docker logs --tail 1000 <container id>" or simply use "View Logs" in VS.Code ;-)
# - waiting some seconds to give the container some time to start
Write-Host ">> Starting the local docker image..." -ForegroundColor DarkCyan
& { docker rm -f -v $config.APP_NAME } -ErrorAction SilentlyContinue
docker run -d -p 8000:8000 -v ($local_model_path + ":$azureml_model_dir") `
-e AZUREML_MODEL_DIR=$azureml_model_dir --name=$($config.APP_NAME) $image_tag
Start-Sleep -s 5
# TEST LOCAL IMAGE/ENDPOINTS
# note: added --noproxy localhost to ensure we don't run into proxy issues
Write-Host "`nTest local container..." -ForegroundColor Cyan
Write-Host ">> Live" -ForegroundColor DarkCyan
curl "http://localhost:8000/live" --noproxy localhost
Write-Host "`n`n>> Ready" -ForegroundColor DarkCyan
curl "http://localhost:8000/ready" --noproxy localhost
Write-Host "`n`n>> Score" -ForegroundColor DarkCyan
curl -H "Content-Type: application/json" --data "@webservice/sample_request.json" http://localhost:8000/score `
--noproxy localhost
Write-Host "`n"
## DEPLOY TO AZURE
if (!$DeployToAzure -and `
(!((Read-Host -Prompt "Do you want to continue and deploy your container to Azure? [y/n]") -match "[yY]"))) {
exit 1
}
Write-Host "`nDeploy to Azure..." -ForegroundColor Cyan
Write-Host ">> Login to Azure..." -ForegroundColor DarkCyan
if (![string]::IsNullOrWhiteSpace($config.AZURE_SUBSCRIPTION_ID)) {
az account set -s $($config.AZURE_SUBSCRIPTION_ID)
}
# -- deploy model(s)
Write-Host ">> Deploying model..." -ForegroundColor DarkCyan
az ml model create --name $($config.MODEL_NAME) --version $next_model_version --local-path $local_model_path `
-g $($config.RESOURCE_GROUP) -w $($config.WORKSPACE)
# -- publish image to ACR
Write-Host "`n>> Pushing container image to Azure Container Registry..." -ForegroundColor DarkCyan
az acr login --name $acr_name
docker push $image_tag
# -- deploy endpoint and deployment to AML managed online endpoint
Write-Host "`n>> Creating/updating AML Managed Online Endpoint and deployment..." -ForegroundColor DarkCyan
# endpoint
Write-Host "Endpoint..."
az ml online-endpoint create -f webservice/endpoint.yml `
-g $($config.RESOURCE_GROUP) -w $($config.WORKSPACE) `
-n $($config.UNIQUE_ENDPOINT_NAME) `
--set name=$($config.APP_NAME)
# deployment
Write-Host "Deployment..."
az ml online-deployment create -f webservice/deployment.yml `
-g $($config.RESOURCE_GROUP) -w $($config.WORKSPACE) `
-n $($config.UNIQUE_ENDPOINT_NAME) `
--all-traffic `
--set endpoint_name=$($config.UNIQUE_ENDPOINT_NAME) `
--set model.name=$($config.MODEL_NAME) `
--set model.version=$next_model_version `
--set environment.name=$($config.APP_NAME) `
--set environment.version=$next_environment_version `
--set environment.image=$image_tag `
--set instance_type=$($config.INSTANCE_TYPE) `
--set instance_count=$($config.INSTANCE_COUNT) `
--set app_insights_enabled=$($config.APP_INSIGHTS_ENABLED.ToString().ToLower())
# -- check logs, esp. for the case that the deployment has failed
#Write-Host "`n>> Getting deployment logs..." -ForegroundColor DarkCyan
#az ml online-deployment get-logs --name $($config.UNIQUE_ENDPOINT_NAME) `
# --endpoint-name default --lines 100 --resource-group $($config.RESOURCE_GROUP) --workspace-name $($config.WORKSPACE)
# -- get URI and credentials for endpoint
Write-Host "`n>> Getting scoring URI and authentication keys for webservice..." -ForegroundColor DarkCyan
$scoring_uri = $(az ml online-endpoint show -n $($config.UNIQUE_ENDPOINT_NAME) -g $($config.RESOURCE_GROUP) `
-w $($config.WORKSPACE) --query "scoring_uri" -o tsv)
Write-Host "Scoring URI: $scoring_uri"
$primary_key = $(az ml online-endpoint get-credentials -n $($config.UNIQUE_ENDPOINT_NAME) -g $($config.RESOURCE_GROUP) `
-w $($config.WORKSPACE) --query "primaryKey" -o tsv)
#Write-Host "Primary Key: $primary_key"
# -- test endpoint in Azure
Write-Host "`n>> Test webservice in Azure..." -ForegroundColor DarkCyan
if ([string]::IsNullOrWhiteSpace($config.HTTPS_PROXY)) {
curl -H "Content-Type: application/json" -H "Authorization: Bearer $primary_key" `
--data "@webservice/sample_request.json" $scoring_uri
}
else {
curl -H "Content-Type: application/json" -H "Authorization: Bearer $primary_key" `
--data "@webservice/sample_request.json" $scoring_uri `
--proxy "$($config.HTTPS_PROXY)"
}
Write-Host
# -- done
Write-Host "`nDone."