JumpStart # 31– Create and deploy new Azure Cloud Service using Package (CSPKG) generated by Azure PowerShell

In this short tutorial, we are going to learn how to create new Azure Cloud Service deployments through Windows Azure PowerShell cmdlets. Deployment though PowerShell can be achieved by first creating a package (CSPKG) for the Visual studio project which is intended to be deployed. As a second step we upload the package to a Windows Azure Blob Storage. Finally we create a new Azure Cloud Service and deploy the uploaded package to it.

As a prerequisite to this tutorial, please follow the steps outlined in my last tutorial, where we create a package (CSPKG) – http://www.intstrings.com/ramivemula/articles/jumpstart-30-create-azure-cloud-service-package-cspkg-of-visual-studio-2013-project-solution/

Once the package is ready, we create a new Azure Cloud Storage and a container in it. PowerShell script for creating Storage account and a container in it is as follows.

 <# ################################## Create Blob Storage ################################## #>

function createBlobStorage($storageName, $storageLocation)
{
    Write-Host "$(Get-Date –f $timeStampFormat) - Storage creation - Started " -foregroundcolor "red"

    $blobStorage = Get-AzureStorageAccount -StorageAccountName $storageName -ErrorVariable errContainer -Verbose:$false -ErrorAction "SilentlyContinue"

    if($blobStorage -eq $null)
    {
         New-AzureStorageAccount -StorageAccountName $storageName -Location $storageLocation -Verbose:$false | Out-Null
    }


    # Check for Storage Up and running
    $storageHealth = (Get-AzureStorageAccount -StorageAccountName $storageName -Verbose:$false)

    While($storageHealth.StorageAccountStatus -ne "Created") 
    {
        Start-Sleep -s 30
        $storageHealth = (Get-AzureStorageAccount -StorageAccountName $storageName -Verbose:$false)
    }


    # Set current storage account as default to present subscription
    $subscriptionName = (Get-AzureSubscription).SubscriptionName
    Set-AzureSubscription -CurrentStorageAccountName $storageName -SubscriptionName $subscriptionName

    Write-Host "$(Get-Date –f $timeStampFormat) - Storage creation - Completed " -foregroundcolor "red"
} 

 

 <# ############################ Create Blob Container in Storage ############################ #>

function createBlobContainer($containerName)
{
    Write-Host "$(Get-Date –f $timeStampFormat) - Storage container creation - Started " -foregroundcolor "red"

    $blobContainer = Get-AzureStorageContainer -Name $containerName -ErrorVariable errContainer -Verbose:$false -ErrorAction "SilentlyContinue"

    if($blobContainer -eq $null)
    {
        New-AzureStorageContainer -Name $containerName -Verbose:$false | Out-Null
    }

    Write-Host "$(Get-Date –f $timeStampFormat) - Storage container creation - Completed " -foregroundcolor "red"
} 

 

As the next step, we upload the generate package to the Cloud Storage Container. PowerShell script to upload package to Cloud Container is as follows.

 <# ############################ Upload package to blob storage and get its URI ############################ #>

function uploadBlobToContainer($file, $containerName, $blobName)
{
    Write-Host "$(Get-Date –f $timeStampFormat) - Package upload - Started " -foregroundcolor "red"

    Set-AzureStorageBlobContent -File $file -Container $containerName -Blob $blobName -Force -Verbose:$false | Out-Null

    return (Get-AzureStorageBlob -blob $blobName -Container $containerName).ICloudBlob.uri.AbsoluteUri

    Write-Host "$(Get-Date –f $timeStampFormat) - Package upload - Completed " -foregroundcolor "red"
} 

 

To check and create new Azure Cloud Service, please check below PowerShell script.

  <# ############################ Create Cloud Services ############################ #>

function createAzureService($serviceName, $serviceLocation)
{
    Write-Host "$(Get-Date –f $timeStampFormat) - Cloud service creation - Started " -foregroundcolor "red"

    # check for existence
    $service = Get-AzureService -ServiceName $serviceName -ErrorVariable errPrimaryService -Verbose:$false -ErrorAction "SilentlyContinue"

    if ($service -ne $null)
    {
        Write-Host "$(Get-Date –f $timeStampFormat) - Error occurred - $($serviceName) Service already exists " -foregroundcolor "red"
        return
    }

    # create new service
    New-AzureService -ServiceName $serviceName -Location $serviceLocation -ErrorVariable errPrimaryService -Verbose:$false 
                     -ErrorAction "SilentlyContinue" | Out-Null

    if ($errPrimaryService[0] -ne $null)
    {
        Write-Host "$(Get-Date –f $timeStampFormat) - Error occurred - $($serviceName) Service failed - $errPrimaryService[0] " -foregroundcolor "red"
        return
    }

    Write-Host "$(Get-Date –f $timeStampFormat) - Cloud service creation - Completed " -foregroundcolor "red"
} 

 

Finally to create a new deployment, use the following script.

 <# ############################ ProductionDeployment to Service ############################ #>

