Created
November 1, 2011 05:11
-
-
Save Romiko/1329978 to your computer and use it in GitHub Desktop.
Windows Azure Deployment Script - UAT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -version 2.0 | |
param ( | |
[parameter(Mandatory=$true)] [string]$AzureAccountName, | |
[parameter(Mandatory=$true)] [string]$AzureServiceName, | |
[parameter(Mandatory=$true)] [string]$AzureDeploymentSlot, | |
[parameter(Mandatory=$true)] [string]$AzureAccountKey, | |
[parameter(Mandatory=$true)] [string]$AzureSubscriptionId, | |
[parameter(Mandatory=$true)] [string]$AzureCertificateThumbprint, | |
[parameter(Mandatory=$true)] [string]$PackageSource, | |
[parameter(Mandatory=$true)] [string]$ConfigSource, | |
[parameter(Mandatory=$true)] [string]$DeploymentName, | |
[parameter(Mandatory=$true)] [string]$Neo4jZippedBinaryFileHttpSource, | |
[parameter(Mandatory=$true)] [string]$Neo4jBlobName | |
) | |
$ErrorActionPreference = "Stop" | |
if ((Get-PSSnapin -Registered -Name AzureManagementCmdletsSnapIn -ErrorAction SilentlyContinue) -eq $null) | |
{ | |
throw "AzureManagementCmdletsSnapIn missing. Install them from Https://www.cerebrata.com/Products/AzureManagementCmdlets/Download.aspx" | |
} | |
Add-PSSnapin AzureManagementCmdletsSnapIn -ErrorAction SilentlyContinue | |
function AddBlobContainerIfNotExists ($blobContainerName) | |
{ | |
Write-Verbose "Finding blob container $blobContainerName" | |
$containers = Get-BlobContainer -AccountName $AzureAccountName -AccountKey $AzureAccountKey | |
$deploymentsContainer = $containers | Where-Object { $_.BlobContainerName -eq $blobContainerName } | |
if ($deploymentsContainer -eq $null) | |
{ | |
Write-Verbose "Container $blobContainerName doesn't exist, creating it" | |
New-BlobContainer $blobContainerName -AccountName $AzureAccountName -AccountKey $AzureAccountKey | |
} | |
else | |
{ | |
Write-Verbose "Found blob container $blobContainerName" | |
} | |
} | |
function UploadBlobIfNotExists{param ([string]$container, [string]$blobName, [string]$fileSource) | |
Write-Verbose "Finding blob $container\$blobName" | |
$blob = Get-Blob -BlobContainerName $container -BlobPrefix $blobName -AccountName $AzureAccountName -AccountKey $AzureAccountKey | |
if ($blob -eq $null) | |
{ | |
Write-Verbose "Uploading blob $blobName to $container/$blobName" | |
Import-File -File $fileSource -BlobName $blobName -BlobContainerName $container -AccountName $AzureAccountName -AccountKey $AzureAccountKey | |
} | |
else | |
{ | |
Write-Verbose "Found blob $container\$blobName" | |
} | |
} | |
function CheckIfDeploymentIsDeleted | |
{ | |
$triesElapsed = 0 | |
$maximumRetries = 10 | |
$waitInterval = [System.TimeSpan]::FromSeconds(30) | |
Do | |
{ | |
$triesElapsed+=1 | |
[System.Threading.Thread]::Sleep($waitInterval) | |
Write-Verbose "Checking if deployment is deleted, current retry is $triesElapsed/$maximumRetries" | |
$deploymentInstance = Get-Deployment ` | |
-ServiceName $AzureServiceName ` | |
-Slot $AzureDeploymentSlot ` | |
-SubscriptionId $AzureSubscriptionId ` | |
-Certificate $certificate ` | |
-ErrorAction SilentlyContinue | |
if($deploymentInstance -eq $null) | |
{ | |
Write-Verbose "Deployment is now deleted" | |
break | |
} | |
if($triesElapsed -ge $maximumRetries) | |
{ | |
throw "Checking if deployment deleted has been running longer than 5 minutes, it seems the delployment is not deleting, giving up this step." | |
} | |
} | |
While($triesElapsed -le $maximumRetries) | |
} | |
function WaitUntilAllRoleInstancesAreReady | |
{ | |
$triesElapsed = 0 | |
$maximumRetries = 60 | |
$waitInterval = [System.TimeSpan]::FromSeconds(60) | |
Do | |
{ | |
$triesElapsed+=1 | |
[System.Threading.Thread]::Sleep($waitInterval) | |
Write-Verbose "Checking if all role instances are ready, current retry is $triesElapsed/$maximumRetries" | |
$roleInstances = Get-RoleInstanceStatus ` | |
-ServiceName $AzureServiceName ` | |
-Slot $AzureDeploymentSlot ` | |
-SubscriptionId $AzureSubscriptionId ` | |
-Certificate $certificate ` | |
-ErrorAction SilentlyContinue | |
$roleInstancesThatAreNotReady = $roleInstances | Where-Object { $_.InstanceStatus -ne "Ready" } | |
if ($roleInstances -ne $null -and | |
$roleInstancesThatAreNotReady -eq $null) | |
{ | |
Write-Verbose "All role instances are now ready" | |
break | |
} | |
if ($triesElapsed -ge $maximumRetries) | |
{ | |
throw "Checking if all roles instances are ready for more than one hour, giving up..." | |
} | |
} | |
While($triesElapsed -le $maximumRetries) | |
} | |
function DownloadNeo4jBinaryZipFileAndUploadToBlobStorageIfNotExists{param ([string]$blobContainerName, [string]$blobName, [string]$HttpSourceFile) | |
Write-Verbose "Finding blob $blobContainerName\$blobName" | |
$blobs = Get-Blob -BlobContainerName $blobContainerName -ListAll -AccountName $AzureAccountName -AccountKey $AzureAccountKey | |
$blob = $blobs | findstr $blobName | |
if ($blob -eq $null) | |
{ | |
Write-Verbose "Neo4j binary does not exist in blob storage. " | |
Write-Verbose "Downloading file $HttpSourceFile..." | |
$temporaryneo4jFile = [System.IO.Path]::GetTempFileName() | |
$WebClient = New-Object -TypeName System.Net.WebClient | |
$WebClient.DownloadFile($HttpSourceFile, $temporaryneo4jFile) | |
UploadBlobIfNotExists $blobContainerName $blobName $temporaryneo4jFile | |
} | |
} | |
Write-Verbose "Retrieving management certificate" | |
$certificate = Get-ChildItem -Path "cert:\CurrentUser\My\$AzureCertificateThumbprint" -ErrorAction SilentlyContinue | |
if ($certificate -eq $null) | |
{ | |
throw "Couldn't find the Azure management certificate in the store" | |
} | |
if (-not $certificate.HasPrivateKey) | |
{ | |
throw "The private key for the Azure management certificate is not available in the certificate store" | |
} | |
Write-Verbose "Deleting Deployment" | |
Remove-Deployment ` | |
-ServiceName $AzureServiceName ` | |
-Slot $AzureDeploymentSlot ` | |
-SubscriptionId $AzureSubscriptionId ` | |
-Certificate $certificate ` | |
-ErrorAction SilentlyContinue | |
Write-Verbose "Sent Delete Deployment Async, will check back later to see if it is deleted" | |
$deploymentsContainerName = "deployments" | |
$neo4jContainerName = "neo4j" | |
AddBlobContainerIfNotExists $deploymentsContainerName | |
AddBlobContainerIfNotExists $neo4jContainerName | |
$deploymentBlobName = "$DeploymentName.cspkg" | |
DownloadNeo4jBinaryZipFileAndUploadToBlobStorageIfNotExists $neo4jContainerName $Neo4jBlobName $Neo4jZippedBinaryFileHttpSource | |
Write-Verbose "Azure Service Information:" | |
Write-Verbose "Service Name: $AzureServiceName" | |
Write-Verbose "Slot: $AzureDeploymentSlot" | |
Write-Verbose "Package Location: $PackageSource" | |
Write-Verbose "Config File Location: $ConfigSource" | |
Write-Verbose "Label: $DeploymentName" | |
Write-Verbose "DeploymentName: $DeploymentName" | |
Write-Verbose "SubscriptionId: $AzureSubscriptionId" | |
Write-Verbose "Certificate: $certificate" | |
CheckIfDeploymentIsDeleted | |
Write-Verbose "Starting Deployment" | |
New-Deployment ` | |
-ServiceName $AzureServiceName ` | |
-Slot $AzureDeploymentSlot ` | |
-PackageLocation $PackageSource ` | |
-ConfigFileLocation $ConfigSource ` | |
-Label $DeploymentName ` | |
-DeploymentName $DeploymentName ` | |
-SubscriptionId $AzureSubscriptionId ` | |
-Certificate $certificate | |
WaitUntilAllRoleInstancesAreReady | |
Write-Verbose "Completed Deployment" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment