Skip to content

Commit

Permalink
AzurePowerShellV2/3 can use Azure and AzureRM modules as zip (#14406)
Browse files Browse the repository at this point in the history
* AzurePowerShellV2/3 can use Azure and AzureRM modules as zip

* Added verbose logs

* Updated task version

* review comments
  • Loading branch information
amit-avit authored Mar 18, 2021
1 parent 3f51ef4 commit 29a436f
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 20 deletions.
6 changes: 2 additions & 4 deletions Tasks/AzurePowerShellV2/AzurePowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
}
71 changes: 71 additions & 0 deletions Tasks/AzurePowerShellV2/TryMakingModuleAvailable.ps1
Original file line number Diff line number Diff line change
@@ -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"
}
8 changes: 6 additions & 2 deletions Tasks/AzurePowerShellV2/Utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
6 changes: 6 additions & 0 deletions Tasks/AzurePowerShellV2/make.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
}
]
}
],
"archivePackages": [
{
"url": "https://vstsagenttools.blob.core.windows.net/tools/7zip/1/7zip.zip",
"dest": "./"
}
]
}
}
4 changes: 2 additions & 2 deletions Tasks/AzurePowerShellV2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 2,
"Minor": 179,
"Patch": 1
"Minor": 184,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzurePowerShellV2/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 2,
"Minor": 179,
"Patch": 1
"Minor": 184,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
6 changes: 2 additions & 4 deletions Tasks/AzurePowerShellV3/AzurePowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
71 changes: 71 additions & 0 deletions Tasks/AzurePowerShellV3/TryMakingModuleAvailable.ps1
Original file line number Diff line number Diff line change
@@ -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"
}
8 changes: 6 additions & 2 deletions Tasks/AzurePowerShellV3/Utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
6 changes: 6 additions & 0 deletions Tasks/AzurePowerShellV3/make.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
}
]
}
],
"archivePackages": [
{
"url": "https://vstsagenttools.blob.core.windows.net/tools/7zip/1/7zip.zip",
"dest": "./"
}
]
}
}
4 changes: 2 additions & 2 deletions Tasks/AzurePowerShellV3/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzurePowerShellV3/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 3,
"Minor": 179,
"Patch": 1
"Minor": 184,
"Patch": 0
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"demands": [
Expand Down

0 comments on commit 29a436f

Please sign in to comment.