Skip to content

Add script functions for reporting Kubernetes manifests from scripts #1474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
216 changes: 140 additions & 76 deletions source/Calamari.Common/Features/Scripting/Bash/Bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sensitiveVariableKey=$1
# -----------------------------------------------------------------------------
function encode_servicemessagevalue
{
echo -n "$1" | openssl enc -base64 -A
echo -n "$1" | openssl enc -base64 -A
}

# -----------------------------------------------------------------------------
Expand All @@ -22,7 +22,7 @@ function encode_servicemessagevalue
# -----------------------------------------------------------------------------
function decode_servicemessagevalue
{
echo -n "$1" | openssl enc -base64 -A -d
echo -n "$1" | openssl enc -base64 -A -d
}

# -----------------------------------------------------------------------------
Expand All @@ -46,7 +46,7 @@ __mask_sensitive_value $sensitiveVariableKey
# -----------------------------------------------------------------------------
function decrypt_variable
{
echo $1 | openssl enc -a -A -d -aes-256-cbc -nosalt -K $sensitiveVariableKey -iv $2
echo $1 | openssl enc -a -A -d -aes-256-cbc -nosalt -K $sensitiveVariableKey -iv $2
}

# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -74,12 +74,12 @@ function get_octopusvariable
function fail_step
{

if [ ! -z "${1:-}" ]
then
echo "##octopus[resultMessage message='$(encode_servicemessagevalue "$1")']"
fi
if [ ! -z "${1:-}" ]
then
echo "##octopus[resultMessage message='$(encode_servicemessagevalue "$1")']"
fi

exit 1;
exit 1;
}

# ---------------------------------------------------------------------------
Expand All @@ -91,26 +91,26 @@ function fail_step
# ---------------------------------------------------------------------------
function set_octopusvariable
{
MESSAGE="##octopus[setVariable"
MESSAGE="##octopus[setVariable"

if [ -n "$1" ]
then
MESSAGE="$MESSAGE name='$(encode_servicemessagevalue "$1")'"
fi
if [ -n "$1" ]
then
MESSAGE="$MESSAGE name='$(encode_servicemessagevalue "$1")'"
fi

if [ -n "$2" ]
then
MESSAGE="$MESSAGE value='$(encode_servicemessagevalue "$2")'"
fi
if [ -n "$2" ]
then
MESSAGE="$MESSAGE value='$(encode_servicemessagevalue "$2")'"
fi

if [ ! -z "${3:-}" ] && [ "$3" = "-sensitive" ]
then
MESSAGE="$MESSAGE sensitive='$(encode_servicemessagevalue "True")'"
fi
if [ ! -z "${3:-}" ] && [ "$3" = "-sensitive" ]
then
MESSAGE="$MESSAGE sensitive='$(encode_servicemessagevalue "True")'"
fi

MESSAGE="$MESSAGE]"
MESSAGE="$MESSAGE]"

echo $MESSAGE
echo $MESSAGE
}

# -----------------------------------------------------------------------------
Expand All @@ -121,31 +121,31 @@ function set_octopusvariable
# -----------------------------------------------------------------------------
function new_octopusartifact
{
echo "Collecting $1 as an artifact..."

if [ ! -e "$1" ]
then
error_exit $PROGNAME $LINENO "\"$(1)\" does not exist." $E_FILE_NOT_FOUND
exit $?
fi

pth=$1
ofn=$2
len=$(wc -c < $1 )

if [ -z "$ofn" ]
then
ofn=`basename "$pth"`
fi

echo "##octopus[stdout-verbose]"
echo "Artifact $ofn will be collected from $pth after this step completes"
echo "##octopus[stdout-default]"
echo "##octopus[createArtifact path='$(encode_servicemessagevalue "$pth")' name='$(encode_servicemessagevalue "$ofn")' length='$(encode_servicemessagevalue $len)']"
echo "Collecting $1 as an artifact..."

if [ ! -e "$1" ]
then
error_exit $PROGNAME $LINENO "\"$(1)\" does not exist." $E_FILE_NOT_FOUND
exit $?
fi

pth=$1
ofn=$2
len=$(wc -c < $1 )

if [ -z "$ofn" ]
then
ofn=`basename "$pth"`
fi

echo "##octopus[stdout-verbose]"
echo "Artifact $ofn will be collected from $pth after this step completes"
echo "##octopus[stdout-default]"
echo "##octopus[createArtifact path='$(encode_servicemessagevalue "$pth")' name='$(encode_servicemessagevalue "$ofn")' length='$(encode_servicemessagevalue $len)']"
}

function remove-octopustarget {
echo "##octopus[delete-target machine='$(encode_servicemessagevalue "$1")']"
echo "##octopus[delete-target machine='$(encode_servicemessagevalue "$1")']"
}

