Skip to content

Commit b33f743

Browse files
authored
Conditional linux resource deployment (#142)
* Check free space before upload to PIR & deploy the correct template * Conditional template deployment
1 parent 6379795 commit b33f743

3 files changed

Lines changed: 799 additions & 40 deletions

File tree

CanaryValidator/Canary.Tests.ps1

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -185,27 +185,30 @@ while ($runCount -le $NumberOfIterations)
185185
}
186186
}
187187

188-
Invoke-Usecase -Name 'UploadLinuxImageToPIR' -Description "Uploads Linux image to the PIR" -UsecaseBlock `
188+
if ((Get-Volume ((Get-Item -Path $ENV:TMP).PSDrive.Name)).SizeRemaining/1GB -gt 35)
189189
{
190-
try
190+
Invoke-Usecase -Name 'UploadLinuxImageToPIR' -Description "Uploads Linux image to the PIR" -UsecaseBlock `
191191
{
192-
if (-not (Get-AzureRmVMImage -Location $ResourceLocation -PublisherName $linuxImagePublisher -Offer $linuxImageOffer -Sku $LinuxOSSku -ErrorAction SilentlyContinue))
192+
try
193193
{
194-
$CanaryCustomImageFolder = Join-Path -Path $env:TMP -childPath "CanaryCustomImage$((Get-Date).Ticks)"
195-
if (Test-Path -Path $CanaryCustomImageFolder)
194+
if (-not (Get-AzureRmVMImage -Location $ResourceLocation -PublisherName $linuxImagePublisher -Offer $linuxImageOffer -Sku $LinuxOSSku -ErrorAction SilentlyContinue))
196195
{
197-
Remove-Item -Path $CanaryCustomImageFolder -Force -Recurse
198-
}
199-
New-Item -Path $CanaryCustomImageFolder -ItemType Directory
200-
$CustomVHDPath = CopyImage -ImagePath $LinuxImagePath -OutputFolder $CanaryCustomImageFolder
201-
Add-VMImage -publisher $linuxImagePublisher -offer $linuxImageOffer -sku $LinuxOSSku -version $linuxImageVersion -osDiskLocalPath $CustomVHDPath -osType Linux -tenantID $TenantID -azureStackCredentials $ServiceAdminCredentials -CreateGalleryItem $false -ArmEndpoint $AdminArmEndpoint
202-
Remove-Item $CanaryCustomImageFolder -Force -Recurse
203-
}
204-
}
205-
catch
206-
{
207-
Remove-Item -Path $CanaryCustomImageFolder -Force -Recurse
208-
throw [System.Exception]"Failed to upload the linux image to PIR. `n$($_.Exception.Message)"
196+
$CanaryCustomImageFolder = Join-Path -Path $env:TMP -childPath "CanaryCustomImage$((Get-Date).Ticks)"
197+
if (Test-Path -Path $CanaryCustomImageFolder)
198+
{
199+
Remove-Item -Path $CanaryCustomImageFolder -Force -Recurse
200+
}
201+
New-Item -Path $CanaryCustomImageFolder -ItemType Directory
202+
$CustomVHDPath = CopyImage -ImagePath $LinuxImagePath -OutputFolder $CanaryCustomImageFolder
203+
Add-VMImage -publisher $linuxImagePublisher -offer $linuxImageOffer -sku $LinuxOSSku -version $linuxImageVersion -osDiskLocalPath $CustomVHDPath -osType Linux -tenantID $TenantID -azureStackCredentials $ServiceAdminCredentials -CreateGalleryItem $false -ArmEndpoint $AdminArmEndpoint
204+
Remove-Item $CanaryCustomImageFolder -Force -Recurse
205+
}
206+
}
207+
catch
208+
{
209+
Remove-Item -Path $CanaryCustomImageFolder -Force -Recurse
210+
throw [System.Exception]"Failed to upload the linux image to PIR. `n$($_.Exception.Message)"
211+
}
209212
}
210213
}
211214

