-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathupgrade-simplechat.ps1
More file actions
389 lines (300 loc) · 13.4 KB
/
Copy pathupgrade-simplechat.ps1
File metadata and controls
389 lines (300 loc) · 13.4 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<#
.SYNOPSIS
Performs a code-only SimpleChat container upgrade for the Azure CLI deployer.
.DESCRIPTION
This PowerShell script builds a new container image in Azure Container Registry
using ACR Tasks and updates an existing Azure App Service to pull that image.
This is the Azure CLI deployer equivalent of an `azd deploy` style code-only
rollout. It does not provision or change infrastructure resources beyond the
target web app's container configuration.
The script supports either:
- explicit targeting with -ResourceGroupName and -WebAppName
- derived targeting with -BaseName and -Environment to match deploy-simplechat.ps1
The deployment model remains a container-based Azure App Service. Gunicorn
startup continues to come from the container entrypoint.
.NOTES
Author: Microsoft Federal
Date: 2026-04-29
Version: 1.0
Prerequisites:
- Azure CLI installed and authenticated.
- Access to the target subscription, ACR, and App Service.
- The target ACR already exists.
- The target web app already exists and is configured for the SimpleChat container path.
Azure Commercial login example:
az cloud set --name AzureCloud
az login --scope https://management.azure.com//.default
az account set -s "<subscription-id>"
Azure Government login example:
az cloud set --name AzureUSGovernment
az login --scope https://management.core.usgovcloudapi.net//.default
az account set -s "<subscription-id>"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$AcrName,
[Parameter(Mandatory = $true)]
[string]$ImageName,
[string]$BaseName,
[string]$Environment,
[string]$ResourceGroupName,
[string]$WebAppName,
[string]$SubscriptionId,
[ValidateSet("AzureCloud", "AzureUSGovernment", "Custom")]
[string]$AzurePlatform = "AzureCloud",
[string]$AzureCliCustomCloudName = "",
[string]$DockerfilePath = "application/single_app/Dockerfile",
[string]$BuildContextPath = "..\..",
[switch]$SkipAcrBuild,
[bool]$PublishLatestTag = $true,
[bool]$RestartWebApp = $true,
[string]$Slot
)
$PSModuleAutoloadingPreference = "All"
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Invoke-AzureCliCommand {
param(
[Parameter(Mandatory = $true)]
[string[]]$Arguments,
[switch]$AllowEmptyOutput
)
$output = & az @Arguments 2>&1
if ($LASTEXITCODE -ne 0) {
$joined = $Arguments -join ' '
throw "Azure CLI command failed: az $joined`n$output"
}
if ($output -is [array]) {
$output = $output -join [Environment]::NewLine
}
if (-not $AllowEmptyOutput -and $null -eq $output) {
return ""
}
return $output
}
function Ensure-AzureCliAuthenticated {
if (-not (Get-Command "az" -ErrorAction SilentlyContinue)) {
throw "Azure CLI is not installed. Please install it before running this script."
}
$expectedCloudName = switch ($AzurePlatform) {
'AzureCloud' { 'AzureCloud' }
'AzureUSGovernment' { 'AzureUSGovernment' }
'Custom' { $AzureCliCustomCloudName }
default { $null }
}
if ([string]::IsNullOrWhiteSpace($expectedCloudName)) {
throw "Custom cloud usage requires -AzureCliCustomCloudName."
}
$currentCloudName = Invoke-AzureCliCommand -Arguments @('cloud', 'show', '--query', 'name', '--output', 'tsv')
if ([string]::IsNullOrWhiteSpace($currentCloudName) -or $currentCloudName.Trim() -ne $expectedCloudName) {
Write-Host "Switching Azure CLI cloud to '$expectedCloudName'..." -ForegroundColor Yellow
Invoke-AzureCliCommand -Arguments @('cloud', 'set', '--name', $expectedCloudName) -AllowEmptyOutput | Out-Null
}
& az account show --output none 2>$null
if ($LASTEXITCODE -ne 0) {
throw "Azure CLI is not authenticated. Run 'az login' first and retry."
}
if (-not [string]::IsNullOrWhiteSpace($SubscriptionId)) {
Write-Host "Setting active subscription to '$SubscriptionId'..." -ForegroundColor Yellow
Invoke-AzureCliCommand -Arguments @('account', 'set', '--subscription', $SubscriptionId) -AllowEmptyOutput | Out-Null
}
}
function Resolve-ContainerImageInfo {
param(
[Parameter(Mandatory = $true)]
[string]$RequestedImageName
)
if ([string]::IsNullOrWhiteSpace($RequestedImageName)) {
throw "ImageName must not be empty."
}
$lastSlashIndex = $RequestedImageName.LastIndexOf('/')
$lastColonIndex = $RequestedImageName.LastIndexOf(':')
if ($lastColonIndex -gt $lastSlashIndex) {
$repository = $RequestedImageName.Substring(0, $lastColonIndex)
$tag = $RequestedImageName.Substring($lastColonIndex + 1)
} else {
$repository = $RequestedImageName
$tag = 'latest'
}
if ([string]::IsNullOrWhiteSpace($repository) -or [string]::IsNullOrWhiteSpace($tag)) {
throw "ImageName must use 'repository' or 'repository:tag'. Current value: $RequestedImageName"
}
return [PSCustomObject]@{
Repository = $repository
Tag = $tag
FullName = "$repository`:$tag"
}
}
function Resolve-TargetNames {
if (-not [string]::IsNullOrWhiteSpace($ResourceGroupName) -and -not [string]::IsNullOrWhiteSpace($WebAppName)) {
return [PSCustomObject]@{
ResourceGroupName = $ResourceGroupName
WebAppName = $WebAppName
}
}
if (-not [string]::IsNullOrWhiteSpace($ResourceGroupName) -or -not [string]::IsNullOrWhiteSpace($WebAppName)) {
throw "Specify either both -ResourceGroupName and -WebAppName, or both -BaseName and -Environment."
}
if ([string]::IsNullOrWhiteSpace($BaseName) -or [string]::IsNullOrWhiteSpace($Environment)) {
throw "Specify either both -ResourceGroupName and -WebAppName, or both -BaseName and -Environment."
}
return [PSCustomObject]@{
ResourceGroupName = "sc-$($BaseName)-$($Environment)-rg".ToLower()
WebAppName = "$($BaseName)-$($Environment)-app".ToLower()
}
}
function Get-AcrConnectionInfo {
$registryInfoRaw = Invoke-AzureCliCommand -Arguments @('acr', 'show', '--name', $AcrName, '--query', '{loginServer:loginServer,adminUserEnabled:adminUserEnabled}', '--output', 'json')
$registryInfo = $registryInfoRaw | ConvertFrom-Json
if ([string]::IsNullOrWhiteSpace($registryInfo.loginServer)) {
throw "Unable to resolve the login server for ACR '$AcrName'."
}
if (-not $registryInfo.adminUserEnabled) {
Write-Host "Enabling ACR admin user on '$AcrName' to match the Azure CLI deployer flow..." -ForegroundColor Yellow
Invoke-AzureCliCommand -Arguments @('acr', 'update', '--name', $AcrName, '--admin-enabled', 'true') -AllowEmptyOutput | Out-Null
}
return [PSCustomObject]@{
LoginServer = $registryInfo.loginServer
RegistryUrl = "https://$($registryInfo.loginServer)"
}
}
function Invoke-AcrContainerBuild {
param(
[Parameter(Mandatory = $true)]
[pscustomobject]$ContainerImageInfo
)
$resolvedContextPath = Resolve-Path -Path (Join-Path $PSScriptRoot $BuildContextPath) -ErrorAction Stop
$dockerfileFullPath = Join-Path $resolvedContextPath $DockerfilePath
if (-not (Test-Path -Path $dockerfileFullPath)) {
throw "Dockerfile not found at '$dockerfileFullPath'."
}
$arguments = @(
'acr', 'build',
'--registry', $AcrName,
'--file', $DockerfilePath,
'--image', $ContainerImageInfo.FullName
)
if ($PublishLatestTag -and $ContainerImageInfo.Tag -ne 'latest') {
$arguments += @('--image', "$($ContainerImageInfo.Repository):latest")
}
$arguments += $resolvedContextPath.Path
Write-Host "`n=====> Building image in Azure Container Registry..." -ForegroundColor Cyan
Write-Host "Registry: $AcrName"
Write-Host "Image: $($ContainerImageInfo.FullName)"
Write-Host "Dockerfile: $DockerfilePath"
Write-Host "Build context: $($resolvedContextPath.Path)"
Invoke-AzureCliCommand -Arguments $arguments -AllowEmptyOutput | Out-Null
}
function Ensure-WebAppExists {
param(
[Parameter(Mandatory = $true)]
[string]$ResolvedResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$ResolvedWebAppName
)
$queryArgs = @('webapp', 'show', '--resource-group', $ResolvedResourceGroupName, '--name', $ResolvedWebAppName)
if (-not [string]::IsNullOrWhiteSpace($Slot)) {
$queryArgs += @('--slot', $Slot)
}
$queryArgs += @('--query', '{name:name,defaultHostName:defaultHostName,state:state}', '--output', 'json')
$webAppRaw = Invoke-AzureCliCommand -Arguments $queryArgs
return $webAppRaw | ConvertFrom-Json
}
function Update-WebAppContainerImage {
param(
[Parameter(Mandatory = $true)]
[string]$ResolvedResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$ResolvedWebAppName,
[Parameter(Mandatory = $true)]
[pscustomobject]$AcrConnectionInfo,
[Parameter(Mandatory = $true)]
[pscustomobject]$ContainerImageInfo
)
$acrUsername = Invoke-AzureCliCommand -Arguments @('acr', 'credential', 'show', '--name', $AcrName, '--query', 'username', '--output', 'tsv')
$acrPassword = Invoke-AzureCliCommand -Arguments @('acr', 'credential', 'show', '--name', $AcrName, '--query', 'passwords[0].value', '--output', 'tsv')
$expectedImageName = "$($AcrConnectionInfo.LoginServer)/$($ContainerImageInfo.FullName)"
$arguments = @(
'webapp', 'config', 'container', 'set',
'--name', $ResolvedWebAppName,
'--resource-group', $ResolvedResourceGroupName,
'--container-image-name', $expectedImageName,
'--container-registry-url', $AcrConnectionInfo.RegistryUrl,
'--container-registry-user', $acrUsername.Trim(),
'--container-registry-password', $acrPassword.Trim()
)
if (-not [string]::IsNullOrWhiteSpace($Slot)) {
$arguments += @('--slot', $Slot)
}
Write-Host "`n=====> Updating App Service container settings..." -ForegroundColor Cyan
Invoke-AzureCliCommand -Arguments $arguments -AllowEmptyOutput | Out-Null
return $expectedImageName
}
function Confirm-WebAppContainerImage {
param(
[Parameter(Mandatory = $true)]
[string]$ResolvedResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$ResolvedWebAppName,
[Parameter(Mandatory = $true)]
[string]$ExpectedImageName
)
$arguments = @(
'webapp', 'config', 'container', 'show',
'--name', $ResolvedWebAppName,
'--resource-group', $ResolvedResourceGroupName,
'--query', '{image:DOCKER_CUSTOM_IMAGE_NAME,registry:DOCKER_REGISTRY_SERVER_URL}',
'--output', 'json'
)
if (-not [string]::IsNullOrWhiteSpace($Slot)) {
$arguments += @('--slot', $Slot)
}
$containerStateRaw = Invoke-AzureCliCommand -Arguments $arguments
$containerState = $containerStateRaw | ConvertFrom-Json
if ($containerState.image -ne $ExpectedImageName) {
throw "Web app container configuration did not update to '$ExpectedImageName'. Current value: '$($containerState.image)'"
}
}
function Restart-WebAppIfRequested {
param(
[Parameter(Mandatory = $true)]
[string]$ResolvedResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$ResolvedWebAppName
)
if (-not $RestartWebApp) {
return
}
$arguments = @(
'webapp', 'restart',
'--name', $ResolvedWebAppName,
'--resource-group', $ResolvedResourceGroupName
)
if (-not [string]::IsNullOrWhiteSpace($Slot)) {
$arguments += @('--slot', $Slot)
}
Write-Host "`n=====> Restarting App Service..." -ForegroundColor Cyan
Invoke-AzureCliCommand -Arguments $arguments -AllowEmptyOutput | Out-Null
}
Write-Host "`nSimpleChat Upgrade Executing" -ForegroundColor Green
Write-Host "This script performs a code-only container rollout for the Azure CLI deployer." -ForegroundColor Green
Ensure-AzureCliAuthenticated
$targetNames = Resolve-TargetNames
$containerImageInfo = Resolve-ContainerImageInfo -RequestedImageName $ImageName
$acrConnectionInfo = Get-AcrConnectionInfo
$webApp = Ensure-WebAppExists -ResolvedResourceGroupName $targetNames.ResourceGroupName -ResolvedWebAppName $targetNames.WebAppName
if (-not $SkipAcrBuild) {
Invoke-AcrContainerBuild -ContainerImageInfo $containerImageInfo
} else {
Write-Host "Skipping ACR build because -SkipAcrBuild was specified." -ForegroundColor Yellow
}
$expectedImageName = Update-WebAppContainerImage -ResolvedResourceGroupName $targetNames.ResourceGroupName -ResolvedWebAppName $targetNames.WebAppName -AcrConnectionInfo $acrConnectionInfo -ContainerImageInfo $containerImageInfo
Confirm-WebAppContainerImage -ResolvedResourceGroupName $targetNames.ResourceGroupName -ResolvedWebAppName $targetNames.WebAppName -ExpectedImageName $expectedImageName
Restart-WebAppIfRequested -ResolvedResourceGroupName $targetNames.ResourceGroupName -ResolvedWebAppName $targetNames.WebAppName
$slotSuffix = if ([string]::IsNullOrWhiteSpace($Slot)) { '' } else { " (slot: $Slot)" }
Write-Host "`nUpgrade complete.$slotSuffix" -ForegroundColor Green
Write-Host "Resource Group: $($targetNames.ResourceGroupName)"
Write-Host "Web App: $($targetNames.WebAppName)"
Write-Host "Image: $expectedImageName"
Write-Host "Default Hostname: $($webApp.defaultHostName)"