|
| 1 | +#Requires -Version 7.0 |
| 2 | + |
| 3 | +param( |
| 4 | + [string]$CosmosDbName, |
| 5 | + [string]$DatabaseName, |
| 6 | + [string]$ContainerName, |
| 7 | + [string]$DirectoryPath, |
| 8 | + [string]$ResourceGroup |
| 9 | +) |
| 10 | + |
| 11 | +# Get parameters from azd env, if not provided |
| 12 | +if (-not $CosmosDbName) { |
| 13 | + $CosmosDbName = $(azd env get-value COSMOSDB_ACCOUNT_NAME) |
| 14 | +} |
| 15 | +if (-not $DatabaseName) { |
| 16 | + $DatabaseName = $(azd env get-value COSMOSDB_DATABASE) |
| 17 | +} |
| 18 | +if (-not $ContainerName) { |
| 19 | + $ContainerName = $(azd env get-value COSMOSDB_CONTAINER) |
| 20 | +} |
| 21 | +if (-not $DirectoryPath) { |
| 22 | + $DirectoryPath = "data/agent_teams" |
| 23 | +} |
| 24 | +if (-not $ResourceGroup) { |
| 25 | + $ResourceGroup = $(azd env get-value AZURE_RESOURCE_GROUP) |
| 26 | +} |
| 27 | +$AzSubscriptionId = $(azd env get-value AZURE_SUBSCRIPTION_ID) |
| 28 | + |
| 29 | +# Check if all required arguments are provided |
| 30 | +if (-not $CosmosDbName -or -not $DatabaseName -or -not $ContainerName -or -not $DirectoryPath) { |
| 31 | + Write-Host "Usage: .\infra\scripts\Upload-Team-Config.ps1 -CosmosDbName <CosmosDbName> -DatabaseName <DatabaseName> -ContainerName <ContainerName> -DirectoryPath <DirectoryPath> [-ResourceGroup <ResourceGroupName>]" |
| 32 | + exit 1 |
| 33 | +} |
| 34 | + |
| 35 | +# Authenticate with Azure |
| 36 | +try { |
| 37 | + $currentAzContext = az account show | ConvertFrom-Json -ErrorAction Stop |
| 38 | + Write-Host "Already authenticated with Azure." |
| 39 | +} catch { |
| 40 | + Write-Host "Not authenticated with Azure. Attempting to authenticate..." |
| 41 | + Write-Host "Authenticating with Azure CLI..." |
| 42 | + az login |
| 43 | + if ($LASTEXITCODE -ne 0) { |
| 44 | + Write-Host "Authentication failed." |
| 45 | + exit 1 |
| 46 | + } |
| 47 | + $currentAzContext = az account show | ConvertFrom-Json |
| 48 | +} |
| 49 | + |
| 50 | +# Check if user has selected the correct subscription |
| 51 | +$currentSubscriptionId = $currentAzContext.id |
| 52 | +$currentSubscriptionName = $currentAzContext.name |
| 53 | +if ($currentSubscriptionId -ne $AzSubscriptionId) { |
| 54 | + Write-Host "Current selected subscription is $currentSubscriptionName ( $currentSubscriptionId )." |
| 55 | + $confirmation = Read-Host "Do you want to continue with this subscription? (y/n)" |
| 56 | + if ($confirmation.ToLower() -ne "y") { |
| 57 | + Write-Host "Fetching available subscriptions..." |
| 58 | + $availableSubscriptions = (az account list --query "[?state=='Enabled']" | ConvertFrom-Json -AsHashtable) |
| 59 | + $subscriptionArray = $availableSubscriptions | ForEach-Object { |
| 60 | + [PSCustomObject]@{ Name = $_.name; Id = $_.id } |
| 61 | + } |
| 62 | + do { |
| 63 | + Write-Host "" |
| 64 | + Write-Host "Available Subscriptions:" |
| 65 | + Write-Host "========================" |
| 66 | + for ($i = 0; $i -lt $subscriptionArray.Count; $i++) { |
| 67 | + Write-Host "$($i+1). $($subscriptionArray[$i].Name) ( $($subscriptionArray[$i].Id) )" |
| 68 | + } |
| 69 | + Write-Host "========================" |
| 70 | + Write-Host "" |
| 71 | + [int]$subscriptionIndex = Read-Host "Enter the number of the subscription (1-$($subscriptionArray.Count)) to use" |
| 72 | + if ($subscriptionIndex -ge 1 -and $subscriptionIndex -le $subscriptionArray.Count) { |
| 73 | + $selectedSubscription = $subscriptionArray[$subscriptionIndex-1] |
| 74 | + $selectedSubscriptionName = $selectedSubscription.Name |
| 75 | + $selectedSubscriptionId = $selectedSubscription.Id |
| 76 | + $result = az account set --subscription $selectedSubscriptionId |
| 77 | + if ($LASTEXITCODE -eq 0) { |
| 78 | + Write-Host "Switched to subscription: $selectedSubscriptionName ( $selectedSubscriptionId )" |
| 79 | + break |
| 80 | + } else { |
| 81 | + Write-Host "Failed to switch to subscription: $selectedSubscriptionName ( $selectedSubscriptionId )." |
| 82 | + } |
| 83 | + } else { |
| 84 | + Write-Host "Invalid selection. Please try again." |
| 85 | + } |
| 86 | + } while ($true) |
| 87 | + } else { |
| 88 | + Write-Host "Proceeding with the current subscription: $currentSubscriptionName ( $currentSubscriptionId )" |
| 89 | + az account set --subscription $currentSubscriptionId |
| 90 | + } |
| 91 | +} else { |
| 92 | + Write-Host "Proceeding with the subscription: $currentSubscriptionName ( $currentSubscriptionId )" |
| 93 | + az account set --subscription $currentSubscriptionId |
| 94 | +} |
| 95 | + |
| 96 | +$userPrincipalId = $(az ad signed-in-user show --query id -o tsv) |
| 97 | + |
| 98 | +$cosmosIsPublicAccessDisabled = $false |
| 99 | +if ($ResourceGroup) { |
| 100 | + $cosmosPublicAccess = $(az cosmosdb show --name $CosmosDbName --resource-group $ResourceGroup --query "publicNetworkAccess" -o tsv) |
| 101 | + if ($cosmosPublicAccess -eq "Disabled") { |
| 102 | + $cosmosIsPublicAccessDisabled = $true |
| 103 | + Write-Host "Enabling public access for CosmosDB: $CosmosDbName" |
| 104 | + az cosmosdb update --name $CosmosDbName --resource-group $ResourceGroup --public-network-access enabled --output none |
| 105 | + if ($LASTEXITCODE -ne 0) { |
| 106 | + Write-Host "Error: Failed to enable public access for CosmosDB." |
| 107 | + exit 1 |
| 108 | + } |
| 109 | + Write-Host "Public access enabled for CosmosDB: $CosmosDbName" |
| 110 | + } else { |
| 111 | + Write-Host "Public access is already enabled for CosmosDB: $CosmosDbName" |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +# Determine the correct Python command |
| 116 | +$pythonCmd = $null |
| 117 | + |
| 118 | +try { |
| 119 | + $pythonVersion = (python --version) 2>&1 |
| 120 | + if ($pythonVersion -match "Python \d") { |
| 121 | + $pythonCmd = "python" |
| 122 | + } |
| 123 | +} |
| 124 | +catch { |
| 125 | + # Do nothing, try python3 next |
| 126 | +} |
| 127 | + |
| 128 | +if (-not $pythonCmd) { |
| 129 | + try { |
| 130 | + $pythonVersion = (python3 --version) 2>&1 |
| 131 | + if ($pythonVersion -match "Python \d") { |
| 132 | + $pythonCmd = "python3" |
| 133 | + } |
| 134 | + } |
| 135 | + catch { |
| 136 | + Write-Host "Python is not installed on this system or it is not added in the PATH." |
| 137 | + exit 1 |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +if (-not $pythonCmd) { |
| 142 | + Write-Host "Python is not installed on this system or it is not added in the PATH." |
| 143 | + exit 1 |
| 144 | +} |
| 145 | + |
| 146 | +# Create virtual environment |
| 147 | +$venvPath = "infra/scripts/scriptenv" |
| 148 | +if (Test-Path $venvPath) { |
| 149 | + Write-Host "Virtual environment already exists. Skipping creation." |
| 150 | +} else { |
| 151 | + Write-Host "Creating virtual environment" |
| 152 | + & $pythonCmd -m venv $venvPath |
| 153 | +} |
| 154 | + |
| 155 | +# Activate the virtual environment |
| 156 | +$activateScript = "" |
| 157 | +if (Test-Path (Join-Path -Path $venvPath -ChildPath "bin/Activate.ps1")) { |
| 158 | + $activateScript = Join-Path -Path $venvPath -ChildPath "bin/Activate.ps1" |
| 159 | +} elseif (Test-Path (Join-Path -Path $venvPath -ChildPath "Scripts/Activate.ps1")) { |
| 160 | + $activateScript = Join-Path -Path $venvPath -ChildPath "Scripts/Activate.ps1" |
| 161 | +} |
| 162 | +if ($activateScript) { |
| 163 | + Write-Host "Activating virtual environment" |
| 164 | + . $activateScript |
| 165 | +} else { |
| 166 | + Write-Host "Error activating virtual environment. Requirements may be installed globally." |
| 167 | +} |
| 168 | + |
| 169 | +# Install the requirements |
| 170 | +Write-Host "Installing requirements" |
| 171 | +pip install --quiet -r infra/scripts/requirements.txt |
| 172 | +Write-Host "Requirements installed" |
| 173 | + |
| 174 | +# Run the Python script to upload team configuration |
| 175 | +Write-Host "Running the python script to upload team configuration" |
| 176 | +$process = Start-Process -FilePath $pythonCmd -ArgumentList "infra/scripts/team-config-scripts/upload_team_config.py", $CosmosDbName, $DatabaseName, $ContainerName, $DirectoryPath, $userPrincipalId -Wait -NoNewWindow -PassThru |
| 177 | +if ($process.ExitCode -ne 0) { |
| 178 | + Write-Host "Error: Team configuration upload failed." |
| 179 | + exit 1 |
| 180 | +} |
| 181 | + |
| 182 | +#disable public access for cosmos |
| 183 | +if ($cosmosIsPublicAccessDisabled) { |
| 184 | + Write-Host "Disabling public access for CosmosDB: $CosmosDbName" |
| 185 | + az cosmosdb update --name $CosmosDbName --resource-group $ResourceGroup --public-network-access disabled --output none |
| 186 | + if ($LASTEXITCODE -ne 0) { |
| 187 | + Write-Host "Error: Failed to disable public access for CosmosDB." |
| 188 | + exit 1 |
| 189 | + } |
| 190 | + Write-Host "Public access disabled for CosmosDB: $CosmosDbName" |
| 191 | +} |
| 192 | + |
| 193 | +Write-Host "Script executed successfully. Team configuration uploaded." |
0 commit comments