Skip to content

Commit e9ab3a7

Browse files
resourcedeployment ps changes
1 parent c6d9bf7 commit e9ab3a7

1 file changed

Lines changed: 121 additions & 93 deletions

File tree

Deployment/resourcedeployment.ps1

Lines changed: 121 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -188,90 +188,104 @@ function LoginAzure([string]$subscriptionID) {
188188
Write-Host "Switched subscription to '$subscriptionID' `r`n" -ForegroundColor Yellow
189189
}
190190

191-
function DeployAzureResources([string]$location, [string]$modelLocation) {
191+
function DeployAzureResources() {
192192
Write-Host "Started Deploying Knowledge Mining Solution Accelerator Service Azure resources.....`r`n" -ForegroundColor Yellow
193-
193+
194194
try {
195-
# Generate a random number between 0 and 99999
195+
# Load variables from .env file
196+
if (Test-Path ".env") {
197+
Get-Content ".env" | ForEach-Object {
198+
if ($_ -match "^\s*([^#][^=]+)=(.+)$") {
199+
$key = $matches[1].Trim()
200+
$value = $matches[2].Trim()
201+
Set-Variable -Name $key -Value $value -Scope Script
202+
}
203+
}
204+
Write-Host "Loaded variables from .env file" -ForegroundColor Green
205+
}
206+
else {
207+
Write-Host ".env file not found. Please create one." -ForegroundColor Red
208+
failureBanner
209+
exit 1
210+
}
211+
212+
# Required values from .env
213+
$envLocation = if ($env:LOCATION) { $env:LOCATION } else { "eastus" }
214+
$environmentName = if ($env:ENVIRONMENT_NAME) { $env:ENVIRONMENT_NAME } else { "dev" }
215+
$resourceGroupName = if ($env:RESOURCE_GROUP_NAME) { $env:RESOURCE_GROUP_NAME } else { "" }
216+
217+
# Generate random deployment name
196218
$randomNumber = Get-Random -Minimum 0 -Maximum 99999
197-
# Pad the number with leading zeros to ensure it is 5 digits long
198219
$randomNumberPadded = $randomNumber.ToString("D5")
199-
# Make deployment name unique by appending random number
200220
$deploymentName = "KM_SA_Deployment$randomNumberPadded"
201221

202-
203-
if (-not $resourceGroupName) {
204-
# Generate a new RG name using your existing logic
205-
206-
# Load abbreviation from abbreviations.json (optional)
222+
if (-not $resourceGroupName -or $resourceGroupName -eq "") {
223+
# Load abbreviation for RG prefix
207224
$abbrs = Get-Content -Raw -Path "./abbreviations.json" | ConvertFrom-Json
208225
if (-not $abbrs -or -not $abbrs.managementGovernance.resourceGroup) {
209226
Write-Host "abbreviations.json is missing or malformed."
210227
failureBanner
211228
exit 1
212229
}
213-
$rgPrefix = $abbrs.managementGovernance.resourceGroup # e.g., "rg-"
214-
215-
# Constants
230+
$rgPrefix = $abbrs.managementGovernance.resourceGroup
216231
$resourceprefix_name = "kmgs"
217232

218-
# Call Bicep to generate resourcePrefix
219-
$resourcePrefix = az deployment sub create `
220-
--location $location `
221-
--name $deploymentName `
222-
--template-file ./resourcePrefix.bicep `
223-
--parameters environmentName=$environmentName location=$location `
224-
--query "properties.outputs.resourcePrefix.value" `
225-
-o tsv
226-
227-
# Final Resource Group Name
228-
$resourceGroupName = "$rgPrefix$resourceprefix_name$resourcePrefix"
233+
# Just generate RG name using prefix and random suffix
234+
$resourceGroupName = "$rgPrefix$resourceprefix_name$randomNumberPadded"
229235

230236
Write-Host "Generated Resource Group Name: $resourceGroupName"
231-
232-
Write-Host "No RG provided. Creating new RG: $resourceGroupName" -ForegroundColor Yellow
233-
az group create --name $resourceGroupName --location $location --tags EnvironmentName=$environmentName TemplateName="DKM" | Out-Null
237+
az group create --name $resourceGroupName --location $envLocation --tags EnvironmentName=$environmentName TemplateName="DKM" | Out-Null
234238
}
235239
else {
236240
$exists = az group exists --name $resourceGroupName | ConvertFrom-Json
237241
if (-not $exists) {
238242
Write-Host "Specified RG does not exist. Creating RG: $resourceGroupName" -ForegroundColor Yellow
239-
az group create --name $resourceGroupName --location $location --tags EnvironmentName=$environmentName TemplateName="DKM" | Out-Null
243+
az group create --name $resourceGroupName --location $envLocation --tags EnvironmentName=$environmentName TemplateName="DKM" | Out-Null
240244
}
241245
else {
242246
az group update --name $resourceGroupName --set tags.EnvironmentName=$environmentName tags.TemplateName="DKM" | Out-Null
243247
Write-Host "Using existing RG: $resourceGroupName" -ForegroundColor Green
244248
}
245249
}
246250

247-
# Perform a what-if deployment to preview changes
248-
Write-Host "Evaluating Deployment resource availabilities to preview changes..." -ForegroundColor Yellow
249-
$whatIfResult = az deployment group what-if --resource-group $resourceGroupName --template-file "./main.bicep" --name $deploymentName --parameters modeldatacenter=$modelLocation location=$location environmentName=$environmentName
250-
251-
if ($LASTEXITCODE -ne 0) {
252-
Write-Host "There might be something wrong with your deployment." -ForegroundColor Red
253-
Write-Host $whatIfResult -ForegroundColor Red
254-
failureBanner
255-
exit 1
251+
# Not taking values from main.bicep.
252+
# Instead, you can deploy resources manually with az CLI using values from .env.
253+
# Example: deploying storage account (if defined in .env)
254+
if ($env:STORAGE_ACCOUNT_NAME) {
255+
Write-Host "Deploying storage account: $($env:STORAGE_ACCOUNT_NAME)" -ForegroundColor Yellow
256+
az storage account create `
257+
--name $env:STORAGE_ACCOUNT_NAME `
258+
--resource-group $resourceGroupName `
259+
--location $envLocation `
260+
--sku Standard_LRS `
261+
--kind StorageV2 | Out-Null
256262
}
257-
# Proceed with the actual deployment
258-
Write-Host "Proceeding with Deployment..." -ForegroundColor Yellow
259-
Write-Host "Resource Group Name: $resourceGroupName" -ForegroundColor Yellow
260-
$deploymentResult = az deployment group create --resource-group $resourceGroupName --template-file "./main.bicep" --name $deploymentName --parameters modeldatacenter=$modelLocation location=$location environmentName=$environmentName
261-
# Check if deploymentResult is valid
262-
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult -variableName "Deployment Result"
263-
if ($LASTEXITCODE -ne 0) {
264-
Write-Host "Deployment failed. Stopping execution." -ForegroundColor Red
265-
Write-Host $deploymentResult -ForegroundColor Red
266-
failureBanner
267-
exit 1
263+
264+
# Example: deploying Cognitive Services (if defined in .env)
265+
if ($env:COGNITIVE_ACCOUNT_NAME) {
266+
Write-Host "Deploying Cognitive Services: $($env:COGNITIVE_ACCOUNT_NAME)" -ForegroundColor Yellow
267+
az cognitiveservices account create `
268+
--name $env:COGNITIVE_ACCOUNT_NAME `
269+
--resource-group $resourceGroupName `
270+
--kind CognitiveServices `
271+
--sku S0 `
272+
--location $envLocation `
273+
--yes | Out-Null
268274
}
269275

270-
$joinedString = $deploymentResult -join ""
271-
$jsonString = ConvertFrom-Json $joinedString
272-
273-
return $jsonString
274-
} catch {
276+
Write-Host "Deployment completed successfully using .env values" -ForegroundColor Green
277+
278+
# Build JSON result
279+
$result = @{
280+
ResourceGroupName = $resourceGroupName
281+
DeploymentName = $deploymentName
282+
Location = $envLocation
283+
Environment = $environmentName
284+
Timestamp = (Get-Date).ToString("s")
285+
}
286+
return ($result | ConvertTo-Json -Depth 3)
287+
}
288+
catch {
275289
Write-Host "An error occurred during the deployment process:" -ForegroundColor Red
276290
Write-Host $_.Exception.Message -ForegroundColor Red
277291
Write-Host $_.InvocationInfo.PositionMessage -ForegroundColor Red
@@ -281,6 +295,7 @@ function DeployAzureResources([string]$location, [string]$modelLocation) {
281295
}
282296
}
283297

298+
284299
function DisplayResult([pscustomobject]$jsonString) {
285300
$resourcegroupName = $jsonString.properties.outputs.gs_resourcegroup_name.value
286301
$solutionPrefix = $jsonString.properties.outputs.gs_solution_prefix.value
@@ -511,62 +526,75 @@ try {
511526
###############################################################
512527
$deploymentResult = [DeploymentResult]::new()
513528
LoginAzure($subscriptionID)
514-
# Deploy Azure Resources
515-
Write-Host "Deploying Azure resources in $location region.....`r`n" -ForegroundColor Yellow
529+
# Deploy Azure Resources using .env values
530+
Write-Host "Deploying Azure resources from .env configuration.....`r`n" -ForegroundColor Yellow
516531

517-
$resultJson = DeployAzureResources -location $location -modelLocation $modelLocation
532+
$resultJson = DeployAzureResources
518533

534+
# Convert JSON string back to object
535+
$resultObj = $resultJson | ConvertFrom-Json
519536
# Ensure ResourceGroupName is set correctly
520-
$deploymentResult.ResourceGroupName = $resourceGroupName
537+
$deploymentResult.ResourceGroupName = $resultObj.ResourceGroupName
521538
# Map the deployment result to DeploymentResult object
522539
$deploymentResult.MapResult($resultJson)
523540
# Display the deployment result
524-
DisplayResult($resultJson)
541+
DisplayResult $resultObj
525542

526543
###############################################################
527544
# Step 2 : Get Secrets from Azure resources
528545
Show-Banner -Title "Step 2 : Get Secrets from Azure resources"
529546
###############################################################
530-
# Validate if the Storage Account Name is empty or null
547+
548+
# Pull values from environment variables (or fall back to defaults)
549+
# Build deployment result object from .env values
550+
$deploymentResult = [PSCustomObject]@{
551+
ResourceGroupName = $env:RESOURCE_GROUP_NAME
552+
StorageAccountName = $env:STORAGE_ACCOUNT_NAME
553+
AzCosmosDBName = $env:AZ_COSMOSDB_NAME
554+
AzCognitiveServiceName = $env:AZ_COGNITIVE_SERVICE_NAME
555+
AzSearchServiceName = $env:AZ_SEARCH_SERVICE_NAME
556+
AzOpenAiServiceName = $env:AZ_OPENAI_SERVICE_NAME
557+
AzOpenAiServiceEndpoint = $env:AZ_OPENAI_SERVICE_ENDPOINT
558+
AzSearchServiceEndpoint = $env:AZ_SEARCH_SERVICE_ENDPOINT
559+
AzCognitiveServiceEndpoint = $env:AZ_COGNITIVE_SERVICE_ENDPOINT
560+
AzGPT4oModelId = $env:AZ_GPT4O_MODEL_ID
561+
AzGPT4oModelName = $env:AZ_GPT4O_MODEL_NAME
562+
AzGPTEmbeddingModelName = $env:AZ_GPT_EMBEDDING_MODEL_NAME
563+
}
564+
565+
# Validate Storage Account Name
531566
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.StorageAccountName -variableName "Storage Account Name"
532567

533568
# Get the storage account key
534-
$storageAccountKey = az storage account keys list --account-name $deploymentResult.StorageAccountName --resource-group $deploymentResult.ResourceGroupName --query "[0].value" -o tsv
535-
536-
# Validate if the storage account key is empty or null
537-
ValidateVariableIsNullOrEmpty -variableValue $storageAccountKey -variableName "Storage account key"
538-
539-
## Construct the connection string manually
569+
$storageAccountKey = az storage account keys list `
570+
--account-name $deploymentResult.StorageAccountName `
571+
--resource-group $deploymentResult.ResourceGroupName `
572+
--query "[0].value" -o tsv
573+
574+
# Validate storage account key
575+
ValidateVariableIsNullOrEmpty -variableValue $storageAccountKey -variableName "Storage account key"
576+
577+
# Construct connection string manually
540578
$storageAccountConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($deploymentResult.StorageAccountName);AccountKey=$storageAccountKey;EndpointSuffix=core.windows.net"
541-
# Validate if the Storage Account Connection String is empty or null
579+
580+
# Validate storage account connection string
542581
ValidateVariableIsNullOrEmpty -variableValue $storageAccountConnectionString -variableName "Storage Account Connection String"
543-
544-
## Assign the connection string to the deployment result object
545-
$deploymentResult.StorageAccountConnectionString = $storageAccountConnectionString
546-
547-
# Check if ResourceGroupName is valid
548-
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.ResourceGroupName -variableName "Resource group name"
549-
550-
# Check if AzCosmosDBName is valid
551-
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzCosmosDBName -variableName "Az Cosmos DB name"
552-
553-
# Check if AzCognitiveServiceName is valid
554-
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzCognitiveServiceName -variableName "Az Cognitive Service name"
555-
556-
# Check if AzSearchServiceName is valid
557-
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzSearchServiceName -variableName "Az Search Service name"
558-
559-
# Check if AzOpenAiServiceName is valid
560-
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzOpenAiServiceName -variableName "Az OpenAI Service name"
561-
562-
# Get MongoDB connection string
563-
$deploymentResult.AzCosmosDBConnectionString = az cosmosdb keys list --name $deploymentResult.AzCosmosDBName --resource-group $deploymentResult.ResourceGroupName --type connection-strings --query "connectionStrings[0].connectionString" -o tsv
564-
# Get Azure Cognitive Service API Key
565-
$deploymentResult.AzCognitiveServiceKey = az cognitiveservices account keys list --name $deploymentResult.AzCognitiveServiceName --resource-group $deploymentResult.ResourceGroupName --query "key1" -o tsv
566-
# Get Azure Search Service Admin Key
567-
$deploymentResult.AzSearchAdminKey = az search admin-key show --service-name $deploymentResult.AzSearchServiceName --resource-group $deploymentResult.ResourceGroupName --query "primaryKey" -o tsv
568-
# Get Azure Open AI Service API Key
569-
$deploymentResult.AzOpenAiServiceKey = az cognitiveservices account keys list --name $deploymentResult.AzOpenAiServiceName --resource-group $deploymentResult.ResourceGroupName --query "key1" -o tsv
582+
583+
# Assign connection string
584+
$deploymentResult | Add-Member -NotePropertyName StorageAccountConnectionString -NotePropertyValue $storageAccountConnectionString -Force
585+
586+
# Validate core service names
587+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.ResourceGroupName -variableName "Resource group name"
588+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzCosmosDBName -variableName "Az Cosmos DB name"
589+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzCognitiveServiceName -variableName "Az Cognitive Service name"
590+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzSearchServiceName -variableName "Az Search Service name"
591+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzOpenAiServiceName -variableName "Az OpenAI Service name"
592+
593+
# Retrieve secrets
594+
$deploymentResult | Add-Member -NotePropertyName AzCosmosDBConnectionString -NotePropertyValue (az cosmosdb keys list --name $deploymentResult.AzCosmosDBName --resource-group $deploymentResult.ResourceGroupName --type connection-strings --query "connectionStrings[0].connectionString" -o tsv) -Force
595+
$deploymentResult | Add-Member -NotePropertyName AzCognitiveServiceKey -NotePropertyValue (az cognitiveservices account keys list --name $deploymentResult.AzCognitiveServiceName --resource-group $deploymentResult.ResourceGroupName --query "key1" -o tsv) -Force
596+
$deploymentResult | Add-Member -NotePropertyName AzSearchAdminKey -NotePropertyValue (az search admin-key show --service-name $deploymentResult.AzSearchServiceName --resource-group $deploymentResult.ResourceGroupName --query "primaryKey" -o tsv) -Force
597+
$deploymentResult | Add-Member -NotePropertyName AzOpenAiServiceKey -NotePropertyValue (az cognitiveservices account keys list --name $deploymentResult.AzOpenAiServiceName --resource-group $deploymentResult.ResourceGroupName --query "key1" -o tsv) -Force
570598

571599
Write-Host "Secrets have been retrieved successfully." -ForegroundColor Green
572600

0 commit comments

Comments
 (0)