From 29a436fb635c5051fca46f4dd72dadc2162cba52 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Thu, 18 Mar 2021 11:35:09 +0530 Subject: [PATCH] AzurePowerShellV2/3 can use Azure and AzureRM modules as zip (#14406) * AzurePowerShellV2/3 can use Azure and AzureRM modules as zip * Added verbose logs * Updated task version * review comments --- Tasks/AzurePowerShellV2/AzurePowerShell.ps1 | 6 +- .../TryMakingModuleAvailable.ps1 | 71 +++++++++++++++++++ Tasks/AzurePowerShellV2/Utility.ps1 | 8 ++- Tasks/AzurePowerShellV2/make.json | 6 ++ Tasks/AzurePowerShellV2/task.json | 4 +- Tasks/AzurePowerShellV2/task.loc.json | 4 +- Tasks/AzurePowerShellV3/AzurePowerShell.ps1 | 6 +- .../TryMakingModuleAvailable.ps1 | 71 +++++++++++++++++++ Tasks/AzurePowerShellV3/Utility.ps1 | 8 ++- Tasks/AzurePowerShellV3/make.json | 6 ++ Tasks/AzurePowerShellV3/task.json | 4 +- Tasks/AzurePowerShellV3/task.loc.json | 4 +- 12 files changed, 178 insertions(+), 20 deletions(-) create mode 100644 Tasks/AzurePowerShellV2/TryMakingModuleAvailable.ps1 create mode 100644 Tasks/AzurePowerShellV3/TryMakingModuleAvailable.ps1 diff --git a/Tasks/AzurePowerShellV2/AzurePowerShell.ps1 b/Tasks/AzurePowerShellV2/AzurePowerShell.ps1 index 10c7a3ef587a..0381260cc131 100644 --- a/Tasks/AzurePowerShellV2/AzurePowerShell.ps1 +++ b/Tasks/AzurePowerShellV2/AzurePowerShell.ps1 @@ -70,6 +70,8 @@ catch Write-Verbose "Unable to get the authScheme $error" } +. $PSScriptRoot\TryMakingModuleAvailable.ps1 -targetVersion $targetAzurePs + Update-PSModulePathForHostedAgent -targetAzurePs $targetAzurePs -authScheme $authScheme try { @@ -142,8 +144,4 @@ finally { Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_ Disconnect-AzureAndClearContext -authScheme $authScheme -ErrorAction SilentlyContinue - - # Telemetry - $telemetryJsonContent = @{ targetAzurePs = $targetAzurePs } | ConvertTo-Json -Compress - Write-Host "##vso[telemetry.publish area=TaskHub;feature=AzurePowerShellV2]$telemetryJsonContent" } diff --git a/Tasks/AzurePowerShellV2/TryMakingModuleAvailable.ps1 b/Tasks/AzurePowerShellV2/TryMakingModuleAvailable.ps1 new file mode 100644 index 000000000000..b0b4662bf0f9 --- /dev/null +++ b/Tasks/AzurePowerShellV2/TryMakingModuleAvailable.ps1 @@ -0,0 +1,71 @@ +[CmdletBinding()] +param ( + [string] + $targetVersion +) + +try { + . "$PSScriptRoot\Utility.ps1" + $moduleContainerPath = Get-SavedModuleContainerPath + + if (-not(Test-Path -Path $moduleContainerPath)) { + $classicModuleSource = "privateAgent" + $nonClassicModuleSource = "privateAgent" + Write-Verbose "Folder layout not as per hosted agent, considering self hosted agent skipping module unzip logic." + return; + } + + if (!$targetVersion) { + $classicModuleSource = "hostedAgentFolder" + $nonClassicModuleSource = "hostedAgentFolder" + Write-Verbose "Latest module selected which will be available as folder." + return; + } + + # value for classic + @($false, $true).ForEach({ + $modulePath = Get-SavedModulePath -azurePowerShellVersion $targetVersion -Classic:$_; + if (Test-Path -Path $modulePath) { + if ($_) { + $classicModuleSource = "hostedAgentFolder" + } else { + $nonClassicModuleSource = "hostedAgentFolder" + } + + Write-Verbose "Module available as folder at $modulePath" + return; + } + + $moduleZipPath = $modulePath + ".zip"; + if (-not(Test-Path -Path $moduleZipPath)) { + if ($_) { + $classicModuleSource = "hostedAgentOthers" + } else { + $nonClassicModuleSource = "hostedAgentOthers" + } + + Write-Verbose "Module zip not available to unzip at $moduleZipPath" + return; + } + + if ($_) { + $classicModuleSource = "hostedAgentZip" + } else { + $nonClassicModuleSource = "hostedAgentZip" + } + + Write-Verbose "Extracting zip $moduleZipPath" + $parameter = @("x", "-o$moduleContainerPath", "$moduleZipPath") + $command = "$PSScriptRoot\7zip\7z.exe" + &$command @parameter + Write-Verbose "Extraction complete" + }) +} finally { + # Telemetry + $telemetryJsonContent = @{ + targetAzurePs = $targetVersion; + classicModuleSource = $classicModuleSource; + nonClassicModuleSource = $nonClassicModuleSource + } | ConvertTo-Json -Compress + Write-Host "##vso[telemetry.publish area=TaskHub;feature=AzurePowerShellV2]$telemetryJsonContent" +} diff --git a/Tasks/AzurePowerShellV2/Utility.ps1 b/Tasks/AzurePowerShellV2/Utility.ps1 index a7e67876b6fd..6e06f57f6fdc 100644 --- a/Tasks/AzurePowerShellV2/Utility.ps1 +++ b/Tasks/AzurePowerShellV2/Utility.ps1 @@ -2,16 +2,20 @@ $rollForwardTable = @{ "5.0.0" = "5.1.1"; }; +function Get-SavedModuleContainerPath { + return $env:SystemDrive + "\Modules"; +} + function Get-SavedModulePath { [CmdletBinding()] param([string] $azurePowerShellVersion, [switch] $Classic) if($Classic -eq $true) { - return $($env:SystemDrive + "\Modules\Azure_" + $azurePowerShellVersion) + return (Get-SavedModuleContainerPath) + "\Azure_" + $azurePowerShellVersion; } else { - return $($env:SystemDrive + "\Modules\AzureRm_" + $azurePowerShellVersion) + return (Get-SavedModuleContainerPath) + "\AzureRm_" + $azurePowerShellVersion; } } diff --git a/Tasks/AzurePowerShellV2/make.json b/Tasks/AzurePowerShellV2/make.json index a0b993ac8696..864745b39daf 100644 --- a/Tasks/AzurePowerShellV2/make.json +++ b/Tasks/AzurePowerShellV2/make.json @@ -30,6 +30,12 @@ } ] } + ], + "archivePackages": [ + { + "url": "https://vstsagenttools.blob.core.windows.net/tools/7zip/1/7zip.zip", + "dest": "./" + } ] } } diff --git a/Tasks/AzurePowerShellV2/task.json b/Tasks/AzurePowerShellV2/task.json index e077dc9d356b..3938678367f4 100644 --- a/Tasks/AzurePowerShellV2/task.json +++ b/Tasks/AzurePowerShellV2/task.json @@ -17,8 +17,8 @@ "author": "Microsoft Corporation", "version": { "Major": 2, - "Minor": 179, - "Patch": 1 + "Minor": 184, + "Patch": 0 }, "demands": [ "azureps" diff --git a/Tasks/AzurePowerShellV2/task.loc.json b/Tasks/AzurePowerShellV2/task.loc.json index d6ef325572c7..3c97a97bbe77 100644 --- a/Tasks/AzurePowerShellV2/task.loc.json +++ b/Tasks/AzurePowerShellV2/task.loc.json @@ -17,8 +17,8 @@ "author": "Microsoft Corporation", "version": { "Major": 2, - "Minor": 179, - "Patch": 1 + "Minor": 184, + "Patch": 0 }, "demands": [ "azureps" diff --git a/Tasks/AzurePowerShellV3/AzurePowerShell.ps1 b/Tasks/AzurePowerShellV3/AzurePowerShell.ps1 index 7f1b280357df..16fc2f59fccc 100644 --- a/Tasks/AzurePowerShellV3/AzurePowerShell.ps1 +++ b/Tasks/AzurePowerShellV3/AzurePowerShell.ps1 @@ -76,6 +76,8 @@ catch Write-Verbose "Unable to get the authScheme $error" } +. $PSScriptRoot\TryMakingModuleAvailable.ps1 -targetVersion $targetAzurePs + Update-PSModulePathForHostedAgent -targetAzurePs $targetAzurePs -authScheme $authScheme # troubleshoot link @@ -179,7 +181,3 @@ finally { } Write-Host "## Script Execution Complete" Disconnect-AzureAndClearContext -authScheme $authScheme -ErrorAction SilentlyContinue - -# Telemetry -$telemetryJsonContent = @{ targetAzurePs = $targetAzurePs } | ConvertTo-Json -Compress -Write-Host "##vso[telemetry.publish area=TaskHub;feature=AzurePowerShellV3]$telemetryJsonContent" \ No newline at end of file diff --git a/Tasks/AzurePowerShellV3/TryMakingModuleAvailable.ps1 b/Tasks/AzurePowerShellV3/TryMakingModuleAvailable.ps1 new file mode 100644 index 000000000000..c834fdffde67 --- /dev/null +++ b/Tasks/AzurePowerShellV3/TryMakingModuleAvailable.ps1 @@ -0,0 +1,71 @@ +[CmdletBinding()] +param ( + [string] + $targetVersion +) + +try { + . "$PSScriptRoot\Utility.ps1" + $moduleContainerPath = Get-SavedModuleContainerPath + + if (-not(Test-Path -Path $moduleContainerPath)) { + $classicModuleSource = "privateAgent" + $nonClassicModuleSource = "privateAgent" + Write-Verbose "Folder layout not as per hosted agent, considering self hosted agent skipping module unzip logic." + return; + } + + if (!$targetVersion) { + $classicModuleSource = "hostedAgentFolder" + $nonClassicModuleSource = "hostedAgentFolder" + Write-Verbose "Latest module selected which will be available as folder." + return; + } + + # value for classic + @($false, $true).ForEach({ + $modulePath = Get-SavedModulePath -azurePowerShellVersion $targetVersion -Classic:$_; + if (Test-Path -Path $modulePath) { + if ($_) { + $classicModuleSource = "hostedAgentFolder" + } else { + $nonClassicModuleSource = "hostedAgentFolder" + } + + Write-Verbose "Module available as folder at $modulePath" + return; + } + + $moduleZipPath = $modulePath + ".zip"; + if (-not(Test-Path -Path $moduleZipPath)) { + if ($_) { + $classicModuleSource = "hostedAgentOthers" + } else { + $nonClassicModuleSource = "hostedAgentOthers" + } + + Write-Verbose "Module zip not available to unzip at $moduleZipPath" + return; + } + + if ($_) { + $classicModuleSource = "hostedAgentZip" + } else { + $nonClassicModuleSource = "hostedAgentZip" + } + + Write-Verbose "Extracting zip $moduleZipPath" + $parameter = @("x", "-o$moduleContainerPath", "$moduleZipPath") + $command = "$PSScriptRoot\7zip\7z.exe" + &$command @parameter + Write-Verbose "Extraction complete" + }) +} finally { + # Telemetry + $telemetryJsonContent = @{ + targetAzurePs = $targetVersion; + classicModuleSource = $classicModuleSource; + nonClassicModuleSource = $nonClassicModuleSource + } | ConvertTo-Json -Compress + Write-Host "##vso[telemetry.publish area=TaskHub;feature=AzurePowerShellV3]$telemetryJsonContent" +} diff --git a/Tasks/AzurePowerShellV3/Utility.ps1 b/Tasks/AzurePowerShellV3/Utility.ps1 index b385ac9ba042..2fbe61c21710 100644 --- a/Tasks/AzurePowerShellV3/Utility.ps1 +++ b/Tasks/AzurePowerShellV3/Utility.ps1 @@ -2,16 +2,20 @@ $rollForwardTable = @{ "5.0.0" = "5.1.1"; }; +function Get-SavedModuleContainerPath { + return $env:SystemDrive + "\Modules"; +} + function Get-SavedModulePath { [CmdletBinding()] param([string] $azurePowerShellVersion, [switch] $Classic) if($Classic -eq $true) { - return $($env:SystemDrive + "\Modules\Azure_" + $azurePowerShellVersion) + return (Get-SavedModuleContainerPath) + "\Azure_" + $azurePowerShellVersion; } else { - return $($env:SystemDrive + "\Modules\AzureRm_" + $azurePowerShellVersion) + return (Get-SavedModuleContainerPath) + "\AzureRm_" + $azurePowerShellVersion; } } diff --git a/Tasks/AzurePowerShellV3/make.json b/Tasks/AzurePowerShellV3/make.json index a0b993ac8696..864745b39daf 100644 --- a/Tasks/AzurePowerShellV3/make.json +++ b/Tasks/AzurePowerShellV3/make.json @@ -30,6 +30,12 @@ } ] } + ], + "archivePackages": [ + { + "url": "https://vstsagenttools.blob.core.windows.net/tools/7zip/1/7zip.zip", + "dest": "./" + } ] } } diff --git a/Tasks/AzurePowerShellV3/task.json b/Tasks/AzurePowerShellV3/task.json index aa65d02614bc..656c181784c5 100644 --- a/Tasks/AzurePowerShellV3/task.json +++ b/Tasks/AzurePowerShellV3/task.json @@ -17,8 +17,8 @@ "author": "Microsoft Corporation", "version": { "Major": 3, - "Minor": 179, - "Patch": 1 + "Minor": 184, + "Patch": 0 }, "releaseNotes": "Added support for Fail on standard error and ErrorActionPreference", "demands": [ diff --git a/Tasks/AzurePowerShellV3/task.loc.json b/Tasks/AzurePowerShellV3/task.loc.json index 7f309cc8d51e..850a29144fe1 100644 --- a/Tasks/AzurePowerShellV3/task.loc.json +++ b/Tasks/AzurePowerShellV3/task.loc.json @@ -17,8 +17,8 @@ "author": "Microsoft Corporation", "version": { "Major": 3, - "Minor": 179, - "Patch": 1 + "Minor": 184, + "Patch": 0 }, "releaseNotes": "ms-resource:loc.releaseNotes", "demands": [