github.com/migueleliasweb/helm@v2.6.1+incompatible/scripts/get (about) 1 #!/bin/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="linux-amd64\ndarwin-amd64\nlinux-386" 64 if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then 65 echo "No prebuild 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\//' | head -n 1 | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}') 82 elif type "wget" > /dev/null; then 83 TAG=$(wget -q -O - $release_url | awk '/\/tag\//' | head -n 1 | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}') 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_FILE="/tmp/$HELM_DIST" 115 HELM_SUM_FILE="/tmp/$HELM_DIST.sha256" 116 echo "Downloading $DOWNLOAD_URL" 117 if type "curl" > /dev/null; then 118 curl -SsL "$CHECKSUM_URL" -o "$HELM_SUM_FILE" 119 elif type "wget" > /dev/null; then 120 wget -q -O "$HELM_SUM_FILE" "$CHECKSUM_URL" 121 fi 122 if type "curl" > /dev/null; then 123 curl -SsL "$DOWNLOAD_URL" -o "$HELM_TMP_FILE" 124 elif type "wget" > /dev/null; then 125 wget -q -O "$HELM_TMP_FILE" "$DOWNLOAD_URL" 126 fi 127 } 128 129 # installFile verifies the SHA256 for the file, then unpacks and 130 # installs it. 131 installFile() { 132 HELM_TMP="/tmp/$PROJECT_NAME" 133 local sum=$(openssl sha -sha256 ${HELM_TMP_FILE} | awk '{print $2}') 134 local expected_sum=$(cat ${HELM_SUM_FILE}) 135 if [ "$sum" != "$expected_sum" ]; then 136 echo "SHA sum of $HELM_TMP does not match. Aborting." 137 exit 1 138 fi 139 140 mkdir -p "$HELM_TMP" 141 tar xf "$HELM_TMP_FILE" -C "$HELM_TMP" 142 HELM_TMP_BIN="$HELM_TMP/$OS-$ARCH/$PROJECT_NAME" 143 echo "Preparing to install into ${HELM_INSTALL_DIR}" 144 runAsRoot cp "$HELM_TMP_BIN" "$HELM_INSTALL_DIR" 145 } 146 147 # fail_trap is executed if an error occurs. 148 fail_trap() { 149 result=$? 150 if [ "$result" != "0" ]; then 151 if [[ -n "$INPUT_ARGUMENTS" ]]; then 152 echo "Failed to install $PROJECT_NAME with the arguments provided: $INPUT_ARGUMENTS" 153 help 154 else 155 echo "Failed to install $PROJECT_NAME" 156 fi 157 echo -e "\tFor support, go to https://github.com/kubernetes/helm." 158 fi 159 exit $result 160 } 161 162 # testVersion tests the installed client to make sure it is working. 163 testVersion() { 164 set +e 165 echo "$PROJECT_NAME installed into $HELM_INSTALL_DIR/$PROJECT_NAME" 166 HELM="$(which $PROJECT_NAME)" 167 if [ "$?" = "1" ]; then 168 echo "$PROJECT_NAME not found. Is $HELM_INSTALL_DIR on your "'$PATH?' 169 exit 1 170 fi 171 set -e 172 echo "Run '$PROJECT_NAME init' to configure $PROJECT_NAME." 173 } 174 175 # help provides possible cli installation arguments 176 help () { 177 echo "Accepted cli arguments are:" 178 echo -e "\t[--help|-h ] ->> prints this help" 179 echo -e "\t[--version|-v <desired_version>] . When not defined it defaults to latest" 180 echo -e "\te.g. --version v2.4.0 or -v latest" 181 } 182 183 # Execution 184 185 #Stop execution on any error 186 trap "fail_trap" EXIT 187 set -e 188 189 # Parsing input arguments (if any) 190 export INPUT_ARGUMENTS="${@}" 191 set -u 192 while [[ $# -gt 0 ]]; do 193 case $1 in 194 '--version'|-v) 195 export DESIRED_VERSION="${2}" 196 shift 197 ;; 198 '--help'|-h) 199 help 200 exit 0 201 ;; 202 *) exit 1 203 ;; 204 esac 205 shift 206 done 207 set +u 208 209 initArch 210 initOS 211 verifySupported 212 checkDesiredVersion 213 if ! checkHelmInstalledVersion; then 214 downloadFile 215 installFile 216 fi 217 testVersion