Areli Fuss | 471a2970 | 2018-02-01 13:09:07 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Copyright 2016 The Kubernetes Authors All rights reserved. |
| 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 | |
| 20 | PROJECT_NAME="helm" |
| 21 | |
| 22 | : ${HELM_INSTALL_DIR:="/usr/local/bin"} |
| 23 | |
| 24 | # initArch discovers the architecture for this system. |
| 25 | initArch() { |
| 26 | ARCH=$(uname -m) |
| 27 | case $ARCH in |
| 28 | armv5*) ARCH="armv5";; |
| 29 | armv6*) ARCH="armv6";; |
| 30 | armv7*) ARCH="armv7";; |
| 31 | aarch64) ARCH="arm64";; |
| 32 | x86) ARCH="386";; |
| 33 | x86_64) ARCH="amd64";; |
| 34 | i686) ARCH="386";; |
| 35 | i386) ARCH="386";; |
| 36 | esac |
| 37 | } |
| 38 | |
| 39 | # initOS discovers the operating system for this system. |
| 40 | initOS() { |
| 41 | OS=$(echo `uname`|tr '[:upper:]' '[:lower:]') |
| 42 | |
| 43 | case "$OS" in |
| 44 | # Minimalist GNU for Windows |
| 45 | mingw*) OS='windows';; |
| 46 | esac |
| 47 | } |
| 48 | |
| 49 | # runs the given command as root (detects if we are root already) |
| 50 | runAsRoot() { |
| 51 | local CMD="$*" |
| 52 | |
| 53 | if [ $EUID -ne 0 ]; then |
| 54 | CMD="sudo $CMD" |
| 55 | fi |
| 56 | |
| 57 | $CMD |
| 58 | } |
| 59 | |
| 60 | # verifySupported checks that the os/arch combination is supported for |
| 61 | # binary builds. |
| 62 | verifySupported() { |
| 63 | local supported="darwin-386\ndarwin-amd64\nlinux-386\nlinux-amd64\nlinux-arm\nlinux-arm64\nlinux-ppc64le\nwindows-386\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/kubernetes/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. |
| 77 | checkDesiredVersion() { |
| 78 | # Use the GitHub releases webpage for the project to find the desired version for this project. |
| 79 | local release_url="https://github.com/kubernetes/helm/releases/${DESIRED_VERSION:-latest}" |
| 80 | if type "curl" > /dev/null; then |
| 81 | TAG=$(curl -SsL $release_url | awk '/\/tag\//' | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}' | awk 'a !~ $0{print}; {a=$0}') |
| 82 | elif type "wget" > /dev/null; then |
| 83 | TAG=$(wget -q -O - $release_url | awk '/\/tag\//' | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}' | awk 'a !~ $0{print}; {a=$0}') |
| 84 | fi |
| 85 | if [ "x$TAG" == "x" ]; then |
| 86 | echo "Cannot determine ${DESIRED_VERSION} tag." |
| 87 | exit 1 |
| 88 | fi |
| 89 | } |
| 90 | |
| 91 | # checkHelmInstalledVersion checks which version of helm is installed and |
| 92 | # if it needs to be changed. |
| 93 | checkHelmInstalledVersion() { |
| 94 | if [[ -f "${HELM_INSTALL_DIR}/${PROJECT_NAME}" ]]; then |
| 95 | local version=$(helm version | grep '^Client' | cut -d'"' -f2) |
| 96 | if [[ "$version" == "$TAG" ]]; then |
| 97 | echo "Helm ${version} is already ${DESIRED_VERSION:-latest}" |
| 98 | return 0 |
| 99 | else |
| 100 | echo "Helm ${TAG} is available. Changing from version ${version}." |
| 101 | return 1 |
| 102 | fi |
| 103 | else |
| 104 | return 1 |
| 105 | fi |
| 106 | } |
| 107 | |
| 108 | # downloadFile downloads the latest binary package and also the checksum |
| 109 | # for that binary. |
| 110 | downloadFile() { |
| 111 | HELM_DIST="helm-$TAG-$OS-$ARCH.tar.gz" |
| 112 | DOWNLOAD_URL="https://kubernetes-helm.storage.googleapis.com/$HELM_DIST" |
| 113 | CHECKSUM_URL="$DOWNLOAD_URL.sha256" |
| 114 | HELM_TMP_ROOT="$(mktemp -dt helm-installer-XXXXXX)" |
| 115 | HELM_TMP_FILE="$HELM_TMP_ROOT/$HELM_DIST" |
| 116 | HELM_SUM_FILE="$HELM_TMP_ROOT/$HELM_DIST.sha256" |
| 117 | echo "Downloading $DOWNLOAD_URL" |
| 118 | if type "curl" > /dev/null; then |
| 119 | curl -SsL "$CHECKSUM_URL" -o "$HELM_SUM_FILE" |
| 120 | elif type "wget" > /dev/null; then |
| 121 | wget -q -O "$HELM_SUM_FILE" "$CHECKSUM_URL" |
| 122 | fi |
| 123 | if type "curl" > /dev/null; then |
| 124 | curl -SsL "$DOWNLOAD_URL" -o "$HELM_TMP_FILE" |
| 125 | elif type "wget" > /dev/null; then |
| 126 | wget -q -O "$HELM_TMP_FILE" "$DOWNLOAD_URL" |
| 127 | fi |
| 128 | } |
| 129 | |
| 130 | # installFile verifies the SHA256 for the file, then unpacks and |
| 131 | # installs it. |
| 132 | installFile() { |
| 133 | HELM_TMP="$HELM_TMP_ROOT/$PROJECT_NAME" |
| 134 | local sum=$(openssl sha1 -sha256 ${HELM_TMP_FILE} | awk '{print $2}') |
| 135 | local expected_sum=$(cat ${HELM_SUM_FILE}) |
| 136 | if [ "$sum" != "$expected_sum" ]; then |
| 137 | echo "SHA sum of $HELM_TMP does not match. Aborting." |
| 138 | exit 1 |
| 139 | fi |
| 140 | |
| 141 | mkdir -p "$HELM_TMP" |
| 142 | tar xf "$HELM_TMP_FILE" -C "$HELM_TMP" |
| 143 | HELM_TMP_BIN="$HELM_TMP/$OS-$ARCH/$PROJECT_NAME" |
| 144 | echo "Preparing to install into ${HELM_INSTALL_DIR}" |
| 145 | runAsRoot cp "$HELM_TMP_BIN" "$HELM_INSTALL_DIR" |
| 146 | } |
| 147 | |
| 148 | # fail_trap is executed if an error occurs. |
| 149 | fail_trap() { |
| 150 | result=$? |
| 151 | if [ "$result" != "0" ]; then |
| 152 | if [[ -n "$INPUT_ARGUMENTS" ]]; then |
| 153 | echo "Failed to install $PROJECT_NAME with the arguments provided: $INPUT_ARGUMENTS" |
| 154 | help |
| 155 | else |
| 156 | echo "Failed to install $PROJECT_NAME" |
| 157 | fi |
| 158 | echo -e "\tFor support, go to https://github.com/kubernetes/helm." |
| 159 | fi |
| 160 | cleanup |
| 161 | exit $result |
| 162 | } |
| 163 | |
| 164 | # testVersion tests the installed client to make sure it is working. |
| 165 | testVersion() { |
| 166 | set +e |
| 167 | echo "$PROJECT_NAME installed into $HELM_INSTALL_DIR/$PROJECT_NAME" |
| 168 | HELM="$(which $PROJECT_NAME)" |
| 169 | if [ "$?" = "1" ]; then |
| 170 | echo "$PROJECT_NAME not found. Is $HELM_INSTALL_DIR on your "'$PATH?' |
| 171 | exit 1 |
| 172 | fi |
| 173 | set -e |
| 174 | echo "Run '$PROJECT_NAME init' to configure $PROJECT_NAME." |
| 175 | } |
| 176 | |
| 177 | # help provides possible cli installation arguments |
| 178 | help () { |
| 179 | echo "Accepted cli arguments are:" |
| 180 | echo -e "\t[--help|-h ] ->> prints this help" |
| 181 | echo -e "\t[--version|-v <desired_version>] . When not defined it defaults to latest" |
| 182 | echo -e "\te.g. --version v2.4.0 or -v latest" |
| 183 | } |
| 184 | |
| 185 | # cleanup temporary files to avoid https://github.com/kubernetes/helm/issues/2977 |
| 186 | cleanup() { |
| 187 | rm -rf "$HELM_TMP_ROOT" |
| 188 | } |
| 189 | |
| 190 | # Execution |
| 191 | |
| 192 | #Stop execution on any error |
| 193 | trap "fail_trap" EXIT |
| 194 | set -e |
| 195 | |
| 196 | # Parsing input arguments (if any) |
| 197 | export INPUT_ARGUMENTS="${@}" |
| 198 | set -u |
| 199 | while [[ $# -gt 0 ]]; do |
| 200 | case $1 in |
| 201 | '--version'|-v) |
| 202 | shift |
| 203 | if [[ $# -ne 0 ]]; then |
| 204 | export DESIRED_VERSION="${1}" |
| 205 | else |
| 206 | echo -e "Please provide the desired version. e.g. --version v2.4.0 or -v latest" |
| 207 | exit 0 |
| 208 | fi |
| 209 | ;; |
| 210 | '--help'|-h) |
| 211 | help |
| 212 | exit 0 |
| 213 | ;; |
| 214 | *) exit 1 |
| 215 | ;; |
| 216 | esac |
| 217 | shift |
| 218 | done |
| 219 | set +u |
| 220 | |
| 221 | initArch |
| 222 | initOS |
| 223 | verifySupported |
| 224 | checkDesiredVersion |
| 225 | if ! checkHelmInstalledVersion; then |
| 226 | downloadFile |
| 227 | installFile |
| 228 | fi |
| 229 | testVersion |
| 230 | cleanup |