Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Last active October 29, 2018 06:18
Show Gist options
  • Save pawelpabich/cb346f485dd7e0f2467fc278728ca495 to your computer and use it in GitHub Desktop.
Save pawelpabich/cb346f485dd7e0f2467fc278728ca495 to your computer and use it in GitHub Desktop.
param([string] $VirtualMachineFullDNSName, [string] $ConnectionString, [string] $AdministratorPassword, [string] $EncodedLicense, [string] $StoragePath, [string] $InstanceName, [Int] $ServerCommPort)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest;
function Log($Message)
{
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss,fff"
$newLine = [System.Environment]::NewLine
$logEntry = "$timestamp $Message $newLine"
Write-Host $logEntry
Add-Content -Path "C:\Temp\Log.txt" -Value $logEntry
}
$serverpath = "C:\\Octopus"
$configpath = $serverpath + "\\${InstanceName}\\OctopusDeploy.config"
$octopusInstallationPath = "${env:ProgramFiles}\\Octopus Deploy\\Octopus"
$friendlyServerUrl = "$VirtualMachineFullDNSName/$InstanceName";
Log "Deploying a new instance of Octopus Server available at http://$friendlyServerUrl"
function Configure-Server
{
Log "Beginning Octopus Server configuration"
& netsh.exe firewall add portopening TCP $ServerCommPort "Octopus Server Comms Port"
if ($lastExitCode -ne 0)
{
throw "Installation failed when modifying firewall rules"
}
Log "Creating instance {$InstanceName}"
cd "${env:ProgramFiles}\\Octopus Deploy\\Octopus"
&".\\Octopus.Server.exe" create-instance --instance $InstanceName --config $configpath --console | Write-Host
if ($lastExitCode -ne 0)
{
throw "Installation failed on create-instance"
}
Log "Setting connection string {$InstanceName}"
&".\\Octopus.Server.exe" database --instance $InstanceName --connectionString $ConnectionString --console | Write-Host
if ($lastExitCode -ne 0)
{
throw "Installation failed on database"
}
Log "Confiuguring instance {$InstanceName}"
&".\\Octopus.Server.exe" configure --instance $InstanceName --webCorsWhitelist=* --home $serverpath --upgradeCheck "False" --upgradeCheckWithStatistics "False" --usernamePasswordIsEnabled=true --webForceSSL "False" --webListenPrefixes "http://localhost/$InstanceName" --commsListenPort "$ServerCommPort" --serverNodeName "$InstanceName" --hstsEnabled=true --hstsMaxAge=31556926 --console | Write-Host
if ($lastExitCode -ne 0)
{
throw "Installation failed on configure"
}
Log "Setting paths {$InstanceName}"
&".\\Octopus.Server.exe" path --instance $InstanceName --taskLogs="$StoragePath\TaksLogs" --artifacts="$StoragePath\Artifacts" --nugetRepository="$StoragePath\Packages"
Log "Setting admin credentials {$InstanceName}"
&".\\Octopus.Server.exe" admin --instance $InstanceName --username "Admin" --password $AdministratorPassword --console | Write-Host
Log "Setting licence {$InstanceName}"
&".\\Octopus.Server.exe" license --instance $InstanceName --licenseBase64 $EncodedLicense --console | Write-Host
Log "Octopus commands complete"
}
function Start-Server
{
Log "Installing server {$InstanceName}"
&".\\Octopus.Server.exe" service --instance $InstanceName --install --console | Write-Host
if ($lastExitCode -ne 0)
{
throw "Installation failed on service install"
}
Log "Starting server {$InstanceName}"
&".\\Octopus.Server.exe" service --instance $InstanceName --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. $InstanceName";
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: $InstanceName";
$status = (Get-Service -Name $serviceName).Status;
if ($status -eq "Stopped") {
Log "$serviceName stopped. Trying to start ...."
Start-Service -Name $serviceName
}
Log "Waiting for server to become available at http://$friendlyServerUrl."
$result = Invoke-RestMethod -Method Get -Uri "http://$friendlyServerUrl/api" -TimeoutSec 300
if ("Octopus Deploy" -ne $result.Application) {
throw "Application name $($result.Application) is not set to 'Octopus Deploy'"
}
}
}
Log "Octopus instance is not installed. Installing $InstanceName"
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