-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathpost_deployment.ps1
More file actions
247 lines (207 loc) · 9.43 KB
/
post_deployment.ps1
File metadata and controls
247 lines (207 loc) · 9.43 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
# Stop script on any error
$ErrorActionPreference = "Stop"
Write-Host "[Search] Fetching container app info from azd environment..."
# Load values from azd env
$CONTAINER_WEB_APP_NAME = azd env get-value CONTAINER_WEB_APP_NAME
$CONTAINER_WEB_APP_FQDN = azd env get-value CONTAINER_WEB_APP_FQDN
$CONTAINER_API_APP_NAME = azd env get-value CONTAINER_API_APP_NAME
$CONTAINER_API_APP_FQDN = azd env get-value CONTAINER_API_APP_FQDN
$CONTAINER_WORKFLOW_APP_NAME = azd env get-value CONTAINER_WORKFLOW_APP_NAME
# Get subscription and resource group (assuming same for both)
$SUBSCRIPTION_ID = azd env get-value AZURE_SUBSCRIPTION_ID
$RESOURCE_GROUP = azd env get-value AZURE_RESOURCE_GROUP
# Construct Azure Portal URLs
$WEB_APP_PORTAL_URL = "https://portal.azure.com/#resource/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.App/containerApps/$CONTAINER_WEB_APP_NAME"
$API_APP_PORTAL_URL = "https://portal.azure.com/#resource/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.App/containerApps/$CONTAINER_API_APP_NAME"
$WORKFLOW_APP_PORTAL_URL = "https://portal.azure.com/#resource/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.App/containerApps/$CONTAINER_WORKFLOW_APP_NAME"
# Get the current script's directory
$ScriptDir = $PSScriptRoot
# Navigate from infra/scripts -> root -> src/api/data/data.sh
$DataScriptPath = Join-Path $ScriptDir "..\..\src\ContentProcessorAPI\samples\schemas"
# Resolve to an absolute path
$FullPath = Resolve-Path $DataScriptPath
# Output
Write-Host ""
Write-Host "[Info] Web App Details:"
Write-Host " [OK] Name: $CONTAINER_WEB_APP_NAME"
Write-Host " [URL] Endpoint: $CONTAINER_WEB_APP_FQDN"
Write-Host " [Link] Portal URL: $WEB_APP_PORTAL_URL"
Write-Host ""
Write-Host "[Info] API App Details:"
Write-Host " [OK] Name: $CONTAINER_API_APP_NAME"
Write-Host " [URL] Endpoint: $CONTAINER_API_APP_FQDN"
Write-Host " [Link] Portal URL: $API_APP_PORTAL_URL"
Write-Host ""
Write-Host "[Info] Workflow App Details:"
Write-Host " [OK] Name: $CONTAINER_WORKFLOW_APP_NAME"
Write-Host " [Link] Portal URL: $WORKFLOW_APP_PORTAL_URL"
Write-Host ""
Write-Host "[Package] Registering schemas and creating schema set..."
# Check if private networking (WAF) is enabled
$ENABLE_PRIVATE_NETWORKING = $null
try {
$ENABLE_PRIVATE_NETWORKING = azd env get-value ENABLE_PRIVATE_NETWORKING 2>$null
} catch { }
# When private networking is enabled, the API is internal-only (ingressExternal=false).
# Use the web app's /api proxy to reach the backend through same-origin routing.
if ($ENABLE_PRIVATE_NETWORKING -eq "true") {
Write-Host " [Info] Private networking (WAF) is enabled. Using web app /api proxy to reach backend."
$ApiBaseUrl = "https://$CONTAINER_WEB_APP_FQDN/api"
} else {
$ApiBaseUrl = "https://$CONTAINER_API_APP_FQDN"
}
Write-Host " [Wait] Waiting for API to be ready..."
$MaxRetries = 10
$RetryInterval = 15
$ApiReady = $false
for ($i = 1; $i -le $MaxRetries; $i++) {
try {
$response = Invoke-WebRequest -Uri "$ApiBaseUrl/schemavault/" -Method GET -UseBasicParsing -TimeoutSec 10 -ErrorAction Stop
if ($response.StatusCode -eq 200) {
Write-Host " [OK] API is ready."
$ApiReady = $true
break
}
} catch {
# Ignore - API not ready yet
}
Write-Host " Attempt $i/$MaxRetries - API not ready, retrying in ${RetryInterval}s..."
Start-Sleep -Seconds $RetryInterval
}
if (-not $ApiReady) {
Write-Host " API did not become ready after $MaxRetries attempts. Skipping schema registration."
Write-Host " Run manually after the API is ready."
} else {
# ---------- Schema registration (no Python dependency) ----------
$SchemaInfoFile = Join-Path $FullPath "schema_info.json"
$Manifest = Get-Content $SchemaInfoFile -Raw | ConvertFrom-Json
$SchemaVaultUrl = "$ApiBaseUrl/schemavault/"
$SchemaSetVaultUrl = "$ApiBaseUrl/schemasetvault/"
# --- Step 1: Register schemas ---
Write-Host ""
Write-Host ("=" * 60)
Write-Host "Step 1: Register schemas"
Write-Host ("=" * 60)
# Fetch existing schemas
$ExistingSchemas = @()
try {
$ExistingSchemas = Invoke-RestMethod -Uri $SchemaVaultUrl -Method GET -TimeoutSec 30 -ErrorAction Stop
Write-Host "Fetched $($ExistingSchemas.Count) existing schema(s)."
} catch {
Write-Host "Warning: Could not fetch existing schemas. Proceeding..."
}
$Registered = @{} # ClassName -> schema Id
foreach ($entry in $Manifest.schemas) {
$ClassName = $entry.ClassName
$Description = $entry.Description
$SchemaFile = Join-Path $FullPath $entry.File
Write-Host ""
Write-Host "Processing schema: $ClassName"
if (-not (Test-Path $SchemaFile)) {
Write-Host "Error: Schema file '$SchemaFile' does not exist. Skipping..."
continue
}
# Check if already registered
$existing = $ExistingSchemas | Where-Object { $_.ClassName -eq $ClassName } | Select-Object -First 1
if ($existing) {
$schemaId = $existing.Id
Write-Host " Schema '$ClassName' already exists with ID: $schemaId"
$Registered[$ClassName] = $schemaId
continue
}
Write-Host " Registering new schema '$ClassName'..."
# Build multipart form data
$dataPayload = @{ ClassName = $ClassName; Description = $Description } | ConvertTo-Json -Compress
$fileBytes = [System.IO.File]::ReadAllBytes($SchemaFile)
$fileName = [System.IO.Path]::GetFileName($SchemaFile)
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"data`"$LF",
$dataPayload,
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"",
"Content-Type: text/x-python$LF",
[System.Text.Encoding]::UTF8.GetString($fileBytes),
"--$boundary--$LF"
) -join $LF
try {
$resp = Invoke-RestMethod -Uri $SchemaVaultUrl -Method POST `
-ContentType "multipart/form-data; boundary=$boundary" `
-Body $bodyLines -TimeoutSec 60 -ErrorAction Stop
$schemaId = $resp.Id
Write-Host " Successfully registered: $Description's Schema Id - $schemaId"
$Registered[$ClassName] = $schemaId
} catch {
Write-Host " Failed to upload '$fileName'. Error: $_"
}
}
# --- Step 2: Create schema set ---
Write-Host ""
Write-Host ("=" * 60)
Write-Host "Step 2: Create schema set"
Write-Host ("=" * 60)
$SetName = $Manifest.schemaset.Name
$SetDesc = $Manifest.schemaset.Description
$ExistingSets = @()
try {
$ExistingSets = Invoke-RestMethod -Uri $SchemaSetVaultUrl -Method GET -TimeoutSec 30 -ErrorAction Stop
Write-Host "Fetched $($ExistingSets.Count) existing schema set(s)."
} catch {
Write-Host "Warning: Could not fetch existing schema sets. Proceeding..."
}
$SchemaSetId = $null
$existingSet = $ExistingSets | Where-Object { $_.Name -eq $SetName } | Select-Object -First 1
if ($existingSet) {
$SchemaSetId = $existingSet.Id
Write-Host " Schema set '$SetName' already exists with ID: $SchemaSetId"
} else {
Write-Host " Creating schema set '$SetName'..."
try {
$setResp = Invoke-RestMethod -Uri $SchemaSetVaultUrl -Method POST `
-ContentType "application/json" `
-Body (@{ Name = $SetName; Description = $SetDesc } | ConvertTo-Json) `
-TimeoutSec 30 -ErrorAction Stop
$SchemaSetId = $setResp.Id
Write-Host " Created schema set '$SetName' with ID: $SchemaSetId"
} catch {
Write-Host " Failed to create schema set. Error: $_"
}
}
if (-not $SchemaSetId) {
Write-Host "Error: Could not create or find schema set. Aborting step 3."
} else {
# --- Step 3: Add schemas to schema set ---
Write-Host ""
Write-Host ("=" * 60)
Write-Host "Step 3: Add schemas to schema set"
Write-Host ("=" * 60)
$AlreadyInSet = @()
try {
$AlreadyInSet = Invoke-RestMethod -Uri "$SchemaSetVaultUrl$SchemaSetId/schemas" -Method GET -TimeoutSec 30 -ErrorAction Stop
} catch { }
$AlreadyInSetIds = $AlreadyInSet | ForEach-Object { $_.Id }
foreach ($className in $Registered.Keys) {
$schemaId = $Registered[$className]
if ($AlreadyInSetIds -contains $schemaId) {
Write-Host " Schema '$className' ($schemaId) already in schema set - skipped"
continue
}
try {
Invoke-RestMethod -Uri "$SchemaSetVaultUrl$SchemaSetId/schemas" -Method POST `
-ContentType "application/json" `
-Body (@{ SchemaId = $schemaId } | ConvertTo-Json) `
-TimeoutSec 30 -ErrorAction Stop | Out-Null
Write-Host " Added '$className' ($schemaId) to schema set"
} catch {
Write-Host " Failed to add '$className' to schema set. Error: $_"
}
}
}
Write-Host ""
Write-Host ("=" * 60)
Write-Host "Schema registration process completed."
Write-Host " Schemas registered: $($Registered.Count)"
Write-Host ("=" * 60)
}