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

Users/ajya/azure rmwebapp task ps3 #1802

Merged
merged 13 commits into from
Jun 7, 2016
134 changes: 134 additions & 0 deletions Tasks/AzureRmWebAppDeployment/AzureRmWebAppDeployment-Legacy.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
param
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't work. You're calling Utility.ps1 and AzureUtility.ps1 which you've updated to the new lib (calls Get-VstsLocString now).

Copy link
Author

Choose a reason for hiding this comment

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

@ericsciple What is correct solution to this problem ?

  • Should I create legacy class for Utility and AzureUtility too

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes copying the originals to Utility-Legacy.ps1/AzureUtility-Legacy.ps1 would solve the problem. Ultimately the legacy implementation should be deleted in a sprint or two after it's served it's purpose.

Copy link
Author

Choose a reason for hiding this comment

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

@ericsciple - Thanks eric , I have done changes and have verified it .

(
[String] [Parameter(Mandatory = $true)]
$ConnectedServiceName,

[String] [Parameter(Mandatory = $true)]
$WebAppName,

[String] [Parameter(Mandatory = $true)]
$DeployToSlotFlag,

[String] [Parameter(Mandatory = $false)]
$ResourceGroupName,

[String] [Parameter(Mandatory = $false)]
$SlotName,

[String] [Parameter(Mandatory = $true)]
$Package,

[String] [Parameter(Mandatory = $false)]
$SetParametersFile,

[String] [Parameter(Mandatory = $false)]
$RemoveAdditionalFilesFlag,

[String] [Parameter(Mandatory = $false)]
$ExcludeFilesFromAppDataFlag,

[String] [Parameter(Mandatory = $false)]
$TakeAppOfflineFlag,

[String] [Parameter(Mandatory = $false)]
$VirtualApplication,

[String] [Parameter(Mandatory = $false)]
[String] $AdditionalArguments,

[String] [Parameter(Mandatory = $false)]
[string]$WebAppUri
)

Write-Verbose "Starting AzureRM WebApp Deployment Task"

Write-Verbose "ConnectedServiceName = $ConnectedServiceName"
Write-Verbose "WebAppName = $WebAppName"
Write-Verbose "DeployToSlotFlag = $DeployToSlotFlag"
Write-Verbose "ResourceGroupName = $ResourceGroupName"
Write-Verbose "SlotName = $SlotName"
Write-Verbose "Package = $Package"
Write-Verbose "SetParametersFile = $SetParametersFile"
Write-Verbose "RemoveAdditionalFilesFlag = $RemoveAdditionalFilesFlag"
Write-Verbose "ExcludeFilesFromAppDataFlag = $ExcludeFilesFromAppDataFlag"
Write-Verbose "TakeAppOfflineFlag = $TakeAppOfflineFlag"
Write-Verbose "VirtualApplication = $VirtualApplication"
Write-Verbose "AdditionalArguments = $AdditionalArguments"
Write-Verbose "WebAppUri = $WebAppUri"

$WebAppUri = $WebAppUri.Trim()
$Package = $Package.Trim('"').Trim()

if( [string]::IsNullOrEmpty($Package) ){
Throw (Get-LocalizedString -Key "Invalid webapp package path provided")
}

$SetParametersFile = $SetParametersFile.Trim('"').Trim()

# Import all the dlls and modules which have cmdlets we need
Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Internal"
Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Common"

# Load all dependent files for execution
Import-Module ./AzureUtility.ps1 -Force
Import-Module ./Utility.ps1 -Force
Import-Module ./FindInstalledMSDeploy.ps1

# Importing required version of azure cmdlets according to azureps installed on machine
$azureUtility = Get-AzureUtility

Write-Verbose "Loading $azureUtility"
Import-Module ./$azureUtility -Force

$ErrorActionPreference = 'Stop'

#### MAIN EXECUTION OF AZURERM WEBAPP DEPLOYMENT TASK BEGINS HERE ####

# Get msdeploy.exe path
$msDeployExePath = Get-MsDeployExePath

# Ensure that at most a package (.zip) file is found
$packageFilePath = Get-SingleFilePath -file $Package

# Since the SetParametersFile is optional, but it's a FilePath type, it will have the value System.DefaultWorkingDirectory when not specified
if( $SetParametersFile -eq $env:SYSTEM_DEFAULTWORKINGDIRECTORY -or $SetParametersFile -eq [String]::Concat($env:SYSTEM_DEFAULTWORKINGDIRECTORY, "\") -or [string]::IsNullOrEmpty($SetParametersFile)){
$setParametersFilePath = ""
} else {
$setParametersFilePath = Get-SingleFilePath -file $SetParametersFile
}

# Get destination azureRM webApp connection details
$azureRMWebAppConnectionDetails = Get-AzureRMWebAppConnectionDetails -webAppName $WebAppName -deployToSlotFlag $DeployToSlotFlag `
-resourceGroupName $ResourceGroupName -slotName $SlotName

# webApp Name to be used in msdeploy command
$webAppNameForMSDeployCmd = Get-WebAppNameForMSDeployCmd -webAppName $WebAppName -deployToSlotFlag $DeployToSlotFlag -slotName $SlotName

# Construct arguments for msdeploy command
$msDeployCmdArgs = Get-MsDeployCmdArgs -packageFile $packageFilePath -webAppNameForMSDeployCmd $webAppNameForMSDeployCmd -azureRMWebAppConnectionDetails $azureRMWebAppConnectionDetails -removeAdditionalFilesFlag $RemoveAdditionalFilesFlag `
-excludeFilesFromAppDataFlag $ExcludeFilesFromAppDataFlag -takeAppOfflineFlag $TakeAppOfflineFlag -virtualApplication $VirtualApplication -AdditionalArguments $AdditionalArguments `
-setParametersFile $setParametersFilePath

# Deploy azureRM webApp using msdeploy Command
Run-MsDeployCommand -msDeployExePath $msDeployExePath -msDeployCmdArgs $msDeployCmdArgs

# Get azure webapp hosted url
$azureWebsitePublishURL = Get-AzureRMWebAppPublishUrl -webAppName $WebAppName -deployToSlotFlag $DeployToSlotFlag `
-resourceGroupName $ResourceGroupName -slotName $SlotName

# Publish azure webApp url
Write-Host (Get-LocalizedString -Key "Webapp successfully published at Url : {0}" -ArgumentList $azureWebsitePublishURL)

# Set ouput vairable with azureWebsitePublishUrl
if(-not [string]::IsNullOrEmpty($WebAppUri))
{

if( [string]::IsNullOrEmpty($azureWebsitePublishURL))
{
Throw (Get-LocalizedString -Key "Unable to retrieve webapp publish url for webapp : '{0}'." -ArgumentList $webAppName)
}

Write-Host "##vso[task.setvariable variable=$WebAppUri;]$azureWebsitePublishURL"
}

Write-Verbose "Completed AzureRM WebApp Deployment Task"
185 changes: 81 additions & 104 deletions Tasks/AzureRmWebAppDeployment/AzureRmWebAppDeployment.ps1
Original file line number Diff line number Diff line change
@@ -1,134 +1,111 @@
param
(
[String] [Parameter(Mandatory = $true)]
$ConnectedServiceName,
[CmdletBinding()]
param()

[String] [Parameter(Mandatory = $true)]
$WebAppName,
Trace-VstsEnteringInvocation $MyInvocation

[String] [Parameter(Mandatory = $true)]
$DeployToSlotFlag,
try{

[String] [Parameter(Mandatory = $false)]
$ResourceGroupName,
# Get inputs.
$WebAppName = Get-VstsInput -Name WebAppName -Require
$DeployToSlotFlag = Get-VstsInput -Name DeployToSlotFlag -Require -AsBool
$ResourceGroupName = Get-VstsInput -Name ResourceGroupName
$SlotName = Get-VstsInput -Name SlotName
$Package = Get-VstsInput -Name Package -Require
$RemoveAdditionalFilesFlag = Get-VstsInput -Name RemoveAdditionalFilesFlag -AsBool
$ExcludeFilesFromAppDataFlag = Get-VstsInput -Name ExcludeFilesFromAppDataFlag -AsBool
$TakeAppOfflineFlag = Get-VstsInput -Name TakeAppOfflineFlag -AsBool
$VirtualApplication = Get-VstsInput -Name VirtualApplication
$AdditionalArguments = Get-VstsInput -Name AdditionalArguments
$WebAppUri = Get-VstsInput -Name WebAppUri
$SetParametersFile = Get-VstsInput -Name SetParametersFile

[String] [Parameter(Mandatory = $false)]
$SlotName,
# Initialize Azure.

[String] [Parameter(Mandatory = $true)]
$Package,

[String] [Parameter(Mandatory = $false)]
$SetParametersFile,
Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
Initialize-Azure

[String] [Parameter(Mandatory = $false)]
$RemoveAdditionalFilesFlag,
# Import the loc strings.
Import-VstsLocStrings -LiteralPath $PSScriptRoot/Task.json

[String] [Parameter(Mandatory = $false)]
$ExcludeFilesFromAppDataFlag,
$WebAppUri = $WebAppUri.Trim()
$Package = "$Package".Trim('"').Trim()

[String] [Parameter(Mandatory = $false)]
$TakeAppOfflineFlag,

[String] [Parameter(Mandatory = $false)]
$VirtualApplication,

[String] [Parameter(Mandatory = $false)]
[String] $AdditionalArguments,

[String] [Parameter(Mandatory = $false)]
[string]$WebAppUri
)
if( [string]::IsNullOrEmpty($Package) ){
Copy link
Contributor

Choose a reason for hiding this comment

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

fyi - simpler powershell syntax is if (!$Package)

also fyi - I think the web UI already trims values on save. The double-quotes issue is interesting, although I have never trimmed it in tasks before, and haven't heard any issues. Just a thought.

Throw (Get-VstsLocString -Key "Invalidwebapppackagepathprovided")
}

Write-Verbose "Starting AzureRM WebApp Deployment Task"
$SetParametersFile = $SetParametersFile.Trim('"').Trim()
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this ever null ref? A safer route would be: "$SetParametersFile".Trim('"').Trim()

Copy link
Contributor

Choose a reason for hiding this comment

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

I had a comment here before about the possibility of null ref exception. Re-adding in case you overlooked it by accident. Again, a safer option would be the following (double quotes added around the variable before calling trim):

$SetParametersFile = "$SetParametersFile".Trim('"').Trim()


Write-Verbose "ConnectedServiceName = $ConnectedServiceName"
Write-Verbose "WebAppName = $WebAppName"
Write-Verbose "DeployToSlotFlag = $DeployToSlotFlag"
Write-Verbose "ResourceGroupName = $ResourceGroupName"
Write-Verbose "SlotName = $SlotName"
Write-Verbose "Package = $Package"
Write-Verbose "SetParametersFile = $SetParametersFile"
Write-Verbose "RemoveAdditionalFilesFlag = $RemoveAdditionalFilesFlag"
Write-Verbose "ExcludeFilesFromAppDataFlag = $ExcludeFilesFromAppDataFlag"
Write-Verbose "TakeAppOfflineFlag = $TakeAppOfflineFlag"
Write-Verbose "VirtualApplication = $VirtualApplication"
Write-Verbose "AdditionalArguments = $AdditionalArguments"
Write-Verbose "WebAppUri = $WebAppUri"

$WebAppUri = $WebAppUri.Trim()
$Package = $Package.Trim('"').Trim()
# Load all dependent files for execution
. $PSScriptRoot/AzureUtility.ps1 -Force
Copy link
Contributor

Choose a reason for hiding this comment

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

I see you switched over to dot sourcing the scripts instead of calling import-module. However, you left the "-Force" switch in place, and the scripts do not expose a force switch.

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought about this a bit more. Beware there are differences between dot sourcing and importing as a module (modules run within a module context with a different variable inheritance hierarchy and separate session state). I was quite confused when I originally saw importing the ps1 file.

If you're comfortable with switching this over to dot sourcing, then all the better. If not, you may want to consider leaving it as is - sometimes I'm hesitant to change something if it's working as is - or consider changing the file names to psm1 so it's not confusing.

It's your call. If you end up leaving it as import-module and ps1 extension, it would be nice to add a comment - or a TODO comment - so it's not confusing to anyone who needs to read the source code in the future.

. $PSScriptRoot/Utility.ps1 -Force
. $PSScriptRoot/FindInstalledMSDeploy.ps1

if( [string]::IsNullOrEmpty($Package) ){
Throw (Get-LocalizedString -Key "Invalid webapp package path provided")
}
# Importing required version of azure cmdlets according to azureps installed on machine
$azureUtility = Get-AzureUtility

$SetParametersFile = $SetParametersFile.Trim('"').Trim()
Write-Verbose "Loading $azureUtility"
. $PSScriptRoot/$azureUtility -Force

# Import all the dlls and modules which have cmdlets we need
Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Internal"
Import-Module "Microsoft.TeamFoundation.DistributedTask.Task.Common"
#### MAIN EXECUTION OF AZURERM WEBAPP DEPLOYMENT TASK BEGINS HERE ####

# Load all dependent files for execution
Import-Module ./AzureUtility.ps1 -Force
Import-Module ./Utility.ps1 -Force
Import-Module ./FindInstalledMSDeploy.ps1
# Get msdeploy.exe path
$msDeployExePath = Get-MsDeployExePath

# Importing required version of azure cmdlets according to azureps installed on machine
$azureUtility = Get-AzureUtility
# Ensure that at most a package (.zip) file is found
$packageFilePath = Get-SingleFilePath -file $Package

Write-Verbose "Loading $azureUtility"
Import-Module ./$azureUtility -Force
Write-Verbose "Value of package file path : $SetParametersFile" -verbose

$ErrorActionPreference = 'Stop'
# Since the SetParametersFile is optional, but it's a FilePath type, it will have the value System.DefaultWorkingDirectory when not specified
if( $SetParametersFile -eq $env:SYSTEM_DEFAULTWORKINGDIRECTORY -or $SetParametersFile -eq [String]::Concat($env:SYSTEM_DEFAULTWORKINGDIRECTORY, "\" ) -or [string]::IsNullOrEmpty($SetParametersFile) ){
Write-Verbose "Is empty" -verbose
$setParametersFilePath = ""
} else {
Write-Verbose "Is not empty" -verbose
$setParametersFilePath = Get-SingleFilePath -file $SetParametersFile
}

#### MAIN EXECUTION OF AZURERM WEBAPP DEPLOYMENT TASK BEGINS HERE ####
# Get destination azureRM webApp connection details
$azureRMWebAppConnectionDetails = Get-AzureRMWebAppConnectionDetails -webAppName $WebAppName -deployToSlotFlag $DeployToSlotFlag `
-resourceGroupName $ResourceGroupName -slotName $SlotName

# Get msdeploy.exe path
$msDeployExePath = Get-MsDeployExePath
# webApp Name to be used in msdeploy command
$webAppNameForMSDeployCmd = Get-WebAppNameForMSDeployCmd -webAppName $WebAppName -deployToSlotFlag $DeployToSlotFlag -slotName $SlotName

# Ensure that at most a package (.zip) file is found
$packageFilePath = Get-SingleFilePath -file $Package
# Construct arguments for msdeploy command
$msDeployCmdArgs = Get-MsDeployCmdArgs -packageFile $packageFilePath -webAppNameForMSDeployCmd $webAppNameForMSDeployCmd -azureRMWebAppConnectionDetails $azureRMWebAppConnectionDetails -removeAdditionalFilesFlag $RemoveAdditionalFilesFlag `
-excludeFilesFromAppDataFlag $ExcludeFilesFromAppDataFlag -takeAppOfflineFlag $TakeAppOfflineFlag -virtualApplication $VirtualApplication -AdditionalArguments $AdditionalArguments `
-setParametersFile $setParametersFilePath

# Since the SetParametersFile is optional, but it's a FilePath type, it will have the value System.DefaultWorkingDirectory when not specified
if( $SetParametersFile -eq $env:SYSTEM_DEFAULTWORKINGDIRECTORY -or $SetParametersFile -eq [String]::Concat($env:SYSTEM_DEFAULTWORKINGDIRECTORY, "\")){
$setParametersFilePath = ""
} else {
$setParametersFilePath = Get-SingleFilePath -file $SetParametersFile
}
# Deploy azureRM webApp using msdeploy Command
Run-MsDeployCommand -msDeployExePath $msDeployExePath -msDeployCmdArgs $msDeployCmdArgs

# Get destination azureRM webApp connection details
$azureRMWebAppConnectionDetails = Get-AzureRMWebAppConnectionDetails -webAppName $WebAppName -deployToSlotFlag $DeployToSlotFlag `
-resourceGroupName $ResourceGroupName -slotName $SlotName
# Get azure webapp hosted url
$azureWebsitePublishURL = Get-AzureRMWebAppPublishUrl -webAppName $WebAppName -deployToSlotFlag $DeployToSlotFlag `
-resourceGroupName $ResourceGroupName -slotName $SlotName

# webApp Name to be used in msdeploy command
$webAppNameForMSDeployCmd = Get-WebAppNameForMSDeployCmd -webAppName $WebAppName -deployToSlotFlag $DeployToSlotFlag -slotName $SlotName
# Publish azure webApp url
Write-Host (Get-VstsLocString -Key "WebappsuccessfullypublishedatUrl0" -ArgumentList $azureWebsitePublishURL)

# Construct arguments for msdeploy command
$msDeployCmdArgs = Get-MsDeployCmdArgs -packageFile $packageFilePath -webAppNameForMSDeployCmd $webAppNameForMSDeployCmd -azureRMWebAppConnectionDetails $azureRMWebAppConnectionDetails -removeAdditionalFilesFlag $RemoveAdditionalFilesFlag `
-excludeFilesFromAppDataFlag $ExcludeFilesFromAppDataFlag -takeAppOfflineFlag $TakeAppOfflineFlag -virtualApplication $VirtualApplication -AdditionalArguments $AdditionalArguments `
-setParametersFile $setParametersFilePath
# Set ouput vairable with azureWebsitePublishUrl
if(-not [string]::IsNullOrEmpty($WebAppUri))
Copy link
Contributor

@ericsciple ericsciple May 27, 2016

Choose a reason for hiding this comment

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

fyi - simpler powershell syntax is if ($WebAppUri)

{

if( [string]::IsNullOrEmpty($azureWebsitePublishURL))
{
Throw (Get-VstsLocString -Key "Unabletoretrievewebapppublishurlforwebapp0" -ArgumentList $webAppName)
}

Set-VstsTaskVariable -Name $WebAppUri -Value $azureWebsitePublishURL
}

# Deploy azureRM webApp using msdeploy Command
Run-MsDeployCommand -msDeployExePath $msDeployExePath -msDeployCmdArgs $msDeployCmdArgs
Write-Verbose "Completed AzureRM WebApp Deployment Task"

# Get azure webapp hosted url
$azureWebsitePublishURL = Get-AzureRMWebAppPublishUrl -webAppName $WebAppName -deployToSlotFlag $DeployToSlotFlag `
-resourceGroupName $ResourceGroupName -slotName $SlotName
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}

# Publish azure webApp url
Write-Host (Get-LocalizedString -Key "Webapp successfully published at Url : {0}" -ArgumentList $azureWebsitePublishURL)

# Set ouput vairable with azureWebsitePublishUrl
if(-not [string]::IsNullOrEmpty($WebAppUri))
{

if( [string]::IsNullOrEmpty($azureWebsitePublishURL))
{
Throw (Get-LocalizedString -Key "Unable to retrieve webapp publish url for webapp : '{0}'." -ArgumentList $webAppName)
}

Write-Host "##vso[task.setvariable variable=$WebAppUri;]$azureWebsitePublishURL"
}

Write-Verbose "Completed AzureRM WebApp Deployment Task"
Loading