Created
September 4, 2018 01:27
-
-
Save droyad/3beec05ba4d40d504e33a4997fc62cac to your computer and use it in GitHub Desktop.
SSH Target Health Check
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
#!/bin/bash | |
# Users can write their own custom script via a machine policy, which is appended here via MachinePolicyCustomScript. | |
function perform_standardhealthcheck | |
{ | |
# Mono is required if not using self-contained Calamari | |
requiresMono={{RequiresMono}} | |
# Ensure required commands are available | |
declare -a arr=("df" "ls" "grep" "openssl" "mkdir" "tar") | |
deps=0 | |
for cmd in "${arr[@]}"; | |
do | |
assert_command_exists "$cmd" | |
done; | |
if [ "$requiresMono" = "true" ]; then | |
assert_command_exists "mono" | |
fi | |
if [[ $deps != 0 ]]; then exit 127; fi | |
# Print standard diagnostics | |
echo "##octopus[stdout-default]" | |
echo "Host Name: $HOSTNAME" | |
echo "Running As: $DOMAINNAME\\$USER" | |
echo "Bash version: $BASH_VERSION" | |
echo "OS: $(uname -rsm)" | |
distro="Unknown" | |
if [ -f /etc/lsb-release ]; then | |
distro="$(lsb_release -sd) ($(lsb_release -sc))" | |
elif [ -f /etc/debian_version ]; then | |
distro="Debian "$(cat /etc/debian_version) | |
elif [ -f /etc/redhat-release ]; then | |
distro=$(cat /etc/redhat-release) | |
elif [ -f /etc/SuSE-release ]; then | |
distro=$(head -1 /etc/SuSE-release) | |
elif [ -f /usr/bin/sw_vers ]; then | |
distro="$(sw_vers -productName) $(sw_vers -productVersion)" | |
fi | |
echo "Distro:" $distro | |
set_octopusvariable "OSName" "$distro" | |
set_octopusvariable "ShellName" "Bash" | |
set_octopusvariable "ShellVersion" "$BASH_VERSION" | |
df -h | |
if [ "$requiresMono" = "true" ]; then | |
# Print and return Mono version | |
property=$(echo MonoVersion | openssl enc -base64 -A) | |
value=$(mono -V | grep version | grep -o '\([0-9]\{1,3\}\.[0-9]\{1,4\}\.\{0,1\}[0-9]\{1,4\}\)' | openssl enc -base64 -A) #OpenBSD has bad support for `grep -o` | |
echo "##octopus[stdout-default]" | |
echo "Mono version:" $(mono -V | grep version) | |
echo "##octopus[stdout-verbose]" | |
echo "##octopus[setVariable name='"$property"' value='"$value"']" | |
fi | |
# Ensure Calamari up to date | |
ls "{{CalamariLocation}}/success" &>/dev/null && echo "Calamari version: {{CalamariVersion}}" && hasLatestCalamari=true || (echo_warning "Not running latest version of Calamari"); | |
property=$(echo -n HasLatestCalamariVersion | openssl enc -base64 -A) | |
if test "$hasLatestCalamari" = ""; then | |
hasLatestCalamari=false | |
fi | |
value=$(echo -n $hasLatestCalamari | openssl enc -base64 -A) | |
echo "##octopus[setVariable name='"$property"' value='"$value"']" | |
} | |
# ----------------------------------------------------------------------------- | |
# Asserts a command is available, writes to StdErr if it is not | |
# Accepts 1 argument: | |
# string: the command | |
# ----------------------------------------------------------------------------- | |
function assert_command_exists | |
{ | |
command -v "$1" &>/dev/null | |
if [[ $? != 0 ]]; then | |
echo >&2 "Required command '$1' is not available."; | |
deps=1; | |
fi | |
} | |
# ----------------------------------------------------------------------------- | |
# Function to base64 encode a service message value | |
# Accepts 1 argument: | |
# string: the value to encode | |
# ----------------------------------------------------------------------------- | |
function encode_servicemessagevalue | |
{ | |
echo -n "$1" | openssl enc -base64 -A | |
} | |
# ----------------------------------------------------------------------------- | |
# Function to base64 decode a service message value | |
# Accepts 1 argument: | |
# string: the value to decode | |
# ----------------------------------------------------------------------------- | |
function decode_servicemessagevalue | |
{ | |
echo -n "$1" | openssl enc -base64 -A -d | |
} | |
# --------------------------------------------------------------------------- | |
# Function for getting an octopus variable | |
# Accepts 1 argument: | |
# string: value of the name of the octopus variable | |
# --------------------------------------------------------------------------- | |
function get_octopusvariable | |
{ | |
INPUT=$( encode_servicemessagevalue "$1" ) | |
case $INPUT in | |
{{VariableDeclarations}} | |
*) | |
echo "Unrecognized variable \"$1\"" | |
;; | |
esac | |
} | |
# --------------------------------------------------------------------------- | |
# Function for setting an octopus variable | |
# Accepts 2 arguments: | |
# string: value of the name of the octopus variable | |
# string: value of the value of the octopus variable | |
# --------------------------------------------------------------------------- | |
function set_octopusvariable | |
{ | |
MESSAGE="##octopus[setVariable" | |
if [ -n "$1" ] | |
then | |
MESSAGE="$MESSAGE name='$(encode_servicemessagevalue "$1")'" | |
fi | |
if [ -n "$2" ] | |
then | |
MESSAGE="$MESSAGE value='$(encode_servicemessagevalue "$2")'" | |
fi | |
MESSAGE="$MESSAGE]" | |
echo $MESSAGE | |
} | |
function fail_healthcheck | |
{ | |
echo_error "$1" | |
finish_healthcheck | |
} | |
function echo_warning | |
{ | |
echo "##octopus[stdout-warning]" && echo "$1" && echo "##octopus[stdout-default]" | |
set_octopusvariable "HasWarnings" "true" | |
add_healthcheckmessage "Warning: $1" | |
} | |
function echo_error | |
{ | |
echo >&2 "$1" | |
add_healthcheckmessage "Error: $1" | |
has_errors=true | |
} | |
function finish_healthcheck | |
{ | |
set_octopusvariable "OctopusHealthCheckMessages" "$details" | |
if [ "$has_errors" = true ] ; then | |
exit 1 | |
fi | |
} | |
function add_healthcheckmessage | |
{ | |
if [ -z "$details" ]; then | |
details="$1" | |
else | |
details=$details$'\n'$1 | |
fi | |
} | |
has_errors=false | |
perform_standardhealthcheck | |
# Machine policy custom script | |
{{MachinePolicyCustomScript}} | |
finish_healthcheck |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment