Skip to content

Commit 739eaa1

Browse files
fix: update post-deployment scripts to use dynamic admin command and improve resource group input handling
1 parent 34a48ad commit 739eaa1

File tree

2 files changed

+58
-50
lines changed

2 files changed

+58
-50
lines changed

scripts/post_deployment_setup.ps1

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ Write-Host " Post-Deployment Setup"
2727
Write-Host " Resource Group: $ResourceGroupName"
2828
Write-Host "=============================================="
2929

30-
# Remove rdbms-connect extension if present (it conflicts with built-in microsoft-entra-admin commands)
30+
# Remove rdbms-connect extension if present (it conflicts with built-in admin commands)
3131
az extension remove --name rdbms-connect 2>$null | Out-Null
3232

33+
# Detect whether to use 'microsoft-entra-admin' (newer CLI) or 'ad-admin' (older CLI)
34+
$pgAdminCmd = "ad-admin"
35+
$entraCheck = az postgres flexible-server microsoft-entra-admin --help 2>$null
36+
if ($LASTEXITCODE -eq 0) { $pgAdminCmd = "microsoft-entra-admin" }
37+
3338
# Track resources that need public access restored to Disabled
3439
$resourcesToRestore = @()
3540

@@ -80,6 +85,8 @@ function Restore-NetworkAccess {
8085
# -------------------------------------------------------
8186
# STEP 1 — Set Function App Client Key
8287
# -------------------------------------------------------
88+
try {
89+
8390
Write-Host ""
8491
Write-Host "--- Step 1: Set Function App Client Key ---"
8592

@@ -145,7 +152,6 @@ else {
145152
Write-Host "✓ Retrieving function key from Key Vault..."
146153
$functionKey = az keyvault secret show --vault-name $keyVaultName --name "FUNCTION-KEY" --query "value" -o tsv
147154
if ($LASTEXITCODE -ne 0 -or -not $functionKey) {
148-
Restore-NetworkAccess
149155
Write-Error "✗ Failed to retrieve 'FUNCTION-KEY' secret from Key Vault '$keyVaultName'."
150156
exit 1
151157
}
@@ -171,7 +177,6 @@ else {
171177
return ($LASTEXITCODE -eq 0)
172178
}
173179
if (-not $keySet) {
174-
Restore-NetworkAccess
175180
Write-Error "✗ Failed to set function key on '$functionAppName' after retries."
176181
exit 1
177182
}
@@ -234,14 +239,13 @@ else {
234239
$currentUserUpn = az ad signed-in-user show --query "userPrincipalName" -o tsv 2>$null
235240
$currentUserOid = az ad signed-in-user show --query "id" -o tsv 2>$null
236241
if (-not $currentUserUpn -or -not $currentUserOid) {
237-
Restore-NetworkAccess
238242
Write-Error "✗ Could not determine current signed-in user. Ensure you are logged in with 'az login'."
239243
exit 1
240244
}
241245
Write-Host "✓ Current user: $currentUserUpn ($currentUserOid)"
242246

243247
# Ensure current user is a PostgreSQL Entra administrator
244-
$existingAdmins = az postgres flexible-server microsoft-entra-admin list --resource-group $ResourceGroupName --server-name $serverName --query "[].objectId" -o tsv 2>$null
248+
$existingAdmins = az postgres flexible-server $pgAdminCmd list --resource-group $ResourceGroupName --server-name $serverName --query "[].objectId" -o tsv 2>$null
245249
$isAdmin = $false
246250
if ($existingAdmins) {
247251
foreach ($adminOid in ($existingAdmins -split "`n")) {
@@ -251,7 +255,7 @@ else {
251255
$addedPgAdmin = $false
252256
if (-not $isAdmin) {
253257
Write-Host "✓ Adding current user as PostgreSQL Entra administrator..."
254-
$adminOutput = az postgres flexible-server microsoft-entra-admin create `
258+
$adminOutput = az postgres flexible-server $pgAdminCmd create `
255259
--resource-group $ResourceGroupName `
256260
--server-name $serverName `
257261
--display-name $currentUserUpn `
@@ -290,7 +294,7 @@ else {
290294
# Remove temporary PostgreSQL admin if we added it
291295
if ($addedPgAdmin) {
292296
Write-Host "✓ Removing temporary PostgreSQL Entra admin for current user..."
293-
az postgres flexible-server microsoft-entra-admin delete `
297+
az postgres flexible-server $pgAdminCmd delete `
294298
--resource-group $ResourceGroupName `
295299
--server-name $serverName `
296300
--object-id $currentUserOid `
@@ -308,10 +312,12 @@ else {
308312
Write-Host "✓ PostgreSQL table creation completed."
309313
}
310314

311-
# -------------------------------------------------------
312-
# STEP 3 — Restore private networking (if it was enabled)
313-
# -------------------------------------------------------
314-
Restore-NetworkAccess
315+
} finally {
316+
# -------------------------------------------------------
317+
# STEP 3 — Restore private networking (if it was enabled)
318+
# -------------------------------------------------------
319+
Restore-NetworkAccess
320+
}
315321

316322
Write-Host ""
317323
Write-Host "=============================================="

scripts/post_deployment_setup.sh

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,32 @@ export MSYS_NO_PATHCONV=1
1717
# Usage: ./scripts/post_deployment_setup.sh <resource-group-name>
1818

1919
if [ -z "$1" ]; then
20-
echo "Usage: $0 <resource-group-name>"
21-
exit 1
20+
read -rp "Enter the resource group name: " RESOURCE_GROUP
21+
if [ -z "$RESOURCE_GROUP" ]; then
22+
echo "Resource group name is required."
23+
exit 1
24+
fi
25+
else
26+
RESOURCE_GROUP="$1"
2227
fi
2328

24-
RESOURCE_GROUP="$1"
25-
2629
echo "=============================================="
2730
echo " Post-Deployment Setup"
2831
echo " Resource Group: ${RESOURCE_GROUP}"
2932
echo "=============================================="
3033

3134
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -W 2>/dev/null || pwd)"
3235

33-
# Remove rdbms-connect extension if present (it conflicts with built-in microsoft-entra-admin commands)
36+
# Remove rdbms-connect extension if present (it conflicts with built-in admin commands)
3437
az extension remove --name rdbms-connect > /dev/null 2>&1 || true
3538

39+
# Detect whether to use 'microsoft-entra-admin' (newer CLI) or 'ad-admin' (older CLI)
40+
if az postgres flexible-server microsoft-entra-admin --help > /dev/null 2>&1; then
41+
PG_ADMIN_CMD="microsoft-entra-admin"
42+
else
43+
PG_ADMIN_CMD="ad-admin"
44+
fi
45+
3646
# Track resources that need public access restored to Disabled
3747
RESTORE_KV_NAME=""
3848
RESTORE_PG_NAME=""
@@ -48,6 +58,30 @@ restore_network_access() {
4858
fi
4959
}
5060

61+
# Global cleanup function — handles PostgreSQL temp resources (if set) and network restore
62+
cleanup() {
63+
# Remove temporary PostgreSQL admin if we added it
64+
if [ "$ADDED_PG_ADMIN" = "true" ]; then
65+
echo "✓ Removing temporary PostgreSQL Entra admin for current user..."
66+
az postgres flexible-server $PG_ADMIN_CMD delete \
67+
--resource-group "$RESOURCE_GROUP" \
68+
--server-name "$SERVER_NAME" \
69+
--object-id "$CURRENT_USER_OID" \
70+
--yes 2>/dev/null || true
71+
fi
72+
# Remove temporary firewall rule if server was discovered
73+
if [ -n "$SERVER_NAME" ]; then
74+
echo "✓ Removing temporary firewall rule..."
75+
az postgres flexible-server firewall-rule delete \
76+
--resource-group "$RESOURCE_GROUP" \
77+
--name "$SERVER_NAME" \
78+
--rule-name "AllowPostDeploySetup" \
79+
--yes 2>/dev/null || true
80+
fi
81+
restore_network_access
82+
}
83+
trap cleanup EXIT
84+
5185
# -------------------------------------------------------
5286
# STEP 1 — Set Function App Client Key
5387
# -------------------------------------------------------
@@ -112,7 +146,6 @@ else
112146
FUNCTION_KEY=$(az keyvault secret show --vault-name "$KEY_VAULT_NAME" --name "FUNCTION-KEY" --query "value" -o tsv 2>/dev/null || true)
113147
if [ -z "$FUNCTION_KEY" ]; then
114148
echo "✗ ERROR: Failed to retrieve 'FUNCTION-KEY' secret from Key Vault '${KEY_VAULT_NAME}'." >&2
115-
restore_network_access
116149
exit 1
117150
fi
118151

@@ -170,7 +203,6 @@ else
170203
if [ -n "$PG_ERR" ]; then
171204
echo "✗ ERROR: Failed to enable public access on PostgreSQL. Cannot proceed." >&2
172205
echo " $PG_ERR" >&2
173-
restore_network_access
174206
exit 1
175207
fi
176208
RESTORE_PG_NAME="$SERVER_NAME"
@@ -207,13 +239,12 @@ else
207239
CURRENT_USER_OID=$(az ad signed-in-user show --query "id" -o tsv 2>/dev/null || true)
208240
if [ -z "$CURRENT_USER_UPN" ] || [ -z "$CURRENT_USER_OID" ]; then
209241
echo "✗ ERROR: Could not determine current signed-in user. Ensure you are logged in with 'az login'." >&2
210-
restore_network_access
211242
exit 1
212243
fi
213244
echo "✓ Current user: ${CURRENT_USER_UPN} (${CURRENT_USER_OID})"
214245

215246
# Ensure current user is a PostgreSQL Entra administrator
216-
EXISTING_ADMINS=$(az postgres flexible-server microsoft-entra-admin list --resource-group "$RESOURCE_GROUP" --server-name "$SERVER_NAME" --query "[].objectId" -o tsv 2>/dev/null || true)
247+
EXISTING_ADMINS=$(az postgres flexible-server $PG_ADMIN_CMD list --resource-group "$RESOURCE_GROUP" --server-name "$SERVER_NAME" --query "[].objectId" -o tsv 2>/dev/null || true)
217248
IS_ADMIN=false
218249
ADDED_PG_ADMIN=false
219250
if [ -n "$EXISTING_ADMINS" ]; then
@@ -226,7 +257,7 @@ else
226257
fi
227258
if [ "$IS_ADMIN" = "false" ]; then
228259
echo "✓ Adding current user as PostgreSQL Entra administrator..."
229-
ADMIN_ERR=$(az postgres flexible-server microsoft-entra-admin create \
260+
ADMIN_ERR=$(az postgres flexible-server $PG_ADMIN_CMD create \
230261
--resource-group "$RESOURCE_GROUP" \
231262
--server-name "$SERVER_NAME" \
232263
--display-name "$CURRENT_USER_UPN" \
@@ -244,27 +275,6 @@ else
244275
echo "✓ Current user is already a PostgreSQL Entra administrator."
245276
fi
246277

247-
# Ensure firewall rule cleanup on exit (along with network restore)
248-
cleanup() {
249-
# Remove temporary PostgreSQL admin if we added it
250-
if [ "$ADDED_PG_ADMIN" = "true" ]; then
251-
echo "✓ Removing temporary PostgreSQL Entra admin for current user..."
252-
az postgres flexible-server microsoft-entra-admin delete \
253-
--resource-group "$RESOURCE_GROUP" \
254-
--server-name "$SERVER_NAME" \
255-
--object-id "$CURRENT_USER_OID" \
256-
--yes 2>/dev/null || true
257-
fi
258-
echo "✓ Removing temporary firewall rule..."
259-
az postgres flexible-server firewall-rule delete \
260-
--resource-group "$RESOURCE_GROUP" \
261-
--name "$SERVER_NAME" \
262-
--rule-name "AllowPostDeploySetup" \
263-
--yes 2>/dev/null || true
264-
restore_network_access
265-
}
266-
trap cleanup EXIT
267-
268278
# Install Python dependencies
269279
REQUIREMENTS_FILE="${SCRIPT_DIR}/data_scripts/requirements.txt"
270280
if [ -f "$REQUIREMENTS_FILE" ]; then
@@ -277,14 +287,6 @@ else
277287
echo "✓ PostgreSQL table creation completed."
278288
fi
279289

280-
# -------------------------------------------------------
281-
# STEP 3 — Restore private networking (if no PostgreSQL trap set it)
282-
# -------------------------------------------------------
283-
# If no PostgreSQL server was found, the trap won't fire, so restore here
284-
if [ -z "$SERVER_FQDN" ]; then
285-
restore_network_access
286-
fi
287-
288290
echo ""
289291
echo "=============================================="
290292
echo " Post-Deployment Setup Complete"

0 commit comments

Comments
 (0)