Skip to content

Commit eccdf0a

Browse files
author
Rajat Agrawal
authored
Merge pull request #9165 from Microsoft/users/raagra/afc-blob-fix-m143
afc blob copy fix for azure logout
2 parents eb51b98 + d188f6e commit eccdf0a

File tree

2 files changed

+269
-259
lines changed

2 files changed

+269
-259
lines changed

Tasks/AzureFileCopyV1/AzureFileCopy.ps1

Lines changed: 112 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -76,128 +76,133 @@ if ($enableDetailedLoggingString -ne "true")
7676
Import-Module $PSScriptRoot\ps_modules\TelemetryHelper
7777

7878
#### MAIN EXECUTION OF AZURE FILE COPY TASK BEGINS HERE ####
79-
try
80-
{
81-
# Importing required version of azure cmdlets according to azureps installed on machine
82-
$azureUtility = Get-AzureUtility $connectedServiceName
83-
84-
Write-Verbose -Verbose "Loading $azureUtility"
85-
. "$PSScriptRoot/$azureUtility"
86-
87-
# Telemetry for endpoint id
88-
$telemetryJsonContent = "{`"endpointId`":`"$connectedServiceName`"}"
89-
Write-Host "##vso[telemetry.publish area=TaskEndpointId;feature=AzureFileCopy]$telemetryJsonContent"
90-
91-
# Getting connection type (Certificate/UserNamePassword/SPN) used for the task
92-
$connectionType = Get-TypeOfConnection -connectedServiceName $connectedServiceName
93-
94-
# Getting storage key for the storage account based on the connection type
95-
$storageKey = Get-StorageKey -storageAccountName $storageAccount -connectionType $connectionType -connectedServiceName $connectedServiceName
79+
try {
80+
try
81+
{
82+
# Importing required version of azure cmdlets according to azureps installed on machine
83+
$azureUtility = Get-AzureUtility $connectedServiceName
84+
85+
Write-Verbose -Verbose "Loading $azureUtility"
86+
. "$PSScriptRoot/$azureUtility"
87+
88+
# Telemetry for endpoint id
89+
$telemetryJsonContent = "{`"endpointId`":`"$connectedServiceName`"}"
90+
Write-Host "##vso[telemetry.publish area=TaskEndpointId;feature=AzureFileCopy]$telemetryJsonContent"
91+
92+
# Getting connection type (Certificate/UserNamePassword/SPN) used for the task
93+
$connectionType = Get-TypeOfConnection -connectedServiceName $connectedServiceName
94+
95+
# Getting storage key for the storage account based on the connection type
96+
$storageKey = Get-StorageKey -storageAccountName $storageAccount -connectionType $connectionType -connectedServiceName $connectedServiceName
97+
98+
# creating storage context to be used while creating container, sas token, deleting container
99+
$storageContext = Create-AzureStorageContext -StorageAccountName $storageAccount -StorageAccountKey $storageKey
100+
101+
# Geting Azure Storage Account type
102+
$storageAccountType = Get-StorageAccountType -storageAccountName $storageAccount -connectionType $connectionType -connectedServiceName $connectedServiceName
103+
Write-Verbose "Obtained Storage Account type: $storageAccountType"
104+
if(-not [string]::IsNullOrEmpty($storageAccountType) -and $storageAccountType.Contains('Premium'))
105+
{
106+
$isPremiumStorage = $true
107+
}
108+
109+
# creating temporary container for uploading files if no input is provided for container name
110+
if([string]::IsNullOrEmpty($containerName))
111+
{
112+
$containerName = [guid]::NewGuid().ToString()
113+
Create-AzureContainer -containerName $containerName -storageContext $storageContext -isPremiumStorage $isPremiumStorage
114+
}
115+
116+
# Geting Azure Blob Storage Endpoint
117+
118+
$blobStorageEndpoint = Get-blobStorageEndpoint -storageAccountName $storageAccount -connectionType $connectionType -connectedServiceName $connectedServiceName
96119

97-
# creating storage context to be used while creating container, sas token, deleting container
98-
$storageContext = Create-AzureStorageContext -StorageAccountName $storageAccount -StorageAccountKey $storageKey
99-
100-
# Geting Azure Storage Account type
101-
$storageAccountType = Get-StorageAccountType -storageAccountName $storageAccount -connectionType $connectionType -connectedServiceName $connectedServiceName
102-
Write-Verbose "Obtained Storage Account type: $storageAccountType"
103-
if(-not [string]::IsNullOrEmpty($storageAccountType) -and $storageAccountType.Contains('Premium'))
120+
}
121+
catch
104122
{
105-
$isPremiumStorage = $true
123+
Write-Verbose $_.Exception.ToString()
124+
Write-Telemetry "Task_InternalError" "TemporaryCopyingToBlobContainerFailed"
125+
throw
106126
}
107127