@@ -361,28 +364,38 @@ while ($runCount -le $NumberOfIterations)
361364
}
362365

363366
Invoke-Usecase -Name 'UploadUtilitiesToBlobStorage' -Description "Upload the canary utilities to the blob storage" -UsecaseBlock `
364-
{
365-
$asStorageAccountKey = Get-AzureRmStorageAccountKey -ResourceGroupName $CanaryUtilitiesRG -Name $storageAccName -ErrorAction Stop
366-
if ($asStorageAccountKey)
367-
{
368-
$storageAccountKey = $asStorageAccountKey.Key1
369-
}
370-
$asStorageContext = New-AzureStorageContext -StorageAccountName $storageAccName -StorageAccountKey $storageAccountKey -ErrorAction Stop
371-
if ($asStorageContext)
372-
{
373-
$files = Get-ChildItem -Path $canaryUtilPath -File
374-
foreach ($file in $files)
367+
{
368+
try
369+
{
370+
$asStorageAccountKey = Get-AzureRmStorageAccountKey -ResourceGroupName $CanaryUtilitiesRG -Name $storageAccName -ErrorAction Stop
371+
if ($asStorageAccountKey)
375372
{
376-
if ($file.Extension -match "VHD")
377-
{
378-
Set-AzureStorageBlobContent -Container $storageCtrName -File $file.FullName -BlobType Page -Context $asStorageContext -Force -ErrorAction Stop
379-
}
380-
else
373+
$storageAccountKey = $asStorageAccountKey.Key1
374+
}
375+
$asStorageContext = New-AzureStorageContext -StorageAccountName $storageAccName -StorageAccountKey $storageAccountKey -ErrorAction Stop
376+
if ($asStorageContext)
377+
{
378+
$files = Get-ChildItem -Path $canaryUtilPath -File
379+
foreach ($file in $files)
381380
{
382-
Set-AzureStorageBlobContent -Container $storageCtrName -File $file.FullName -Context $asStorageContext -Force -ErrorAction Stop
381+
if ($file.Extension -match "VHD")
382+
{
383+
Set-AzureStorageBlobContent -Container $storageCtrName -File $file.FullName -BlobType Page -Context $asStorageContext -Force -ErrorAction Stop
384+
}
385+
else
386+
{
387+
Set-AzureStorageBlobContent -Container $storageCtrName -File $file.FullName -Context $asStorageContext -Force -ErrorAction Stop
388+
}
383389
}
384390
}
385391
}
392+
finally
393+
{
394+
if (Test-Path -Path $canaryUtilPath)
395+
{
396+
Remove-Item -Path $canaryUtilPath -Recurse -Force
397+
}
398+
}
386399
}
387400

388401
Invoke-Usecase -Name 'CreateKeyVaultStoreForCertSecret' -Description "Create a key vault store to put the certificate secret" -UsecaseBlock `
@@ -442,8 +455,13 @@ while ($runCount -le $NumberOfIterations)
442455
elseif (Get-AzureRmVMImage -Location $ResourceLocation -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Sku "2012-R2-Datacenter" -ErrorAction SilentlyContinue)
443456
{
444457
$osVersion = "2012-R2-Datacenter"
445-
}
446-
458+
}
459+
$linuxImgExists = $false
460+
if (Get-AzureRmVMImage -Location $ResourceLocation -PublisherName "Canonical" -Offer "UbuntuServer" -Sku $LinuxOSSku -ErrorAction SilentlyContinue)
461+
{
462+
$linuxImgExists = $true
463+
}
464+
447465
$templateDeploymentName = "CanaryVMDeployment"
448466
$parameters = @{"VMAdminUserName" = $VMAdminUserName;
449467
"VMAdminUserPassword" = $VMAdminUserPass;
@@ -456,11 +474,22 @@ while ($runCount -le $NumberOfIterations)
456474
"LinuxImagePublisher" = $linuxImagePublisher;
457475
"LinuxImageOffer" = $linuxImageOffer;
458476
"LinuxImageSku" = $LinuxOSSku}
459-
$templateError = Test-AzureRmResourceGroupDeployment -ResourceGroupName $CanaryVMRG -TemplateFile $PSScriptRoot\azuredeploy.json -TemplateParameterObject $parameters
460-
if (-not $templateError)
477+
if (-not $linuxImgExists)
478+
{
479+
$templateError = Test-AzureRmResourceGroupDeployment -ResourceGroupName $CanaryVMRG -TemplateFile $PSScriptRoot\azuredeploy.json -TemplateParameterObject $parameters
480+
}
481+
elseif ($linuxImgExists)
482+
{
483+
$templateError = Test-AzureRmResourceGroupDeployment -ResourceGroupName $CanaryVMRG -TemplateFile $PSScriptRoot\azuredeploy.nolinux.json -TemplateParameterObject $parameters
484+
}
485+
486+
if ((-not $templateError) -and ($linuxImgExists))
461487
{
462488
New-AzureRmResourceGroupDeployment -Name $templateDeploymentName -ResourceGroupName $CanaryVMRG -TemplateFile $PSScriptRoot\azuredeploy.json -TemplateParameterObject $parameters -Verbose -ErrorAction Stop
463489
}
490+
elseif (-not $templateError) {
491+
New-AzureRmResourceGroupDeployment -Name $templateDeploymentName -ResourceGroupName $CanaryVMRG -TemplateFile $PSScriptRoot\azuredeploy.nolinux.json -TemplateParameterObject $parameters -Verbose -ErrorAction Stop
492+
}
464493
else
465494
{
466495
throw [System.Exception] "Template validation failed. `n$($templateError.Message)"
@@ -707,6 +736,11 @@ while ($runCount -le $NumberOfIterations)
707736
}
708737
}
709738

