Windows Azure

# How To Clean up old Windows Azure diagnostics

Last week I blogged about how to easily enable Windows Azure diagnostics remotely using PowerShell. Because of this, and the simple fact you enabled diagnostics in the first place means that you now have a small collection of folders building up in the wad-* folders. Every time you deploy, you will see a new folder created for that deployment. In this post I’m going to show you how you can write a script to remove the old deployments.

# What the script will do

The script will:

1. Find out the current deployment id.
2. Connect to blob storage
3. List all of the blobs that don’t have the deployment id in the uri.
4. Delete those blobs.

Note: This assumes you have 1 storage account for each deployment. If you use the same storage account for diagnostics from multiple apps you need to change the script. Also Why?

# Setup

This is the same as the previous post, so if you already have done this, you can skip to the next section.

First you need to make sure you have a management certificate created and installed.

Once you have that, you need to download the PowerShell cmdlets from codeplex.

Next figure out what the thumbprint of your certificate is. You can see this in the Windows Azure management portal when you click your certificate. While you are there, make a note of your subscription Id, Storage Account Name and Key and the service name.

# Defining the service and storage variables

# TODO: Cert thumbprint of a certificate already installed into the Windows Azure Portal
$thumb = "ENTERTHUMBHERE"$cert = get-item cert:\CurrentUser\My\$thumb # TODO: Subscription Id$subid = "ENTERSUBIDHERE"
# TODO: Storage Account Name
$SAN = "ENTERSTORAGEACCOUNTNAMEHERE" # TODO: Storage Account Key$SAK = "ENTERSTORAGEACCOUNTKEYHERE"
# TODO: Service Name

# Connecting to blob storage

First thing you need to do is to make sure the assemblies for storage are loaded. You can do this using

add-type -Path 'C:\Program Files\Windows Azure SDK\v1.5\ref\Microsoft.WindowsAzure.StorageClient.dll'

To access storage, we will use the CloudBlobClient class from the storage client library. This requires some credentials:

$creds = New-Object Microsoft.WindowsAzure.StorageCredentialsAccountAndKey($SAN, $SAK)$client = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient("https://$SAN.blob.core.windows.net",$creds)

# Grabbing a list of blobs & deleting them

One of the great things we can do is grab a list of blobs in a container using a “flat blob listing” basically, it will list everything in the container, including all the sub folders. To do this, you need to define a BlobRequestOptions object:

$bro = New-Object Microsoft.WindowsAzure.StorageClient.BlobRequestOptions$bro.UseFlatBlobListing = $true You can grab a list of blobs for a container by using the ListBlobs method of a directory reference, which you need to grab for the container: $wadcontainer = $client.GetBlobDirectoryReference("wad-control-container")$todelete = $wadcontainer.ListBlobs($bro) | where {$_.Uri -notmatch$did}

To delete the blob, you can simply call the Delete method.

$todelete | foreach{$_.Delete() }

Awesome.

# Full Script

Here is the full script that also handles multiple wad-* containers:

# TODO: Cert thumbprint of a certificate already installed into the Windows Azure Portal
$thumb = "ENTERTHUMBHERE"$cert = get-item cert:\CurrentUser\My\$thumb # TODO: Subscription Id$subid = "ENTERSUBSCRIPTIONIDHERE"
# TODO: Storage Account Name
$SAN = "ENTERSTORAGEACCOUNTNAMEHERE" # TODO: Storage Account Key$SAK = "ENTERSTORAGEACCOUNTKEYHERE"
# TODO: Service Name
$serviceName = "ENTERSERVICENAMEHERE" # deployment Id$did = (Get-Deployment -ServiceName $serviceName -Certificate$cert -SubscriptionId $subid -Slot Production).DeploymentId write-host "current deployment " +$did

$creds = New-Object Microsoft.WindowsAzure.StorageCredentialsAccountAndKey($SAN, $SAK)$client = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient("https://$SAN.blob.core.windows.net",$creds)

$bro = New-Object Microsoft.WindowsAzure.StorageClient.BlobRequestOptions$bro.UseFlatBlobListing = $true$containersToProcess = @("wad-control-container","wad-iis-logfiles")
$containersToProcess | foreach {$wadcontainer = $client.GetBlobDirectoryReference($_)

$todelete =$wadcontainer.ListBlobs($bro) | where {$_.Uri -notmatch $did}$todelete | select Uri | fl | out-host

$todelete | foreach {$_.Delete() }
}

Tip: If you want to see what it does before committing, simply comment out the $todelete | foreach{$_.Delete() } statement.

THIS POSTING IS PROVIDED “AS IS” WITH NO WARRANTIES, AND CONFERS NO RIGHTS, UNLESS YOU HAVE A NOTE FROM MY MUM