Skip to content

Instantly share code, notes, and snippets.

@larsfuehrer
Last active February 27, 2021 10:02
Show Gist options
  • Save larsfuehrer/c21e5c529cf546627b8ac8a8891412df to your computer and use it in GitHub Desktop.
Save larsfuehrer/c21e5c529cf546627b8ac8a8891412df to your computer and use it in GitHub Desktop.
Script to get Kubeval
#!/usr/bin/env bash
: ${BINARY_NAME:="kubeval"}
: ${USE_SUDO:="true"}
: ${DEBUG:="false"}
: ${KUBEVAL_INSTALL_DIR:="/usr/local/bin"}
HAS_CURL="$(type "curl" &> /dev/null && echo true || echo false)"
HAS_WGET="$(type "wget" &> /dev/null && echo true || echo false)"
# initArch discovers the architecture for this system.
initArch() {
ARCH=$(uname -m)
case $ARCH in
armv5*) ARCH="armv5";;
armv6*) ARCH="armv6";;
armv7*) ARCH="arm";;
aarch64) ARCH="arm64";;
x86) ARCH="386";;
x86_64) ARCH="amd64";;
i686) ARCH="386";;
i386) ARCH="386";;
esac
}
# initOS discovers the operating system for this system.
initOS() {
OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')
case "$OS" in
# Minimalist GNU for Windows
mingw*) OS='windows';;
esac
}
# runs the given command as root (detects if we are root already)
runAsRoot() {
local CMD="$*"
if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then
CMD="sudo $CMD"
fi
$CMD
}
# verifySupported checks that the os/arch combination is supported for
# binary builds, as well whether or not necessary tools are present.
verifySupported() {
local supported="darwin-amd64\nlinux-386\nlinux-amd64\nlinux-arm\nlinux-arm64\nlinux-ppc64le\nlinux-s390x\nwindows-amd64"
if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
echo "No prebuilt binary for ${OS}-${ARCH}."
echo "To build from source, go to https://github.com/instrumenta/kubeval"
exit 1
fi
if [ "${HAS_CURL}" != "true" ] && [ "${HAS_WGET}" != "true" ]; then
echo "Either curl or wget is required"
exit 1
fi
}
# checkDesiredVersion checks if the desired version is available.
checkDesiredVersion() {
if [ "x$DESIRED_VERSION" == "x" ]; then
# Get tag from release URL
local latest_release_url="https://github.com/instrumenta/kubeval/releases"
if [ "${HAS_CURL}" == "true" ]; then
TAG=$(curl -Ls $latest_release_url | grep 'href="/instrumenta/kubeval/releases/tag/0.[0-9]*.[0-9]*\"' | grep -v no-underline | head -n 1 | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}' | awk 'a !~ $0{print}; {a=$0}')
elif [ "${HAS_WGET}" == "true" ]; then
TAG=$(wget $latest_release_url -O - 2>&1 | grep 'href="/instrumenta/kubeval/releases/tag/0.[0-9]*.[0-9]*\"' | grep -v no-underline | head -n 1 | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}' | awk 'a !~ $0{print}; {a=$0}')
fi
else
TAG=$DESIRED_VERSION
fi
}
# checkKubevalInstalledVersion checks which version of kubeval is installed and
# if it needs to be changed.
checkKubevalInstalledVersion() {
if [[ -f "${KUBEVAL_INSTALL_DIR}/${BINARY_NAME}" ]]; then
local version=$("${KUBEVAL_INSTALL_DIR}/${BINARY_NAME}" --version | grep Version | awk '{print $2}')
if [[ "$version" == "$TAG" ]]; then
echo "Kubeval ${version} is already ${DESIRED_VERSION:-latest}"
return 0
else
echo "Kubeval ${TAG} is available. Changing from version ${version}."
return 1
fi
else
return 1
fi
}
# downloadFile downloads the latest binary package
downloadFile() {
KUBEVAL_DIST="kubeval-$OS-$ARCH.tar.gz"
DOWNLOAD_URL="https://github.com/instrumenta/kubeval/releases/download/$TAG/$KUBEVAL_DIST"
KUBEVAL_TMP_ROOT="$(mktemp -dt kubeval-installer-XXXXXX)"
KUBEVAL_TMP_FILE="$KUBEVAL_TMP_ROOT/$KUBEVAL_DIST"
echo "Downloading $DOWNLOAD_URL"
if [ "${HAS_CURL}" == "true" ]; then
curl -SsL "$DOWNLOAD_URL" -o "$KUBEVAL_TMP_FILE"
elif [ "${HAS_WGET}" == "true" ]; then
wget -q -O "$KUBEVAL_TMP_FILE" "$DOWNLOAD_URL"
fi
}
# installFile installs the Kubeval binary.
installFile() {
KUBEVAL_TMP="$KUBEVAL_TMP_ROOT/$BINARY_NAME"
mkdir -p "$KUBEVAL_TMP"
tar xf "$KUBEVAL_TMP_FILE" -C "$KUBEVAL_TMP"
KUBEVAL_TMP_BIN="$KUBEVAL_TMP/$BINARY_NAME"
echo "Preparing to install $BINARY_NAME into ${KUBEVAL_INSTALL_DIR}"
runAsRoot cp "$KUBEVAL_TMP_BIN" "$KUBEVAL_INSTALL_DIR/$BINARY_NAME"
echo "$BINARY_NAME installed into $KUBEVAL_INSTALL_DIR/$BINARY_NAME"
}
# fail_trap is executed if an error occurs.
fail_trap() {
result=$?
if [ "$result" != "0" ]; then
if [[ -n "$INPUT_ARGUMENTS" ]]; then
echo "Failed to install $BINARY_NAME with the arguments provided: $INPUT_ARGUMENTS"
help
else
echo "Failed to install $BINARY_NAME"
fi
echo -e "\tFor support, go to https://github.com/instrumenta/kubeval."
fi
cleanup
exit $result
}
# testVersion tests the installed client to make sure it is working.
testVersion() {
set +e
KUBEVAL="$(command -v $BINARY_NAME)"
if [ "$?" = "1" ]; then
echo "$BINARY_NAME not found. Is $KUBEVAL_INSTALL_DIR on your "'$PATH?'
exit 1
fi
set -e
}
# help provides possible cli installation arguments
help () {
echo "Accepted cli arguments are:"
echo -e "\t[--help|-h ] ->> prints this help"
echo -e "\t[--version|-v <desired_version>] . When not defined it fetches the latest release from GitHub"
echo -e "\te.g. --version 0.15.0"
echo -e "\t[--no-sudo] ->> install without sudo"
}
# cleanup temporary files
cleanup() {
if [[ -d "${KUBEVAL_TMP_ROOT:-}" ]]; then
rm -rf "$KUBEVAL_TMP_ROOT"
fi
}
# Execution
#Stop execution on any error
trap "fail_trap" EXIT
set -e
# Set debug if desired
if [ "${DEBUG}" == "true" ]; then
set -x
fi
# Parsing input arguments (if any)
export INPUT_ARGUMENTS="${@}"
set -u
while [[ $# -gt 0 ]]; do
case $1 in
'--version'|-v)
shift
if [[ $# -ne 0 ]]; then
export DESIRED_VERSION="${1}"
else
echo -e "Please provide the desired version. e.g. --version 0.15.0"
exit 0
fi
;;
'--no-sudo')
USE_SUDO="false"
;;
'--help'|-h)
help
exit 0
;;
*) exit 1
;;
esac
shift
done
set +u
initArch
initOS
verifySupported
checkDesiredVersion
if ! checkKubevalInstalledVersion; then
downloadFile
installFile
fi
testVersion
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment