|
[CmdletBinding()] |
|
Param( |
|
[Parameter()] |
|
[guid]$SubscriptionId = "16a1fd8b-1684-4b0f-9c72-383d91e35cd7" |
|
, |
|
[Parameter()] |
|
[ValidateSet("Canada Central","Canada East","Central US","East US","East US 2","North Central US","South Central US","West Central US","West US","West US 2")] |
|
[string]$Region = "Central US" |
|
, |
|
[Parameter()] |
|
[string]$ResourceGroupName = ("${env:USERNAME}-solr-rg") |
|
, |
|
[Parameter()] |
|
[Alias("Name")] |
|
[string]$WebAppName = ("${env:USERNAME}-solr") |
|
, |
|
[Parameter()] |
|
[Alias("Version")] |
|
[string]$SolrVersion = "7.2.1" |
|
) |
|
Begin { |
|
$eap = $ErrorActionPreference |
|
$ErrorActionPreference = "Stop" |
|
} |
|
Process { |
|
Write-Host "Installing Az Module..." |
|
If ($null -eq (Get-InstalledModule "Az" -ErrorAction "SilentlyContinue")) { |
|
Install-Module -Name "Az" -AllowClobber -Scope "CurrentUser" -SkipPublisherCheck -Force |
|
Write-Host "> Done." |
|
} |
|
|
|
Write-Host "Connecting..." |
|
$azCtx = Get-AzContext -ErrorAction "SilentlyContinue" |
|
If ($null -eq $azCtx) { |
|
$azCtx = Connect-AzAccount |
|
Write-Host "> Done." |
|
} Else { |
|
Write-Host "> Using existing: $($azCtx.Name)" |
|
} |
|
|
|
Write-Host "Setting subscription..." |
|
If ($azCtx.Subscription.Id -ne $SubscriptionId) { |
|
$azSub = Get-AzSubscription -SubscriptionId $SubscriptionId -ErrorAction "SilentlyContinue" |
|
If ($null -ne $azSub) { |
|
$azSub | Set-AzContext |
|
Write-Host "> Done." |
|
} Else { |
|
Write-Error "Unknown subscription '${SubscriptionId}'" |
|
} |
|
} Else { |
|
Write-Host "> Already set" |
|
} |
|
|
|
Write-Host "Creating resource group..." |
|
$azRg = Get-AzResourceGroup $ResourceGroupName -ErrorAction "SilentlyContinue" |
|
If ($null -eq $azRg) { |
|
$azRg = New-AzResourceGroup -Name $ResourceGroupName -Location $Region |
|
} Else { |
|
Write-Host "> Using existing: $($azRg.ResourceGroupName)" |
|
} |
|
|
|
Write-Host "Creating web app..." |
|
$azWebApp = Get-AzWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName -ErrorAction "SilentlyContinue" |
|
If ($null -eq $azWebApp) { |
|
$azWebApp = New-AzWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName -Location $Region |
|
Write-Host "> Done." |
|
} Else { |
|
Write-Host "> Using existing: $($azWebApp.Name)" |
|
} |
|
$azWebApp.EnabledHostnames | ForEach-Object { Write-Verbose " ${_}" } |
|
|
|
Write-Host "Getting publish profile..." |
|
$azPubProfXml = [xml](Get-AzWebAppPublishingProfile -ResourceGroupName $ResourceGroupName -Name $WebAppName) |
|
|
|
Write-Host "Determining FTP credentials..." |
|
$userName = $azPubProfXml.SelectNodes("//publishProfile[@publishMethod=`"MSDeploy`"]/@userName").value |
|
$userPWD = $azPubProfXml.SelectNodes("//publishProfile[@publishMethod=`"MSDeploy`"]/@userPWD").value |
|
$publishUrl = $azPubProfXml.SelectNodes("//publishProfile[@publishMethod=`"MSDeploy`"]/@publishUrl").value |
|
$destinationAppUrl = $azPubProfXml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@destinationAppUrl").value |
|
|
|
Write-Host "Uploading SOLR files..." |
|
$zipDeployParams = @{ |
|
Uri = "https://${publishUrl}/api/zipdeploy" |
|
Method = "Put" |
|
ContentType = "application/json" |
|
Body = @{ |
|
packageUri = "https://archive.apache.org/dist/lucene/solr/{0}/solr-{0}.zip" -f $SolrVersion |
|
} | ConvertTo-Json -Compress |
|
Headers = @{ |
|
Authorization = "Basic {0}" -f ([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $userName, $userPWD)))) |
|
} |
|
} |
|
Try { |
|
Invoke-RestMethod @zipDeployParams |
|
Write-Host "Done." |
|
} Catch { |
|
Write-Error "> Failed." |
|
} |
|
|
|
Write-Host "Uploading web.config..." |
|
$webConfig = @" |
|
<?xml version="1.0" encoding="UTF-8"?> |
|
<configuration> |
|
<system.webServer> |
|
<handlers> |
|
<add name="httpPlatformHandler" |
|
path="*" |
|
verb="*" |
|
modules="httpPlatformHandler" |
|
resourceType="Unspecified" /> |
|
</handlers> |
|
<httpPlatform processPath="%HOME%\site\wwwroot\solr-${SolrVersion}\bin\solr.cmd" |
|
arguments="start -p %HTTP_PLATFORM_PORT%" |
|
startupTimeLimit="20" |
|
startupRetryCount="10" |
|
stdoutLogEnabled="true"> |
|
</httpPlatform> |
|
</system.webServer> |
|
</configuration> |
|
"@ |
|
Write-Host "> Done." |
|
$vfsParams = @{ |
|
Uri = "https://${publishUrl}/api/vfs/site/wwwroot/web.config" |
|
Method = "Put" |
|
Headers = $zipDeployParams.Headers |
|
Body = $webConfig |
|
ContentType = "multipart/form-data" |
|
} |
|
Try { |
|
Invoke-RestMethod @vfsParams |
|
Write-Host "> Done." |
|
} Catch { |
|
Write-Error "> Failed." |
|
} |
|
|
|
Write-Host "Starting site..." |
|
Start-Process $destinationAppUrl |
|
} |
|
End { |
|
$ErrorActionPreference = $eap |
|
} |