Skip to content

Commit

Permalink
Use agent's temp dir if available (#6673)
Browse files Browse the repository at this point in the history
* Use agent's temp dir if available

* check for path length as well

* Minor fix. Added L0 tests

* Move temp-path helper to common helpers

* PR comments

* resolve merge conflict
  • Loading branch information
bishal-pdMSFT authored Mar 15, 2018
1 parent 4e7965f commit d7699c0
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions Tasks/Common/PowershellHelpers/PowershellHelpers.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Export-ModuleMember -Function @("Invoke-ActionWithRetries", "Get-TempDirectoryPath")
46 changes: 46 additions & 0 deletions Tasks/Common/PowershellHelpers/Tests/Get-TempDirectoryPath.ps1
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
}
}
Expand Down
3 changes: 3 additions & 0 deletions Tasks/Common/PowershellHelpers/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
}
});
6 changes: 3 additions & 3 deletions Tasks/ServiceFabricDeploy/Create-DiffPackage.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,15 +82,15 @@ 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
Write-Host (Get-VstsLocString -Key DIFFPKG_ServiceDoesNotExist -ArgumentList @($localServiceManifestName, $ApplicationName, $ConnectedServiceEndpoint.Url))
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 33 additions & 32 deletions Tasks/ServiceFabricDeploy/ServiceFabricSDK/Utilities.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function Expand-ToFolder
{
<#
.SYNOPSIS
.SYNOPSIS
Unzips the zip file to the specified folder.
.PARAMETER From
Expand All @@ -16,16 +16,16 @@
(
[String]
$File,

[String]
$Destination
)

if (!(Test-Path $File))
{
return
}
}

if (Test-Path $Destination)
{
Remove-Item -Path $Destination -Recurse -ErrorAction Stop | Out-Null
Expand All @@ -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()]
Expand All @@ -68,7 +68,7 @@ function Get-NamesFromApplicationManifest
throw (Get-VstsLocString -Key PathDoesNotExist -ArgumentList $ApplicationManifestPath)
}


$appXml = [xml] (Get-Content $ApplicationManifestPath)
if (!$appXml)
{
Expand All @@ -80,18 +80,18 @@ 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)
}

function Get-ImageStoreConnectionStringFromClusterManifest
{
<#
.SYNOPSIS
.SYNOPSIS
Returns the value of the image store connection string from the cluster manifest.
.PARAMETER ClusterManifest
Expand All @@ -113,7 +113,7 @@ function Get-ImageStoreConnectionStringFromClusterManifest
function Get-ApplicationNameFromApplicationParameterFile
{
<#
.SYNOPSIS
.SYNOPSIS
Returns Application Name from ApplicationParameter xml file.
.PARAMETER ApplicationParameterFilePath
Expand All @@ -126,7 +126,7 @@ function Get-ApplicationNameFromApplicationParameterFile
[String]
$ApplicationParameterFilePath
)

if (!(Test-Path $ApplicationParameterFilePath))
{
$errMsg = (Get-VstsLocString -Key PathDoesNotExist -ArgumentList $ApplicationParameterFilePath)
Expand All @@ -140,7 +140,7 @@ function Get-ApplicationNameFromApplicationParameterFile
function Get-ApplicationParametersFromApplicationParameterFile
{
<#
.SYNOPSIS
.SYNOPSIS
Reads ApplicationParameter xml file and returns HashTable containing ApplicationParameters.
.PARAMETER ApplicationParameterFilePath
Expand All @@ -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
Expand All @@ -174,26 +175,26 @@ 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()]
Param
(
[HashTable]
$HashTableOld,

[HashTable]
$HashTableNew
)

$keys = $HashTableOld.getenumerator() | foreach-object {$_.key}
$keys | foreach-object {
$key = $_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
4 changes: 3 additions & 1 deletion Tasks/ServiceFabricDeploy/Tests/CreateDiffPkg.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ServiceFabricDeploy/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 7,
"Patch": 2
"Patch": 3
},
"demands": [
"Cmd"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/ServiceFabricDeploy/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 7,
"Patch": 2
"Patch": 3
},
"demands": [
"Cmd"
Expand Down
Loading

0 comments on commit d7699c0

Please sign in to comment.