Automating Azure deployment with Windows PowerShell
One of the pre-requisites I had for using Azure was that it could be deployed automatically as part of an integration and deployment process.
A quick scan through the Labs in the Windows Azure Platform Kit, which by the way have been an excellent resource so far, gave me Windows PowerShell as the option.
It proved pretty easy to get up and running. Here's my quick start guide.
Download PowerShell and the Azure CmdLets
If you're running anything other than Windows 7 you'll need to download and install PowerShell from here: http://support.microsoft.com/kb/926139.
Next you need the Azure Power Shell CmdLets available here: http://code.msdn.microsoft.com/azurecmdlets. Once you've downloaded and extracted the scripts, you then just need to execute the startHere script to get them installed.
Script the deployment
I won't regurgitate the full instructions as it's best to read through the Lab Deploying and Monitoring Applications in Windows Azure - Exercise 2 - Using PowerShell to Manage Windows Azure Applications in the Windows Azure Platform Kit.
Is also worth checking reading about them on Ryan Dunn's blog: http://dunnry.com/blog/WindowsAzureServiceManagementCmdLets.aspx
This is the script I ended up with
Add-PSSnapin AzureManagementToolsSnapIn
$service = "<service_name>"
$sub = "<subscription_id>"
$cert = Get-Item cert:\CurrentUser\My\"<thumbnail>"
$package = ""<package_file>""
$config = ""<config_file>""
if ($args.Length -eq 1)
{
$label = $args[0]
}
else
{
$label = "unknown"
}
/*
If the staging deployment doesn't exist, this will create it
*/
/*
New-Deployment -serviceName $service -subscriptionId $sub -certificate $cert -slot staging -package $package -configuration $config -label $label |
Get-OperationStatus –WaitToComplete
*/
/*
Upgrade the current staging deployment
*/
Get-HostedService -serviceName $service -subscriptionId $sub -certificate $cert |
Get-Deployment -slot staging |
Set-Deployment -package $package -label $label |
Get-OperationStatus –WaitToComplete
/*
Set to running
*/
Get-HostedService -serviceName $service -subscriptionId $sub -certificate $cert |
Get-Deployment -slot staging |
Set-DeploymentStatus running |
Get-OperationStatus –WaitToComplete
/*
Move staging to production, this will actually swap them over
*/
Get-HostedService -serviceName $service -subscriptionId $sub -certificate $cert |
Get-Deployment -slot staging |
Move-Deployment |
Get-OperationStatus –WaitToCompleteYou'll note that I have two options for the deployment New-Deployment and Set-Deployment.
When you start off, Azure has nothing in either of the staging or production slots so you have to use New-Deployment.
When you create the first staging deployment then move to production, this empties the staging slot.
When you run through the process again, with New-Deployment, the Move swaps the deployments over so staging then has the first deployment and production the second.
At this point you can then start upgrading the staging deployment with Set-Deployment
You can of course avoid all this by just doing two manual deploys before automating it.