Created
July 10, 2017 10:01
-
-
Save ThomPuiman/7128fba54255416f56ba629773dfe6b3 to your computer and use it in GitHub Desktop.
Script to deploy Sitecore to Azure PaaS using Sitecore Azure Toolkit
This file contains hidden or 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
| Function GenerateIncrementalPackage { | |
| [CmdletBinding(SupportsShouldProcess = $true)] | |
| param( | |
| #The full path to the Sitecore Azure Toolkit | |
| [Parameter(Mandatory = $true)] | |
| [string]$toolsPath, | |
| #Sitecore directory (containing Website, Data and Databases folder) | |
| [Parameter(Mandatory = $true)] | |
| [string]$sitecorePath, | |
| #Output folder | |
| [Parameter(Mandatory = $true)] | |
| [string]$outputPath, | |
| #Name of the Azure Storage Container | |
| [Parameter(Mandatory = $true)] | |
| [string]$storageAccountName, | |
| #Azure Storage access key | |
| [Parameter(Mandatory = $true)] | |
| [string]$storageAccountKey, | |
| #arm tpl folder | |
| [Parameter(Mandatory = $true)] | |
| [string]$armTemplatePath, | |
| #arm params path | |
| [Parameter(Mandatory = $true)] | |
| [string]$armParams, | |
| #Version | |
| #[Parameter(Mandatory = $true)] | |
| [string]$Version, | |
| #Sitecore License XML | |
| [Parameter(Mandatory = $true)] | |
| [string]$LicenseXML, | |
| #Azure Resource Group name | |
| [Parameter(Mandatory = $true)] | |
| [string]$resourceGroupName, | |
| #SKU xm/xp/etc | |
| #[Parameter(Mandatory = $true)] | |
| [string]$SKU, | |
| #Azure Region Name (default: West Europe) | |
| [string]$regionName | |
| ) | |
| Try { | |
| Get-AzureRmContext -ErrorAction Continue | |
| Write-Host "You're logged on to Azure. Using subscription $((Get-AzureRmContext).SubscriptionName)" -ForegroundColor "green" | |
| } Catch { | |
| if ($_ -like "*Login-AzureRmAccount to login*") { | |
| Write-Host "You're not logged in to Azure. Please login using the login screen popping up." -ForegroundColor "Red" | |
| Login-AzureRmAccount | |
| } | |
| } | |
| if ($toolsPath.Substring($toolsPath.Length - 1) -eq "\"){ | |
| $toolsPath = $toolsPath.Substring(0, $toolsPath.Length - 1) | |
| } | |
| # Import commandlets | |
| Import-Module "$toolsPath\tools\Sitecore.Cloud.Cmdlets.psm1" | |
| Import-Module "$toolsPath\tools\Sitecore.Cloud.CmdLets.dll" -Verbose | |
| $resourceVersions = Get-ChildItem "$toolsPath\resources" | ?{ $_.PSIsContainer } | %{Split-Path $_.FullName -Leaf} | |
| #Verify whether the provided version is valid and exists in the SAT | |
| if([string]::IsNullOrEmpty($Version) -Or ($resourceVersions -contains $Version) -eq $FALSE){ | |
| $options = $resourceVersions | %{ | |
| New-Object System.Management.Automation.Host.ChoiceDescription "&$_", $_ | |
| } | |
| $result = $host.ui.PromptForChoice("Select the version", "What version do you want to install?", $options, 0) | |
| $Version = $resourceVersions[$result] | |
| } | |
| Write-Host "Continuing with Sitecore version $Version" | |
| # Set the parameter variables | |
| $Resources="$toolsPath\resources\$Version" | |
| #Extracting the available SKUs from the filenames | |
| $SKUs = Get-ChildItem "$Resources\configs" | %{Split-Path $_.FullName -Leaf} | %{$_.split(".")[0]} | |
| #Verify whether the provided SKU is valid and exists in the SAT | |
| if([string]::IsNullOrEmpty($SKU) -Or ($SKUs -contains $SKU) -eq $FALSE){ | |
| $skuoptions = $SKUs.where({$_ -NotIn @("common")}) | %{ | |
| New-Object System.Management.Automation.Host.ChoiceDescription "&$_", $_ | |
| } | |
| $skuresult = $host.ui.PromptForChoice("Select the SKU", "What setup do you want to use?", $skuoptions, 0) | |
| $SKU = $SKUs[$skuresult + 1] | |
| } | |
| Write-Host "Continuing with SKU $SKU" | |
| if(!(Test-Path -Path $outputPath)){ | |
| Write-Host "Creating the output directory at $outputPath" | |
| mkdir $outputPath | |
| } else { | |
| Write-Host "Output path already exists, using $outputPath. Clearing the folder from any existing scwdp-files." | |
| Get-ChildItem -Path $outputPath -Include *.scwdp.zip -Recurse | foreach { $_.Delete()} | |
| } | |
| $packagePrefix = Split-Path $sitecorePath -Leaf | |
| $SkuConfigPath = "$Resources\configs\$SKU.packaging.config.json" | |
| Write-Host (Get-Content $SkuConfigPath -Raw) | |
| $skuconfigs = (Get-Content $SkuConfigPath -Raw) | ConvertFrom-Json | |
| $packages = @() | |
| foreach($pkg in $skuconfigs.scwdps) { | |
| $obj = New-Object System.Object | |
| $obj | Add-Member -type NoteProperty -name FileName -value $($packagePrefix + "_" + $pkg.role + ".{0}.zip") | |
| $obj | Add-Member -type NoteProperty -name Role -value $pkg.role | |
| $packages += $obj | |
| } | |
| Write-Host "Start packing the Sitecore folder $sitecorePath" -ForegroundColor "magenta" | |
| Start-SitecoreAzurePackaging ` | |
| -sitecorePath "$sitecorePath" ` | |
| -destinationFolderPath $outputPath ` | |
| -cargoPayloadFolderPath "$Resources\cargopayloads" ` | |
| -commonConfigPath "$Resources\configs\common.packaging.config.json" ` | |
| -skuConfigPath "$Resources\configs\$SKU.packaging.config.json" ` | |
| -parameterXmlPath "$Resources\MsDeployXmls" ` | |
| -fileVersion 1.0 | |
| Write-Host "Finished with packaging the Sitecore solution" -ForegroundColor "green" | |
| $hash = New-Object System.Collections.HashTable | |
| foreach($pkg in $packages){ | |
| Write-Host "Removing databases from $($pkg.FileName -f "scwdp")" -ForegroundColor "magenta" | |
| Remove-SCDatabaseOperations -Path "$outputPath\$($pkg.FileName -f "scwdp")" -Destination "$outputPath" | |
| Write-Host "Starting upload to blob storage for $($pkg.FileName -f "withoutdb.scwdp")" -ForegroundColor "magenta" | |
| $url = UploadPackageToStorage ` | |
| -packageFilePath "$outputPath\$($pkg.FileName -f "withoutdb.scwdp")" ` | |
| -storageAccountName $storageAccountName ` | |
| -storageContainerName "sitecore-azure-packages" ` | |
| -storageAccountKey $storageAccountKey | |
| $hash.Add($pkg.Role + "MsDeployPackageUrl", $url) | |
| Write-Host "Finished uploading package to blob storage with URL: $($url)" -ForegroundColor "green" | |
| } | |
| if($regionName -eq [string]::Empty) { | |
| $regionName = "West Europe" | |
| Write-Host "Region name wasn't provided, setting the default to: $regionName" -foregroundcolor "yellow" | |
| } | |
| Write-Host "Starting provisioning of resources to resource group $resourceGroupName. This might take a while." -ForegroundColor "magenta" | |
| Start-SitecoreAzureDeployment ` | |
| -location $regionName ` | |
| -Name $resourceGroupName ` | |
| -ArmTemplateUrl $armTemplatePath ` | |
| -ArmParametersPath $armParams ` | |
| -LicenseXmlPath $LicenseXML ` | |
| -SetKeyValue $hash | |
| Write-Host "Deployment has been finished. Bye!" -ForegroundColor "green" | |
| } | |
| Function UploadPackageToStorage { | |
| [CmdLetBinding()] | |
| param( | |
| [Parameter(Mandatory=$true)] | |
| $packageFilePath, | |
| [Parameter(Mandatory=$true)] | |
| $storageAccountName, | |
| [Parameter(Mandatory=$true)] | |
| $storageContainerName, | |
| [Parameter(Mandatory=$true)] | |
| $storageAccountKey | |
| ) | |
| $StorageContext = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey | |
| $existingContainer = Get-AzureStorageContainer -Context $StorageContext | | |
| Where-Object { $_.Name -like $storageContainerName } | |
| if (!$existingContainer) | |
| { | |
| $newContainer = New-AzureStorageContainer -Name $storageContainerName -Context $StorageContext | |
| "Storage container '" + $newContainer.Name + "' created." | |
| } | |
| $time = [DateTime]::UtcNow | |
| $duration = [DateTime]::UtcNow - $time | |
| $fileName = Split-Path $packageFilePath -Leaf | |
| $blobFileName = $(Get-Date -format "yyyyMMddhhmm") + "_" + $fileName | |
| #Write-Host "Copying file $($fileName) to new blob with name: $blobFileName" | |
| try | |
| { | |
| Set-AzureStorageBlobContent -Container $storageContainerName ` | |
| -File "$($packageFilePath)" -Blob "$blobFileName" -Context $StorageContext -Force >$null | |
| } | |
| catch | |
| { | |
| $warningMessage = "Unable to upload file " + $file.FullName | |
| Write-Warning -Message $warningMessage | |
| } | |
| #"Uploaded " + $blobFileName + " to blob container '" + $storageContainerName + "'." | |
| #"Total upload time: " + $duration.TotalMinutes + " minutes." | |
| $now = Get-Date | |
| return "https://$($storageAccountName).blob.core.windows.net/$($storageContainerName)/" + $blobFileName + $(New-AzureStorageContainerSASToken -Name $storageContainerName -Context $StorageContext -Permission rl -StartTime $now.AddHours(-1) -ExpiryTime $now.AddMonths(1)) | |
| } | |
| Export-ModuleMember -Function GenerateIncrementalPackage | |
| Export-ModuleMember -Function UploadPackageToStorage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment