Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AzureFileCopy] Pick up latest Azure PowerShell module on Hosted agent #13067

Merged
merged 12 commits into from
Jun 8, 2020
Merged
2 changes: 1 addition & 1 deletion Tasks/AzureCloudPowerShellDeploymentV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we currently fixing this for AzureCloudPowerShellDeployment task as well ?

"Major": 1,
"Minor": 167,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the minor version to the current sprint number.

"Patch": 3
"Patch": 4
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureCloudPowerShellDeploymentV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 167,
"Patch": 3
"Patch": 4
},
"demands": [
"azureps"
Expand Down
3 changes: 3 additions & 0 deletions Tasks/AzureFileCopyV2/AzureFileCopy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ $azCopyLocation = [System.IO.Path]::GetDirectoryName($azCopyExeLocation)

# Import RemoteDeployer
Import-Module $PSScriptRoot\ps_modules\RemoteDeployer
. "$PSScriptRoot\EndpointUtility.ps1"

# Initialize Azure.
Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
$endpoint = Get-Endpoint -connectedServiceName $connectedServiceName
Update-PSModulePathForHostedAgentWithLatestModule -Endpoint $endpoint
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the line . "$PSScriptRoot\Utility.ps1" before the Get-Endpoint call

Initialize-Azure

# Import the loc strings.
Expand Down
27 changes: 27 additions & 0 deletions Tasks/AzureFileCopyV2/EndpointUtility.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Get-EndpointAuthenticationScheme
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this utility ?

{
[CmdletBinding()]
param()

Trace-VstsEnteringInvocation $MyInvocation
$authenticationScheme = ""

try
{
$serviceNameInput = Get-VstsInput -Name "ConnectedServiceNameSelector" -Default "ConnectedServiceNameARM"
$serviceName = Get-VstsInput -Name $serviceNameInput

$endpoint = Get-VstsEndpoint -Name $serviceName -Require
$authenticationScheme = $endpoint.Auth.Scheme
}
catch
{
Write-Verbose "Get-EndpointAuthenticationScheme. Exception $($_.Exception.Message)"
}
finally
{
Trace-VstsLeavingInvocation $MyInvocation
}

return $authenticationScheme
}
2 changes: 1 addition & 1 deletion Tasks/AzureFileCopyV2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 2,
"Minor": 167,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the minor version to the current sprint number.

"Patch": 4
"Patch": 5
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureFileCopyV2/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 2,
"Minor": 167,
"Patch": 4
"Patch": 5
},
"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
2 changes: 1 addition & 1 deletion Tasks/AzureFileCopyV3/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 3,
"Minor": 167,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the minor version to the current sprint number.

"Patch": 4
"Patch": 5
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureFileCopyV3/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 3,
"Minor": 167,
"Patch": 4
"Patch": 5
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureFileCopyV4/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 4,
"Minor": 170,
"Patch": 2
"Patch": 3
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzureFileCopyV4/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"version": {
"Major": 4,
"Minor": 170,
"Patch": 2
"Patch": 3
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV2/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 2,
"Minor": 167,
"Patch": 3
"Patch": 4
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV2/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 2,
"Minor": 167,
"Patch": 3
"Patch": 4
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV3/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 3,
"Minor": 167,
"Patch": 3
"Patch": 4
},
"releaseNotes": "Added support for Fail on standard error and ErrorActionPreference",
"demands": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV3/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 3,
"Minor": 167,
"Patch": 3
"Patch": 4
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"demands": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV4/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 4,
"Minor": 168,
"Patch": 2
"Patch": 3
},
"releaseNotes": "Added support for Az Module and cross platform agents.",
"groups": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV4/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 4,
"Minor": 168,
"Patch": 2
"Patch": 3
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"groups": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV5/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 5,
"Minor": 170,
"Patch": 1
"Patch": 2
},
"releaseNotes": "Added support for Az Module and cross platform agents.",
"groups": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV5/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"version": {
"Major": 5,
"Minor": 170,
"Patch": 1
"Patch": 2
},
"releaseNotes": "ms-resource:loc.releaseNotes",
"groups": [
Expand Down
88 changes: 88 additions & 0 deletions Tasks/Common/VstsAzureHelpers_/PsModuleUtility.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
function Update-PSModulePathForHostedAgentWithLatestModule
{
[CmdletBinding()]
param(
[object] $Endpoint
)

Trace-VstsEnteringInvocation $MyInvocation
try
{
if ($Endpoint.Type -eq "AzureRM")
{
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."
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line

}
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

2 changes: 1 addition & 1 deletion Tasks/SqlAzureDacpacDeploymentV1/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 167,
"Patch": 4
"Patch": 5
},
"demands": [
"sqlpackage"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/SqlAzureDacpacDeploymentV1/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 167,
"Patch": 4
"Patch": 5
},
"demands": [
"sqlpackage"
Expand Down