108-
# creating temporary container for uploading files if no input is provided for container name
109-
if([string]::IsNullOrEmpty($containerName))
128+
if($isPremiumStorage -and $additionalArguments -notLike "*/BlobType:page*")
110129
{
111-
$containerName = [guid]::NewGuid().ToString()
112-
Create-AzureContainer -containerName $containerName -storageContext $storageContext -isPremiumStorage $isPremiumStorage
130+
Write-Verbose "Setting BlobType to page for Premium Storage account."
131+
$uploadAdditionalArguments = $additionalArguments + " /BlobType:page"
132+
}
133+
else
134+
{
135+
$uploadAdditionalArguments = $additionalArguments
113136
}
114-
115-
# Geting Azure Blob Storage Endpoint
116-
117-
$blobStorageEndpoint = Get-blobStorageEndpoint -storageAccountName $storageAccount -connectionType $connectionType -connectedServiceName $connectedServiceName
118-
119-
}
120-
catch
121-
{
122-
Write-Verbose $_.Exception.ToString()
123-
Write-Telemetry "Task_InternalError" "TemporaryCopyingToBlobContainerFailed"
124-
throw
125-
}
126-
127-
if($isPremiumStorage -and $additionalArguments -notLike "*/BlobType:page*")
128-
{
129-
Write-Verbose "Setting BlobType to page for Premium Storage account."
130-
$uploadAdditionalArguments = $additionalArguments + " /BlobType:page"
131-
}
132-
else
133-
{
134-
$uploadAdditionalArguments = $additionalArguments
135-
}
136137

137-
Check-ContainerNameAndArgs -containerName $containerName -additionalArguments $additionalArguments
138+
Check-ContainerNameAndArgs -containerName $containerName -additionalArguments $additionalArguments
138139

139-
# Uploading files to container
140-
Upload-FilesToAzureContainer -sourcePath $sourcePath -storageAccountName $storageAccount -containerName $containerName -blobPrefix $blobPrefix -blobStorageEndpoint $blobStorageEndpoint -storageKey $storageKey `
141-
-azCopyLocation $azCopyLocation -additionalArguments $uploadAdditionalArguments -destinationType $destination
140+
# Uploading files to container
141+
Upload-FilesToAzureContainer -sourcePath $sourcePath -storageAccountName $storageAccount -containerName $containerName -blobPrefix $blobPrefix -blobStorageEndpoint $blobStorageEndpoint -storageKey $storageKey `
142+
-azCopyLocation $azCopyLocation -additionalArguments $uploadAdditionalArguments -destinationType $destination
142143

