Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

New-AzureWebsitewithDB #13

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2f29179
Create WAWebsites_v1.ps
guadacasuso Jul 25, 2013
17e0cda
Create WAWebsitesv01.ps
guadacasuso Jul 25, 2013
633b5df
Delete WAWebsitesv01.ps
guadacasuso Jul 25, 2013
edd9f4d
Delete WAWebsites_v1.ps
guadacasuso Jul 25, 2013
e5f4557
Create WAWebSitesv1alpha.ps1
guadacasuso Jul 25, 2013
a834c26
Update WAWebSitesv1alpha.ps1
guadacasuso Jul 25, 2013
a35ce56
Create wawsstoragecreationIncl.ps1
guadacasuso Jul 26, 2013
deb4a53
Create generals.ps1
guadacasuso Jul 26, 2013
a7d9c27
Create wawebsite_storage_git.ps
guadacasuso Jul 26, 2013
a67049c
Delete wawebsite_storage_git.ps
guadacasuso Jul 31, 2013
1ac135a
Delete wawsstoragecreationIncl.ps1
guadacasuso Jul 31, 2013
5281fb3
Create WAWebsiteProvisioning.ps1
guadacasuso Jul 31, 2013
bd3d402
Update WAWebsiteProvisioning.ps1
guadacasuso Jul 31, 2013
9d0bde6
Update WAWebsiteProvisioning.ps1
guadacasuso Jul 31, 2013
c3cfbef
Update WAWebsiteProvisioning.ps1
guadacasuso Jul 31, 2013
4989bc5
Update WAWebsiteProvisioning.ps1
guadacasuso Jul 31, 2013
ff0bfca
Update WAWebsiteProvisioning.ps1
guadacasuso Jul 31, 2013
e6d1376
Create New-AzureWebsitewithDB.ps1
guadacasuso Aug 2, 2013
c068c63
Delete WAWebSitesv1alpha.ps1
guadacasuso Aug 2, 2013
d89ce53
Delete WAWebsiteProvisioning.ps1
guadacasuso Aug 2, 2013
404c499
Delete generals.ps1
guadacasuso Aug 2, 2013
e9f57a4
Create Enable-AzureStorageMeteringAndLogs.ps1
guadacasuso Aug 8, 2013
d252a1d
Update Enable-AzureStorageMeteringAndLogs.ps1
guadacasuso Aug 8, 2013
d3d6c63
Update New-AzureWebsitewithDB.ps1
guadacasuso Aug 13, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions solutions/data-management/Enable-AzureStorageMeteringAndLogs.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<#
.SYNOPSIS
Turn on storage logging and metrics for all or a selected storage account under one Subscription.
.DESCRIPTION
This script will turn on the logging and metrics settings for all storage accounts under the default subscription.
You can also, provide the storage account name to restrict the action to it.

.EXAMPLE
Enable-AzureStorageMeteringAndLogs.ps1 -StorageAccountName "StorageAccountName"
#>


param(
[CmdletBinding( SupportsShouldProcess=$true)]

# The Storage account name to which you will enable logging and monitoring
[Parameter(Mandatory = $false)]
[String]$StorageAccountName

)

<#
.SYNOPSIS
Enable the logging and monitoring for the storage account
.DESCRIPTION
1. Get the Storage account by name
2. Configure the Logging and Metering properties.
3. Set them to the storage account.
.EXAMPLE
EnableMeteringtoStorage -storageAccount "storageAccount"
#>
Function EnableMeteringtoStorage($StorageAccount)
{

$name = $StorageAccount.StorageAccountName
Write-Verbose ("[Start] Enabling meter and Logging for storage account {0}" -f $name )
$fullkeys = Get-AzureStorageKey -StorageAccountName $name
$key = $fullkeys[0].Primary
$context = New-AzureStorageContext -StorageAccountName $name -StorageAccountKey $key
$blobClient = $context.StorageAccount.CreateCloudBlobClient();

#Configure the Logging parameters
$sp = New-Object -TypeName Microsoft.WindowsAzure.Storage.Shared.Protocol.ServiceProperties
$sp.Logging.Version = "1.0";
$sp.Logging.RetentionDays = 7;
$sp.Logging.LoggingOperations = [Microsoft.WindowsAzure.Storage.Shared.Protocol.LoggingOperations]::All;

#Configure the metrics parameters
$sp.Metrics.Version = "1.0";
$sp.Metrics.MetricsLevel = [Microsoft.WindowsAzure.Storage.Shared.Protocol.MetricsLevel]::ServiceAndApi;

#Set the service properties
$blobClient.SetServiceProperties($sp);

#display service properties
$blobClient.GetServiceProperties();
Write-Verbose ("[Finish] Enabling meter and Logging for storage account {0}" -f $name )
}

Set-StrictMode -Version 3


# Mark the start time of the script execution
$startTime = Get-Date

# Check if Windows Azure Powershell is available
if ((Get-Module Azure) -eq $null)
{
throw "Windows Azure Powershell not found! Please make sure to install them
from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
}

