2017-11-20 19:15:20 +08:00
#!/usr/bin/env bash
2016-10-21 07:56:04 +08:00
2019-10-22 01:54:17 +08:00
# Copyright The Helm Authors.
2016-10-21 07:56:04 +08:00
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# The install script is based off of the MIT-licensed script from glide,
# the package manager for Go: https://github.com/Masterminds/glide.sh/blob/master/get
PROJECT_NAME="helm"
2019-10-22 01:54:17 +08:00
TILLER_NAME="tiller"
2016-10-21 07:56:04 +08:00
2019-10-22 01:54:17 +08:00
: ${USE_SUDO:="true"}
2016-10-21 07:56:04 +08:00
: ${HELM_INSTALL_DIR:="/usr/local/bin"}
# initArch discovers the architecture for this system.
initArch() {
ARCH=$(uname -m)
case $ARCH in
armv5*) ARCH="armv5";;
armv6*) ARCH="armv6";;
2019-10-22 01:54:17 +08:00
armv7*) ARCH="arm";;
2016-10-21 07:56:04 +08:00
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
}
2017-06-28 15:41:23 +08:00
# runs the given command as root (detects if we are root already)
runAsRoot() {
2021-02-09 17:47:27 +08:00
if [ $EUID -ne 0 -a "$USE_SUDO" = "true" ]; then
sudo "${@}"
else
"${@}"
2017-06-28 15:41:23 +08:00
fi
}
2016-10-21 07:56:04 +08:00
# verifySupported checks that the os/arch combination is supported for
# binary builds.
verifySupported() {
2024-05-12 16:19:40 +08:00
local supported="darwin-amd64\nlinux-386\nlinux-amd64\nlinux-arm\nlinux-arm64\nlinux-ppc64le\nlinux-s390x\nlinux-riscv64\nwindows-amd64\nwindows-arm64"
2016-10-21 07:56:04 +08:00
if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
2017-12-30 06:40:42 +08:00
echo "No prebuilt binary for ${OS}-${ARCH}."
2018-11-29 02:08:38 +08:00
echo "To build from source, go to https://github.com/helm/helm"
2016-10-21 07:56:04 +08:00
exit 1
fi
2016-11-15 02:55:33 +08:00
if ! type "curl" > /dev/null && ! type "wget" > /dev/null; then
2016-10-21 07:56:04 +08:00
echo "Either curl or wget is required"
exit 1
fi
}
2017-05-03 17:36:09 +08:00
# checkDesiredVersion checks if the desired version is available.
checkDesiredVersion() {
2020-08-17 10:47:26 +08:00
if [ "x$DESIRED_VERSION" == "x" ]; then
2021-04-20 14:50:40 +08:00
# Pinning tag to v2.17.0 as per https://github.com/helm/helm/issues/9607
TAG=v2.17.0
2019-10-22 01:54:17 +08:00
else
TAG=$DESIRED_VERSION
2016-10-21 07:56:04 +08:00
fi
2017-03-20 06:11:28 +08:00
}
# checkHelmInstalledVersion checks which version of helm is installed and
2017-05-03 17:36:09 +08:00
# if it needs to be changed.
2017-03-20 06:11:28 +08:00
checkHelmInstalledVersion() {
if [[ -f "${HELM_INSTALL_DIR}/${PROJECT_NAME}" ]]; then
2019-10-22 01:54:17 +08:00
local version=$("${HELM_INSTALL_DIR}/${PROJECT_NAME}" version -c | grep '^Client' | cut -d'"' -f2)
2017-03-20 06:11:28 +08:00
if [[ "$version" == "$TAG" ]]; then
2017-05-03 17:36:09 +08:00
echo "Helm ${version} is already ${DESIRED_VERSION:-latest}"
2017-03-20 06:11:28 +08:00
return 0
else
2017-05-03 17:36:09 +08:00
echo "Helm ${TAG} is available. Changing from version ${version}."
2017-03-20 06:11:28 +08:00
return 1
fi
else
return 1
fi
}
2016-10-21 07:56:04 +08:00
2017-03-20 06:11:28 +08:00
# downloadFile downloads the latest binary package and also the checksum
# for that binary.
downloadFile() {
2016-10-21 07:56:04 +08:00
HELM_DIST="helm-$TAG-$OS-$ARCH.tar.gz"
2019-05-10 23:04:27 +08:00
DOWNLOAD_URL="https://get.helm.sh/$HELM_DIST"
2016-10-21 07:56:04 +08:00
CHECKSUM_URL="$DOWNLOAD_URL.sha256"
2017-10-03 16:41:34 +08:00
HELM_TMP_ROOT="$(mktemp -dt helm-installer-XXXXXX)"
2017-09-29 22:39:42 +08:00
HELM_TMP_FILE="$HELM_TMP_ROOT/$HELM_DIST"
HELM_SUM_FILE="$HELM_TMP_ROOT/$HELM_DIST.sha256"
2016-10-21 07:56:04 +08:00
echo "Downloading $DOWNLOAD_URL"
if type "curl" > /dev/null; then
2017-04-24 18:29:59 +08:00
curl -SsL "$CHECKSUM_URL" -o "$HELM_SUM_FILE"
2016-10-21 07:56:04 +08:00
elif type "wget" > /dev/null; then
wget -q -O "$HELM_SUM_FILE" "$CHECKSUM_URL"
fi
if type "curl" > /dev/null; then
2017-04-24 18:29:59 +08:00
curl -SsL "$DOWNLOAD_URL" -o "$HELM_TMP_FILE"
2016-10-21 07:56:04 +08:00
elif type "wget" > /dev/null; then
wget -q -O "$HELM_TMP_FILE" "$DOWNLOAD_URL"
fi
}
# installFile verifies the SHA256 for the file, then unpacks and
# installs it.
installFile() {
2017-09-29 22:39:42 +08:00
HELM_TMP="$HELM_TMP_ROOT/$PROJECT_NAME"
2017-08-26 03:43:44 +08:00
local sum=$(openssl sha1 -sha256 ${HELM_TMP_FILE} | awk '{print $2}')
2016-10-21 07:56:04 +08:00
local expected_sum=$(cat ${HELM_SUM_FILE})
if [ "$sum" != "$expected_sum" ]; then
2018-03-15 16:05:13 +08:00
echo "SHA sum of ${HELM_TMP_FILE} does not match. Aborting."
2016-10-21 07:56:04 +08:00
exit 1
fi
mkdir -p "$HELM_TMP"
tar xf "$HELM_TMP_FILE" -C "$HELM_TMP"
HELM_TMP_BIN="$HELM_TMP/$OS-$ARCH/$PROJECT_NAME"
2019-10-22 01:54:17 +08:00
TILLER_TMP_BIN="$HELM_TMP/$OS-$ARCH/$TILLER_NAME"
echo "Preparing to install $PROJECT_NAME and $TILLER_NAME into ${HELM_INSTALL_DIR}"
2021-11-01 18:06:51 +08:00
runAsRoot cp "$HELM_TMP_BIN" "$HELM_INSTALL_DIR/$PROJECT_NAME"
2019-10-22 01:54:17 +08:00
echo "$PROJECT_NAME installed into $HELM_INSTALL_DIR/$PROJECT_NAME"
if [ -x "$TILLER_TMP_BIN" ]; then
2021-11-01 18:06:51 +08:00
runAsRoot cp "$TILLER_TMP_BIN" "$HELM_INSTALL_DIR/$TILLER_NAME"
2019-10-22 01:54:17 +08:00
echo "$TILLER_NAME installed into $HELM_INSTALL_DIR/$TILLER_NAME"
else
echo "info: $TILLER_NAME binary was not found in this release; skipping $TILLER_NAME installation"
fi
2016-10-21 07:56:04 +08:00
}
# fail_trap is executed if an error occurs.
fail_trap() {
result=$?
if [ "$result" != "0" ]; then
2017-05-03 17:36:09 +08:00
if [[ -n "$INPUT_ARGUMENTS" ]]; then
echo "Failed to install $PROJECT_NAME with the arguments provided: $INPUT_ARGUMENTS"
help
else
echo "Failed to install $PROJECT_NAME"
fi
2018-11-29 02:08:38 +08:00
echo -e "\tFor support, go to https://github.com/helm/helm."
2016-10-21 07:56:04 +08:00
fi
2017-09-29 22:39:42 +08:00
cleanup
2016-10-21 07:56:04 +08:00
exit $result
}
# testVersion tests the installed client to make sure it is working.
testVersion() {
set +e
2020-05-13 07:08:53 +08:00
HELM="$(command -v $PROJECT_NAME)"
2016-10-21 07:56:04 +08:00
if [ "$?" = "1" ]; then
echo "$PROJECT_NAME not found. Is $HELM_INSTALL_DIR on your "'$PATH?'
exit 1
fi
set -e
echo "Run '$PROJECT_NAME init' to configure $PROJECT_NAME."
}
2017-05-03 17:36:09 +08:00
# help provides possible cli installation arguments
help () {
echo "Accepted cli arguments are:"
echo -e "\t[--help|-h ] ->> prints this help"
2019-11-13 10:11:56 +08:00
echo -e "\t[--version|-v <desired_version>]"
2017-05-03 17:36:09 +08:00
echo -e "\te.g. --version v2.4.0 or -v latest"
2019-10-22 01:54:17 +08:00
echo -e "\t[--no-sudo] ->> install without sudo"
2017-05-03 17:36:09 +08:00
}
2018-11-29 02:08:38 +08:00
# cleanup temporary files to avoid https://github.com/helm/helm/issues/2977
2017-09-29 22:39:42 +08:00
cleanup() {
2018-03-30 14:59:06 +08:00
if [[ -d "${HELM_TMP_ROOT:-}" ]]; then
rm -rf "$HELM_TMP_ROOT"
fi
2017-09-29 22:39:42 +08:00
}
2016-10-21 07:56:04 +08:00
# Execution
#Stop execution on any error
trap "fail_trap" EXIT
set -e
2017-05-03 17:36:09 +08:00
# Parsing input arguments (if any)
export INPUT_ARGUMENTS="${@}"
set -u
while [[ $# -gt 0 ]]; do
case $1 in
2017-09-06 16:40:50 +08:00
'--version'|-v)
2017-05-03 17:36:09 +08:00
shift
2017-09-06 16:40:50 +08:00
if [[ $# -ne 0 ]]; then
2019-11-13 11:56:34 +08:00
export DESIRED_VERSION="${1}"
2022-12-17 04:42:35 +08:00
if [[ "$1" != "v"* ]]; then
echo "Expected version arg ('${DESIRED_VERSION}') to begin with 'v', fixing..."
export DESIRED_VERSION="v${1}"
fi
2017-09-06 16:40:50 +08:00
else
echo -e "Please provide the desired version. e.g. --version v2.4.0 or -v latest"
exit 0
fi
2017-05-03 17:36:09 +08:00
;;
2019-10-22 01:54:17 +08:00
'--no-sudo')
USE_SUDO="false"
;;
2017-09-29 22:39:42 +08:00
'--help'|-h)
2017-05-03 17:36:09 +08:00
help
exit 0
;;
*) exit 1
;;
esac
shift
done
set +u
2016-10-21 07:56:04 +08:00
initArch
initOS
verifySupported
2017-05-03 17:36:09 +08:00
checkDesiredVersion
2017-03-20 06:11:28 +08:00
if ! checkHelmInstalledVersion; then
downloadFile
installFile
fi
2016-10-21 07:56:04 +08:00
testVersion
2017-09-29 22:39:42 +08:00
cleanup