github.com/zoumo/helm@v2.5.0+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  # verifySupported checks that the os/arch combination is supported for
    50  # binary builds.
    51  verifySupported() {
    52    local supported="linux-amd64\ndarwin-amd64\nlinux-386"
    53    if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
    54      echo "No prebuild binary for ${OS}-${ARCH}."
    55      echo "To build from source, go to https://github.com/kubernetes/helm"
    56      exit 1
    57    fi
    58  
    59    if ! type "curl" > /dev/null && ! type "wget" > /dev/null; then
    60      echo "Either curl or wget is required"
    61      exit 1
    62    fi
    63  }
    64  
    65  # checkDesiredVersion checks if the desired version is available.
    66  checkDesiredVersion() {
    67    # Use the GitHub releases webpage for the project to find the desired version for this project.
    68    local release_url="https://github.com/kubernetes/helm/releases/${DESIRED_VERSION:-latest}"
    69    if type "curl" > /dev/null; then
    70      TAG=$(curl -SsL $release_url | awk '/\/tag\//' | head -n 1 | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}')
    71    elif type "wget" > /dev/null; then
    72      TAG=$(wget -q -O - $release_url | awk '/\/tag\//' | head -n 1 | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}')
    73    fi
    74    if [ "x$TAG" == "x" ]; then
    75      echo "Cannot determine ${DESIRED_VERSION} tag."
    76      exit 1
    77    fi
    78  }
    79  
    80  # checkHelmInstalledVersion checks which version of helm is installed and
    81  # if it needs to be changed.
    82  checkHelmInstalledVersion() {
    83    if [[ -f "${HELM_INSTALL_DIR}/${PROJECT_NAME}" ]]; then
    84      local version=$(helm version | grep '^Client' | cut -d'"' -f2)
    85      if [[ "$version" == "$TAG" ]]; then
    86        echo "Helm ${version} is already ${DESIRED_VERSION:-latest}"
    87        return 0
    88      else
    89        echo "Helm ${TAG} is available. Changing from version ${version}."
    90        return 1
    91      fi
    92    else
    93      return 1
    94    fi
    95  }
    96  
    97  # downloadFile downloads the latest binary package and also the checksum
    98  # for that binary.
    99  downloadFile() {
   100    HELM_DIST="helm-$TAG-$OS-$ARCH.tar.gz"
   101    DOWNLOAD_URL="https://kubernetes-helm.storage.googleapis.com/$HELM_DIST"
   102    CHECKSUM_URL="$DOWNLOAD_URL.sha256"
   103    HELM_TMP_FILE="/tmp/$HELM_DIST"
   104    HELM_SUM_FILE="/tmp/$HELM_DIST.sha256"
   105    echo "Downloading $DOWNLOAD_URL"
   106    if type "curl" > /dev/null; then
   107      curl -SsL "$CHECKSUM_URL" -o "$HELM_SUM_FILE"
   108    elif type "wget" > /dev/null; then
   109      wget -q -O "$HELM_SUM_FILE" "$CHECKSUM_URL"
   110    fi
   111    if type "curl" > /dev/null; then
   112      curl -SsL "$DOWNLOAD_URL" -o "$HELM_TMP_FILE"
   113    elif type "wget" > /dev/null; then
   114      wget -q -O "$HELM_TMP_FILE" "$DOWNLOAD_URL"
   115    fi
   116  }
   117  
   118  # installFile verifies the SHA256 for the file, then unpacks and
   119  # installs it.
   120  installFile() {
   121    HELM_TMP="/tmp/$PROJECT_NAME"
   122    local sum=$(openssl sha -sha256 ${HELM_TMP_FILE} | awk '{print $2}')
   123    local expected_sum=$(cat ${HELM_SUM_FILE})
   124    if [ "$sum" != "$expected_sum" ]; then
   125      echo "SHA sum of $HELM_TMP does not match. Aborting."
   126      exit 1
   127    fi
   128  
   129    mkdir -p "$HELM_TMP"
   130    tar xf "$HELM_TMP_FILE" -C "$HELM_TMP"
   131    HELM_TMP_BIN="$HELM_TMP/$OS-$ARCH/$PROJECT_NAME"
   132    echo "Preparing to install into ${HELM_INSTALL_DIR} (sudo)"
   133    sudo cp "$HELM_TMP_BIN" "$HELM_INSTALL_DIR"
   134  }
   135  
   136  # fail_trap is executed if an error occurs.
   137  fail_trap() {
   138    result=$?
   139    if [ "$result" != "0" ]; then
   140      if [[ -n "$INPUT_ARGUMENTS" ]]; then
   141        echo "Failed to install $PROJECT_NAME with the arguments provided: $INPUT_ARGUMENTS"
   142        help
   143      else
   144        echo "Failed to install $PROJECT_NAME"
   145      fi
   146      echo -e "\tFor support, go to https://github.com/kubernetes/helm."
   147    fi
   148    exit $result
   149  }
   150  
   151  # testVersion tests the installed client to make sure it is working.
   152  testVersion() {
   153    set +e
   154    echo "$PROJECT_NAME installed into $HELM_INSTALL_DIR/$PROJECT_NAME"
   155    HELM="$(which $PROJECT_NAME)"
   156    if [ "$?" = "1" ]; then
   157      echo "$PROJECT_NAME not found. Is $HELM_INSTALL_DIR on your "'$PATH?'
   158      exit 1
   159    fi
   160    set -e
   161    echo "Run '$PROJECT_NAME init' to configure $PROJECT_NAME."
   162  }
   163  
   164  # help provides possible cli installation arguments
   165  help () {
   166    echo "Accepted cli arguments are:"
   167    echo -e "\t[--help|-h ] ->> prints this help"
   168    echo -e "\t[--version|-v <desired_version>] . When not defined it defaults to latest"
   169    echo -e "\te.g. --version v2.4.0  or -v latest"
   170  }
   171  
   172  # Execution
   173  
   174  #Stop execution on any error
   175  trap "fail_trap" EXIT
   176  set -e
   177  
   178  # Parsing input arguments (if any)
   179  export INPUT_ARGUMENTS="${@}"
   180  set -u
   181  while [[ $# -gt 0 ]]; do
   182    case $1 in
   183      '--version'|-v) 
   184         export DESIRED_VERSION="${2}" 
   185         shift
   186         ;;
   187      '--help'|-h) 
   188         help
   189         exit 0
   190         ;;
   191      *) exit 1
   192         ;;
   193    esac
   194    shift
   195  done
   196  set +u
   197  
   198  initArch
   199  initOS
   200  verifySupported
   201  checkDesiredVersion
   202  if ! checkHelmInstalledVersion; then
   203    downloadFile
   204    installFile
   205  fi
   206  testVersion