github.com/koderover/helm@v2.17.0+incompatible/scripts/get (about) 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 20 PROJECT_NAME="helm" 21 TILLER_NAME="tiller" 22 23 : ${USE_SUDO:="true"} 24 : ${HELM_INSTALL_DIR:="/usr/local/bin"} 25 26 # initArch discovers the architecture for this system. 27 initArch() { 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. 42 initOS() { 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) 52 runAsRoot() { 53 local CMD="$*" 54 55 if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then 56 CMD="sudo $CMD" 57 fi 58 59 $CMD 60 } 61 62 # verifySupported checks that the os/arch combination is supported for 63 # binary builds. 64 verifySupported() { 65 local supported="darwin-386\ndarwin-amd64\nlinux-386\nlinux-amd64\nlinux-arm\nlinux-arm64\nlinux-ppc64le\nwindows-386\nwindows-amd64" 66 if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then 67 echo "No prebuilt binary for ${OS}-${ARCH}." 68 echo "To build from source, go to https://github.com/helm/helm" 69 exit 1 70 fi 71 72 if ! type "curl" > /dev/null && ! type "wget" > /dev/null; then 73 echo "Either curl or wget is required" 74 exit 1 75 fi 76 } 77 78 # checkDesiredVersion checks if the desired version is available. 79 checkDesiredVersion() { 80 if [ "x$DESIRED_VERSION" == "x" ]; then 81 # Get tag from release URL 82 local latest_release_url="https://github.com/helm/helm/releases/latest" 83 if type "curl" > /dev/null; then 84 TAG=$(curl -Ls -o /dev/null -w %{url_effective} $latest_release_url | grep -oE "[^/]+$" ) 85 elif type "wget" > /dev/null; then 86 TAG=$(wget $latest_release_url --server-response -O /dev/null 2>&1 | awk '/^ Location: /{DEST=$2} END{ print DEST}' | grep -oE "[^/]+$") 87 fi 88 else 89 TAG=$DESIRED_VERSION 90 fi 91 } 92 93 # checkHelmInstalledVersion checks which version of helm is installed and 94 # if it needs to be changed. 95 checkHelmInstalledVersion() { 96 if [[ -f "${HELM_INSTALL_DIR}/${PROJECT_NAME}" ]]; then 97 local version=$("${HELM_INSTALL_DIR}/${PROJECT_NAME}" version -c | grep '^Client' | cut -d'"' -f2) 98 if [[ "$version" == "$TAG" ]]; then 99 echo "Helm ${version} is already ${DESIRED_VERSION:-latest}" 100 return 0 101 else 102 echo "Helm ${TAG} is available. Changing from version ${version}." 103 return 1 104 fi 105 else 106 return 1 107 fi 108 } 109 110 # downloadFile downloads the latest binary package and also the checksum 111 # for that binary. 112 downloadFile() { 113 HELM_DIST="helm-$TAG-$OS-$ARCH.tar.gz" 114 DOWNLOAD_URL="https://get.helm.sh/$HELM_DIST" 115 CHECKSUM_URL="$DOWNLOAD_URL.sha256" 116 HELM_TMP_ROOT="$(mktemp -dt helm-installer-XXXXXX)" 117 HELM_TMP_FILE="$HELM_TMP_ROOT/$HELM_DIST" 118 HELM_SUM_FILE="$HELM_TMP_ROOT/$HELM_DIST.sha256" 119 echo "Downloading $DOWNLOAD_URL" 120 if type "curl" > /dev/null; then 121 curl -SsL "$CHECKSUM_URL" -o "$HELM_SUM_FILE" 122 elif type "wget" > /dev/null; then 123 wget -q -O "$HELM_SUM_FILE" "$CHECKSUM_URL" 124 fi 125 if type "curl" > /dev/null; then 126 curl -SsL "$DOWNLOAD_URL" -o "$HELM_TMP_FILE" 127 elif type "wget" > /dev/null; then 128 wget -q -O "$HELM_TMP_FILE" "$DOWNLOAD_URL" 129 fi 130 } 131 132 # installFile verifies the SHA256 for the file, then unpacks and 133 # installs it. 134 installFile() { 135 HELM_TMP="$HELM_TMP_ROOT/$PROJECT_NAME" 136 local sum=$(openssl sha1 -sha256 ${HELM_TMP_FILE} | awk '{print $2}') 137 local expected_sum=$(cat ${HELM_SUM_FILE}) 138 if [ "$sum" != "$expected_sum" ]; then 139 echo "SHA sum of ${HELM_TMP_FILE} does not match. Aborting." 140 exit 1 141 fi 142 143 mkdir -p "$HELM_TMP" 144 tar xf "$HELM_TMP_FILE" -C "$HELM_TMP" 145 HELM_TMP_BIN="$HELM_TMP/$OS-$ARCH/$PROJECT_NAME" 146 TILLER_TMP_BIN="$HELM_TMP/$OS-$ARCH/$TILLER_NAME" 147 echo "Preparing to install $PROJECT_NAME and $TILLER_NAME into ${HELM_INSTALL_DIR}" 148 runAsRoot cp "$HELM_TMP_BIN" "$HELM_INSTALL_DIR" 149 echo "$PROJECT_NAME installed into $HELM_INSTALL_DIR/$PROJECT_NAME" 150 if [ -x "$TILLER_TMP_BIN" ]; then 151 runAsRoot cp "$TILLER_TMP_BIN" "$HELM_INSTALL_DIR" 152 echo "$TILLER_NAME installed into $HELM_INSTALL_DIR/$TILLER_NAME" 153 else 154 echo "info: $TILLER_NAME binary was not found in this release; skipping $TILLER_NAME installation" 155 fi 156 } 157 158 # fail_trap is executed if an error occurs. 159 fail_trap() { 160 result=$? 161 if [ "$result" != "0" ]; then 162 if [[ -n "$INPUT_ARGUMENTS" ]]; then 163 echo "Failed to install $PROJECT_NAME with the arguments provided: $INPUT_ARGUMENTS" 164 help 165 else 166 echo "Failed to install $PROJECT_NAME" 167 fi 168 echo -e "\tFor support, go to https://github.com/helm/helm." 169 fi 170 cleanup 171 exit $result 172 } 173 174 # testVersion tests the installed client to make sure it is working. 175 testVersion() { 176 set +e 177 HELM="$(which $PROJECT_NAME)" 178 if [ "$?" = "1" ]; then 179 echo "$PROJECT_NAME not found. Is $HELM_INSTALL_DIR on your "'$PATH?' 180 exit 1 181 fi 182 set -e 183 echo "Run '$PROJECT_NAME init' to configure $PROJECT_NAME." 184 } 185 186 # help provides possible cli installation arguments 187 help () { 188 echo "Accepted cli arguments are:" 189 echo -e "\t[--help|-h ] ->> prints this help" 190 echo -e "\t[--version|-v <desired_version>] . When not defined it defaults to latest" 191 echo -e "\te.g. --version v2.4.0 or -v latest" 192 echo -e "\t[--no-sudo] ->> install without sudo" 193 } 194 195 # cleanup temporary files to avoid https://github.com/helm/helm/issues/2977 196 cleanup() { 197 if [[ -d "${HELM_TMP_ROOT:-}" ]]; then 198 rm -rf "$HELM_TMP_ROOT" 199 fi 200 } 201 202 # Execution 203 204 #Stop execution on any error 205 trap "fail_trap" EXIT 206 set -e 207 208 # Parsing input arguments (if any) 209 export INPUT_ARGUMENTS="${@}" 210 set -u 211 while [[ $# -gt 0 ]]; do 212 case $1 in 213 '--version'|-v) 214 shift 215 if [[ $# -ne 0 ]]; then 216 export DESIRED_VERSION="${1}" 217 else 218 echo -e "Please provide the desired version. e.g. --version v2.4.0 or -v latest" 219 exit 0 220 fi 221 ;; 222 '--no-sudo') 223 USE_SUDO="false" 224 ;; 225 '--help'|-h) 226 help 227 exit 0 228 ;; 229 *) exit 1 230 ;; 231 esac 232 shift 233 done 234 set +u 235 236 initArch 237 initOS 238 verifySupported 239 checkDesiredVersion 240 if ! checkHelmInstalledVersion; then 241 downloadFile 242 installFile 243 fi 244 testVersion 245 cleanup