Skip to content

Commit

Permalink
Improve sql azure deployment task errors handling (#3263)
Browse files Browse the repository at this point in the history
* Improve sql azure deployment task errors handling

* Updated as per review comments

* Invoke expression used to call sqlpackage.exe

* Test correction and @ sysmbol issue in user name fixed

* Rename method
  • Loading branch information
Ajay Kumar Yadav authored Dec 22, 2016
1 parent 331a662 commit 26a606f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 29 deletions.
18 changes: 17 additions & 1 deletion Tasks/Common/VstsAzureRestHelpers_/VstsAzureRestHelpers_.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ function Remove-AzureSqlDatabaseServerFirewallRule

function Parse-Exception($exception){
if($exception) {
Write-Verbose "Exception message - $($exception.ToString())"
$response = $exception.Response
if($response) {
$responseStream = $response.GetResponseStream()
Expand All @@ -722,7 +723,22 @@ function Parse-Exception($exception){
$responseBody = $streamReader.ReadToEnd()
$streamReader.Close()
Write-Verbose "Exception message extracted from response $responseBody"
return $responseBody
$exceptionMessage = "";
try
{
if($responseBody)
{
$exceptionJson = $responseBody | ConvertFrom-Json
$exceptionMessage = $exceptionJson.Message
}
}
catch{
$exceptionMessage = $responseBody
}
if($response.statusCode -eq 404 -or (-not $exceptionMessage)){
$exceptionMessage += " Please verify request URL : $($response.ResponseUri)"
}
return $exceptionMessage
}
}
return $null
Expand Down
20 changes: 17 additions & 3 deletions Tasks/SqlAzureDacpacDeployment/DeploySqlAzure.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Try

Write-Verbose "Executing : $commandToBeLogged"

Run-Command $SqlPackageCommand
Execute-Command -FileName $SqlPackagePath -Arguments $scriptArgument
}
else
{
Expand Down Expand Up @@ -202,12 +202,26 @@ Catch [System.Management.Automation.CommandNotFoundException]
Write-Host "3. Run Import-Module SQLPS on your agent Powershell prompt. (This step is not required on Powershell 3.0 enabled machines)"
}

Write-Error ($_.Exception|Format-List -Force|Out-String)
if($_.Exception.Message)
{
Write-Error ($_.Exception.Message)
}
else
{
Write-Error ($_.Exception)
}
throw
}
Catch [Exception]
{
Write-Error ($_.Exception|Format-List -Force|Out-String)
if($_.Exception.Message)
{
Write-Error ($_.Exception.Message)
}
else
{
Write-Error ($_.Exception)
}
throw
}
Finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@
"loc.messages.SAD_InvalidSqlFile": "Invalid Sql file '{0}' provided",
"loc.messages.SAD_NoPassword": "No password specified for the SQL User: '{0}'",
"loc.messages.SAD_InvalidPublishProfile": "Invalid Publish Profile '{0}' provided",
"loc.messages.SAD_InvalidServerNameFormat": "Server name '{0}' is not in the right format. Use FQDN format like '{1}'"
"loc.messages.SAD_InvalidServerNameFormat": "Server name '{0}' is not in the right format. Use FQDN format like '{1}'",
"loc.messages.SAD_AzureSQLDacpacTaskFailed": "Azure SQL Dacpac task failed."
}
10 changes: 5 additions & 5 deletions Tasks/SqlAzureDacpacDeployment/Tests/L0ValidDacpacInput.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ Register-Mock Get-SqlPackageOnTargetMachine { 'path/to/dac/bin/sqlpackage.exe' }

Register-Mock Delete-AzureSqlDatabaseServerFirewallRule { }

Register-Mock Run-Command { 'executed command ! '} -ArgumentsEvaluator {
$args.count -eq 1 -and $args[0] -eq '"path/to/dac/bin/sqlpackage.exe" /SourceFile:"dacpacFile.dacpac" /Action:Publish /TargetServerName:"a0nuel7r2k.database.windows.net" /TargetDatabaseName:"TestDatabase" /TargetUser:"TestUser" /TargetPassword:"TestPassword" /Profile:"PublishProfile.xml" AdditionalArguments /TargetTimeout:120'
Register-Mock Execute-Command { 'executed command ! '} -ArgumentsEvaluator {
$args.count -eq 2 -and $args[0] -eq 'path/to/dac/bin/sqlpackage.exe' -and $args[1] -eq '/SourceFile:"dacpacFile.dacpac" /Action:Publish /TargetServerName:"a0nuel7r2k.database.windows.net" /TargetDatabaseName:"TestDatabase" /TargetUser:"TestUser" /TargetPassword:"TestPassword" /Profile:"PublishProfile.xml" AdditionalArguments /TargetTimeout:120'
}

Register-Mock Run-Command { throw 'Invalid Command passed !' } -ArgumentsEvaluator {
$args.count -eq 1 -and $args[0] -ne '"path/to/dac/bin/sqlpackage.exe" /SourceFile:"dacpacFile.dacpac" /Action:Publish /TargetServerName:"a0nuel7r2k.database.windows.net" /TargetDatabaseName:"TestDatabase" /TargetUser:"TestUser" /TargetPassword:"TestPassword" /Profile:"PublishProfile.xml" AdditionalArguments /TargetTimeout:120'
Register-Mock Execute-Command { throw 'Invalid Command passed !' } -ArgumentsEvaluator {
$args.count -eq 2 -and $args[0] -eq 'path/to/dac/bin/sqlpackage.exe' -and $args[1] -ne '/SourceFile:"dacpacFile.dacpac" /Action:Publish /TargetServerName:"a0nuel7r2k.database.windows.net" /TargetDatabaseName:"TestDatabase" /TargetUser:"TestUser" /TargetPassword:"TestPassword" /Profile:"PublishProfile.xml" AdditionalArguments /TargetTimeout:120'
}

& "$PSScriptRoot\..\DeploySqlAzure.ps1"

Assert-WasCalled Run-Command -Times 1
Assert-WasCalled Execute-Command -Times 1
38 changes: 23 additions & 15 deletions Tasks/SqlAzureDacpacDeployment/Utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ function Get-SqlPackageCommandArguments

if($sqlUsername)
{
if($sqlUsername.Contains('@'))
{
$sqlUsername = $sqlUsername + "@" + $serverName
}
$sqlPackageArguments += @($SqlPackageOptions.TargetUser + "`"$sqlUsername`"")
if(-not($sqlPassword))
{
Expand Down Expand Up @@ -185,25 +189,29 @@ function Get-SqlPackageCommandArguments
return $scriptArgument
}

function Run-Command
function Execute-Command
{
param([String][Parameter(Mandatory=$true)] $command)

try
{
if( $psversiontable.PSVersion.Major -le 4)
{
cmd.exe /c "`"$command`"" 2>&1
param(
[String][Parameter(Mandatory=$true)] $FileName,
[String][Parameter(Mandatory=$true)] $Arguments
)

$ErrorActionPreference = 'Continue'
Invoke-Expression "& '$FileName' --% $Arguments" 2>&1 -ErrorVariable errors | ForEach-Object {
if ($_ -is [System.Management.Automation.ErrorRecord]) {
Write-Error $_
} else {
Write-Host $_
}
else
{
cmd.exe /c "$command" 2>&1
}

}

foreach($errorMsg in $errors){
Write-Error $errorMsg
}
catch [System.Exception]
$ErrorActionPreference = 'Stop'
if($LASTEXITCODE -ne 0)
{
throw $_.Exception
throw (Get-VstsLocString -Key "SAD_AzureSQLDacpacTaskFailed")
}
}

Expand Down
5 changes: 3 additions & 2 deletions Tasks/SqlAzureDacpacDeployment/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 1,
"Patch": 3
"Patch": 4
},
"demands": [
"sqlpackage"
Expand Down Expand Up @@ -258,6 +258,7 @@
"SAD_InvalidSqlFile": "Invalid Sql file '{0}' provided",
"SAD_NoPassword": "No password specified for the SQL User: '{0}'",
"SAD_InvalidPublishProfile": "Invalid Publish Profile '{0}' provided",
"SAD_InvalidServerNameFormat": "Server name '{0}' is not in the right format. Use FQDN format like '{1}'"
"SAD_InvalidServerNameFormat": "Server name '{0}' is not in the right format. Use FQDN format like '{1}'",
"SAD_AzureSQLDacpacTaskFailed": "Azure SQL Dacpac task failed."
}
}
5 changes: 3 additions & 2 deletions Tasks/SqlAzureDacpacDeployment/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 1,
"Minor": 1,
"Patch": 3
"Patch": 4
},
"demands": [
"sqlpackage"
Expand Down Expand Up @@ -258,6 +258,7 @@
"SAD_InvalidSqlFile": "ms-resource:loc.messages.SAD_InvalidSqlFile",
"SAD_NoPassword": "ms-resource:loc.messages.SAD_NoPassword",
"SAD_InvalidPublishProfile": "ms-resource:loc.messages.SAD_InvalidPublishProfile",
"SAD_InvalidServerNameFormat": "ms-resource:loc.messages.SAD_InvalidServerNameFormat"
"SAD_InvalidServerNameFormat": "ms-resource:loc.messages.SAD_InvalidServerNameFormat",
"SAD_AzureSQLDacpacTaskFailed": "ms-resource:loc.messages.SAD_AzureSQLDacpacTaskFailed"
}
}

0 comments on commit 26a606f

Please sign in to comment.