Skip to content

Commit

Permalink
[AzureFileCopy] Pick up latest Azure PowerShell module on Hosted agent (
Browse files Browse the repository at this point in the history
#13067)

* update azure file copy to use latest available azure rm module on hosted agent environment

* update task version

* check for null for latestazurermmodulepath

* move psmoduleutility to common. afcv2 and afcv3

* refactor out get-endpointauthenticationscheme function

* use endpoint.type instead of authentication scheme to check whether or not to update env:psmodulepath

* update task version of tasks using common modules dependency

* remove endpoint utility as it is not needed

* load utility.ps1 before calling get-endpoint

* update minor version

Co-authored-by: Ashish Ranjan 💩 <asranja@microsoft.com>
  • Loading branch information
arjgupta and asranja committed Jun 8, 2020
1 parent 78dec87 commit a09a1ba
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Tasks/AzureCloudPowerShellDeploymentV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 167,
"Patch": 3
"Minor": 171,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureCloudPowerShellDeploymentV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 167,
"Patch": 3
"Minor": 171,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
4 changes: 3 additions & 1 deletion Tasks/AzureFileCopyV2/AzureFileCopy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ Import-Module $PSScriptRoot\ps_modules\RemoteDeployer

# Initialize Azure.
Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
. "$PSScriptRoot\Utility.ps1"
$endpoint = Get-Endpoint -connectedServiceName $connectedServiceName
Update-PSModulePathForHostedAgentWithLatestModule -Endpoint $endpoint
Initialize-Azure

# Import the loc strings.
Import-VstsLocStrings -LiteralPath $PSScriptRoot/Task.json

# Load all dependent files for execution
. "$PSScriptRoot\AzureFileCopyRemoteJob.ps1"
. "$PSScriptRoot\Utility.ps1"

# Enabling detailed logging only when system.debug is true
$enableDetailedLogging = ($env:system_debug -eq "true")
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureFileCopyV2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 2,
"Minor": 167,
"Patch": 4
"Minor": 171,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureFileCopyV2/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 2,
"Minor": 167,
"Patch": 4
"Minor": 171,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
1 change: 1 addition & 0 deletions Tasks/AzureFileCopyV3/AzureFileCopy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if (Get-Module Az.Accounts -ListAvailable){
Initialize-AzModule -Endpoint $endpoint
}
else{
Update-PSModulePathForHostedAgentWithLatestModule -Endpoint $endpoint
Initialize-AzureRMModule -Endpoint $endpoint
}

Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureFileCopyV3/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 3,
"Minor": 167,
"Patch": 4
"Minor": 171,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureFileCopyV3/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 3,
"Minor": 167,
"Patch": 4
"Minor": 171,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureFileCopyV4/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 4,
"Minor": 170,
"Patch": 2
"Minor": 171,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzureFileCopyV4/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 4,
"Minor": 170,
"Patch": 2
"Minor": 171,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"loc.input.label.ScriptArguments": "Script Arguments",
"loc.input.help.ScriptArguments": "Additional parameters to pass to PowerShell. Can be either ordinal or named parameters.",
"loc.input.label.TargetAzurePs": "Azure PowerShell Version",
"loc.input.help.TargetAzurePs": "In case of hosted agents, the supported Azure PowerShell Versions are: 2.1.0, 3.8.0, 4.2.1 and 5.1.1(Hosted VS2017 Queue), 3.6.0(Hosted Queue).\nTo pick the latest version available on the agent, select \"Latest installed version\".\n\nFor private agents you can specify preferred version of Azure PowerShell using \"Specify version\"",
"loc.input.help.TargetAzurePs": "In case of hosted agents, the supported Azure PowerShell Versions are: 2.1.0, 3.8.0, 4.2.1, 5.1.1 and 6.7.0(Hosted VS2017 Queue).\nTo pick the latest version available on the agent, select \"Latest installed version\".\n\nFor private agents you can specify preferred version of Azure PowerShell using \"Specify version\"",
"loc.input.label.CustomTargetAzurePs": "Preferred Azure PowerShell Version",
"loc.input.help.CustomTargetAzurePs": "Preferred Azure PowerShell Version needs to be a proper semantic version eg. 1.2.3. Regex like 2.\\*,2.3.\\* is not supported. The Hosted VS2017 Pool currently supports versions: 2.1.0, 3.8.0, 4.2.1, 5.1.1",
"loc.input.help.CustomTargetAzurePs": "Preferred Azure PowerShell Version needs to be a proper semantic version eg. 1.2.3. Regex like 2.\\*,2.3.\\* is not supported. The Hosted VS2017 Pool currently supports Azure module versions: 2.1.0, 3.8.0, 4.2.1, 5.1.1 and AzureRM module versions: 2.1.0, 3.8.0, 4.2.1, 5.1.1, 6.7.0",
"loc.messages.InvalidScriptArguments0": "Invalid script arguments '{0}'. Line breaks are not allowed.",
"loc.messages.InvalidScriptPath0": "Invalid script path '{0}'. Invalid path characters specified.",
"loc.messages.InvalidAzurePsVersion": "The Azure PowerShell version '{0}' specified is not in the correct format. Please check the format. An example of correct format is 1.0.1",
Expand Down
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": 167,
"Patch": 3
"Minor": 171,
"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": 167,
"Patch": 3
"Minor": 171,
"Patch": 0
},
"demands": [
"azureps"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"loc.input.label.FailOnStandardError": "Fail on Standard Error",
"loc.input.help.FailOnStandardError": "If this is true, this task will fail if any errors are written to the error pipeline, or if any data is written to the Standard Error stream.",
"loc.input.label.TargetAzurePs": "Azure PowerShell Version",
"loc.input.help.TargetAzurePs": "In case of hosted agents, the supported Azure PowerShell Versions are: 2.1.0, 3.8.0, 4.2.1, 5.1.1 and 6.7.0(Hosted VS2017 Queue), 3.6.0(Hosted Queue).\nTo pick the latest version available on the agent, select \"Latest installed version\".\n\nFor private agents you can specify preferred version of Azure PowerShell using \"Specify version\"",
"loc.input.help.TargetAzurePs": "In case of hosted agents, the supported Azure PowerShell Versions are: 2.1.0, 3.8.0, 4.2.1, 5.1.1 and 6.7.0(Hosted VS2017 Queue).\nTo pick the latest version available on the agent, select \"Latest installed version\".\n\nFor private agents you can specify preferred version of Azure PowerShell using \"Specify version\"",
"loc.input.label.CustomTargetAzurePs": "Preferred Azure PowerShell Version",
"loc.input.help.CustomTargetAzurePs": "Preferred Azure PowerShell Version needs to be a proper semantic version eg. 1.2.3. Regex like 2.\\*,2.3.\\* is not supported. The Hosted VS2017 Pool currently supports Azure module versions: 2.1.0, 3.8.0, 4.2.1, 5.1.1 and AzureRM module versions: 2.1.0, 3.8.0, 4.2.1, 5.1.1, 6.7.0",
"loc.messages.InvalidScriptArguments0": "Invalid script arguments '{0}'. Line breaks are not allowed.",
Expand Down
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": 167,
"Patch": 3
"Minor": 171,
"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": 167,
"Patch": 3
"Minor": 171,
"Patch": 0
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"demands": [
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzurePowerShellV4/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 4,
"Minor": 168,
"Patch": 2
"Minor": 171,
"Patch": 0
},
"releaseNotes": "Added support for Az Module and cross platform agents.",
"groups": [
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzurePowerShellV4/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 4,
"Minor": 168,
"Patch": 2
"Minor": 171,
"Patch": 0
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"groups": [
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzurePowerShellV5/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 5,
"Minor": 170,
"Patch": 1
"Minor": 171,
"Patch": 0
},
"releaseNotes": "Added support for Az Module and cross platform agents.",
"groups": [
Expand Down
4 changes: 2 additions & 2 deletions Tasks/AzurePowerShellV5/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 5,
"Minor": 170,
"Patch": 1
"Minor": 171,
"Patch": 0
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"groups": [
Expand Down
91 changes: 91 additions & 0 deletions Tasks/Common/VstsAzureHelpers_/PsModuleUtility.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
function Update-PSModulePathForHostedAgentWithLatestModule
{
[CmdletBinding()]
param(
[object] $Endpoint
)

Trace-VstsEnteringInvocation $MyInvocation
try
{
$authenticationScheme = $Endpoint.Auth.Scheme

if ($authenticationScheme -eq "ServicePrincipal" -or
$authenticationScheme -eq "ManagedServiceIdentity")
{
Write-Verbose "Updating PSModulePath with latest AzureRM module."
$latestAzureRmModulePath = Get-LatestAzureRmModulePath

if (![string]::IsNullOrEmpty($latestAzureRmModulePath))
{
$env:PSModulePath = "$latestAzureRmModulePath;$env:PSModulePath"
}
else
{
Write-Verbose "Latest AzureRM module path is null or empty."
}
}

}
finally
{
Write-Verbose "The updated value of the PSModulePath is: $($env:PSModulePath)"
Trace-VstsLeavingInvocation $MyInvocation
}
}

function Get-LatestAzureRmModulePath
{
[CmdletBinding()]
param()

Trace-VstsEnteringInvocation $MyInvocation
$hostedAgentAzureModulesPath = Join-Path -Path $env:SystemDrive -ChildPath "modules"
$latestAzureRmModulePath = ""
try
{
if ($(Test-Path $hostedAgentAzureModulesPath))
{
$regexToMatch = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList "^azurerm_[0-9]+\.[0-9]+\.[0-9]+$"
$regexToExtract = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList "[0-9]+\.[0-9]+\.[0-9]+$"
$maxVersion = [version] "0.0.0"

$moduleFolders = Get-ChildItem -Directory -Path $hostedAgentAzureModulesPath | Where-Object { $regexToMatch.IsMatch($_.Name) }

foreach ($moduleFolder in $moduleFolders)
{
$moduleVersion = [version] $($regexToExtract.Match($moduleFolder.Name).Groups[0].Value)

if ($moduleVersion -gt $maxVersion)
{
$modulePath = [System.IO.Path]::Combine($moduleFolder.FullName,"AzureRM\$moduleVersion\AzureRM.psm1")

if (Test-Path -LiteralPath $modulePath -PathType Leaf)
{
$maxVersion = $moduleVersion
$latestAzureRmModulePath = $moduleFolder.FullName
}
else
{
Write-Verbose "A folder matching the module folder pattern was found at $($moduleFolder.FullName) but didn't contain a valid module file"
}
}
}
}
else
{
Write-Verbose "Hosted Agent Azure modules path '$hostedAgentAzureModulesPath' does not exist."
}

return $latestAzureRmModulePath
}
catch
{
Write-Verbose "Get-LatestAzureRmModulePath: Exception: $($_.Exception.Message)"
$latestAzureRmModulePath = ""
}
finally
{
Trace-VstsLeavingInvocation $MyInvocation
}
}
3 changes: 3 additions & 0 deletions Tasks/Common/VstsAzureHelpers_/VstsAzureHelpers_.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Add-Tls12InSession
. $PSScriptRoot/ImportFunctions.ps1
. $PSScriptRoot/InitializeAzureRMFunctions.ps1
. $PSScriptRoot/InitializeAzModuleFunctions.ps1
. $PSScriptRoot/PsModuleUtility.ps1

# This is the only public function.
function Initialize-Azure {
Expand Down Expand Up @@ -73,3 +74,5 @@ Export-ModuleMember -Function Remove-EndpointSecrets
Export-ModuleMember -Function Initialize-AzureRMModule
Export-ModuleMember -Function Initialize-AzModule
Export-ModuleMember -Function Disconnect-AzureAndClearContext
Export-ModuleMember -Function Update-PSModulePathForHostedAgentWithLatestModule

4 changes: 2 additions & 2 deletions Tasks/SqlAzureDacpacDeploymentV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 167,
"Patch": 4
"Minor": 171,
"Patch": 0
},
"demands": [
"sqlpackage"
Expand Down
4 changes: 2 additions & 2 deletions Tasks/SqlAzureDacpacDeploymentV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 1,
"Minor": 167,
"Patch": 4
"Minor": 171,
"Patch": 0
},
"demands": [
"sqlpackage"
Expand Down

0 comments on commit a09a1ba

Please sign in to comment.