Last active
August 29, 2015 13:57
-
-
Save codingoutloud/e1a904253b94404430a6 to your computer and use it in GitHub Desktop.
Create a multi-VM environment (example for demonstration purposes)
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
# Create Test Environment for FooApp | |
# Assumes you have done these: | |
# 1. Import-AzurePublishSettingsFile | |
# 2. Set SubscriptionName and SubscriptionId from Get-AzureSubscription | |
$startTime = Get-Date -DisplayHint DateTime | |
$subscriptionName = 'FooAppTestSubscriptionName' | |
$subscriptionId = '01234567-890a-bcde-f012-3456789abcde' | |
$subject = 'FooAppTest' | |
# Storage Account names limited to 24 alphanumeric chars, lower-case only | |
$subjectSafe = 'fooapptest1' | |
$subjectLabel = 'FooApp Test Environment' | |
$subjectDescription = 'FooApp Test Environment - Test Team 1 Data Center' | |
$primaryDataCenter = 'East US' | |
#Set-AzureSubscription -SubscriptionId $subscriptionId -SubscriptionName $subscriptionName -CurrentStorageAccount $storageAccountName | |
Select-AzureSubscription -SubscriptionName $subscriptionName | |
# also works: Select-AzureSubscription $subscriptionName | |
# shows what's happened: Get-AzureSubscription | select SubscriptionName, IsDefault | |
# to undo the above: Select-AzureSubscription -Clear | |
<# Smart Create #> | |
Function NewIfNotExists-StorageAccount | |
{ | |
Param( | |
[string]$StorageAccountName, | |
[string]$AffinityGroup, | |
[string]$Label=$StorageAccountName, | |
[string]$Description=$StorageAccountName, | |
[boolean]$Analytics=$false) | |
$exists = Test-AzureName -Storage -Name $storageAccountName | |
if ($exists -eq $True) | |
{ | |
echo "Storage Account $storageAccountName already exists." | |
} | |
else | |
{ | |
New-AzureStorageAccount -StorageAccountName $storageAccountName -AffinityGroup $affinityGroup -Label $label -Description $description | |
if ($Analytics -eq $True) | |
{ | |
echo "TODO: Implement turning on Analytics for $StorageAccountName" | |
} | |
} | |
} | |
## | |
## Derive the remainder, carefully | |
## | |
$exists = Get-AzureAffinityGroup | where {$_.Name -eq $subject} | |
if ($exists -eq $Null) | |
{ | |
New-AzureAffinityGroup -Name $subject -Location $primaryDataCenter -Label $subjectLabel -Description $subjectDescription | |
} | |
else | |
{ | |
echo "Affinity Group $subject already exists." | |
} | |
$storageDeploy = "${subjectSafe}deploy" | |
NewIfNotExists-StorageAccount -StorageAccountName $storageDeploy -AffinityGroup $subject -Label "$subjectLabel - Deployment" -Description "$subjectDescription - Used by platform for Cloud Service Deployments" | |
$storageDiagnostics = "${subjectSafe}diags" | |
NewIfNotExists-StorageAccount -StorageAccountName $storageDiagnostics -AffinityGroup $subject -Label "$subjectLabel - Diagnostics / Logging / Telemetry" -Description "$subjectDescription - To collect aggregated Diagnostics, Logging, and Telemetry" | |
$storageBackups = "${subjectSafe}backups" | |
NewIfNotExists-StorageAccount -StorageAccountName $storageBackups -AffinityGroup $subject -Label "$subjectLabel - Backups" -Description "$subjectDescription - Backups" | |
$storageAgent = "${subjectSafe}agentuploads" | |
NewIfNotExists-StorageAccount -StorageAccountName $storageAgent -AffinityGroup $subject -Analytics $True -Label "$subjectLabel - Agent Uploads" -Description "$subjectDescription - Agent Uploads" | |
$storageRuntime = "${subjectSafe}runtime" | |
NewIfNotExists-StorageAccount -StorageAccountName $storageRuntime -AffinityGroup $subject -Label "$subjectLabel - Runtime Support" -Description "$subjectDescription - Runtime Support, such as a cache or for temporary Access Tokens" | |
$exists = Test-AzureName -Service $subject | |
if ($exists -eq $True) | |
{ | |
echo "Cloud Service $subject already exists." # TODO: Check to see if it belongs to my account or to someone else - and is in the right data center | |
# Get-AzureService | |
} | |
else | |
{ | |
New-AzureService -ServiceName $subject -AffinityGroup $subject -Label $subject -Description $subject | |
} | |
$sbNamespace = "${subject}-workflow" | |
$exists = Test-AzureName -ServiceBusNamespace $sbNamespace | |
if ($exists -eq $True) | |
{ | |
echo "Service Bus Namespace $sbNamespace already exists." # TODO: Check to see if it belongs to my account or to someone else - and is it in the right Data Center | |
} | |
else | |
{ | |
New-AzureSBNamespace -Name $sbNamespace -Location $primaryDataCenter | |
} | |
<# | |
# Create SQL Database | |
#> | |
$sqlDatabaseServerCredential = Get-Credential -Message "Enter SQL Database Server username and password for $subject" | |
$sqlDatabaseServerContext = New-AzureSqlDatabaseServerContext -Credential $sqlDatabaseServerCredential -ServerName "${subjectSafe}" | |
$sqlDatabaseServer = New-AzureSqlDatabaseServer -Location $primaryDataCenter -AdministratorLogin $sqlDatabaseServerCredential.UserName -AdministratorLoginPassword $sqlDatabaseServerCredential.Password | |
### New-AzureSqlDatabaseServerFirewallRule -ServerName $sqlServer.ServerName -RuleName "Something" -StartIPAddress "111.111.111.111" -EndIPAddress "111.111.222.222" | |
echo "WARNING: SQL Database Server created. Name not obfuscated. (Name = #{subjectSafe})." | |
echo "WARNING: No Database Server firewall rules have been created." | |
$sqlDatabaseServer | |
$sqlDatabase = New-AzureSqlDatabase $sqlDatabaseServerContext –DatabaseName “${subjectSafe}” | |
echo "WARNING: SQL Database created. Name not obfuscated. (Name = #{subjectSafe})." | |
$sqlDatabase | |
<# | |
# Done | |
#> | |
$finishTime = Get-Date -DisplayHint DateTime | |
$elapsedTime = $finishTime - $startTime | |
echo "Started at $startTime, finished at $finishTime, elapsed time = $elapsedTime" | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment