Skip to content

Commit 2b5ccc3

Browse files
refactor: added required changes to skip quota check script if AI Foundry and models already exist
1 parent 7bdb69b commit 2b5ccc3

4 files changed

Lines changed: 82 additions & 87 deletions

File tree

azure.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ hooks:
2626
posix:
2727
shell: sh
2828
run: >
29-
chmod u+r+x ./scripts/validate_model_deployment_quota.sh; chmod u+r+x ./scripts/validate_model_quota.sh; ./scripts/validate_model_deployment_quota.sh --subscription "$AZURE_SUBSCRIPTION_ID" --location "${AZURE_AISERVICE_LOCATION:-japaneast}" --models-parameter "aiModelDeployments"
29+
chmod u+r+x ./scripts/validate_model_deployment_quota.sh; chmod u+r+x ./scripts/validate_model_quota.sh; ./scripts/validate_model_deployment_quota.sh --SubscriptionId "$AZURE_SUBSCRIPTION_ID" --Location "${AZURE_AISERVICE_LOCATION:-japaneast}" --ModelsParameter "aiModelDeployments"
3030
interactive: false
3131
continueOnError: false
3232

infra/main.bicep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,5 @@ module deploymentScriptCLI 'br/public:avm/res/resources/deployment-script:0.5.1'
506506
scriptContent: cosmosAssignCli
507507
}
508508
}
509+
510+
output AZURE_AIFOUNDRY_NAME string = azureAiServices.name

scripts/validate_model_deployment_quota.ps1

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,72 @@ param (
44
[string]$ModelsParameter
55
)
66

7-
# Read from environment variables (do not pass in azure.yaml)
8-
$AiServiceName = $env:AZURE_AISERVICE_NAME
7+
$AiFoundryName = $env:AZURE_AIFOUNDRY_NAME
98
$ResourceGroup = $env:AZURE_RESOURCE_GROUP
109

1110
# Validate required parameters
1211
$MissingParams = @()
13-
14-
if (-not $SubscriptionId) {
15-
$MissingParams += "subscription"
16-
}
17-
if (-not $Location) {
18-
$MissingParams += "location"
19-
}
20-
if (-not $ModelsParameter) {
21-
$MissingParams += "models-parameter"
22-
}
12+
if (-not $SubscriptionId) { $MissingParams += "SubscriptionId" }
13+
if (-not $Location) { $MissingParams += "Location" }
14+
if (-not $ModelsParameter) { $MissingParams += "ModelsParameter" }
15+
if (-not $AiFoundryName) { $MissingParams += "AZURE_AISERVICE_NAME" }
16+
if (-not $ResourceGroup) { $MissingParams += "AZURE_RESOURCE_GROUP" }
2317

2418
if ($MissingParams.Count -gt 0) {
2519
Write-Error "❌ ERROR: Missing required parameters: $($MissingParams -join ', ')"
26-
Write-Host "Usage: .\validate_model_deployment_quotas.ps1 -SubscriptionId <SUBSCRIPTION_ID> -Location <LOCATION> -ModelsParameter <MODELS_PARAMETER>"
2720
exit 1
2821
}
2922

30-
# Load main.parameters.json
23+
# Load model deployment parameters
3124
$JsonContent = Get-Content -Path "./infra/main.parameters.json" -Raw | ConvertFrom-Json
3225
if (-not $JsonContent) {
3326
Write-Error "❌ ERROR: Failed to parse main.parameters.json. Ensure the JSON file is valid."
3427
exit 1
3528
}
3629

3730
$aiModelDeployments = $JsonContent.parameters.$ModelsParameter.value
31+
3832
if (-not $aiModelDeployments -or -not ($aiModelDeployments -is [System.Collections.IEnumerable])) {
3933
Write-Error "❌ ERROR: The specified property '$ModelsParameter' does not exist or is not an array."
4034
exit 1
4135
}
4236

