@@ -17,22 +17,32 @@ export MSYS_NO_PATHCONV=1
1717# Usage: ./scripts/post_deployment_setup.sh <resource-group-name>
1818
1919if [ -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 "
2227fi
2328
24- RESOURCE_GROUP=" $1 "
25-
2629echo " =============================================="
2730echo " Post-Deployment Setup"
2831echo " Resource Group: ${RESOURCE_GROUP} "
2932echo " =============================================="
3033
3134SCRIPT_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)
3437az 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
3747RESTORE_KV_NAME=" "
3848RESTORE_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# -------------------------------------------------------
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
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
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."
278288fi
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-
288290echo " "
289291echo " =============================================="
290292echo " Post-Deployment Setup Complete"
0 commit comments