Skip to content

Commit 7a60aae

Browse files
script updated
1 parent 35149fa commit 7a60aae

1 file changed

Lines changed: 82 additions & 35 deletions

File tree

scripts/validate_model_quota.sh

Lines changed: 82 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,58 +51,105 @@ if [[ "$DEPLOYMENT_TYPE" != "Standard" && "$DEPLOYMENT_TYPE" != "GlobalStandard"
5151
fi
5252

5353
MODEL_TYPE="OpenAI.$DEPLOYMENT_TYPE.$MODEL"
54-
FALLBACK_REGIONS=()
54+
ALL_RESULTS=()
55+
FALLBACK_RESULTS=()
5556
ROW_NO=1
56-
QUOTA_CHECKED=false
5757

58-
# -------------------- Output Table Header --------------------
59-
printf "\n%-5s | %-20s | %-40s | %-10s | %-10s | %-10s\n" "No." "Region" "Model Name" "Limit" "Used" "Available"
60-
printf -- "---------------------------------------------------------------------------------------------------------------------\n"
58+
# Print validating message only once
59+
echo -e "\n🔍 Validating model deployment: $MODEL ..."
6160

62-
# -------------------- Check Quota --------------------
63-
for region in "${ALL_REGIONS[@]}"; do
64-
MODEL_INFO=$(az cognitiveservices usage list --location "$region" --query "[?name.value=='$MODEL_TYPE']" --output json 2>/dev/null)
61+
echo "🔍 Checking quota in the requested region '$LOCATION'..."
6562

66-
if [[ -n "$MODEL_INFO" && "$MODEL_INFO" != "[]" ]]; then
67-
CURRENT_VALUE=$(echo "$MODEL_INFO" | jq -r '.[0].currentValue // 0' | cut -d'.' -f1)
68-
LIMIT=$(echo "$MODEL_INFO" | jq -r '.[0].limit // 0' | cut -d'.' -f1)
69-
AVAILABLE=$((LIMIT - CURRENT_VALUE))
63+
# -------------------- Function: Check Quota --------------------
64+
check_quota() {
65+
local region="$1"
66+
local output
67+
output=$(az cognitiveservices usage list --location "$region" --query "[?name.value=='$MODEL_TYPE']" --output json 2>/dev/null)
7068

71-
printf "%-5s | %-20s | %-40s | %-10s | %-10s | %-10s\n" "$ROW_NO" "$region" "$MODEL_TYPE" "$LIMIT" "$CURRENT_VALUE" "$AVAILABLE"
69+
if [[ -z "$output" || "$output" == "[]" ]]; then
70+
return 2 # No data
71+
fi
7272

73-
if [[ "$region" == "$LOCATION" ]]; then
74-
QUOTA_CHECKED=true
75-
if [[ "$AVAILABLE" -ge "$CAPACITY" ]]; then
76-
echo -e "\n✅ Sufficient quota available in user-specified region: $LOCATION"
77-
exit 0
78-
fi
79-
elif [[ "$AVAILABLE" -ge "$CAPACITY" ]]; then
80-
FALLBACK_REGIONS+=("$region ($AVAILABLE)")
81-
fi
73+
local CURRENT_VALUE
74+
local LIMIT
75+
CURRENT_VALUE=$(echo "$output" | jq -r '.[0].currentValue // 0' | cut -d'.' -f1)
76+
LIMIT=$(echo "$output" | jq -r '.[0].limit // 0' | cut -d'.' -f1)
77+
local AVAILABLE=$((LIMIT - CURRENT_VALUE))
78+
79+
ALL_RESULTS+=("$region|$LIMIT|$CURRENT_VALUE|$AVAILABLE")
80+
81+
if [[ "$AVAILABLE" -ge "$CAPACITY" ]]; then
82+
return 0
83+
else
84+
return 1
85+
fi
86+
}
87+
88+
# -------------------- Check User-Specified Region --------------------
89+
check_quota "$LOCATION"
90+
primary_status=$?
91+
92+
if [[ $primary_status -eq 2 ]]; then
93+
echo -e "\n⚠️ Could not retrieve quota info for region: '$LOCATION'."
94+
exit 1
95+
fi
96+
97+
if [[ $primary_status -eq 1 ]]; then
98+
# Get available quota from ALL_RESULTS for LOCATION to use in warning
99+
primary_entry="${ALL_RESULTS[0]}"
100+
IFS='|' read -r _ limit used available <<< "$primary_entry"
101+
echo -e "\n⚠️ Insufficient quota in '$LOCATION' (Available: $available, Required: $CAPACITY). Checking fallback regions..."
102+
fi
103+
104+
# -------------------- Check Fallback Regions --------------------
105+
for region in "${ALL_REGIONS[@]}"; do
106+
[[ "$region" == "$LOCATION" ]] && continue
107+
check_quota "$region"
108+
if [[ $? -eq 0 ]]; then
109+
FALLBACK_RESULTS+=("$region")
82110
fi
83-
((ROW_NO++))
84111
done
85112

86-
printf -- "---------------------------------------------------------------------------------------------------------------------\n"
113+
# -------------------- Print Results Table --------------------
114+
echo ""
115+
printf "%-6s | %-18s | %-35s | %-8s | %-8s | %-9s\n" "No." "Region" "Model Name" "Limit" "Used" "Available"
116+
printf -- "-------------------------------------------------------------------------------------------------------------\n"
117+
118+
index=1
119+
for result in "${ALL_RESULTS[@]}"; do
120+
IFS='|' read -r region limit used available <<< "$result"
121+
printf "| %-4s | %-16s | %-33s | %-7s | %-7s | %-9s |\n" "$index" "$region" "$MODEL_TYPE" "$limit" "$used" "$available"
122+
((index++))
123+
done
124+
printf -- "-------------------------------------------------------------------------------------------------------------\n"
87125

88126
# -------------------- Output Result --------------------
89-
if ! $QUOTA_CHECKED; then
90-
echo -e "\n⚠️ Could not retrieve quota info for region: $LOCATION"
127+
if [[ $primary_status -eq 0 ]]; then
128+
echo -e "\n✅ Sufficient quota found in original region '$LOCATION'."
129+
exit 0
91130
fi
92131

93-
if [[ ${#FALLBACK_REGIONS[@]} -gt 0 ]]; then
94-
echo -e "\n❌ Insufficient quota in '$LOCATION'."
95-
echo "➡️ You may retry using one of the following fallback regions with enough quota:"
96-
for region in "${FALLBACK_REGIONS[@]}"; do
97-
echo "$region"
132+
if [[ ${#FALLBACK_RESULTS[@]} -gt 0 ]]; then
133+
echo -e "\n❌ Deployment cannot proceed in '$LOCATION'."
134+
echo "➡️ You can retry using one of the following regions with sufficient quota:"
135+
echo ""
136+
for region in "${FALLBACK_RESULTS[@]}"; do
137+
for result in "${ALL_RESULTS[@]}"; do
138+
IFS='|' read -r rgn _ _ avail <<< "$result"
139+
if [[ "$rgn" == "$region" ]]; then
140+
echo "$region (Available: $avail)"
141+
break
142+
fi
143+
done
98144
done
145+
99146
echo -e "\n🔧 To proceed, run:"
100147
echo " azd env set AZURE_AISERVICE_LOCATION '<region>'"
101-
echo "📌 To confirm, run:"
148+
echo "📌 To confirm it's set correctly, run:"
102149
echo " azd env get-value AZURE_AISERVICE_LOCATION"
103-
echo "▶️ Then re-run: azd up"
150+
echo "▶️ Once confirmed, re-run azd up to deploy the model in the new region."
104151
exit 2
105152
fi
106153

107-
echo "❌ ERROR: No region has sufficient quota for '$MODEL_TYPE'."
108-
exit 1
154+
echo -e "\n❌ ERROR: No available quota found in any region."
155+
exit 1

0 commit comments

Comments
 (0)