github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+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  # checkLatestVersion checks the latest available version.
    66  checkLatestVersion() {
    67    # Use the GitHub API to find the latest version for this project.
    68    local latest_url="https://api.github.com/repos/kubernetes/helm/releases/latest"
    69    if type "curl" > /dev/null; then
    70      TAG=$(curl -s $latest_url | awk '/\"tag_name\":/{gsub( /[,\"]/,"", $2); print $2}')
    71    elif type "wget" > /dev/null; then
    72      TAG=$(wget -q -O - $latest_url | awk '/\"tag_name\":/{gsub( /[,\"]/,"", $2); print $2}')
    73    fi
    74  }
    75  
    76  # checkHelmInstalledVersion checks which version of helm is installed and
    77  # if it needs to be updated.
    78  checkHelmInstalledVersion() {
    79    if [[ -f "${HELM_INSTALL_DIR}/${PROJECT_NAME}" ]]; then
    80      local version=$(helm version | grep '^Client' | cut -d'"' -f2)
    81      if [[ "$version" == "$TAG" ]]; then
    82        echo "Helm ${version} is up-to-date."
    83        return 0
    84      else
    85        echo "Helm ${TAG} is available. Upgrading from version ${version}."
    86        return 1
    87      fi
    88    else
    89      return 1
    90    fi
    91  }
    92  
    93  # downloadFile downloads the latest binary package and also the checksum
    94  # for that binary.
    95  downloadFile() {
    96    HELM_DIST="helm-$TAG-$OS-$ARCH.tar.gz"
    97    DOWNLOAD_URL="https://kubernetes-helm.storage.googleapis.com/$HELM_DIST"
    98    CHECKSUM_URL="$DOWNLOAD_URL.sha256"
    99    HELM_TMP_FILE="/tmp/$HELM_DIST"
   100    HELM_SUM_FILE="/tmp/$HELM_DIST.sha256"
   101    echo "Downloading $DOWNLOAD_URL"
   102    if type "curl" > /dev/null; then
   103      curl -Ls "$CHECKSUM_URL" -o "$HELM_SUM_FILE"
   104    elif type "wget" > /dev/null; then
   105      wget -q -O "$HELM_SUM_FILE" "$CHECKSUM_URL"
   106    fi
   107    if type "curl" > /dev/null; then
   108      curl -L "$DOWNLOAD_URL" -o "$HELM_TMP_FILE"
   109    elif type "wget" > /dev/null; then
   110      wget -q -O "$HELM_TMP_FILE" "$DOWNLOAD_URL"
   111    fi
   112  }
   113  
   114  # installFile verifies the SHA256 for the file, then unpacks and
   115  # installs it.
   116  installFile() {
   117    HELM_TMP="/tmp/$PROJECT_NAME"
   118    local sum=$(openssl sha -sha256 ${HELM_TMP_FILE} | awk '{print $2}')
   119    local expected_sum=$(cat ${HELM_SUM_FILE})
   120    if [ "$sum" != "$expected_sum" ]; then
   121      echo "SHA sum of $HELM_TMP does not match. Aborting."
   122      exit 1
   123    fi
   124  
   125    mkdir -p "$HELM_TMP"
   126    tar xf "$HELM_TMP_FILE" -C "$HELM_TMP"
   127    HELM_TMP_BIN="$HELM_TMP/$OS-$ARCH/$PROJECT_NAME"
   128    echo "Preparing to install into ${HELM_INSTALL_DIR} (sudo)"
   129    sudo cp "$HELM_TMP_BIN" "$HELM_INSTALL_DIR"
   130  }
   131  
   132  # fail_trap is executed if an error occurs.
   133  fail_trap() {
   134    result=$?
   135    if [ "$result" != "0" ]; then
   136      echo "Failed to install $PROJECT_NAME"
   137      echo "\tFor support, go to https://github.com/kubernetes/helm."
   138    fi
   139    exit $result
   140  }
   141  
   142  # testVersion tests the installed client to make sure it is working.
   143  testVersion() {
   144    set +e
   145    echo "$PROJECT_NAME installed into $HELM_INSTALL_DIR/$PROJECT_NAME"
   146    HELM="$(which $PROJECT_NAME)"
   147    if [ "$?" = "1" ]; then
   148      echo "$PROJECT_NAME not found. Is $HELM_INSTALL_DIR on your "'$PATH?'
   149      exit 1
   150    fi
   151    set -e
   152    echo "Run '$PROJECT_NAME init' to configure $PROJECT_NAME."
   153  }
   154  
   155  # Execution
   156  
   157  #Stop execution on any error
   158  trap "fail_trap" EXIT
   159  set -e
   160  initArch
   161  initOS
   162  verifySupported
   163  checkLatestVersion
   164  if ! checkHelmInstalledVersion; then
   165    downloadFile
   166    installFile
   167  fi
   168  testVersion