# Execute the Enabling Meter and Logging function if the Storage Account Name is provided as parameter
if ($StorageAccountName.Length -gt 1)
{
$storageAccount = Get-AzureStorageAccount | Where-Object {$_.StorageAccountName -eq $StorageAccountName }
# Verify if the Storage Account exists
if($storageAccount -eq $null) {
#exit
throw "Storage account does no exists"
}
else
{
#Execute the Enabling Meter and Logging function
EnableMeteringtoStorage($storageAccount)

}

}
else
{
#Get the storage accounts for the default suscription
$accounts = Get-AzureStorageAccount

#Iterate
foreach($StorageAccount in $accounts)
{
#Execute the Enabling Meter and Logging function
EnableMeteringtoStorage($StorageAccount)
}
}

# Output the time consumed in seconds
$finishTime = Get-Date
Write-Verbose ("Total time used (seconds): {0}" -f ($finishTime - $startTime).TotalSeconds)
262 changes: 262 additions & 0 deletions solutions/web/New-AzureWebsitewithDB.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
<#
.SYNOPSIS
Creates a Windows Azure Website and connects to a SQL Azure DB and a storage account.
.DESCRIPTION
This script will create a website, SQl DB and storage account based on the provided
websitename, Azure Data center location, storage name and the credential(user id and password)
provided for Database.
`
.EXAMPLE
New-AzureWebsitewithDB.ps1 -WebSiteName "WebSiteName" -AzureDcLocation "Location"
-StorageAccountName "StorageAccountName"
#>


param(
[CmdletBinding( SupportsShouldProcess=$true)]

# The webSite Name you want to create
[Parameter(Mandatory = $true)]
[string]$WebSiteName,

# The Azure Data center Location
[Parameter(Mandatory = $true)]
[string]$AzureDcLocation,

# The Storage account that will be linked to the website
[Parameter(Mandatory = $true)]
[String]$StorageAccountName,

# The application database name that will be created and used by the website in this script
[String]$AppDatabaseName = "appdb",


# The database firewall rule
[String]$FirewallRuleName,

# Users machine ip range first address
[String]$StartIPAddress,

# Rule name
[String]$RuleName,

# End ip address for the range for user machine to access the db
[String]$EndIPAddress

)

<#
.SYNOPSIS
Creates a storage account from the parameters accountname and Azure Data center location.
.DESCRIPTION
This function will first check to verify that the storage account exists and
if not, then creates a storage account using the storage account name and the
datacenter location provided. The verification is done using Get-AzureStorageAccount
cmdLet.
`
.EXAMPLE
$storage = CreateStorageAccount -StorageAccountName "testaccount" -AzureDcLocation "EAST US"
#>
Function CreateStorageAccount($StorageAccountName,$AzureDcLocation)
{

$storageResult = Get-AzureStorageAccount | Where-Object `
{$_.StorageAccountName -eq $StorageAccountName }

if($storageResult -eq $null)
{
# Create a new storage account
New-AzureStorageAccount -StorageAccountName $StorageAccountName `
-Location $AzureDcLocation
}
else
{
Write-Verbose ("Storage account {0} in location {1} exist" `
-f $StorageAccountName, $AzureDcLocation)
}


# Get the access key of the storage account
$storageAccountKey = Get-AzureStorageKey -StorageAccountName $StorageAccountName


# when call Get-AzureStorageKey both primary and seconrady key is
#returned but we are only using the primary key.
$appSettings = @{"StorageAccountName" = $StorageAccountName;`
"StorageAccountAccessKey" = $storageAccountKey.Primary}


Return @{AppSettings=$appSettings}
}

<#
.SYNOPSIS
creates a sql db and set server firewall rule based on the parameters location,
name, user id, password
.DESCRIPTION
This function creates a database server, sets up server firewall rules and then
creates a Database. It uses New-AzureSqlDatabaseServer cmdlet to create the server,
New-AzureSqlDatabaseServerFirewallRule to create the firewall rule,
New-AzureSqlDatabase cmdlet to create the database.

