@@ -8,24 +8,19 @@ RECOMMENDED_TOKENS=200
88
99ALL_REGIONS=(' australiaeast' ' eastus' ' eastus2' ' francecentral' ' japaneast' ' norwayeast' ' southindia' ' swedencentral' ' uksouth' ' westus' ' westus3' )
1010
11- # Globals for recommended/not recommended regions
1211RECOMMENDED_REGIONS=()
1312NOT_RECOMMENDED_REGIONS=()
1413ALL_RESULTS=()
1514FALLBACK_RESULTS=()
1615
17- # -------------------- Utility: Update .env and main.parameters.json --------------------
1816update_env_and_parameters () {
1917 local new_location=" $1 "
2018 local new_capacity=" $2 "
21-
2219 echo " ➡️ Updating environment and parameters with Location='$new_location ' and Capacity='$new_capacity '..."
2320
24- # Update the AZD environment
2521 azd env set AZURE_AISERVICE_LOCATION " $new_location "
2622 azd env set AZURE_ENV_MODEL_CAPACITY " $new_capacity "
2723
28- # Update main.parameters.json
2924 local PARAM_FILE=" ./infra/main.parameters.json"
3025 if [[ ! -f " $PARAM_FILE " ]]; then
3126 echo " ❌ ERROR: $PARAM_FILE not found, cannot update parameters."
@@ -41,7 +36,6 @@ update_env_and_parameters() {
4136 echo " ✅ Updated .env and $PARAM_FILE successfully."
4237}
4338
44- # -------------------- Function: Check Quota --------------------
4539check_quota () {
4640 local region=" $1 "
4741 local MODEL_TYPE=" OpenAI.$DEPLOYMENT_TYPE .$MODEL "
@@ -50,7 +44,7 @@ check_quota() {
5044 output=$( az cognitiveservices usage list --location " $region " --query " [?name.value=='$MODEL_TYPE ']" --output json 2> /dev/null)
5145
5246 if [[ -z " $output " || " $output " == " []" ]]; then
53- return 2 # No data
47+ return 2
5448 fi
5549
5650 local CURRENT_VALUE=$( echo " $output " | jq -r ' .[0].currentValue // 0' | cut -d' .' -f1)
@@ -72,117 +66,21 @@ check_quota() {
7266 fi
7367}
7468
75- # -------------------- Input Validation --------------------
76- while [[ $# -gt 0 ]]; do
77- case " $1 " in
78- --model)
79- MODEL=" $2 " ; shift 2 ;;
80- --capacity)
81- CAPACITY=" $2 " ; shift 2 ;;
82- --deployment-type)
83- DEPLOYMENT_TYPE=" $2 " ; shift 2 ;;
84- --location)
85- LOCATION=" $2 " ; shift 2 ;;
86- * )
87- echo " ❌ ERROR: Unknown option: $1 " ; exit 1 ;;
88- esac
89- done
90-
91- [[ -z " $LOCATION " ]] && MISSING_PARAMS+=(" location" )
92- [[ -z " $MODEL " ]] && MISSING_PARAMS+=(" model" )
93- if ! [[ " $CAPACITY " =~ ^[0-9]+$ ]] || [[ " $CAPACITY " -le 0 ]]; then
94- MISSING_PARAMS+=(" capacity" )
95- fi
96-
97- if [[ ${# MISSING_PARAMS[@]} -ne 0 ]]; then
98- echo " ❌ ERROR: Missing/invalid: ${MISSING_PARAMS[*]} "
99- echo " Usage: $0 --location <LOCATION> --model <MODEL> --capacity <CAPACITY> [--deployment-type <DEPLOYMENT_TYPE>]"
100- exit 1
101- fi
102-
103- if [[ " $DEPLOYMENT_TYPE " != " Standard" && " $DEPLOYMENT_TYPE " != " GlobalStandard" ]]; then
104- echo " ❌ ERROR: Invalid deployment type: $DEPLOYMENT_TYPE "
105- exit 1
106- fi
107-
108- # -------------------- Main Logic Starts --------------------
109- echo " 🔍 Checking quota in '$LOCATION ' for model '$MODEL '..."
110-
111- check_quota " $LOCATION "
112- primary_status=$?
113-
114- if [[ $primary_status -eq 1 ]]; then
115- primary_entry=" ${ALL_RESULTS[0]} "
116- IFS=' |' read -r _ limit used available <<< " $primary_entry"
117- echo -e " \n⚠️ Insufficient quota in '$LOCATION ' (Available: $available , Required: $CAPACITY ). Checking fallback regions..."
118- fi
119-
120- for region in " ${ALL_REGIONS[@]} " ; do
121- [[ " $region " == " $LOCATION " ]] && continue
122- check_quota " $region "
123- [[ $? -eq 0 ]] && FALLBACK_RESULTS+=(" $region " )
124- done
125-
126- # -------------------- Quota Table Output --------------------
127- echo " "
128- printf " %-5s | %-16s | %-33s | %-6s | %-6s | %-9s\n" " No." " Region" " Model Name" " Limit" " Used" " Available"
129- printf -- " ---------------------------------------------------------------------------------------------\n"
130-
131- index=1
132- REGIONS_WITH_QUOTA=()
133- for result in " ${ALL_RESULTS[@]} " ; do
134- IFS=' |' read -r region limit used available <<< " $result"
135- if (( available >= 50 )) ; then
136- printf " | %-3s | %-16s | %-33s | %-6s | %-6s | %-9s |\n" " $index " " $region " " OpenAI.$DEPLOYMENT_TYPE .$MODEL " " $limit " " $used " " $available "
137- REGIONS_WITH_QUOTA+=(" $region |$available " )
138- (( index++ ))
139- fi
140- done
141- printf -- " ---------------------------------------------------------------------------------------------\n"
142-
143- # -------------------- Prompt if No Region Has Enough --------------------
144- if [[ $primary_status -ne 0 && ${# FALLBACK_RESULTS[@]} -eq 0 ]]; then
145- echo -e " \n❌ No region has sufficient quota (≥ $CAPACITY tokens)."
146-
147- max_available=0; max_region=" "
69+ show_table () {
70+ echo -e " \n--------------------------------------------------------------------------------------------------"
71+ echo -e " | No. | Region | Model Name | Limit | Used | Available |"
72+ echo -e " --------------------------------------------------------------------------------------------------"
73+ local index=1
14874 for result in " ${ALL_RESULTS[@]} " ; do
14975 IFS=' |' read -r region limit used available <<< " $result"
150- if (( available > max_available )) ; then
151- max_available= $ available
152- max_region= $region
76+ if (( available >= 50 )) ; then
77+ printf " | %-3s | %-16s | %-33s | %-6s | %-6s | %-9s |\n " " $index " " $region " " OpenAI. $DEPLOYMENT_TYPE . $MODEL " " $limit " " $used " " $ available"
78+ (( index ++ ))
15379 fi
15480 done
81+ echo -e " --------------------------------------------------------------------------------------------------"
82+ }
15583
156- if (( max_available == 0 )) ; then
157- echo " ⚠️ No quota info from any region. Cannot proceed."
158- exit 1
159- fi
160-
161- echo " ➡️ Highest available quota: $max_available tokens in '$max_region '."
162- echo -n " ❓ Enter new capacity to use (<= $max_available ): "
163- read -r new_capacity < /dev/tty
164-
165- if ! [[ " $new_capacity " =~ ^[0-9]+$ ]] || (( new_capacity > max_available )) || (( new_capacity <= 0 )) ; then
166- echo " ❌ Invalid capacity entered. Exiting."
167- exit 1
168- fi
169-
170- echo -n " ❓ Enter location to use (default: $max_region ): "
171- read -r new_location < /dev/tty
172- new_location=" ${new_location:- $max_region } "
173-
174- CAPACITY=$new_capacity
175- LOCATION=$new_location
176-
177- check_quota " $LOCATION "
178- [[ $? -eq 0 ]] || { echo " ❌ Insufficient quota in '$LOCATION '. Exiting." ; exit 1; }
179-
180- update_env_and_parameters " $LOCATION " " $CAPACITY "
181- echo " ✅ Deployment settings updated."
182- exit 0
183- fi
184-
185- # -------------------- Handle Fallback Prompt --------------------
18684ask_for_location () {
18785 echo -e " \nPlease choose a region from the above list:"
18886 echo -n " 📍 Enter region: "
@@ -202,6 +100,20 @@ ask_for_location() {
202100 return
203101 fi
204102
103+ if (( new_capacity < RECOMMENDED_TOKENS )) ; then
104+ recommended_list=$( IFS=, ; echo " ${RECOMMENDED_REGIONS[*]} " )
105+ echo -e " \n⚠️ You have entered a capacity of $new_capacity , which is less than the recommended minimum ($RECOMMENDED_TOKENS )."
106+ echo -e " 🚨 This may cause performance issues or unexpected behavior."
107+ echo -e " ℹ️ Recommended regions (≥ $RECOMMENDED_TOKENS tokens available): $recommended_list "
108+ echo -n " ❓ Proceed anyway? (y/n): "
109+ read -r proceed_anyway < /dev/tty
110+ if [[ ! " $proceed_anyway " =~ ^[Yy]$ ]]; then
111+ ask_for_location
112+ return
113+ fi
114+ fi
115+
116+ echo -e " \n🔍 Checking quota in region '$new_location ' for requested capacity: $new_capacity ..."
205117 CAPACITY=$new_capacity
206118 LOCATION=$new_location
207119
@@ -211,36 +123,120 @@ ask_for_location() {
211123 echo " ✅ Updated and ready to deploy in '$LOCATION '."
212124 exit 0
213125 else
214- echo " ❌ Insufficient quota in '$LOCATION '. Try another."
126+ echo " ❌ Insufficient quota in '$LOCATION '. Checking fallback regions..."
127+ check_fallback_regions
128+ fi
129+ }
130+
131+ check_fallback_regions () {
132+ # Check fallback regions if the primary location fails
133+ for region in " ${ALL_REGIONS[@]} " ; do
134+ [[ " $region " == " $LOCATION " ]] && continue
135+ check_quota " $region "
136+ if [[ $? -eq 0 ]]; then
137+ FALLBACK_RESULTS+=(" $region " )
138+ fi
139+ done
140+
141+ show_table
142+
143+ if [[ ${# FALLBACK_RESULTS[@]} -gt 0 ]]; then
144+ echo -e " \n➡️ Found fallback regions with sufficient quota."
145+ if [[ ${# RECOMMENDED_REGIONS[@]} -gt 0 ]]; then
146+ echo -e " \nℹ️ Recommended regions (≥ $RECOMMENDED_TOKENS tokens available):"
147+ for region in " ${RECOMMENDED_REGIONS[@]} " ; do
148+ echo " - $region "
149+ done
150+ fi
151+
152+ echo -e " \n❗ The originally selected region '$LOCATION ' does not have enough quota."
153+ echo -e " 👉 You can manually choose one of the recommended fallback regions for deployment."
154+ else
155+ echo -e " \n❌ ERROR: No region has sufficient quota."
156+ fi
157+
158+ # Allow retry or exit
159+ echo -n " ❓ Would you like to retry with a different region? (y/n): "
160+ read -r retry < /dev/tty
161+ if [[ " $retry " =~ ^[Yy]$ ]]; then
215162 ask_for_location
163+ else
164+ echo " Exiting... No region with sufficient quota."
165+ exit 1
216166 fi
217167}
218168
219- # -------------------- Final Decision Logic --------------------
220- if [[ $primary_status -eq 0 ]]; then
221-
222- if [[ " ${NOT_RECOMMENDED_REGIONS[*]} " == * " $LOCATION " * ]]; then
223- recommended_list=$( IFS=, ; echo " ${RECOMMENDED_REGIONS[*]} " )
224- bold_regions=$( printf " \033[1m%s\033[0m" " $recommended_list " )
225- echo -e " \n⚠️ \033[1mWarning:\033[0m Region '$LOCATION ' has available tokens less than the recommended threshold ($RECOMMENDED_TOKENS )."
226- echo -e " 🚨 Your application may not work as expected due to limited quota."
227- echo -e " \nℹ️ Recommended regions (≥ $RECOMMENDED_TOKENS tokens available): $bold_regions "
228- echo -e " 👉 It's advisable to deploy in one of these regions for optimal app performance."
229-
169+ manual_region_input () {
170+ echo -n " Please enter a region you want to try manually: "
171+ read -r manual_region < /dev/tty
172+ if [[ -z " $manual_region " ]]; then
173+ echo " ❌ ERROR: No region entered. Exiting."
174+ exit 1
175+ fi
176+
177+ echo -n " Enter the capacity you want to use (numeric value): "
178+ read -r manual_capacity < /dev/tty
179+
180+ if ! [[ " $manual_capacity " =~ ^[0-9]+$ ]] || (( manual_capacity <= 0 )) ; then
181+ echo " ❌ Invalid capacity value. Try again."
182+ manual_region_input
183+ return
184+ fi
185+
186+ if (( manual_capacity < RECOMMENDED_TOKENS )) ; then
187+ echo -e " \n⚠️ You have entered a capacity of $manual_capacity , which is less than the recommended minimum ($RECOMMENDED_TOKENS )."
188+ echo -e " 🚨 This may cause performance issues or unexpected behavior."
189+ echo -e " ℹ️ Recommended regions (≥ $RECOMMENDED_TOKENS tokens available): ${RECOMMENDED_REGIONS[*]} "
230190 echo -n " ❓ Proceed anyway? (y/n): "
231- read -r proceed < /dev/tty
232- if [[ " $proceed " =~ ^[Yy]$ ]]; then
233- update_env_and_parameters " $LOCATION " " $CAPACITY "
234- echo " ✅ Proceeding with '$LOCATION '."
235- exit 0
236- else
237- ask_for_location
191+ read -r proceed_anyway < /dev/tty
192+ if [[ ! " $proceed_anyway " =~ ^[Yy]$ ]]; then
193+ manual_region_input
194+ return
238195 fi
239- else
196+ fi
197+
198+ echo -e " \n🔍 Checking quota in region '$manual_region ' for requested capacity: $manual_capacity ..."
199+ CAPACITY=$manual_capacity
200+ LOCATION=$manual_region
201+ check_quota " $LOCATION "
202+
203+ if [[ $? -eq 0 ]]; then
240204 update_env_and_parameters " $LOCATION " " $CAPACITY "
241- echo " ✅ Quota is sufficient in ' $LOCATION '. Proceeding ."
205+ echo " ✅ Deployment values set. Exiting ."
242206 exit 0
207+ else
208+ echo " ❌ Quota in region '$LOCATION ' is insufficient. Available: $manual_capacity "
243209 fi
244- else
245- ask_for_location
210+ }
211+
212+ # ---------- Parse Inputs ----------
213+ while [[ $# -gt 0 ]]; do
214+ case " $1 " in
215+ --location) LOCATION=" $2 " ; shift ;;
216+ --model) MODEL=" $2 " ; shift ;;
217+ --deployment-type) DEPLOYMENT_TYPE=" $2 " ; shift ;;
218+ --capacity) CAPACITY=" $2 " ; shift ;;
219+ * ) echo " ❌ Unknown option: $1 " ; exit 1 ;;
220+ esac
221+ shift
222+ done
223+
224+ # ---------- Validate Inputs ----------
225+ if [[ -z " $LOCATION " || -z " $MODEL " || -z " $CAPACITY " || " $CAPACITY " -le 0 ]]; then
226+ echo " ❌ Missing required parameters. Usage: $0 --location <LOCATION> --model <MODEL> --capacity <CAPACITY>"
227+ exit 1
246228fi
229+
230+ # ---------- Start Process ----------
231+ echo -e " \n🔍 Checking quota in the requested region '$LOCATION '..."
232+ check_quota " $LOCATION "
233+ if [[ $? -eq 0 ]]; then
234+ update_env_and_parameters " $LOCATION " " $CAPACITY "
235+ echo " ✅ Proceeding with deployment in '$LOCATION '."
236+ exit 0
237+ else
238+ primary_entry=" ${ALL_RESULTS[0]} "
239+ IFS=' |' read -r _ limit used available <<< " $primary_entry"
240+ echo " ❌ Quota insufficient in '$LOCATION ' (Available: $available , Required: $CAPACITY ). Checking fallback regions..."
241+ check_fallback_regions
242+ fi
0 commit comments