739+
Invoke-Usecase -Name 'EnumerateAllResources' -Description "List out all the resources that have been deployed" -UsecaseBlock `
740+
{
741+
Get-AzureRmResource
742+
}
743+
710744
if (-not $NoCleanup)
711745
{
712746
Invoke-Usecase -Name 'DeleteVMWithPrivateIP' -Description "Delete the VM with private IP" -UsecaseBlock `

CanaryValidator/Canary.Utilities.psm1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ function Invoke-Usecase
242242
catch [System.Exception]
243243
{
244244
Log-Exception ($_.Exception)
245+
Log-Info ("###### <FAULTING SCRIPTBLOCK> ######")
246+
Log-Info ("$UsecaseBlock")
247+
Log-Info ("###### </FAULTING SCRIPTBLOCK> ######")
245248
Log-Error ("###### [END] Usecase: $Name ###### [RESULT = FAIL] ######`n")
246249
if ($Global:wttLogFileName)
247250
{
@@ -501,8 +504,12 @@ function NewAzureStackToken
501504
)
502505

503506
$endpoints = GetAzureStackEndpoints -EnvironmentDomainFQDN $EnvironmentDomainFQDN -ArmEndPoint $ArmEndpoint
504-
$asToken = Get-AzureStackToken -Authority $endpoints.ActiveDirectoryEndpoint -Resource $endpoints.ActiveDirectoryServiceEndpointResourceId -AadTenantId $aadTenantId -Credential $Credentials -ErrorAction Stop
505-
return $asToken
507+
$clientId = "1950a258-227b-4e31-a9cf-717495945fc2"
508+
509+
$contextAuthorityEndpoint = ([System.IO.Path]::Combine($endpoints.ActiveDirectoryEndpoint, $AADTenantID)).Replace('\','/')
510+
$authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext($contextAuthorityEndpoint, $false)
511+
$userCredential = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential($Credentials.UserName, $Credentials.Password)
512+
return ($authContext.AcquireToken($endpoints.ActiveDirectoryServiceEndpointResourceId, $clientId, $userCredential)).AccessToken
506513
}
507514

508515
function NewAzureStackDefaultQuotas

0 commit comments

Comments
 (0)