`
.EXAMPLE
$db = CreateDatabase -AzureDcLocation "AzureDcLocation" -AppDatabaseName "AppDatabaseName"
-UserName "UserName" -Password "Password" -RuleName "RuleName"
-FirewallRuleName "FirewallRuleName" -StartIPAddress "0.0.0.0" -EndIPAddress "0.0.0.0"
#>

Function CreateDatabase($AzureDcLocation,$AppDatabaseName, $UserName, $Password,`
$RuleName, $FirewallRuleName, $StartIPAddress, $EndIPAddress)
{
Write-Verbose ("[Start] creating SQL Azure database server")
$databaseServer = New-AzureSqlDatabaseServer -AdministratorLogin $UserName `
-AdministratorLoginPassword $Password -Location $AzureDcLocation
Write-Verbose ("[Finish] creating SQL Azure database server")


# Setting server firewall rule
Write-Verbose ("[Start] creating firewall rule")
New-AzureSqlDatabaseServerFirewallRule -ServerName $databaseServer.ServerName `
-RuleName "AllowUserIP" -StartIpAddress $StartIPAddress -EndIpAddress $EndIPAddress
New-AzureSqlDatabaseServerFirewallRule -ServerName $databaseServer.ServerName `
-RuleName "AllowAllAzureIP" -StartIpAddress "0.0.0.0" -EndIpAddress "0.0.0.0"
Write-Verbose ("[Finish] created firewall rule")


# Create a database context which includes the server name and credential
$credential = New-PSCredentialFromPlainText -UserName $UserName -Password $Password
$context = New-AzureSqlDatabaseServerContext -ServerName $databaseServer.ServerName`
-Credential $credential


# Use the database context to create app database
Write-Verbose ("[Start] creating database {0} in database server {1}" `
-f $AppDatabaseName, $databaseServer.ServerName)
New-AzureSqlDatabase -DatabaseName $AppDatabaseName -Context $context
Write-Verbose ("[Finish] creating database {0} in database server {1}" `
-f $AppDatabaseName, $databaseServer.ServerName)

$appDatabaseConnectionString = Get-SQLAzureDatabaseConnectionString -DatabaseServerName `
$databaseServer.ServerName -DatabaseName $AppDatabaseName -UserName `
$UserName -Password $Password


Return @{ `
Server = $databaseServer.ServerName; UserName = $UserName; Password = $Password; `
AppDatabase = @{Name = $AppDatabaseName; AppDatabaseConnectionString = $appDatabaseConnectionString};
}
}

<#
.SYNOPSIS
Detect IP address for user machine
.DESCRIPTION
This functions retrieves users machine IP range for setting SQLDB server firewall rule.
`
.EXAMPLE
$ipRange = Detect-IPAddress
#>
Function Detect-IPAddress
{
$ipregex = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
$text = Invoke-RestMethod 'http://www.whatismyip.com/api/wimi.php'
$result = $null

If($text -match $ipregex)
{
$ipaddress = $matches[0]
$ipparts = $ipaddress.Split('.')
$ipparts[3] = 0
$startip = [string]::Join('.',$ipparts)
$ipparts[3] = 255
$endip = [string]::Join('.',$ipparts)


$result = @{StartIPAddress = $startip; EndIPAddress = $endip}
}

Return $result
}
<#
.SYNOPSIS
Detect IP address for user machine
.DESCRIPTION
This functions retrieves users machine IP range for setting SQLDB server firewall rule.
.EXAMPLE
$ipRange = Detect-IPAddress
#>
Function Get-SQLAzureDatabaseConnectionString
{
Param(
[String]$DatabaseServerName,
[String]$DatabaseName,
[String]$UserName,
[String]$Password
)

Return "Server=tcp:{0}.database.windows.net,1433;Database={1};
User ID={2}@{0};Password={3};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" -f
$DatabaseServerName, $DatabaseName, $UserName, $Password
}


Set-StrictMode -Version 3

# Detect IP range for SQL Azure whitelisting if the IP range is not specified
If (-not ($StartIPAddress -and $EndIPAddress))
{
$ipRange = Detect-IPAddress
$StartIPAddress = $ipRange.StartIPAddress
$EndIPAddress = $ipRange.EndIPAddress
}

# Mark the start time of the script execution
$startTime = Get-Date

# Check if Windows Azure Powershell is avaiable
if ((Get-Module Azure) -eq $null)
{
throw "Windows Azure Powershell not found! Please make sure to install them
from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
}

# 1 - Create the website
#Check if the Website exists
$result = Get-AzureWebsite | Where-Object {$_.Name -eq $WebSiteName }

if ($result -eq $null)
{
Write-Verbose "creating website"
}
else
{
throw "Website already exists, please try a different website name"
}

# Get credentials from user to use to configure the new Virtual Machine
$credential = Get-Credential

# Configure the new Virtual Machine.
$UserName = $credential.GetNetworkCredential().UserName
$Password = $credential.GetNetworkCredential().Password

#Start Creation
Write-Verbose ("Starting Website {0} Create process" -f $WebSiteName)
$website = New-AzureWebsite -Name $WebSiteName -Location $AzureDcLocation

# 2 - Create the storage account
$storage = CreateStorageAccount -StorageAccountName $StorageAccountName -AzureDcLocation $AzureDcLocation

# 3 - Create the SQL DB
$db = CreateDatabase -AzureDcLocation $AzureDcLocation -AppDatabaseName $AppDatabaseName `
-UserName $UserName -Password $Password -RuleName $RuleName `
-FirewallRuleName $FirewallRuleName -StartIPAddress $StartIPAddress `
-EndIPAddress $EndIPAddress

# 4 - Link the website to the storage account and SQLDB
#Set-AzureWebsite -Name $WebSiteName -AppSettings $storage.AppSettings -ConnectionStrings $db.AppDatabaseConnectionString

# Output the time consumed in seconds
$finishTime = Get-Date
Write-Verbose ("Total time used (seconds): {0}" -f ($finishTime - $startTime).TotalSeconds)