github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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    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.
    62  verifySupported() {
    63    local supported="darwin-amd64\nlinux-386\nlinux-amd64\nlinux-arm\nlinux-arm64\nlinux-ppc64le\nlinux-s390x\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.
    77  checkDesiredVersion() {
    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.
    88  checkHelmInstalledVersion() {
    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.
   105  downloadFile() {
   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.
   127  installFile() {
   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.
   152  fail_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.
   168  testVersion() {
   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
   180  help () {
   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
   189  cleanup() {
   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
   198  trap "fail_trap" EXIT
   199  set -e
   200  
   201  # Parsing input arguments (if any)
   202  export INPUT_ARGUMENTS="${@}"
   203  set -u
   204  while [[ $# -gt 0 ]]; do
   205    case $1 in
   206      '--version'|-v)
   207         shift
   208         if [[ $# -ne 0 ]]; then
   209             export DESIRED_VERSION="${1}"
   210         else
   211             echo -e "Please provide the desired version. e.g. --version v2.4.0 or -v latest"
   212             exit 0
   213         fi
   214         ;;
   215      '--no-sudo')
   216         USE_SUDO="false"
   217         ;;
   218      '--help'|-h)
   219         help
   220         exit 0
   221         ;;
   222      *) exit 1
   223         ;;
   224    esac
   225    shift
   226  done
   227  set +u
   228  
   229  initArch
   230  initOS
   231  verifySupported
   232  checkDesiredVersion
   233  if ! checkHelmInstalledVersion; then
   234    downloadFile
   235    installFile
   236  fi
   237  testVersion
   238  cleanup