|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Parameters |
| 4 | +IFS=',' read -r -a MODEL_CAPACITY_PAIRS <<< "$1" # Split the comma-separated model and capacity pairs into an array |
| 5 | +USER_REGION="$2" |
| 6 | + |
| 7 | +if [ ${#MODEL_CAPACITY_PAIRS[@]} -lt 1 ]; then |
| 8 | + echo "❌ ERROR: At least one model and capacity pairs must be provided as arguments." |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +# Extract model names and required capacities into arrays |
| 13 | +declare -a MODEL_NAMES |
| 14 | +declare -a CAPACITIES |
| 15 | + |
| 16 | +for PAIR in "${MODEL_CAPACITY_PAIRS[@]}"; do |
| 17 | + MODEL_NAME=$(echo "$PAIR" | cut -d':' -f1) |
| 18 | + CAPACITY=$(echo "$PAIR" | cut -d':' -f2) |
| 19 | + |
| 20 | + if [ -z "$MODEL_NAME" ] || [ -z "$CAPACITY" ]; then |
| 21 | + echo "❌ ERROR: Invalid model and capacity pair '$PAIR'. Both model and capacity must be specified." |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | + |
| 25 | + MODEL_NAMES+=("$MODEL_NAME") |
| 26 | + CAPACITIES+=("$CAPACITY") |
| 27 | +done |
| 28 | + |
| 29 | +echo "🔄 Using Models: ${MODEL_NAMES[*]} with respective Capacities: ${CAPACITIES[*]}" |
| 30 | + |
| 31 | +echo "🔄 Fetching available Azure subscriptions..." |
| 32 | +SUBSCRIPTIONS=$(az account list --query "[?state=='Enabled'].{Name:name, ID:id}" --output tsv) |
| 33 | +SUB_COUNT=$(echo "$SUBSCRIPTIONS" | wc -l) |
| 34 | + |
| 35 | +if [ "$SUB_COUNT" -eq 1 ]; then |
| 36 | + # If only one subscription, automatically select it |
| 37 | + AZURE_SUBSCRIPTION_ID=$(echo "$SUBSCRIPTIONS" | awk '{print $2}') |
| 38 | + echo "✅ Using the only available subscription: $AZURE_SUBSCRIPTION_ID" |
| 39 | +else |
| 40 | + # If multiple subscriptions exist, prompt the user to choose one |
| 41 | + echo "Multiple subscriptions found:" |
| 42 | + echo "$SUBSCRIPTIONS" | awk '{print NR")", $1, "-", $2}' |
| 43 | + |
| 44 | + while true; do |
| 45 | + echo "Enter the number of the subscription to use:" |
| 46 | + read SUB_INDEX |
| 47 | + |
| 48 | + # Validate user input |
| 49 | + if [[ "$SUB_INDEX" =~ ^[0-9]+$ ]] && [ "$SUB_INDEX" -ge 1 ] && [ "$SUB_INDEX" -le "$SUB_COUNT" ]; then |
| 50 | + AZURE_SUBSCRIPTION_ID=$(echo "$SUBSCRIPTIONS" | awk -v idx="$SUB_INDEX" 'NR==idx {print $2}') |
| 51 | + echo "✅ Selected Subscription: $AZURE_SUBSCRIPTION_ID" |
| 52 | + break |
| 53 | + else |
| 54 | + echo "❌ Invalid selection. Please enter a valid number from the list." |
| 55 | + fi |
| 56 | + done |
| 57 | +fi |
| 58 | + |
| 59 | +# Set the selected subscription |
| 60 | +az account set --subscription "$AZURE_SUBSCRIPTION_ID" |
| 61 | +echo "🎯 Active Subscription: $(az account show --query '[name, id]' --output table)" |
| 62 | + |
| 63 | +# List of regions to check |
| 64 | +DEFAULT_REGIONS=("eastus" "uksouth" "eastus2" "northcentralus" "swedencentral" "westus" "westus2" "southcentralus" "canadacentral") |
| 65 | + |
| 66 | +# Prioritize user-provided region if given |
| 67 | +if [ -n "$USER_REGION" ]; then |
| 68 | + # Ensure the user-provided region is checked first |
| 69 | + REGIONS=("$USER_REGION" "${DEFAULT_REGIONS[@]}") |
| 70 | +else |
| 71 | + REGIONS=("${DEFAULT_REGIONS[@]}") |
| 72 | +fi |
| 73 | + |
| 74 | +echo "✅ Retrieved Azure regions. Checking availability..." |
| 75 | + |
| 76 | +VALID_REGIONS=() |
| 77 | +for REGION in "${REGIONS[@]}"; do |
| 78 | + echo "----------------------------------------" |
| 79 | + echo "🔍 Checking region: $REGION" |
| 80 | + |
| 81 | + # Fetch quota information for the region |
| 82 | + QUOTA_INFO=$(az cognitiveservices usage list --location "$REGION" --output json) |
| 83 | + if [ -z "$QUOTA_INFO" ]; then |
| 84 | + echo "⚠️ WARNING: Failed to retrieve quota for region $REGION. Skipping." |
| 85 | + continue |
| 86 | + fi |
| 87 | + |
| 88 | + # Initialize a flag to track if both models have sufficient quota in the region |
| 89 | + BOTH_MODELS_AVAILABLE=true |
| 90 | + |
| 91 | + for index in "${!MODEL_NAMES[@]}"; do |
| 92 | + MODEL_NAME="${MODEL_NAMES[$index]}" |
| 93 | + REQUIRED_CAPACITY="${CAPACITIES[$index]}" |
| 94 | + |
| 95 | + echo "🔍 Checking model: $MODEL_NAME with required capacity: $REQUIRED_CAPACITY" |
| 96 | + |
| 97 | + # Extract model quota information |
| 98 | + MODEL_INFO=$(echo "$QUOTA_INFO" | awk -v model="\"value\": \"OpenAI.Standard.$MODEL_NAME\"" ' |
| 99 | + BEGIN { RS="},"; FS="," } |
| 100 | + $0 ~ model { print $0 } |
| 101 | + ') |
| 102 | + |
| 103 | + if [ -z "$MODEL_INFO" ]; then |
| 104 | + echo "⚠️ WARNING: No quota information found for model: OpenAI.Standard.$MODEL_NAME in $REGION. Skipping." |
| 105 | + BOTH_MODELS_AVAILABLE=false |
| 106 | + break # If any model is not available, no need to check further for this region |
| 107 | + fi |
| 108 | + |
| 109 | + CURRENT_VALUE=$(echo "$MODEL_INFO" | awk -F': ' '/"currentValue"/ {print $2}' | tr -d ',' | tr -d ' ') |
| 110 | + LIMIT=$(echo "$MODEL_INFO" | awk -F': ' '/"limit"/ {print $2}' | tr -d ',' | tr -d ' ') |
| 111 | + |
| 112 | + CURRENT_VALUE=${CURRENT_VALUE:-0} |
| 113 | + LIMIT=${LIMIT:-0} |
| 114 | + |
| 115 | + CURRENT_VALUE=$(echo "$CURRENT_VALUE" | cut -d'.' -f1) |
| 116 | + LIMIT=$(echo "$LIMIT" | cut -d'.' -f1) |
| 117 | + |
| 118 | + AVAILABLE=$((LIMIT - CURRENT_VALUE)) |
| 119 | + |
| 120 | + echo "✅ Model: OpenAI.Standard.$MODEL_NAME | Used: $CURRENT_VALUE | Limit: $LIMIT | Available: $AVAILABLE" |
| 121 | + |
| 122 | + # Check if quota is sufficient |
| 123 | + if [ "$AVAILABLE" -lt "$REQUIRED_CAPACITY" ]; then |
| 124 | + echo "❌ ERROR: 'OpenAI.Standard.$MODEL_NAME' in $REGION has insufficient quota. Required: $REQUIRED_CAPACITY, Available: $AVAILABLE" |
| 125 | + BOTH_MODELS_AVAILABLE=false |
| 126 | + break |
| 127 | + fi |
| 128 | + done |
| 129 | + |
| 130 | + # If both models have sufficient quota, add region to valid regions |
| 131 | + if [ "$BOTH_MODELS_AVAILABLE" = true ]; then |
| 132 | + echo "✅ All models have sufficient quota in $REGION." |
| 133 | + VALID_REGIONS+=("$REGION") |
| 134 | + fi |
| 135 | +done |
| 136 | + |
| 137 | +# Determine final result and display in table format |
| 138 | +if [ ${#VALID_REGIONS[@]} -eq 0 ]; then |
| 139 | + echo "----------------------------------------" |
| 140 | + echo "❌ No region with sufficient quota found for all models. Blocking deployment." |
| 141 | + echo "----------------------------------------" |
| 142 | + exit 0 |
| 143 | +else |
| 144 | + echo "----------------------------------------" |
| 145 | + echo "✅ Suggested Regions with Sufficient Quota" |
| 146 | + echo "----------------------------------------" |
| 147 | + printf "| %-5s | %-20s |\n" "No." "Region" |
| 148 | + echo "----------------------------------------" |
| 149 | + |
| 150 | + INDEX=1 |
| 151 | + for REGION in "${VALID_REGIONS[@]}"; do |
| 152 | + printf "| %-5s | %-20s |\n" "$INDEX" "$REGION" |
| 153 | + INDEX=$((INDEX + 1)) |
| 154 | + done |
| 155 | + |
| 156 | + echo "----------------------------------------" |
| 157 | + exit 0 |
| 158 | +fi |
0 commit comments