143-
# Complete the task if destination is azure blob
144-
if ($destination -eq "AzureBlob")
145-
{
146-
# Get URI and SaSToken for output if needed
147-
if(-not [string]::IsNullOrEmpty($outputStorageURI))
144+
# Complete the task if destination is azure blob
145+
if ($destination -eq "AzureBlob")
148146
{
149-
$storageAccountContainerURI = $storageContext.BlobEndPoint + $containerName
150-
Write-Host "##vso[task.setvariable variable=$outputStorageURI;]$storageAccountContainerURI"
147+
# Get URI and SaSToken for output if needed
148+
if(-not [string]::IsNullOrEmpty($outputStorageURI))
149+
{
150+
$storageAccountContainerURI = $storageContext.BlobEndPoint + $containerName
151+
Write-Host "##vso[task.setvariable variable=$outputStorageURI;]$storageAccountContainerURI"
152+
}
153+
if(-not [string]::IsNullOrEmpty($outputStorageContainerSASToken))
154+
{
155+
$storageContainerSaSToken = New-AzureStorageContainerSASToken -Container $containerName -Context $storageContext -Permission r -ExpiryTime (Get-Date).AddHours($defaultSasTokenTimeOutInHours)
156+
Write-Host "##vso[task.setvariable variable=$outputStorageContainerSASToken;]$storageContainerSasToken"
157+
}
158+
159+
Remove-EndpointSecrets
160+
Disconnect-AzureAndClearContext -authScheme $connectionType -ErrorAction SilentlyContinue
161+
Write-Verbose "Completed Azure File Copy Task for Azure Blob Destination"
162+
163+
return
151164
}
152-
if(-not [string]::IsNullOrEmpty($outputStorageContainerSASToken))
165+
166+
# Copying files to Azure VMs
167+
try
153168
{
154-
$storageContainerSaSToken = New-AzureStorageContainerSASToken -Container $containerName -Context $storageContext -Permission r -ExpiryTime (Get-Date).AddHours($defaultSasTokenTimeOutInHours)
155-
Write-Host "##vso[task.setvariable variable=$outputStorageContainerSASToken;]$storageContainerSasToken"
169+
# Normalize admin username
170+
if($vmsAdminUserName -and (-not $vmsAdminUserName.StartsWith(".\")) -and ($vmsAdminUserName.IndexOf("\") -eq -1) -and ($vmsAdminUserName.IndexOf("@") -eq -1))
171+
{
172+
$vmsAdminUserName = ".\" + $vmsAdminUserName
173+
}
174+
# getting azure vms properties(name, fqdn, winrmhttps port)
175+
$azureVMResourcesProperties = Get-AzureVMResourcesProperties -resourceGroupName $environmentName -connectionType $connectionType `
176+
-resourceFilteringMethod $resourceFilteringMethod -machineNames $machineNames -enableCopyPrerequisites $enableCopyPrerequisites -connectedServiceName $connectedServiceName
177+
178+
$skipCACheckOption = Get-SkipCACheckOption -skipCACheck $skipCACheck
179+
$azureVMsCredentials = Get-AzureVMsCredentials -vmsAdminUserName $vmsAdminUserName -vmsAdminPassword $vmsAdminPassword
180+
181+
# generate container sas token with full permissions
182+
$containerSasToken = Generate-AzureStorageContainerSASToken -containerName $containerName -storageContext $storageContext -tokenTimeOutInHours $defaultSasTokenTimeOutInHours
183+
184+
#copies files on azureVMs
185+
Copy-FilesToAzureVMsFromStorageContainer `
186+
-storageAccountName $storageAccount -containerName $containerName -containerSasToken $containerSasToken -blobStorageEndpoint $blobStorageEndpoint -targetPath $targetPath -azCopyLocation $azCopyLocation `
187+
-resourceGroupName $environmentName -azureVMResourcesProperties $azureVMResourcesProperties -azureVMsCredentials $azureVMsCredentials `
188+
-cleanTargetBeforeCopy $cleanTargetBeforeCopy -communicationProtocol $useHttpsProtocolOption -skipCACheckOption $skipCACheckOption `
189+
-enableDetailedLoggingString $enableDetailedLoggingString -additionalArguments $additionalArguments -copyFilesInParallel $copyFilesInParallel -connectionType $connectionType
156190
}
191+
catch
192+
{
193+
Write-Verbose $_.Exception.ToString()
157194

158-
Remove-EndpointSecrets
159-
Write-Verbose "Completed Azure File Copy Task for Azure Blob Destination"
160-
161-
return
162-
}
163-
164-
# Copying files to Azure VMs
165-
try
166-
{
167-
# Normalize admin username
168-
if($vmsAdminUserName -and (-not $vmsAdminUserName.StartsWith(".\")) -and ($vmsAdminUserName.IndexOf("\") -eq -1) -and ($vmsAdminUserName.IndexOf("@") -eq -1))
195+
Write-Telemetry "Task_InternalError" "CopyingToAzureVMFailed"
196+
throw
197+
}
198+
finally
169199
{
170-
$vmsAdminUserName = ".\" + $vmsAdminUserName
200+
Remove-AzureContainer -containerName $containerName -storageContext $storageContext
201+
Remove-EndpointSecrets
202+
Write-Verbose "Completed Azure File Copy Task for Azure VMs Destination" -Verbose
203+
Trace-VstsLeavingInvocation $MyInvocation
171204
}
172-
# getting azure vms properties(name, fqdn, winrmhttps port)
173-
$azureVMResourcesProperties = Get-AzureVMResourcesProperties -resourceGroupName $environmentName -connectionType $connectionType `
174-
-resourceFilteringMethod $resourceFilteringMethod -machineNames $machineNames -enableCopyPrerequisites $enableCopyPrerequisites -connectedServiceName $connectedServiceName
175-
176-
$skipCACheckOption = Get-SkipCACheckOption -skipCACheck $skipCACheck
177-
$azureVMsCredentials = Get-AzureVMsCredentials -vmsAdminUserName $vmsAdminUserName -vmsAdminPassword $vmsAdminPassword
178-
179-
# generate container sas token with full permissions
180-
$containerSasToken = Generate-AzureStorageContainerSASToken -containerName $containerName -storageContext $storageContext -tokenTimeOutInHours $defaultSasTokenTimeOutInHours
181-
182-
#copies files on azureVMs
183-
Copy-FilesToAzureVMsFromStorageContainer `
184-
-storageAccountName $storageAccount -containerName $containerName -containerSasToken $containerSasToken -blobStorageEndpoint $blobStorageEndpoint -targetPath $targetPath -azCopyLocation $azCopyLocation `
185-
-resourceGroupName $environmentName -azureVMResourcesProperties $azureVMResourcesProperties -azureVMsCredentials $azureVMsCredentials `
186-
-cleanTargetBeforeCopy $cleanTargetBeforeCopy -communicationProtocol $useHttpsProtocolOption -skipCACheckOption $skipCACheckOption `
187-
-enableDetailedLoggingString $enableDetailedLoggingString -additionalArguments $additionalArguments -copyFilesInParallel $copyFilesInParallel -connectionType $connectionType
188205
}
189-
catch
190-
{
191-
Write-Verbose $_.Exception.ToString()
192-
193-
Write-Telemetry "Task_InternalError" "CopyingToAzureVMFailed"
194-
throw
195-
}
196-
finally
197-
{
198-
Remove-AzureContainer -containerName $containerName -storageContext $storageContext
199-
Remove-EndpointSecrets
206+
finally {
200207
Disconnect-AzureAndClearContext -authScheme $connectionType -ErrorAction SilentlyContinue
201-
Write-Verbose "Completed Azure File Copy Task for Azure VMs Destination" -Verbose
202-
Trace-VstsLeavingInvocation $MyInvocation
203-
}
208+
}

0 commit comments

Comments
 (0)