Created
April 4, 2018 23:38
-
-
Save droyad/eb3f788d9ef03a8d81761e7bdb9d6f07 to your computer and use it in GitHub Desktop.
Octopus Health Check Script
This file contains 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
# Users can write their own custom script via a machine policy, which is appended here via MachinePolicyCustomScript. | |
$hasErrors = $false | |
$details = @() | |
function Convert-ServiceMessageValue([string]$value) | |
{ | |
$valueBytes = [System.Text.Encoding]::UTF8.GetBytes($value) | |
return [Convert]::ToBase64String($valueBytes) | |
} | |
function Set-OctopusVariable([string]$name, [string]$value) | |
{ | |
$name = Convert-ServiceMessageValue($name) | |
$value = Convert-ServiceMessageValue($value) | |
Write-Host "##octopus[setVariable name='$($name)' value='$($value)']" | |
} | |
function Write-Warning([string]$message) | |
{ | |
Write-Host "##octopus[stdout-warning]" | |
Write-Host $message | |
Set-OctopusVariable "HasWarnings" "true" | |
Write-Host "##octopus[stdout-default]" | |
$global:details += "Warning: $message" | |
} | |
function Write-Error([string] $message) | |
{ | |
Write-Host "##octopus[stdout-error]" | |
Write-Host $message | |
$global:hasErrors = $true | |
Write-Host "##octopus[stdout-default]" | |
$global:details += "Error: $message" | |
} | |
function Fail-HealthCheck([string]$message) | |
{ | |
Write-Error $message | |
Finish-HealthCheck | |
} | |
function Invoke-HealthCheck() | |
{ | |
$hostname = [Environment]::MachineName | |
Write-Host "Host Name: $hostname" | |
$domainName = [Environment]::UserDomainName | |
$userName = [Environment]::UserName | |
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() | |
$windowsPrincipal = new-object 'System.Security.Principal.WindowsPrincipal' $windowsIdentity | |
$isAdmin = $windowsPrincipal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) | |
Write-Host "Running As: $domainName\$userName (Local Administrator: $isAdmin)" | |
Write-Host "Running Tentacle version ${env:TentacleVersion}" | |
Set-OctopusVariable "TentacleVersion" ${env:TentacleVersion} | |
Write-Host "Tentacle communication uses a '${env:TentacleCertificateSignatureAlgorithm}' certificate" | |
Set-OctopusVariable "TentacleCertificateSignatureAlgorithm" ${env:TentacleCertificateSignatureAlgorithm} | |
$hasLatestCalamari = ((Test-Path "${env:TentacleHome}\Calamari")) -and (Get-ChildItem "${env:TentacleHome}\Calamari" | ? { $_ -match "{{CalamariVersion}}" }).Count -ne 0 | |
if($hasLatestCalamari) { | |
Write-Host "Running latest version of Calamari: {{CalamariVersion}}" | |
} else { | |
Write-Warning "Not running latest version of Calamari. Expected: {{CalamariVersion}}" | |
if ((Test-Path "${env:TentacleHome}\Calamari") -eq $false) { | |
Write-Warning "No versions of Calamari are installed." | |
} | |
} | |
Set-OctopusVariable "HasLatestCalamariVersion" $hasLatestCalamari | |
# ----------------------------------------------------------------- | |
# Variables | |
# ----------------------------------------------------------------- | |
{{VariableDeclarations}} | |
# ----------------------------------------------------------------- | |
# Machine policy custom script | |
# ----------------------------------------------------------------- | |
{{MachinePolicyCustomScript}} | |
} | |
function CheckDriveCapacity($DriveDetails){ | |
if($DriveDetails.FreeSpace -lt $freeDiskSpaceThreshold) { | |
Write-Warning $("Drive {0} on {1} only has {2} available" -f $DriveDetails.Name, $hostName, $(Get-FileSizeString $DriveDetails.FreeSpace)) | |
} else { | |
Write-Host $("Drive {0} has {1} available" -f $DriveDetails.Name, $(Get-FileSizeString $DriveDetails.FreeSpace)) | |
} | |
} | |
function Get-FileSizeString([long]$bytes) | |
{ | |
if ($bytes -ge 1TB) { return ($bytes / 1TB).ToString("0 TB") } | |
if ($bytes -ge 1GB) { return ($bytes / 1GB).ToString("0 GB") } | |
if ($bytes -ge 1MB) { return ($bytes / 1MB).ToString("0 MB") } | |
if ($bytes -ge 1KB) { return ($bytes / 1KB).ToString("0 KB") } | |
return $bytes.ToString("0 bytes"); | |
} | |
function Finish-HealthCheck() { | |
if ($global:details.Length -gt 0) { | |
$detailsStr = [string]::Join([Environment]::NewLine, $global:details) | |
Set-OctopusVariable "OctopusHealthCheckMessages" $detailsStr | |
} | |
if ($hasErrors) { | |
exit 1 | |
} | |
} | |
Invoke-HealthCheck | |
Finish-HealthCheck |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment