Skip to content

Instantly share code, notes, and snippets.

@pawelpabich pawelpabich/azurepoc.ps1

Last active Oct 8, 2018
Embed
What would you like to do?
param([string] $IpAddress, [string] $ConnectionString, [string] $AdministratorPassword, [string] $EncodedLicense, [string] $StoragePath, [string] $StorageAccount, [string] $StorageUser, [string] $StoragePassword)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest;
function Log($Message, $IncludeTimestamp)
{
$logEntry = $Message
if ($IncludeTimestamp)
{
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss,fff"
$newLine = [System.Environment].NewLine
$logEntry = "$timestamp $Message $newLine"
}
Write-Host $logEntry
}
$TenantName = "master"
$serverPort = 80
$serverCommPort = 10943
$serverpath = "C:\\Octopus"
$version = "2018.8.8";
$configpath = $serverpath + "\\OctopusDeploy.config"
$serverUrl = "$IpAddress"
$installerName = "Octopus.$version.msi"
$octopusInstallationPath = "${env:ProgramFiles}\\Octopus Deploy\\Octopus"
$friendlyServerUrl = $IpAddress;
Log "Deploying a new instance of Octopus Server available at http://$friendlyServerUrl"
$serverDownloadPath = "https://octopus-testing.s3.amazonaws.com/server/Octopus.$version-x64.msi"
function Download-File
{
param (
[string]$url,
[string]$saveAs
)
Log "Downloading $url to $saveAs"
$downloader = new-object System.Net.WebClient
$downloader.DownloadFile($url, $saveAs)
}
function Install-Server
{
Log "Beginning Octopus Server installation"
$pathspec = "$env:SystemDrive\\OctopusTest"
if(Test-Path -path $pathspec)
{
Remove-Item $pathspec -recurse -force
}
$serverDownloadSavePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\\$installerName")
if ((test-path $serverDownloadSavePath) -ne $true)
{
Log "Downloading latest Octopus Server MSI from '$serverDownloadPath' to '$serverDownloadSavePath'..."
Download-File $serverDownloadPath $serverDownloadSavePath
}
else
{
Log "Skipping download of latest Octopus Server MSI as '$serverDownloadSavePath' already exists"
}
Log "Installing MSI"
$msiExitCode = (Start-Process -FilePath "msiexec.exe" -ArgumentList "/i $installerName /quiet /l*v C:\\ServerMsiInstallation.log" -Wait -Passthru).ExitCode
Log "Octopus Server MSI installer returned exit code $msiExitCode"
Log "Removing msi from '$serverDownloadSavePath'"
Remove-Item -Path $serverDownloadSavePath -Force
}
function Configure-Server
{
Log "Beginning Octopus Server configuration"
& netsh.exe firewall add portopening TCP $serverPort "Octopus Server"
& netsh.exe firewall add portopening TCP 443 "Octopus Server"
& netsh.exe firewall add portopening TCP $serverCommPort "Octopus Server Comms Port"
if ($lastExitCode -ne 0)
{
throw "Installation failed when modifying firewall rules"
}
Log "Configuring Server"
cd "${env:ProgramFiles}\\Octopus Deploy\\Octopus"
&".\\Octopus.Server.exe" create-instance --instance $TenantName --config $configpath --console | Write-Host
if ($lastExitCode -ne 0)
{
throw "Installation failed on create-instance"
}
&".\\Octopus.Server.exe" database --instance $TenantName --connectionString $ConnectionString --console | Write-Host
if ($lastExitCode -ne 0)
{
throw "Installation failed on database"
}
&".\\Octopus.Server.exe" database --instance $TenantName --create --console | Write-Host
&".\\Octopus.Server.exe" configure --instance $TenantName --webCorsWhitelist=* --home $serverpath --upgradeCheck "False" --upgradeCheckWithStatistics "False" --usernamePasswordIsEnabled=true --webForceSSL "False" --webListenPrefixes "http://localhost" --commsListenPort "$serverCommPort" --serverNodeName $IpAddress --hstsEnabled=true --hstsMaxAge=31556926 --console | Write-Host
if ($lastExitCode -ne 0)
{
throw "Installation failed on configure"
}
&".\\Octopus.Server.exe" path --instance $TenantName --taskLogs=$StoragePath
&".\\Octopus.Server.exe" admin --instance $TenantName --username "Admin" --password $AdministratorPassword --console | Write-Host
&".\\Octopus.Server.exe" license --instance $TenantName --licenseBase64 $EncodedLicense --console | Write-Host
Log "Octopus commands complete"
}
function Start-Server
{
&".\\Octopus.Server.exe" service --instance $TenantName --install --console | Write-Host
if ($lastExitCode -ne 0)
{
throw "Installation failed on service install"
}
&".\\Octopus.Server.exe" service --instance $TenantName --start --console | Write-Host
if ($lastExitCode -ne 0)
{
throw "Installation failed on service run"
}
}
function Execute-WithRetries([ScriptBlock]$Command, $CommandName, $MiniumumWaitInSeconds = 1)
{
$currentRetry = 0;
$success = $false;
do {
try
{
$result = & $Command;
$success = $true;
Log "Successfully executed [$CommandName] command. Number of retries: $currentRetry";
return $result
}
catch [System.Exception]
{
$message = "Exception occurred while trying to execute [$CommandName] command:" + $_.Exception.ToString();
Log $message;
if ($currentRetry -gt 10) {
$message = "The last allowed retry ($currentRetry) for [$CommandName] command failed. There will be no more retries."
throw $message;
} else {
Log "Sleeping before $currentRetry retry of [$CommandName] command";
Start-Sleep -s ($MiniumumWaitInSeconds + $currentRetry);
}
$currentRetry = $currentRetry + 1;
}
}while (!$success);
}
function Wait-ForServerToBecomeAvailable
{
Execute-WithRetries -CommandName "Wait-ForServerToBecomeAvailable" -MiniumumWaitInSeconds 10 -Command {
$serviceName = "OctopusDeploy: $TenantName";
$status = (Get-Service -Name $serviceName).Status;
if ($status -eq "Stopped") {
Log "$serviceName stopped. Trying to start ...."
Start-Service -Name $serviceName
}
Log "Waiting for $Version of server to become available at http://$friendlyServerUrl."
$result = Invoke-RestMethod -Method Get -Uri "http://$friendlyServerUrl/api" -TimeoutSec 300
Log "Waiting for $Version of server to become available. Current version: $($result.Version)"
if ($Version -ne $result.Version) {
throw "Expected server version ($Version) is different from the current version $($result.Version)"
}
}
}
function Setup-VM
{
$serverDownloadSavePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\\PSTools.zip")
if ((test-path $serverDownloadSavePath) -ne $true)
{
Log "Downloading tools 'https://download.sysinternals.com/files/PSTools.zip' to '$serverDownloadSavePath'..."
Download-File -url "https://download.sysinternals.com/files/PSTools.zip" -saveAs $serverDownloadSavePath
$folder = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\\PSTools")
Expand-Archive $serverDownloadSavePath -DestinationPath $folder
}
else
{
Log "Skipping downloading tools '$serverDownloadSavePath' already exists"
}
& .\PsTools\PsExec64.exe -accepteula -s cmdkey /add:$StorageAccount /user:$StorageUser /pass:$StoragePassword
}
Log "Octopus is not installed. Installing $version"
Setup-VM
Install-Server
Configure-Server
Start-Server
Wait-ForServerToBecomeAvailable
Log "Server deployment finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.