|
| 1 | +# List of Azure regions to check for quota (update as needed) |
| 2 | +$AZURE_REGIONS = "$env:AZURE_REGIONS" |
| 3 | +# Ensure regions are correctly split and trimmed |
| 4 | +$REGIONS = ($AZURE_REGIONS -split '[,\s]') | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" } |
| 5 | + |
| 6 | +Write-Output "📍 Processed Regions: $($REGIONS -join ', ')" |
| 7 | + |
| 8 | +$SUBSCRIPTION_ID = $env:AZURE_SUBSCRIPTION_ID |
| 9 | +$GPT_MIN_CAPACITY = $env:GPT_MIN_CAPACITY |
| 10 | +$TEXT_EMBEDDING_MIN_CAPACITY = $env:TEXT_EMBEDDING_MIN_CAPACITY |
| 11 | +$AZURE_CLIENT_ID = $env:AZURE_CLIENT_ID |
| 12 | +$AZURE_TENANT_ID = $env:AZURE_TENANT_ID |
| 13 | +$AZURE_CLIENT_SECRET = $env:AZURE_CLIENT_SECRET |
| 14 | + |
| 15 | +# Authenticate using Service Principal |
| 16 | +Write-Host "Authentication using Service Principal..." |
| 17 | +# Ensure Azure PowerShell module is installed and imported |
| 18 | +Install-Module -Name Az -AllowClobber -Force -Scope CurrentUser |
| 19 | +Import-Module Az |
| 20 | + |
| 21 | +# Create a PSCredential object for authentication |
| 22 | +$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AZURE_CLIENT_ID, (ConvertTo-SecureString $AZURE_CLIENT_SECRET -AsPlainText -Force) |
| 23 | + |
| 24 | +# Attempt to connect using Service Principal |
| 25 | +try { |
| 26 | + Connect-AzAccount -ServicePrincipal -TenantId $AZURE_TENANT_ID -Credential $creds |
| 27 | +} catch { |
| 28 | + Write-Host "❌ Error: Failed to authenticate using Service Principal. $_" |
| 29 | + exit 1 |
| 30 | +} |
| 31 | + |
| 32 | +Write-Host "🔄 Validating required environment variables..." |
| 33 | +if (-not $SUBSCRIPTION_ID -or -not $GPT_MIN_CAPACITY -or -not $TEXT_EMBEDDING_MIN_CAPACITY) { |
| 34 | + Write-Host "❌ ERROR: Missing required environment variables." |
| 35 | + exit 1 |
| 36 | +} |
| 37 | + |
| 38 | +Write-Host "🔄 Setting Azure subscription..." |
| 39 | +$setSubscriptionResult = Set-AzContext -SubscriptionId $SUBSCRIPTION_ID |
| 40 | +if ($setSubscriptionResult -eq $null) { |
| 41 | + Write-Host "❌ ERROR: Invalid subscription ID or insufficient permissions." |
| 42 | + exit 1 |
| 43 | +} |
| 44 | +Write-Host "✅ Azure subscription set successfully." |
| 45 | + |
| 46 | +# Define models and their minimum required capacities |
| 47 | +$MIN_CAPACITY = @{ |
| 48 | + "OpenAI.Standard.gpt-4o-mini" = $GPT_MIN_CAPACITY |
| 49 | + "OpenAI.Standard.text-embedding-3-large" = $TEXT_EMBEDDING_MIN_CAPACITY |
| 50 | +} |
| 51 | + |
| 52 | +$VALID_REGION = "" |
| 53 | + |
| 54 | +foreach ($REGION in $REGIONS) { |
| 55 | + Write-Host "----------------------------------------" |
| 56 | + Write-Host "🔍 Checking region: $REGION" |
| 57 | + |
| 58 | + # Get the Cognitive Services usage information for the region |
| 59 | + $QUOTA_INFO = Get-AzCognitiveServicesUsage -Location $REGION |
| 60 | + if (-not $QUOTA_INFO) { |
| 61 | + Write-Host "⚠️ WARNING: Failed to retrieve quota for region $REGION. Skipping." |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + $INSUFFICIENT_QUOTA = $false |
| 66 | + |
| 67 | + foreach ($MODEL in $MIN_CAPACITY.Keys) { |
| 68 | + |
| 69 | + $MODEL_INFO = $QUOTA_INFO | Where-Object { $_.Name -eq $MODEL } |
| 70 | + |
| 71 | + if (-not $MODEL_INFO) { |
| 72 | + Write-Host "⚠️ WARNING: No quota information found for model: $MODEL in $REGION. Skipping." |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + $CURRENT_VALUE = [int]$MODEL_INFO.CurrentValue |
| 77 | + $LIMIT = [int]$MODEL_INFO.Limit |
| 78 | + |
| 79 | + $AVAILABLE = $LIMIT - $CURRENT_VALUE |
| 80 | + |
| 81 | + Write-Host "✅ Model: $MODEL | Used: $CURRENT_VALUE | Limit: $LIMIT | Available: $AVAILABLE" |
| 82 | + |
| 83 | + if ($AVAILABLE -lt $MIN_CAPACITY[$MODEL]) { |
| 84 | + Write-Host "❌ ERROR: $MODEL in $REGION has insufficient quota." |
| 85 | + $INSUFFICIENT_QUOTA = $true |
| 86 | + break |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if ($INSUFFICIENT_QUOTA -eq $false) { |
| 91 | + $VALID_REGION = $REGION |
| 92 | + break |
| 93 | + } |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +if (-not $VALID_REGION) { |
| 98 | + Write-Host "❌ No region with sufficient quota found. Blocking deployment." |
| 99 | + echo "QUOTA_FAILED=true" >> $env:GITHUB_ENV # Set QUOTA_FAILED for subsequent steps |
| 100 | + exit 0 |
| 101 | +} else { |
| 102 | + Write-Host "✅ Suggested Region: $VALID_REGION" |
| 103 | + echo "VALID_REGION=$VALID_REGION" >> $env:GITHUB_ENV # Set VALID_REGION for subsequent steps |
| 104 | + exit 0 |
| 105 | +} |
0 commit comments