Tales About Working With The Azure Service Management API – Chapter II
After I elaborated a bit on one of the base requirements to work with the Azure Service Management API at all which is enabling a valid certificate to authenticate your calls to the API I want today share some thoughts about how to automate tasks related with Azure deployments. As already pointed out in Chapter I you probably want to use Windows Powershell together with the Windows Azure Service Management CmdLets which will make your life in interacting with the Windows Azure Fabric from a shell environment much easier. The commandlets support most of the relevant tasks to do deployments and undeployments.
So in my example here I will show how those commandlets can be used in scripts to optimize the consumption of Windows Azure compute services. As you might know the compute service is billed based on service hours which basically means that as long as a service is deployed it will produce cost although the service may not be used at all during certain times. And as there may be quite some solutions where there are known time windows in which the service isn’t used (e.g. non busines hours) it may come in handy if there would be the possibility to undeploy and deploy the services based on their availability needs.
To have this work the only thing needed is two Windows Powershell scripts where on does the deployment and the other one the undeployment. The script for undeploying a service could look like the following:
<#
.Synopsis
Will suspend and undeploy a Windows Azure Service
.Description
Will suspend and undeploy a Windows Azure Service
.Parameter servicename
The name of the Azure service that holds the deployment
.Parameter thumbprint
The thumbprint of a locally installed and valid certificate for accessing
the Windows Azure Management Service
.Parameter subscriptionid
The ID for the subscription of the Azure Account hosting the respective service
.Parameter slot
The deployment slot in which the package to be undeployed is physically situated
.Example
undeployAzureService.ps1 -thumbprint "5643060E8F0029FB8C6DB76AAE905C7FA697" -servicename "MyService" -subscriptionid "00000000-0000-0000-0000-000000000000" -slot "Production"
#>
param([PARAMETER(Mandatory=$true)][string]$servicename, [PARAMETER(Mandatory=$true)][string]$thumbprint, [PARAMETER(Mandatory=$true)][string]$subscriptionid, [PARAMETER(Mandatory=$true)][string]$slot)
if(!($snapin = Get-PSSnapin AzureManagementToolsSnapIn -erroraction silentlycontinue))
{
Write-Host "Adding ManagementTools SnapIn...please wait"
Add-PSSnapin AzureManagementToolsSnapIn
}
$cert = Get-Item cert:\CurrentUser\My\$thumbprint
$service = Get-HostedService $servicename -Certificate $cert -SubscriptionId $subscriptionid
if($service)
{
$deployment = Get-Deployment -Slot $slot -ServiceName $servicename -SubscriptionId $subscriptionid -Certificate $cert -erroraction silentlycontinue
if($deployment.Name)
{
Write-Host "Suspending deployment...please wait"
Get-Deployment -Slot $slot -ServiceName $servicename -SubscriptionId $subscriptionid -Certificate $cert |
Set-DeploymentStatus 'Suspended' |
Get-OperationStatus -WaitToComplete
Write-Host "Removing deployment...please wait"
Get-Deployment -Slot $slot -ServiceName $servicename -SubscriptionId $subscriptionid -Certificate $cert |
Remove-Deployment |
Get-OperationStatus -WaitToComplete
} else
{
Write-Host "No deployment found for the service named $servicename"
}
} else
{
Write-Host "No service found with the name $servicename"
}
After creating this script you then can create a scheduled job using the Windows Scheduler. When creating the command line for the scheduled task you have to be carefull about two things:
- Set the execution policy for powershell so that it allows to run scripts. Depending on your environment and security policies you should set the execution policy to either “AllSigned”, “RemoteSigned” or “Unrestricted”. For more information consult the powershell documentation. Example: Set-ExecutionPolicy Unrestricted
- If your path to the powershell script which you need to pass over to powershell as an argument does contain blanks you need to use double quotes surrounding the whole argument including the ampersand (which is the call operator in Windows Powershell) and inside enclose the path information with single quotes. So a correct statement would look like this: powershell.exe -command "& 'C:\My Scripts\undeployAzureService.ps1'" …
To create the scheduled task using the wizard or use a XML template like the one below, insert your data and import it into the scheduler.
- <?xml version="1.0" encoding="utf-16"?>
- <Task version="1.3" xmlns="https://schemas.microsoft.com/windows/2004/02/mit/task">
- <RegistrationInfo>
- <Date>2010-02-12T13:44:02.910461</Date>
- <Author>[userid]</Author>
- <Description>Timed undeployment of Azure Service during non business hours to optimize Azure consumption of service compute hours.</Description>
- </RegistrationInfo>
- <Triggers>
- <CalendarTrigger>
- <StartBoundary>2010-02-12T19:00:00Z</StartBoundary>
- <Enabled>true</Enabled>
- <ScheduleByDay>
- <DaysInterval>1</DaysInterval>
- </ScheduleByDay>
- </CalendarTrigger>
- </Triggers>
- <Principals>
- <Principal id="Author">
- <UserId>[userid</UserId>
- <LogonType>Password</LogonType>
- <RunLevel>LeastPrivilege</RunLevel>
- </Principal>
- </Principals>
- <Settings>
- <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
- <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
- <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
- <AllowHardTerminate>true</AllowHardTerminate>
- <StartWhenAvailable>true</StartWhenAvailable>
- <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
- <IdleSettings>
- <StopOnIdleEnd>true</StopOnIdleEnd>
- <RestartOnIdle>false</RestartOnIdle>
- </IdleSettings>
- <AllowStartOnDemand>true</AllowStartOnDemand>
- <Enabled>false</Enabled>
- <Hidden>false</Hidden>
- <RunOnlyIfIdle>false</RunOnlyIfIdle>
- <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
- <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
- <WakeToRun>false</WakeToRun>
- <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
- <Priority>7</Priority>
- </Settings>
- <Actions Context="Author">
- <Exec>
- <Command>powershell</Command>
- <Arguments>-command "& 'C:\my scripts\undeployAzureService.ps1'" -servicename "MyService" -thumbprint "5643060E8F0029FB8C6DB76AAE905C7FA697" -subscriptionid "00000000-0000-0000-0000-000000000000" -slot "Production" > log.txt</Arguments>
- <WorkingDirectory>C:\deployments\logs</WorkingDirectory>
- </Exec>
- </Actions>
- </Task>
Then do the same for the deployment process. A respective Windows Powershell script could look like this:
<#
.Synopsis
Will deploy and start a Windows Azure Service
.Description
Will deploy and start a Windows Azure Service
.Parameter servicename
The name of the Azure service that will hold the deployment
.Parameter thumbprint
The thumbprint of a locally installed and valid certificate for accessing
the Windows Azure Management Service
.Parameter subscriptionid
The ID for the subscription of the Azure Account hosting the respective service
.Parameter slot
The deployment slot in which the package shall be deployed <Production | Staging>
.Parameter storageservicename
The storage account that can be used to upload the deployment package
.Parameter package
The full qualified path to the deployment package
.Parameter config
The full qualified path to the configuration file for this deployment
.Example
deployAzureService.ps1 -servicename "MyService" -thumbprint "5643060E8F0029FB8C6DB76AAE905C7FA697" -subscriptionid "00000000-0000-0000-0000-000000000000" -slot "Production" -storageservicename "StorageService" -package "C:\deployment\HelloWorld.cspkg" -config "C:\deployments\ServiceConfiguration.cscfg" -label "MyFirstDeployment"
#>
param([PARAMETER(Mandatory=$true)][string]$servicename, [PARAMETER(Mandatory=$true)][string]$thumbprint, [PARAMETER(Mandatory=$true)][string]$subscriptionid, [PARAMETER(Mandatory=$true)][string]$slot, [PARAMETER(Mandatory=$true)][string]$storageservicename, [PARAMETER(Mandatory=$true)][string]$package, [PARAMETER(Mandatory=$true)][string]$config, [PARAMETER(Mandatory=$true)][string]$label)
if(!($snapin = Get-PSSnapin AzureManagementToolsSnapIn -erroraction silentlycontinue))
{
Write-Host "Adding ManagementTools SnapIn...please wait"
Add-PSSnapin AzureManagementToolsSnapIn
}
$cert = Get-Item cert:\CurrentUser\My\$thumbprint
$service = Get-HostedService $servicename -Certificate $cert -SubscriptionId $subscriptionid
if($service)
{
$deployment = Get-Deployment -Slot $slot -ServiceName $servicename -SubscriptionId $subscriptionid -Certificate $cert -erroraction silentlycontinue
$name = $deployment.Name
if(!$deployment.Name)
{
Write-Host "Creating new deployment...please wait"
New-Deployment -Slot $slot -ServiceName $servicename -SubscriptionId $subscriptionid -Certificate $cert -Package $package -Configuration $config -Label $label -StorageServiceName $storageservicename |
Get-OperationStatus -WaitToComplete
Write-Host "Starting the new deployment...please wait"
Get-Deployment -Slot $slot -ServiceName $servicename -SubscriptionId $subscriptionid -Certificate $cert |
Set-DeploymentStatus "Running" |
Get-OperationStatus -WaitToComplete
} else
{
Write-Host "Service $servicename already has an active deployment with the name $name...please remove the active deployment first"
}
} else
{
Write-Host "No service found with the name $servicename"
}
And the template for the scheduler could look like this:
- <?xml version="1.0" encoding="UTF-16"?>
- <Task version="1.2" xmlns="https://schemas.microsoft.com/windows/2004/02/mit/task">
- <RegistrationInfo>
- <Date>2010-02-15T09:50:14.2255756</Date>
- <Author>[userid]</Author>
- </RegistrationInfo>
- <Triggers>
- <CalendarTrigger>
- <StartBoundary>2010-02-15T07:00:00</StartBoundary>
- <Enabled>true</Enabled>
- <ScheduleByDay>
- <DaysInterval>1</DaysInterval>
- </ScheduleByDay>
- </CalendarTrigger>
- </Triggers>
- <Principals>
- <Principal id="Author">
- <UserId>[userid]</UserId>
- <LogonType>Password</LogonType>
- <RunLevel>HighestAvailable</RunLevel>
- </Principal>
- </Principals>
- <Settings>
- <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
- <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
- <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
- <AllowHardTerminate>true</AllowHardTerminate>
- <StartWhenAvailable>false</StartWhenAvailable>
- <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
- <IdleSettings>
- <StopOnIdleEnd>true</StopOnIdleEnd>
- <RestartOnIdle>false</RestartOnIdle>
- </IdleSettings>
- <AllowStartOnDemand>true</AllowStartOnDemand>
- <Enabled>true</Enabled>
- <Hidden>false</Hidden>
- <RunOnlyIfIdle>false</RunOnlyIfIdle>
- <WakeToRun>false</WakeToRun>
- <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
- <Priority>7</Priority>
- </Settings>
- <Actions Context="Author">
- <Exec>
- <Command>powershell</Command>
- <Arguments>-command "& 'C:\My Scripts\deployAzureService.ps1'" -servicename "MyService" -thumbprint "5643060E8F0029FB8C6DB76AAE905C7FA697" -subscriptionid "00000000-0000-0000-0000-000000000000" -slot "Production" -storageservicename "mystorageservice" -package "'C:\deployments\packages\HelloWorld.cspkg'" -config "'C:\deployments\packages\ServiceConfiguration.cscfg'" -label "MyFirstDeployment" > deployment.log</Arguments>
- <WorkingDirectory>C:\deployments\packages\logs</WorkingDirectory>
- </Exec>
- </Actions>
- </Task>
This is all to automate your deployment tasks. One thing however is to remember here. As the New-Deployment commandlet uploads your deployment package into the blob storage of the specified Azure Storage account you should check your blob storage from time and do a clean up otherwise this will also produce unnecessary cost.
Comments
- Anonymous
October 04, 2010
Could you provide me some sample project of how you did?Thanks.you can send it to syoo022@gmail.com. thanks.