...
1#!/usr/bin/env bash
2
3# Copyright The Helm Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# The install script is based off of the MIT-licensed script from glide,
18# the package manager for Go: https://github.com/Masterminds/glide.sh/blob/master/get
19
20PROJECT_NAME="helm"
21TILLER_NAME="tiller"
22
23: ${USE_SUDO:="true"}
24: ${HELM_INSTALL_DIR:="/usr/local/bin"}
25
26# initArch discovers the architecture for this system.
27initArch() {
28 ARCH=$(uname -m)
29 case $ARCH in
30 armv5*) ARCH="armv5";;
31 armv6*) ARCH="armv6";;
32 armv7*) ARCH="arm";;
33 aarch64) ARCH="arm64";;
34 x86) ARCH="386";;
35 x86_64) ARCH="amd64";;
36 i686) ARCH="386";;
37 i386) ARCH="386";;
38 esac
39}
40
41# initOS discovers the operating system for this system.
42initOS() {
43 OS=$(echo `uname`|tr '[:upper:]' '[:lower:]')
44
45 case "$OS" in
46 # Minimalist GNU for Windows
47 mingw*) OS='windows';;
48 esac
49}
50
51# runs the given command as root (detects if we are root already)
52runAsRoot() {
53 if [ $EUID -ne 0 -a "$USE_SUDO" = "true" ]; then
54 sudo "${@}"
55 else
56 "${@}"
57 fi
58}
59
60# verifySupported checks that the os/arch combination is supported for
61# binary builds.
62verifySupported() {
63 local supported="darwin-amd64\nlinux-386\nlinux-amd64\nlinux-arm\nlinux-arm64\nlinux-ppc64le\nlinux-s390x\nlinux-riscv64\nwindows-amd64"
64 if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
65 echo "No prebuilt binary for ${OS}-${ARCH}."
66 echo "To build from source, go to https://github.com/helm/helm"
67 exit 1
68 fi
69
70 if ! type "curl" > /dev/null && ! type "wget" > /dev/null; then
71 echo "Either curl or wget is required"
72 exit 1
73 fi
74}
75
76# checkDesiredVersion checks if the desired version is available.
77checkDesiredVersion() {
78 if [ "x$DESIRED_VERSION" == "x" ]; then
79 # Pinning tag to v2.17.0 as per https://github.com/helm/helm/issues/9607
80 TAG=v2.17.0
81 else
82 TAG=$DESIRED_VERSION
83 fi
84}
85
86# checkHelmInstalledVersion checks which version of helm is installed and
87# if it needs to be changed.
88checkHelmInstalledVersion() {
89 if [[ -f "${HELM_INSTALL_DIR}/${PROJECT_NAME}" ]]; then
90 local version=$("${HELM_INSTALL_DIR}/${PROJECT_NAME}" version -c | grep '^Client' | cut -d'"' -f2)
91 if [[ "$version" == "$TAG" ]]; then
92 echo "Helm ${version} is already ${DESIRED_VERSION:-latest}"
93 return 0
94 else
95 echo "Helm ${TAG} is available. Changing from version ${version}."
96 return 1
97 fi
98 else
99 return 1
100 fi
101}
102
103# downloadFile downloads the latest binary package and also the checksum
104# for that binary.
105downloadFile() {
106 HELM_DIST="helm-$TAG-$OS-$ARCH.tar.gz"
107 DOWNLOAD_URL="https://get.helm.sh/$HELM_DIST"
108 CHECKSUM_URL="$DOWNLOAD_URL.sha256"
109 HELM_TMP_ROOT="$(mktemp -dt helm-installer-XXXXXX)"
110 HELM_TMP_FILE="$HELM_TMP_ROOT/$HELM_DIST"
111 HELM_SUM_FILE="$HELM_TMP_ROOT/$HELM_DIST.sha256"
112 echo "Downloading $DOWNLOAD_URL"
113 if type "curl" > /dev/null; then
114 curl -SsL "$CHECKSUM_URL" -o "$HELM_SUM_FILE"
115 elif type "wget" > /dev/null; then
116 wget -q -O "$HELM_SUM_FILE" "$CHECKSUM_URL"
117 fi
118 if type "curl" > /dev/null; then
119 curl -SsL "$DOWNLOAD_URL" -o "$HELM_TMP_FILE"
120 elif type "wget" > /dev/null; then
121 wget -q -O "$HELM_TMP_FILE" "$DOWNLOAD_URL"
122 fi
123}
124
125# installFile verifies the SHA256 for the file, then unpacks and
126# installs it.
127installFile() {
128 HELM_TMP="$HELM_TMP_ROOT/$PROJECT_NAME"
129 local sum=$(openssl sha1 -sha256 ${HELM_TMP_FILE} | awk '{print $2}')
130 local expected_sum=$(cat ${HELM_SUM_FILE})
131 if [ "$sum" != "$expected_sum" ]; then
132 echo "SHA sum of ${HELM_TMP_FILE} does not match. Aborting."
133 exit 1
134 fi
135
136 mkdir -p "$HELM_TMP"
137 tar xf "$HELM_TMP_FILE" -C "$HELM_TMP"
138 HELM_TMP_BIN="$HELM_TMP/$OS-$ARCH/$PROJECT_NAME"
139 TILLER_TMP_BIN="$HELM_TMP/$OS-$ARCH/$TILLER_NAME"
140 echo "Preparing to install $PROJECT_NAME and $TILLER_NAME into ${HELM_INSTALL_DIR}"
141 runAsRoot cp "$HELM_TMP_BIN" "$HELM_INSTALL_DIR/$PROJECT_NAME"
142 echo "$PROJECT_NAME installed into $HELM_INSTALL_DIR/$PROJECT_NAME"
143 if [ -x "$TILLER_TMP_BIN" ]; then
144 runAsRoot cp "$TILLER_TMP_BIN" "$HELM_INSTALL_DIR/$TILLER_NAME"
145 echo "$TILLER_NAME installed into $HELM_INSTALL_DIR/$TILLER_NAME"
146 else
147 echo "info: $TILLER_NAME binary was not found in this release; skipping $TILLER_NAME installation"
148 fi
149}
150
151# fail_trap is executed if an error occurs.
152fail_trap() {
153 result=$?
154 if [ "$result" != "0" ]; then
155 if [[ -n "$INPUT_ARGUMENTS" ]]; then
156 echo "Failed to install $PROJECT_NAME with the arguments provided: $INPUT_ARGUMENTS"
157 help
158 else
159 echo "Failed to install $PROJECT_NAME"
160 fi
161 echo -e "\tFor support, go to https://github.com/helm/helm."
162 fi
163 cleanup
164 exit $result
165}
166
167# testVersion tests the installed client to make sure it is working.
168testVersion() {
169 set +e
170 HELM="$(command -v $PROJECT_NAME)"
171 if [ "$?" = "1" ]; then
172 echo "$PROJECT_NAME not found. Is $HELM_INSTALL_DIR on your "'$PATH?'
173 exit 1
174 fi
175 set -e
176 echo "Run '$PROJECT_NAME init' to configure $PROJECT_NAME."
177}
178
179# help provides possible cli installation arguments
180help () {
181 echo "Accepted cli arguments are:"
182 echo -e "\t[--help|-h ] ->> prints this help"
183 echo -e "\t[--version|-v <desired_version>]"
184 echo -e "\te.g. --version v2.4.0 or -v latest"
185 echo -e "\t[--no-sudo] ->> install without sudo"
186}
187
188# cleanup temporary files to avoid https://github.com/helm/helm/issues/2977
189cleanup() {
190 if [[ -d "${HELM_TMP_ROOT:-}" ]]; then
191 rm -rf "$HELM_TMP_ROOT"
192 fi
193}
194
195# Execution
196
197#Stop execution on any error
198trap "fail_trap" EXIT
199set -e
200
201# Parsing input arguments (if any)
202export INPUT_ARGUMENTS="${@}"
203set -u
204while [[ $# -gt 0 ]]; do
205 case $1 in
206 '--version'|-v)
207 shift
208 if [[ $# -ne 0 ]]; then
209 export DESIRED_VERSION="${1}"
210 if [[ "$1" != "v"* ]]; then
211 echo "Expected version arg ('${DESIRED_VERSION}') to begin with 'v', fixing..."
212 export DESIRED_VERSION="v${1}"
213 fi
214 else
215 echo -e "Please provide the desired version. e.g. --version v2.4.0 or -v latest"
216 exit 0
217 fi
218 ;;
219 '--no-sudo')
220 USE_SUDO="false"
221 ;;
222 '--help'|-h)
223 help
224 exit 0
225 ;;
226 *) exit 1
227 ;;
228 esac
229 shift
230done
231set +u
232
233initArch
234initOS
235verifySupported
236checkDesiredVersion
237if ! checkHelmInstalledVersion; then
238 downloadFile
239 installFile
240fi
241testVersion
242cleanup
View as plain text