diff --git a/Tasks/Common/PowershellHelpers/Invoke-ActionWithRetries.ps1 b/Tasks/Common/PowershellHelpers/Helpers.ps1 similarity index 83% rename from Tasks/Common/PowershellHelpers/Invoke-ActionWithRetries.ps1 rename to Tasks/Common/PowershellHelpers/Helpers.ps1 index 99f9b7edfd2c..9fb28f639e0a 100644 --- a/Tasks/Common/PowershellHelpers/Invoke-ActionWithRetries.ps1 +++ b/Tasks/Common/PowershellHelpers/Helpers.ps1 @@ -75,6 +75,25 @@ function Invoke-ActionWithRetries { Trace-VstsLeavingInvocation $MyInvocation } +function Get-TempDirectoryPath +{ + <# + .SYNOPSIS + Returns a temp directory path. Uses Agent.TempDirectory if available and shorter than env temp + #> + + Param () + + $agentTemp = Get-VstsTaskVariable -Name 'agent.tempDirectory' + $envTemp = $env:Temp + if ($agentTemp -and ($agentTemp.Length -le $envTemp.Length)) + { + return $agentTemp + } + + return $envTemp +} + function Test-RetryableException { [CmdletBinding()] param( diff --git a/Tasks/Common/PowershellHelpers/PowershellHelpers.psm1 b/Tasks/Common/PowershellHelpers/PowershellHelpers.psm1 index bf4aaf50da7c..c894281f46c6 100644 --- a/Tasks/Common/PowershellHelpers/PowershellHelpers.psm1 +++ b/Tasks/Common/PowershellHelpers/PowershellHelpers.psm1 @@ -3,6 +3,6 @@ param() Import-VstsLocStrings "$PSScriptRoot\module.json" # Dot-source all script files -. $PSScriptRoot\Invoke-ActionWithRetries.ps1 +. $PSScriptRoot\Helpers.ps1 -Export-ModuleMember -Function Invoke-ActionWithRetries \ No newline at end of file +Export-ModuleMember -Function @("Invoke-ActionWithRetries", "Get-TempDirectoryPath") \ No newline at end of file diff --git a/Tasks/Common/PowershellHelpers/Tests/Get-TempDirectoryPath.ps1 b/Tasks/Common/PowershellHelpers/Tests/Get-TempDirectoryPath.ps1 new file mode 100644 index 000000000000..8a16e383ce92 --- /dev/null +++ b/Tasks/Common/PowershellHelpers/Tests/Get-TempDirectoryPath.ps1 @@ -0,0 +1,46 @@ +[CmdletBinding()] +param() + +. $PSScriptRoot\..\..\..\..\Tests\lib\Initialize-Test.ps1 +$module = Microsoft.PowerShell.Core\Import-Module $PSScriptRoot\.. -PassThru + +## test 1 +Register-Mock Get-VstsTaskVariable { "C:\agent\temp" } -- -Name "agent.tempDirectory" +$env:Temp = "C:\env\temp\longPath" + +# Act +$tempDir = & $module Get-TempDirectoryPath + +# Assert +Assert-AreEqual -Expected "C:\agent\temp" -Actual $tempDir -Message "Agent temp dir should be used if shorter than env temp" + +# Cleanup +Unregister-Mock Get-VstsTaskVariable + + +## test 2 +Register-Mock Get-VstsTaskVariable { "C:\agent\temp\longPath" } -- -Name "agent.tempDirectory" +$env:Temp = "C:\env\temp" + +# Act +$tempDir = & $module Get-TempDirectoryPath + +# Assert +Assert-AreEqual -Expected "C:\env\temp" -Actual $tempDir -Message "Env temp dir should be used if shorter than agent temp" + +# Cleanup +Unregister-Mock Get-VstsTaskVariable + + +## test 3 +Register-Mock Get-VstsTaskVariable { $null } -- -Name "agent.tempDirectory" +$env:Temp = "C:\env\temp\longPath" + +# Act +$tempDir = & $module Get-TempDirectoryPath + +# Assert +Assert-AreEqual -Expected "C:\env\temp\longPath" -Actual $tempDir -Message "Env temp dir should be used if agent version is less than 2.115.0" + +# Cleanup +Unregister-Mock Get-VstsTaskVariable \ No newline at end of file diff --git a/Tasks/Common/PowershellHelpers/Tests/Invoke-ActionWithRetries.ShouldHandleMultipleRetryableExceptions.ps1 b/Tasks/Common/PowershellHelpers/Tests/Invoke-ActionWithRetries.ShouldHandleMultipleRetryableExceptions.ps1 index dda57ca6c76f..9ccebd5591c4 100644 --- a/Tasks/Common/PowershellHelpers/Tests/Invoke-ActionWithRetries.ShouldHandleMultipleRetryableExceptions.ps1 +++ b/Tasks/Common/PowershellHelpers/Tests/Invoke-ActionWithRetries.ShouldHandleMultipleRetryableExceptions.ps1 @@ -7,11 +7,16 @@ $module = Microsoft.PowerShell.Core\Import-Module $PSScriptRoot\.. -PassThru $global:retriesAttempted = 0 $action = { $global:retriesAttempted++ - if($global:retriesAttempted -eq 5) { + if ($global:retriesAttempted -eq 5) + { return $true - } elseif($global:retriesAttempted -eq 2) { + } + elseif ($global:retriesAttempted -eq 2) + { throw [System.IO.FileNotFoundException] "File not found error!" - } else { + } + else + { throw [System.IO.DirectoryNotFoundException] "dir not found error!" } } diff --git a/Tasks/Common/PowershellHelpers/Tests/L0.ts b/Tasks/Common/PowershellHelpers/Tests/L0.ts index 6ca8eefa7555..ee3dd6eda6d0 100644 --- a/Tasks/Common/PowershellHelpers/Tests/L0.ts +++ b/Tasks/Common/PowershellHelpers/Tests/L0.ts @@ -51,5 +51,8 @@ describe('PowershellHelpers Suite', function () { it('(Invoke-ActionWithRetries) should handle multiple retryable exceptions', (done) => { psr.run(path.join(__dirname, 'Invoke-ActionWithRetries.ShouldHandleMultipleRetryableExceptions.ps1'), done); }) + it('(Get-TempDirectoryPath) get temp directory', (done) => { + psr.run(path.join(__dirname, 'Get-TempDirectoryPath.ps1'), done); + }) } }); \ No newline at end of file diff --git a/Tasks/ServiceFabricDeploy/Create-DiffPackage.psm1 b/Tasks/ServiceFabricDeploy/Create-DiffPackage.psm1 index 7dbb5a6e8b18..5d2a03ffd622 100644 --- a/Tasks/ServiceFabricDeploy/Create-DiffPackage.psm1 +++ b/Tasks/ServiceFabricDeploy/Create-DiffPackage.psm1 @@ -39,7 +39,7 @@ function Create-DiffPackage Return } - $diffPackagePath = Join-Path $env:Temp "DiffPackage" + $diffPackagePath = Join-Path (Get-TempDirectoryPath) "DiffPackage" if (Test-Path -PathType Container -Path $diffPackagePath) { Remove-Item -Path $diffPackagePath -Recurse -Force @@ -82,7 +82,7 @@ function Create-DiffPackage $diffServicePkgPath = [System.IO.Path]::Combine($diffPackagePath, $localServiceManifestName) $clusterServiceManifest = $clusterServiceManifestByName[$localServiceManifestName].ServiceManifest - # If there's no matching manifest from the cluster it means this is a newly added service that doesn't exist yet on the cluster. + # If there's no matching manifest from the cluster it means this is a newly added service that doesn't exist yet on the cluster. if (!$clusterServiceManifest) { # Copy this service and all the children @@ -90,7 +90,7 @@ function Create-DiffPackage Copy-Item $localServicePkgPath $diffServicePkgPath -Recurse continue } - + # If the Version of the Service is not changed, don't include the service in the diff package if ($clusterServiceManifest.Version -eq $localServiceManifestVersion) { diff --git a/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Publish-NewServiceFabricApplication.ps1 b/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Publish-NewServiceFabricApplication.ps1 index 34eb72dd8feb..e81b628e0d0c 100644 --- a/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Publish-NewServiceFabricApplication.ps1 +++ b/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Publish-NewServiceFabricApplication.ps1 @@ -113,7 +113,7 @@ { if((Get-Item $ApplicationPackagePath).Extension -eq ".sfpkg") { - $AppPkgPathToUse=[io.path]::combine($env:Temp, (Get-Item $ApplicationPackagePath).BaseName) + $AppPkgPathToUse = [io.path]::combine((Get-TempDirectoryPath), (Get-Item $ApplicationPackagePath).BaseName) Expand-ToFolder $ApplicationPackagePath $AppPkgPathToUse } else diff --git a/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Publish-UpgradedServiceFabricApplication.ps1 b/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Publish-UpgradedServiceFabricApplication.ps1 index d341c6250819..abfdc8604845 100644 --- a/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Publish-UpgradedServiceFabricApplication.ps1 +++ b/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Publish-UpgradedServiceFabricApplication.ps1 @@ -119,7 +119,7 @@ function Publish-UpgradedServiceFabricApplication { if((Get-Item $ApplicationPackagePath).Extension -eq ".sfpkg") { - $AppPkgPathToUse=[io.path]::combine($env:Temp, (Get-Item $ApplicationPackagePath).BaseName) + $AppPkgPathToUse = [io.path]::combine((Get-TempDirectoryPath), (Get-Item $ApplicationPackagePath).BaseName) Expand-ToFolder $ApplicationPackagePath $AppPkgPathToUse } else diff --git a/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Utilities.ps1 b/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Utilities.ps1 index 4435c2f03c16..88f0d102945c 100644 --- a/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Utilities.ps1 +++ b/Tasks/ServiceFabricDeploy/ServiceFabricSDK/Utilities.ps1 @@ -1,7 +1,7 @@ function Expand-ToFolder { <# - .SYNOPSIS + .SYNOPSIS Unzips the zip file to the specified folder. .PARAMETER From @@ -16,7 +16,7 @@ ( [String] $File, - + [String] $Destination ) @@ -24,8 +24,8 @@ if (!(Test-Path $File)) { return - } - + } + if (Test-Path $Destination) { Remove-Item -Path $Destination -Recurse -ErrorAction Stop | Out-Null @@ -35,25 +35,25 @@ Write-Verbose -Message (Get-VstsLocString -Key SFSDK_UnzipPackage -ArgumentList @($File, $Destination)) - try + try + { + [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null + [System.IO.Compression.ZipFile]::ExtractToDirectory("$File", "$Destination") + } + catch { - [System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null - [System.IO.Compression.ZipFile]::ExtractToDirectory("$File", "$Destination") - } - catch - { - Write-Error -Message (Get-VstsLocString -Key SFSDK_UnexpectedError -ArgumentList $_.Exception.Message) - } + Write-Error -Message (Get-VstsLocString -Key SFSDK_UnexpectedError -ArgumentList $_.Exception.Message) + } } function Get-NamesFromApplicationManifest { <# - .SYNOPSIS + .SYNOPSIS Returns an object containing common information from the application manifest. .PARAMETER ApplicationManifestPath - Path to the application manifest file. + Path to the application manifest file. #> [CmdletBinding()] @@ -68,7 +68,7 @@ function Get-NamesFromApplicationManifest throw (Get-VstsLocString -Key PathDoesNotExist -ArgumentList $ApplicationManifestPath) } - + $appXml = [xml] (Get-Content $ApplicationManifestPath) if (!$appXml) { @@ -80,10 +80,10 @@ function Get-NamesFromApplicationManifest $appTypeSuffix = 'Type' $h = @{ - FabricNamespace = $FabricNamespace; - ApplicationTypeName = $appMan.ApplicationTypeName; + FabricNamespace = $FabricNamespace; + ApplicationTypeName = $appMan.ApplicationTypeName; ApplicationTypeVersion = $appMan.ApplicationTypeVersion; - } + } Write-Output (New-Object psobject -Property $h) } @@ -91,7 +91,7 @@ function Get-NamesFromApplicationManifest function Get-ImageStoreConnectionStringFromClusterManifest { <# - .SYNOPSIS + .SYNOPSIS Returns the value of the image store connection string from the cluster manifest. .PARAMETER ClusterManifest @@ -113,7 +113,7 @@ function Get-ImageStoreConnectionStringFromClusterManifest function Get-ApplicationNameFromApplicationParameterFile { <# - .SYNOPSIS + .SYNOPSIS Returns Application Name from ApplicationParameter xml file. .PARAMETER ApplicationParameterFilePath @@ -126,7 +126,7 @@ function Get-ApplicationNameFromApplicationParameterFile [String] $ApplicationParameterFilePath ) - + if (!(Test-Path $ApplicationParameterFilePath)) { $errMsg = (Get-VstsLocString -Key PathDoesNotExist -ArgumentList $ApplicationParameterFilePath) @@ -140,7 +140,7 @@ function Get-ApplicationNameFromApplicationParameterFile function Get-ApplicationParametersFromApplicationParameterFile { <# - .SYNOPSIS + .SYNOPSIS Reads ApplicationParameter xml file and returns HashTable containing ApplicationParameters. .PARAMETER ApplicationParameterFilePath @@ -153,19 +153,20 @@ function Get-ApplicationParametersFromApplicationParameterFile [String] $ApplicationParameterFilePath ) - + if (!(Test-Path $ApplicationParameterFilePath)) { throw (Get-VstsLocString -Key PathDoesNotExist -ArgumentList $ApplicationParameterFilePath) } - + $ParametersXml = ([xml] (Get-Content $ApplicationParameterFilePath)).Application.Parameters $hash = @{} $ParametersXml.ChildNodes | foreach { - if ($_.LocalName -eq 'Parameter') { - $hash[$_.Name] = $_.Value - } + if ($_.LocalName -eq 'Parameter') + { + $hash[$_.Name] = $_.Value + } } return $hash @@ -174,14 +175,14 @@ function Get-ApplicationParametersFromApplicationParameterFile function Merge-HashTables { <# - .SYNOPSIS + .SYNOPSIS Merges 2 hashtables. Key, value pairs form HashTableNew are preserved if any duplciates are found between HashTableOld & HashTableNew. .PARAMETER HashTableOld First Hashtable. - + .PARAMETER HashTableNew - Second Hashtable + Second Hashtable #> [CmdletBinding()] @@ -189,11 +190,11 @@ function Merge-HashTables ( [HashTable] $HashTableOld, - + [HashTable] $HashTableNew ) - + $keys = $HashTableOld.getenumerator() | foreach-object {$_.key} $keys | foreach-object { $key = $_ diff --git a/Tasks/ServiceFabricDeploy/Strings/resources.resjson/en-US/resources.resjson b/Tasks/ServiceFabricDeploy/Strings/resources.resjson/en-US/resources.resjson index 8441ad7d81d9..5183d17838d7 100644 --- a/Tasks/ServiceFabricDeploy/Strings/resources.resjson/en-US/resources.resjson +++ b/Tasks/ServiceFabricDeploy/Strings/resources.resjson/en-US/resources.resjson @@ -67,9 +67,11 @@ "loc.messages.DIFFPKG_CopyingToDiffPackge": "Copying {0} from full application package to {1} in diff package...", "loc.messages.DIFFPKG_CreatingDiffPackage": "Trying to create diff package...", "loc.messages.DIFFPKG_CreatingDiffPackageForService": "Creating diff package for service: {0}, clusterServiceManifest.Version: {1}, localServiceManifest.Version: {2}", - "loc.messages.DIFFPKG_NoServicesRunning": "No services of application {0} are running in the cluster {1}. Applying full application packge to do the deployment now...", + "loc.messages.DIFFPKG_NoServicesRunning": "No services of application {0} are running in the cluster {1}. Applying full application package to do the deployment now...", + "loc.messages.DIFFPKG_PackageDoesNotExist": "The package {0} does not exist in the full application package. Skip copying it to diff package.", "loc.messages.DIFFPKG_ServiceDoesNotExist": "The service {0} of application {1} to be upgraded by using diff package does not exist in the cluster {2}. Copying it to diff package now...", "loc.messages.DIFFPKG_ServiceIsNotChanged": "The service {0} of application {1} to be upgraded by using diff package has the same version {2} running in the cluster {3}. Skip upgrading it.", + "loc.messages.DIFFPKG_TestAppPkgFailed": "Testing application package failed before creating diff package. Skip creating diff package.", "loc.messages.ItemSearchMoreThanOneFound": "Found more than one item with search pattern {0}. There can be only one.", "loc.messages.ItemSearchNoFilesFound": "No items were found with search pattern {0}.", "loc.messages.SearchingForPath": "Searching for path: {0}", diff --git a/Tasks/ServiceFabricDeploy/Tests/CreateDiffPkg.ps1 b/Tasks/ServiceFabricDeploy/Tests/CreateDiffPkg.ps1 index 1b30a70275ee..06b5ea5b39e7 100644 --- a/Tasks/ServiceFabricDeploy/Tests/CreateDiffPkg.ps1 +++ b/Tasks/ServiceFabricDeploy/Tests/CreateDiffPkg.ps1 @@ -3,9 +3,11 @@ param() . $PSScriptRoot\..\..\..\Tests\lib\Initialize-Test.ps1 +Register-Mock Get-TempDirectoryPath { "C:\some-path" } + $publishProfilePath = "$PSScriptRoot\data\NoAuthPublishProfile.xml" $applicationPackagePath = "$PSScriptRoot\data\DiffPkgAssets\AppPkg" -$diffPackagePath = (Get-Item $env:TEMP).FullName + "\DiffPackage" +$diffPackagePath = (Get-TempDirectoryPath) + "\DiffPackage" $serviceConnectionName = "random connection name" $serviceFabricSdkModulePath = "$PSScriptRoot\data\ServiceFabricSDK.ps1" $appName = "AppName" diff --git a/Tasks/ServiceFabricDeploy/task.json b/Tasks/ServiceFabricDeploy/task.json index a86a0b77383a..f0098ba500a9 100644 --- a/Tasks/ServiceFabricDeploy/task.json +++ b/Tasks/ServiceFabricDeploy/task.json @@ -17,7 +17,7 @@ "version": { "Major": 1, "Minor": 7, - "Patch": 2 + "Patch": 3 }, "demands": [ "Cmd" diff --git a/Tasks/ServiceFabricDeploy/task.loc.json b/Tasks/ServiceFabricDeploy/task.loc.json index 525ef7dfd1de..d14a3f6b0c0f 100644 --- a/Tasks/ServiceFabricDeploy/task.loc.json +++ b/Tasks/ServiceFabricDeploy/task.loc.json @@ -17,7 +17,7 @@ "version": { "Major": 1, "Minor": 7, - "Patch": 2 + "Patch": 3 }, "demands": [ "Cmd" diff --git a/Tasks/ServiceFabricUpdateManifests/Get-VstsBuild.psm1 b/Tasks/ServiceFabricUpdateManifests/Get-VstsBuild.psm1 index 49c24f40e77e..40e4e183229a 100644 --- a/Tasks/ServiceFabricUpdateManifests/Get-VstsBuild.psm1 +++ b/Tasks/ServiceFabricUpdateManifests/Get-VstsBuild.psm1 @@ -4,16 +4,16 @@ [OutputType([string])] Param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $PkgArtifactName, - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [boolean] $OverwritePkgArtifact, - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateSet("Specific", "LastSuccessful")] [string] $CompareType, @@ -23,7 +23,8 @@ ) Trace-VstsEnteringInvocation $MyInvocation - try { + try + { $vstsEndpoint = Get-VstsEndpoint -Name SystemVssConnection -Require $authHeader = @{ Authorization = "Bearer $($vstsEndpoint.auth.parameters.AccessToken)" } $projectId = Get-VstsTaskVariable -Name System.TeamProjectId -Require @@ -34,9 +35,9 @@ { if ([string]::IsNullOrEmpty($BuildNumber)) { - throw (Get-VstsLocString -Key BuildNumberNotSpecified) + throw (Get-VstsLocString -Key BuildNumberNotSpecified) } - + $escapedBuildNumber = [System.Uri]::EscapeDataString($BuildNumber) # Query for a specific build number (regardless of build status or result) @@ -64,7 +65,8 @@ } elseif ($artifact.resource.type -eq "container") # The artifact is in a hosted server and must be downloaded into a temp folder { - $agentTmpFolder = Join-Path $ENV:AGENT_TEMPDIRECTORY $build.buildNumber + Import-Module $PSScriptRoot\ps_modules\PowershellHelpers + $agentTmpFolder = Join-Path (Get-TempDirectory) $build.buildNumber $artifactZipFile = Join-Path $agentTmpFolder "$PkgArtifactName.zip" $artifactPath = Join-Path $agentTmpFolder $PkgArtifactName $downloadArtifact = $true @@ -109,7 +111,9 @@ throw (Get-VstsLocString -Key UnrecognizedArtifactType -ArgumentList $artifact.resource.type) } } - } finally { + } + finally + { Trace-VstsLeavingInvocation $MyInvocation } } @@ -119,24 +123,25 @@ function Get-FileWithProgress [CmdletBinding()] Param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $Url, - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $FileName, - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string] $AuthorizationHeader ) Trace-VstsEnteringInvocation $MyInvocation - try { + try + { $webClient = New-Object System.Net.WebClient try @@ -170,7 +175,9 @@ function Get-FileWithProgress Remove-Event -SourceIdentifier FinishedDownload Unregister-Event -SourceIdentifier FinishedDownload } - } finally { + } + finally + { Trace-VstsLeavingInvocation $MyInvocation } } diff --git a/Tasks/ServiceFabricUpdateManifests/make.json b/Tasks/ServiceFabricUpdateManifests/make.json index 05f9a2a79c6c..b92715732c10 100644 --- a/Tasks/ServiceFabricUpdateManifests/make.json +++ b/Tasks/ServiceFabricUpdateManifests/make.json @@ -1,4 +1,10 @@ { + "common": [ + { + "module": "../Common/PowershellHelpers", + "type": "ps" + } + ], "externals": { "nugetv2": [ { diff --git a/Tasks/ServiceFabricUpdateManifests/task.json b/Tasks/ServiceFabricUpdateManifests/task.json index 1738040bda30..a621d64444e4 100644 --- a/Tasks/ServiceFabricUpdateManifests/task.json +++ b/Tasks/ServiceFabricUpdateManifests/task.json @@ -19,7 +19,7 @@ "version": { "Major": 2, "Minor": 2, - "Patch": 0 + "Patch": 1 }, "minimumAgentVersion": "1.95.0", "instanceNameFormat": "Update Service Fabric Manifests ($(updateType))", diff --git a/Tasks/ServiceFabricUpdateManifests/task.loc.json b/Tasks/ServiceFabricUpdateManifests/task.loc.json index 50ae9b7fb615..a6bc26bd44a7 100644 --- a/Tasks/ServiceFabricUpdateManifests/task.loc.json +++ b/Tasks/ServiceFabricUpdateManifests/task.loc.json @@ -19,7 +19,7 @@ "version": { "Major": 2, "Minor": 2, - "Patch": 0 + "Patch": 1 }, "minimumAgentVersion": "1.95.0", "instanceNameFormat": "ms-resource:loc.instanceNameFormat",