function new_octopustarget() (
Expand Down Expand Up @@ -203,38 +203,38 @@ function new_octopustarget() (
# -----------------------------------------------------------------------------
function update_progress
{
echo "##octopus[progress percentage='$(encode_servicemessagevalue "$1")' message='$(encode_servicemessagevalue "$2")']"
echo "##octopus[progress percentage='$(encode_servicemessagevalue "$1")' message='$(encode_servicemessagevalue "$2")']"
}

# -----------------------------------------------------------------------------
# Functions write a messages as different levels
# -----------------------------------------------------------------------------
function write_verbose
{
echo "##octopus[stdout-verbose]"
echo $1
echo "##octopus[stdout-default]"
echo "##octopus[stdout-verbose]"
echo $1
echo "##octopus[stdout-default]"
}

function write_highlight
{
echo "##octopus[stdout-highlight]"
echo $1
echo "##octopus[stdout-default]"
echo "##octopus[stdout-highlight]"
echo $1
echo "##octopus[stdout-default]"
}

function write_wait
{
echo "##octopus[stdout-wait]"
echo $1
echo "##octopus[stdout-default]"
echo "##octopus[stdout-wait]"
echo $1
echo "##octopus[stdout-default]"
}

function write_warning
{
echo "##octopus[stdout-warning]"
echo $1
echo "##octopus[stdout-default]"
echo "##octopus[stdout-warning]"
echo $1
echo "##octopus[stdout-default]"
}


Expand All @@ -243,24 +243,24 @@ function write_warning
# -----------------------------------------------------------------------------
function log_environment_information
{
suppressEnvironmentLogging=$(get_octopusvariable "Octopus.Action.Script.SuppressEnvironmentLogging")
if [ "$suppressEnvironmentLogging" == "True" ]
then
return 0
fi

echo "##octopus[stdout-verbose]"
echo "Bash Environment Information:"
echo " OperatingSystem: $(uname -a)"
echo " CurrentUser: $(whoami)"
echo " HostName: $(hostname)"
echo " ProcessorCount: $(getconf _NPROCESSORS_ONLN)"
currentDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo " CurrentDirectory: $currentDirectory"
tempDirectory=$(dirname $(mktemp -u))
echo " TempDirectory: $tempDirectory"
echo " HostProcessID: $$"
echo "##octopus[stdout-default]"
suppressEnvironmentLogging=$(get_octopusvariable "Octopus.Action.Script.SuppressEnvironmentLogging")
if [ "$suppressEnvironmentLogging" == "True" ]
then
return 0
fi

echo "##octopus[stdout-verbose]"
echo "Bash Environment Information:"
echo " OperatingSystem: $(uname -a)"
echo " CurrentUser: $(whoami)"
echo " HostName: $(hostname)"
echo " ProcessorCount: $(getconf _NPROCESSORS_ONLN)"
currentDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo " CurrentDirectory: $currentDirectory"
tempDirectory=$(dirname $(mktemp -u))
echo " TempDirectory: $tempDirectory"
echo " HostProcessID: $$"
echo "##octopus[stdout-default]"
}

log_environment_information
Expand Down Expand Up @@ -307,6 +307,70 @@ function decrypt_and_parse_variables {
exec 3<&-
}

# -----------------------------------------------------------------------------
# Functions to report Kubernetes Manifests via service message
# Accepts 2 arguments:
# string: the namespace
# string: the manifest
# -----------------------------------------------------------------------------
function report_kubernetes_manifest
{
MANIFEST="$1"
NAMESPACE="$2"

LINES=()

# Read lines from file into the array
while IFS= read -r LINE; do
LINES+=("$LINE")
done <<< "$MANIFEST"

MANIFESTS=()

CURRENT=""
for LINE in "${LINES[@]}"; do
if [ "$(echo "$LINE" | xargs)" = "---" ]; then
MANIFESTS+=("$CURRENT")
CURRENT=""
else
CURRENT="$CURRENT$LINE\n"
fi
done

MANIFESTS+=("$CURRENT")

for mf in "${MANIFESTS[@]}"; do
if [ -z "$mf" ]; then
continue
fi

MESSAGE="##octopus[k8s-manifest-applied manifest='$(encode_servicemessagevalue "$mf")'"

if [ -n "$NAMESPACE" ]; then
MESSAGE="$MESSAGE ns='$(encode_servicemessagevalue "$NAMESPACE")'"
fi

MESSAGE="$MESSAGE]"

echo "$MESSAGE"
done
}

function report_kubernetes_manifest_file
{
FULL_PATH="$1"
NAMESPACE="$2"

if [ ! -f "$FULL_PATH" ]; then
write_verbose "No Kubernetes manifest file was found at '$FULL_PATH'."
return
fi

MANIFEST=$(cat "$FULL_PATH")

report_kubernetes_manifest "$MANIFEST" "$NAMESPACE"
}

bashParametersArrayFeatureToggle=#### BashParametersArrayFeatureToggle ####

if [ "$bashParametersArrayFeatureToggle" = true ]; then
Expand Down
Loading