More on Publishing to Azure and VIP

Last week I did some more research into publishing to Azure from MSBuild.  One of the requests we received is to keep virtual IP address (VIP) the same throughout publishing.  I blogged on the subject before here.

This code worked just fine, but it was replacing virtual IP every time when deployment was deleted and re-created.  In order to keep the IP the same, you must instead update the deployment.  To do that I needed to switch to use a different cmdlet – Update-Deployment. Just on the side note, in the newer version of the cmdlets this one was renamed to Update-AzureDeployment.  So, in the script below I made this change, while handling the user case of very first deployment at the same time.  First thing I do below is to get a handle to a hosted service, then I check to see if it exists (by checking its status, then fork out to to execution paths: first one is using Update-Deployment, second one is using New-Deployment.

Why keeping the same VIP is important?  Realistically speaking it does not matter.  In our case it does because we have to integrate with locally installed services, and we wanted to setup firewall to only allow communication from our Azure app.  In this specific use case keeping the VIP the same would allow us to setup firewall rules ones, and not adjust them after every deployment.

Thanks and here is the script.

 

$pathToFolderWithPackage = $args[0]
$packagename = $args[1]
$serviceconfig = $args[2]
$servicename = $args[3]
$thumbprint = $args[4]

 
$cert = Get-Item cert:CurrentUserMy$thumbprint
$sub = $args[5]
$slot = $args[6]
$storage = $args[7]
$package = join-path $pathToFolderWithPackage $packageName
$config = join-path $pathToFolderWithPackage $serviceconfig
$a = Get-Date
$buildLabel = $a.ToShortDateString() + "-" + $a.ToShortTimeString()
/* Exit code is to control MSBuild - 1 will stop the build*/
$exitCode=0
Try
{
    $ErrorActionPreference = "stop"

    Add-PSSnapin WAPPSCmdlets

    $hostedService = Get-HostedService $servicename -Certificate $cert -SubscriptionId $sub | Get-Deployment -Slot $slot

    if ($hostedService.Status -ne $null)
    {
                 Get-HostedService -ServiceName $servicename -Certificate $cert -SubscriptionId $sub | 
                         Update-Deployment -Slot $slot -Package $package -Configuration $config -Label $buildLabel -ServiceName $servicename -StorageServiceName $storage |
                         Get-OperationStatus -WaitToComplete

                 Get-HostedService -ServiceName $servicename -Certificate $cert -SubscriptionId $sub | 
                         Get-Deployment -Slot $slot | 
                         Set-DeploymentStatus 'Running' | 
                         Get-OperationStatus -WaitToComplete
    }
        else
        {
                 Get-HostedService -ServiceName $servicename -Certificate $cert -SubscriptionId $sub | 
                         New-Deployment -Slot $slot -Package $package -Configuration $config -Label $buildLabel -ServiceName $servicename -StorageServiceName $storage | 
                         Get-OperationStatus -WaitToComplete

                 Get-HostedService -ServiceName $servicename -Certificate $cert -SubscriptionId $sub | 
                         Get-Deployment -Slot $slot | 
                         Set-DeploymentStatus 'Running' | 
                         Get-OperationStatus -WaitToComplete
        }

}    
Catch
{
    $exitCode=-1
    write-host $error
}
exit $exitCode

Leave a Reply

Your email address will not be published. Required fields are marked *