43-
# Check if AI resource + all deployments already exist
44-
if ($AiServiceName -and $ResourceGroup) {
45-
$existing = az cognitiveservices account show `
46-
--name $AiServiceName `
47-
--resource-group $ResourceGroup `
48-
--query "name" --output tsv 2>$null
49-
50-
if ($existing) {
51-
$deployedModels = az cognitiveservices account deployment list `
52-
--name $AiServiceName `
53-
--resource-group $ResourceGroup `
54-
--query "[].name" --output tsv 2>$null
37+
# Check if AI Foundry and model deployments already exist
38+
$existing = az cognitiveservices account show `
39+
--name $AiFoundryName `
40+
--resource-group $ResourceGroup `
41+
--query "name" --output tsv 2>$null
5542

56-
$requiredDeployments = @()
57-
foreach ($deployment in $aiModelDeployments) {
58-
$requiredDeployments += $deployment.name
59-
}
43+
if ($existing) {
44+
$deployedModelsOutput = az cognitiveservices account deployment list `
45+
--name $AiFoundryName `
46+
--resource-group $ResourceGroup `
47+
--query "[].name" --output tsv 2>$null
48+
49+
# Normalize output to array
50+
$deployedModels = @()
51+
if ($deployedModelsOutput -is [string]) {
52+
$deployedModels += $deployedModelsOutput
53+
} elseif ($deployedModelsOutput) {
54+
$deployedModels = $deployedModelsOutput -split "`r?`n"
55+
}
6056

61-
$missingDeployments = @()
62-
foreach ($required in $requiredDeployments) {
63-
if ($deployedModels -notcontains $required) {
64-
$missingDeployments += $required
65-
}
66-
}
57+
$requiredDeployments = $aiModelDeployments | ForEach-Object { $_.name }
58+
$missingDeployments = $requiredDeployments | Where-Object { $_ -notin $deployedModels }
6759

68-
if ($missingDeployments.Count -eq 0) {
69-
Write-Host "ℹ️ Azure AI service '$AiServiceName' exists and all required model deployments are provisioned."
70-
Write-Host "⏭️ Skipping quota validation."
71-
exit 0
72-
} else {
73-
Write-Host "🔍 AI service exists, but the following model deployments are missing: $($missingDeployments -join ', ')"
74-
Write-Host "➡️ Proceeding with quota validation for missing models..."
75-
}
60+
if ($missingDeployments.Count -eq 0) {
61+
Write-Host "ℹ️ AI Foundry '$AiFoundryName' exists and all required model deployments are already provisioned."
62+
Write-Host "⏭️ Skipping quota validation."
63+
exit 0
64+
} else {
65+
Write-Host "🔍 AI Foundry exists, but the following model deployments are missing: $($missingDeployments -join ', ')"
66+
Write-Host "➡️ Proceeding with quota validation for missing models..."
7667
}
68+
} else {
69+
Write-Host "❌ AI Foundry '$AiFoundryName' not found. Proceeding with quota validation."
7770
}
7871

79-
# Start quota validation
72+
# Run quota validation
8073
az account set --subscription $SubscriptionId
8174
Write-Host "🎯 Active Subscription: $(az account show --query '[name, id]' --output tsv)"
8275

@@ -94,17 +87,17 @@ foreach ($deployment in $aiModelDeployments) {
9487

9588
if ($exitCode -ne 0) {
9689
if ($exitCode -eq 2) {
97-
exit 1 # already printed, graceful
90+
exit 1
9891
}
9992
Write-Error "❌ ERROR: Quota validation failed for model deployment: $name"
10093
$QuotaAvailable = $false
10194
}
10295
}
10396

10497
if (-not $QuotaAvailable) {
105-
Write-Error "❌ ERROR: One or more model deployments failed validation."
98+
Write-Error "❌ ERROR: One or more model deployments failed quota validation."
10699
exit 1
107100
} else {
108101
Write-Host "✅ All model deployments passed quota validation successfully."
109102
exit 0
110-
}
103+
}

scripts/validate_model_deployment_quota.sh

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ MODELS_PARAMETER=""
66