function createProductionDeployment($packageLocation, $configLocation, $serviceName)
{
    Write-Host "$(Get-Date –f $timeStampFormat) - Cloud service deployment - Started " -foregroundcolor "red"

    # check for deployment existence
    $serviceDeployment = (Get-AzureDeployment -ServiceName $serviceName -Slot Production -ErrorAction "SilentlyContinue"  -Verbose:$false)

    if ($serviceDeployment -ne $null)
    {
        Write-Host "$(Get-Date –f $timeStampFormat) - Error occurred - $($serviceName) deployment already exists " -foregroundcolor "red"
        return
    }

    # create new deployment
    New-AzureDeployment -Slot "Production" -Package $packageLocation -Configuration $configLocation -label $serviceName -ServiceName 
                        $serviceName -Name $serviceName -ErrorVariable errPrimaryDeployment -Verbose:$false | Out-Null

    if ($errPrimaryDeployment[0] -ne $null)
    {
        Write-Host "$(Get-Date –f $timeStampFormat) - Error occurred - Creating new deployment in $($serviceName) service" -foregroundcolor "red"
        return
    }

    Write-Host "$(Get-Date –f $timeStampFormat) - Cloud service deployment - Completed " -foregroundcolor "red"
} 

Finally to check the health of the created service, use the following script.

 
<# ############################ Check health of Cloud Service Deployment ############################ #>

function checkCloudServiceHealth($serviceName)
{
    Write-Host "$(Get-Date –f $timeStampFormat) - Cloud service deployment - Creating VM " -foregroundcolor "red"

    $serviceHealth = (Get-AzureDeployment -ServiceName $serviceName -Slot Production  -Verbose:$false)


    While($serviceHealth.RoleInstanceList[0].InstanceStatus -ne "ReadyRole") 
    {
        Write-Host "." -NoNewLine -foregroundcolor "yellow"
        Start-Sleep -s 30
        $serviceHealth = (Get-AzureDeployment -ServiceName $serviceName -Slot Production  -Verbose:$false)

        if($serviceHealth.RoleInstanceList[0].InstanceStatus -eq "ReadyRole") 
        {
            Write-Host "." -foregroundcolor "yellow"
        }
    }

    Write-Host "$(Get-Date –f $timeStampFormat) - Cloud service deployment - Up and running " -foregroundcolor "red"

} 

 

Place all the above scripts in a file and save it in PS1 format. In my case I am calling this script as “Create-Deployment.ps1”. now lets create a master script to use all the functions which are created above and make the deployment.

Point to be remembered: I strongly encourage to read the previous blog post on Creating a package – http://www.intstrings.com/ramivemula/articles/jumpstart-30-create-azure-cloud-service-package-cspkg-of-visual-studio-2013-project-solution/. That tutorial not only going to show how to create package, but also sets up the initial context for script execution, for example setting the current subscription to the PowerShell execution.

  <# ################################## Create new Azure Cloud Service Deployment ################################## #>

function createCloudServiceDeployment($storageName, $storageLocation, $storageContainer, $serviceName, $serviceLocation, $packageLocation, 
                                      $configLocation)
{
    # Create and upload package to Blob Storage
    createBlobStorage -storageName $storageName -storageLocation $storageLocation
    createBlobContainer -containerName $storageContainer
    $blobUri = uploadBlobToContainer -file $packageLocation -containerName $storageContainer -blobName "NewPackage"

    # check cloud service existence and create it
    createAzureService -serviceName $serviceName -serviceLocation $serviceLocation

    # create new production deployment
    createProductionDeployment -packageLocation $packageLocation -configLocation $configLocation -serviceName $serviceName

    # Check Cloud Service health
    checkCloudServiceHealth -serviceName $serviceName
}  

Execute the command in the following way.

image

Now we can browse the application in browser using the <servicename>.cloudapp.net. This completes our present tutorial. In next set of tutorial, we are going to see on how to upgrade a deployment along with its number instances.

Happy Coding!!!

You may also like...

One Pingback/Trackback

  • Alex

    Brilliant tutorial.
    Thanks.

  • Alex

    Hi Rami,

    A couple of points. Your process uploads the package file to blob storage, however, then doesn’t use that uploaded file during the deployment, you just upload the local file. So your instructions actually lead to the package file being uploaded twice. Not good.

    You also seem to have some javascript that is modifying the clipboard upon copying, to include a link to your website. I have no problem at all with you asking for credit for your code, but this is a highly irritating practice that means anyone wanting to learn from your post, has to spend an inordinate amount of time reformatting copy/pasted code snippets.

    Thanks for the effort in writing about this topic, it seems to be something rarely written about.

    Alex

  • RamiVemula

    @Alex –

    For your first question, Package location cannot be a local file path in previous versions of Azure Cmdlets. Check this post – http://stackoverflow.com/questions/14167990/use-new-azuredeployment-to-deploy-cloud-service-but-error-occurs-the-http-vers. So thats the reason why I have use Blob storage to upload the package, but fortunately even the local path worked in the tutorial. I will update it sooner with right explanation.

    For your second question, Yes Code copying is bit of problem as of now. I am planning to give new look and feel for my site with altogether new theme. That should solve the problem. I use VSPaste for Syntax highlighting, I will also check if there is a new version of it. If nothing works, then I will make the source code available through download.

    Thanks for you Suggestions/corrections Alex. I appreciate your constructive feedback.

  • Pingback: Can I somehow do an auto swap between staging and production in azure cloud services? | Zebell Answers()

  • ramiramilu

    Can you share more details on your comment? What script you want an what would it serve?