77
while [[ $# -gt 0 ]]; do
88
case "$1" in
9-
--subscription)
9+
--SubscriptionId)
1010
SUBSCRIPTION_ID="$2"
1111
shift 2
1212
;;
13-
--location)
13+
--Location)
1414
LOCATION="$2"
1515
shift 2
1616
;;
17-
--models-parameter)
17+
--ModelsParameter)
1818
MODELS_PARAMETER="$2"
1919
shift 2
2020
;;
@@ -25,57 +25,57 @@ while [[ $# -gt 0 ]]; do
2525
esac
2626
done
2727

28+
AIFOUNDRY_NAME="${AZURE_AIFOUNDRY_NAME}"
29+
RESOURCE_GROUP="${AZURE_RESOURCE_GROUP}"
30+
2831
# Validate required parameters
2932
MISSING_PARAMS=()
30-
[[ -z "$SUBSCRIPTION_ID" ]] && MISSING_PARAMS+=("subscription")
31-
[[ -z "$LOCATION" ]] && MISSING_PARAMS+=("location")
32-
[[ -z "$MODELS_PARAMETER" ]] && MISSING_PARAMS+=("models-parameter")
33+
[[ -z "$SUBSCRIPTION_ID" ]] && MISSING_PARAMS+=("SubscriptionId")
34+
[[ -z "$LOCATION" ]] && MISSING_PARAMS+=("Location")
35+
[[ -z "$MODELS_PARAMETER" ]] && MISSING_PARAMS+=("ModelsParameter")
36+
[[ -z "$AIFOUNDRY_NAME" ]] && MISSING_PARAMS+=("AZURE_AIFOUNDRY_NAME")
37+
[[ -z "$RESOURCE_GROUP" ]] && MISSING_PARAMS+=("AZURE_RESOURCE_GROUP")
3338

3439
if [[ ${#MISSING_PARAMS[@]} -ne 0 ]]; then
3540
echo "❌ ERROR: Missing required parameters: ${MISSING_PARAMS[*]}"
36-
echo "Usage: $0 --subscription <SUBSCRIPTION_ID> --location <LOCATION> --models-parameter <MODELS_PARAMETER>"
41+
echo "Usage: $0 --SubscriptionId <SUBSCRIPTION_ID> --Location <LOCATION> --ModelsParameter <MODELS_PARAMETER>"
3742
exit 1
3843
fi
3944

40-
# Read from environment
41-
AISERVICE_NAME="${AZURE_AISERVICE_NAME}"
42-
RESOURCE_GROUP="${AZURE_RESOURCE_GROUP}"
43-
44-
# Check service and deployment existence
45-
if [[ -n "$AISERVICE_NAME" && -n "$RESOURCE_GROUP" ]]; then
46-
existing=$(az cognitiveservices account show --name "$AISERVICE_NAME" --resource-group "$RESOURCE_GROUP" --query "name" --output tsv 2>/dev/null)
47-
if [[ -n "$existing" ]]; then
48-
echo "ℹ️ Found Azure AI service: $AISERVICE_NAME"
45+
# Check if AI Foundry exists and has required deployments
46+
existing=$(az cognitiveservices account show --name "$AIFOUNDRY_NAME" --resource-group "$RESOURCE_GROUP" --query "name" --output tsv 2>/dev/null)
47+
if [[ -n "$existing" ]]; then
48+
echo "ℹ️ Found AI Foundry: $AIFOUNDRY_NAME"
4949

50-
existing_deployments=$(az cognitiveservices account deployment list --name "$AISERVICE_NAME" --resource-group "$RESOURCE_GROUP" --query "[].name" --output tsv 2>/dev/null)
50+
existing_deployments=$(az cognitiveservices account deployment list --name "$AIFOUNDRY_NAME" --resource-group "$RESOURCE_GROUP" --query "[].name" --output tsv 2>/dev/null)
5151

52-
# Extract required model names
53-
required_models=$(jq -r ".parameters.$MODELS_PARAMETER.value[].name" ./infra/main.parameters.json 2>/dev/null)
52+
required_models=$(jq -r ".parameters.$MODELS_PARAMETER.value[].name" ./infra/main.parameters.json 2>/dev/null)
5453

55-
if [[ -z "$required_models" ]]; then
56-
echo "❌ ERROR: Failed to extract required model names from main.parameters.json"
57-
exit 1
58-
fi
54+
if [[ -z "$required_models" ]]; then
55+
echo "❌ ERROR: Failed to extract required model names from main.parameters.json"
56+
exit 1
57+
fi
5958

60-
all_present=true
61-
for model in $required_models; do
62-
if ! grep -q -w "$model" <<< "$existing_deployments"; then
63-
all_present=false
64-
break
65-
fi
66-
done
67-
68-
if [[ "$all_present" == "true" ]]; then
69-
echo "✅ All required model deployments already exist in AI service '$AISERVICE_NAME'."
70-
echo "⏭️ Skipping quota validation."
71-
exit 0
72-
else
73-
echo "🔍 AI service exists but some model deployments are missing — proceeding with quota validation."
59+
all_present=true
60+
for model in $required_models; do
61+
if ! grep -q -w "$model" <<< "$existing_deployments"; then
62+
all_present=false
63+
break
7464
fi
65+
done
66+
67+
if [[ "$all_present" == "true" ]]; then
68+
echo "✅ All required model deployments already exist in AI Foundry '$AIFOUNDRY_NAME'."
69+
echo "⏭️ Skipping quota validation."
70+
exit 0
71+
else
72+
echo "🔍 AI Foundry exists, but some model deployments are missing — proceeding with quota validation."
7573
fi
74+
else
75+
echo "❌ AI Foundry '$AIFOUNDRY_NAME' not found. Proceeding with quota validation."
7676
fi
7777

78-
# If we reach here, continue with normal quota checks
78+
# Load deployment definitions
7979
aiModelDeployments=$(jq -c ".parameters.$MODELS_PARAMETER.value[]" ./infra/main.parameters.json)
8080
if [[ $? -ne 0 ]]; then
8181
echo "❌ ERROR: Failed to parse main.parameters.json. Ensure jq is installed and the JSON is valid."

0 commit comments

Comments
 (0)