github.com/confluentinc/cli@v1.100.0/install-confluent.sh (about)

     1  #!/bin/sh
     2  set -e
     3  # Code generated by godownloader on 2018-06-07T07:36:49Z. DO NOT EDIT.
     4  # Manually modified to download from S3 for confluentinc/cli goreleaser config
     5  #
     6  
     7  S3_URL=https://s3-us-west-2.amazonaws.com/confluent.cloud
     8  
     9  usage() {
    10    this=$1
    11    cat <<EOF
    12  $this: download binaries for confluentinc/cli
    13  
    14  Usage: $this [-b] bindir [-d] [tag] | -l
    15    -b sets bindir or installation directory, Defaults to ./bin
    16    -d turns on debug logging
    17     [tag] is a valid version tag shown with -l
    18     If tag is missing, then the latest will be used.
    19  
    20    -l returns a list of all available tags/versions
    21  
    22  EOF
    23    exit 2
    24  }
    25  
    26  parse_args() {
    27    #BINDIR is ./bin unless set be ENV
    28    # over-ridden by flag below
    29  
    30    BINDIR=${BINDIR:-./bin}
    31    while getopts "b:ldh?" arg; do
    32      case "$arg" in
    33        b) BINDIR="$OPTARG" ;;
    34        l) s3_releases ; exit 0 ;;
    35        d) log_set_priority 10 ;;
    36        h | \?) usage "$0" ;;
    37      esac
    38    done
    39    shift $((OPTIND - 1))
    40    TAG=$1
    41  }
    42  # this function wraps all the destructive operations
    43  # if a curl|bash cuts off the end of the script due to
    44  # network, either nothing will happen or will syntax error
    45  # out preventing half-done work
    46  execute() {
    47    tmpdir=$(mktmpdir)
    48    log_debug "downloading files into ${tmpdir}"
    49    http_download "${tmpdir}/${TARBALL}" "${TARBALL_URL}" "Accept:application/octet-stream"
    50    http_download "${tmpdir}/${CHECKSUM}" "${CHECKSUM_URL}" "Accept:application/octet-stream"
    51    hash_sha256_verify "${tmpdir}/${TARBALL}" "${tmpdir}/${CHECKSUM}"
    52    srcdir="${tmpdir}/${BINARY}"
    53    rm -rf "${srcdir}"
    54    (cd "${tmpdir}" && untar "${TARBALL}")
    55    install -d "${BINDIR}"
    56    for binexe in "${BINARY}" ; do
    57      if [ "$OS" = "windows" ]; then
    58        binexe="${binexe}.exe"
    59      fi
    60      install "${srcdir}/${binexe}" "${BINDIR}/"
    61      log_info "NOTICE: see licenses located in ${tmpdir}/${BINARY}"
    62      log_info "installed ${BINDIR}/${binexe}"
    63      log_info "please ensure ${BINDIR} is in your PATH"
    64    done
    65  }
    66  is_supported_platform() {
    67    platform=$1
    68    found=1
    69    case "$platform" in
    70      linux/amd64) found=0 ;;
    71      linux/386) found=0 ;;
    72      darwin/amd64) found=0 ;;
    73      darwin/386) found=0 ;;
    74      windows/amd64) found=0 ;;
    75      windows/386) found=0 ;;
    76    esac
    77    case "$platform" in
    78      darwin/386) found=1 ;;
    79    esac
    80    return $found
    81  }
    82  check_platform() {
    83    if is_supported_platform "$PLATFORM"; then
    84      # optional logging goes here
    85      true
    86    else
    87      log_crit "platform $PLATFORM is not supported.  Please contact Confluent support if you believe this is a mistake."
    88      exit 1
    89    fi
    90  }
    91  tag_to_version() {
    92    if [ -z "${TAG}" ]; then
    93      log_info "checking S3 for latest tag"
    94    else
    95      log_info "checking S3 for tag '${TAG}'"
    96    fi
    97    REALTAG=$(s3_release "${TAG}") && true
    98    if test -z "$REALTAG"; then
    99      log_crit "unable to find '${TAG}' - use 'latest' or see https://github.com/${PREFIX}/releases for details"
   100      exit 1
   101    fi
   102    # if version starts with 'v', don't remove it
   103    TAG="$REALTAG"
   104    VERSION=${TAG}
   105  }
   106  adjust_format() {
   107    # change format (tar.gz or zip) based on ARCH
   108    case ${ARCH} in
   109      windows) FORMAT=zip ;;
   110    esac
   111    true
   112  }
   113  adjust_os() {
   114    # adjust archive name based on OS
   115    case ${OS} in
   116      386) OS=i386 ;;
   117      amd64) OS=x86_64 ;;
   118      darwin) OS=darwin ;;
   119      linux) OS=linux ;;
   120      windows) OS=windows ;;
   121    esac
   122    true
   123  }
   124  s3_releases() {
   125    s3url="${S3_URL}?prefix=${PROJECT_NAME}/archives/&delimiter=/"
   126    if [ ${RELEASE_TEST} = "true" ]; then
   127      s3url="${S3_URL}?prefix=cli-release-test/${PROJECT_NAME}/archives/&delimiter=/"
   128    fi
   129    xml=$(http_copy "$s3url")
   130    versions=$(echo "$xml" | sed -n 's/</\
   131  </gp' | sed -n "s/<Prefix>${PROJECT_NAME}\/archives\/\(.*\)\//\1/p") || return 1
   132    test -z "$versions" && return 1
   133    echo "$versions"
   134  }
   135  s3_release() {
   136    version=$1
   137    test -z "$version" && version="latest"
   138    s3url="${S3_URL}?prefix=${PROJECT_NAME}/archives/${version#v}/&delimiter=/"
   139    if [ ${RELEASE_TEST} = "true" ]; then
   140      s3url="${S3_URL}?prefix=cli-release-test/${PROJECT_NAME}/archives/${version#v}/&delimiter=/"
   141    fi
   142    xml=$(http_copy "$s3url")
   143    exists=$(echo "$xml" | grep "<Key>") || return 1
   144    test -z "$version" && return 1
   145    echo "$version"
   146  }
   147  
   148  cat /dev/null <<EOF
   149  ------------------------------------------------------------------------
   150  https://github.com/client9/shlib - portable posix shell functions
   151  Public domain - http://unlicense.org
   152  https://github.com/client9/shlib/blob/master/LICENSE.md
   153  but credit (and pull requests) appreciated.
   154  ------------------------------------------------------------------------
   155  EOF
   156  is_command() {
   157    command -v "$1" >/dev/null
   158  }
   159  echoerr() {
   160    echo "$@" 1>&2
   161  }
   162  log_prefix() {
   163    echo "$0"
   164  }
   165  _logp=6
   166  log_set_priority() {
   167    _logp="$1"
   168  }
   169  log_priority() {
   170    if test -z "$1"; then
   171      echo "$_logp"
   172      return
   173    fi
   174    [ "$1" -le "$_logp" ]
   175  }
   176  log_tag() {
   177    case $1 in
   178      0) echo "emerg" ;;
   179      1) echo "alert" ;;
   180      2) echo "crit" ;;
   181      3) echo "err" ;;
   182      4) echo "warning" ;;
   183      5) echo "notice" ;;
   184      6) echo "info" ;;
   185      7) echo "debug" ;;
   186      *) echo "$1" ;;
   187    esac
   188  }
   189  log_debug() {
   190    log_priority 7 || return 0
   191    echoerr "$(log_prefix)" "$(log_tag 7)" "$@"
   192  }
   193  log_info() {
   194    log_priority 6 || return 0
   195    echoerr "$(log_prefix)" "$(log_tag 6)" "$@"
   196  }
   197  log_err() {
   198    log_priority 3 || return 0
   199    echoerr "$(log_prefix)" "$(log_tag 3)" "$@"
   200  }
   201  log_crit() {
   202    log_priority 2 || return 0
   203    echoerr "$(log_prefix)" "$(log_tag 2)" "$@"
   204  }
   205  uname_os() {
   206    os=$(uname -s | tr '[:upper:]' '[:lower:]')
   207    case "$os" in
   208      msys*) os="windows" ;;
   209      mingw*) os="windows" ;;
   210      cygwin*) os="windows" ;;
   211    esac
   212    echo "$os"
   213  }
   214  uname_arch() {
   215    arch=$(uname -m)
   216    case $arch in
   217      x86_64) arch="amd64" ;;
   218      x86) arch="386" ;;
   219      i686) arch="386" ;;
   220      i386) arch="386" ;;
   221      aarch64) arch="arm64" ;;
   222      armv5*) arch="armv5" ;;
   223      armv6*) arch="armv6" ;;
   224      armv7*) arch="armv7" ;;
   225    esac
   226    echo ${arch}
   227  }
   228  uname_os_check() {
   229    os=$(uname_os)
   230    case "$os" in
   231      darwin) return 0 ;;
   232      dragonfly) return 0 ;;
   233      freebsd) return 0 ;;
   234      linux) return 0 ;;
   235      android) return 0 ;;
   236      nacl) return 0 ;;
   237      netbsd) return 0 ;;
   238      openbsd) return 0 ;;
   239      plan9) return 0 ;;
   240      solaris) return 0 ;;
   241      windows) return 0 ;;
   242    esac
   243    log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib"
   244    return 1
   245  }
   246  uname_arch_check() {
   247    arch=$(uname_arch)
   248    case "$arch" in
   249      386) return 0 ;;
   250      amd64) return 0 ;;
   251      arm64) return 0 ;;
   252      armv5) return 0 ;;
   253      armv6) return 0 ;;
   254      armv7) return 0 ;;
   255      ppc64) return 0 ;;
   256      ppc64le) return 0 ;;
   257      mips) return 0 ;;
   258      mipsle) return 0 ;;
   259      mips64) return 0 ;;
   260      mips64le) return 0 ;;
   261      s390x) return 0 ;;
   262      amd64p32) return 0 ;;
   263    esac
   264    log_crit "uname_arch_check '$(uname -m)' got converted to '$arch' which is not a GOARCH value.  Please file bug report at https://github.com/client9/shlib"
   265    return 1
   266  }
   267  untar() {
   268    tarball=$1
   269    case "${tarball}" in
   270      *.tar.gz | *.tgz) tar -xzf "${tarball}" ;;
   271      *.tar) tar -xf "${tarball}" ;;
   272      *.zip) unzip "${tarball}" ;;
   273      *)
   274        log_err "untar unknown archive format for ${tarball}"
   275        return 1
   276        ;;
   277    esac
   278  }
   279  mktmpdir() {
   280    test -z "$TMPDIR" && TMPDIR="$(mktemp -d)"
   281    mkdir -p "${TMPDIR}"
   282    echo "${TMPDIR}"
   283  }
   284  http_download_curl() {
   285    local_file=$1
   286    source_url=$2
   287    header=$3
   288    if [ -z "$header" ]; then
   289      code=$(curl -n -w '%{http_code}' -sL -o "$local_file" "$source_url")
   290    else
   291      code=$(curl -n -w '%{http_code}' -sL -H "$header" -o "$local_file" "$source_url")
   292    fi
   293    if [ "$code" != "200" ]; then
   294      log_debug "http_download_curl received HTTP status $code"
   295      return 1
   296    fi
   297    return 0
   298  }
   299  http_download_wget() {
   300    local_file=$1
   301    source_url=$2
   302    header=$3
   303    if [ -z "$header" ]; then
   304      wget -q -O "$local_file" "$source_url"
   305    else
   306      wget -q --header "$header" -O "$local_file" "$source_url"
   307    fi
   308  }
   309  http_download() {
   310    log_debug "http_download $2"
   311    if is_command curl; then
   312      http_download_curl "$@"
   313      return
   314    elif is_command wget; then
   315      http_download_wget "$@"
   316      return
   317    fi
   318    log_crit "http_download unable to find wget or curl"
   319    return 1
   320  }
   321  http_copy() {
   322    tmp=$(mktemp)
   323    http_download "${tmp}" "$1" "$2" || return 1
   324    body=$(cat "$tmp")
   325    rm -f "${tmp}"
   326    echo "$body"
   327  }
   328  hash_sha256() {
   329    TARGET=${1:-/dev/stdin}
   330    if is_command gsha256sum; then
   331      hash=$(gsha256sum "$TARGET") || return 1
   332      echo "$hash" | cut -d ' ' -f 1
   333    elif is_command sha256sum; then
   334      hash=$(sha256sum "$TARGET") || return 1
   335      echo "$hash" | cut -d ' ' -f 1
   336    elif is_command shasum; then
   337      hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
   338      echo "$hash" | cut -d ' ' -f 1
   339    elif is_command openssl; then
   340      hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
   341      echo "$hash" | cut -d ' ' -f a
   342    else
   343      log_crit "hash_sha256 unable to find command to compute sha-256 hash"
   344      return 1
   345    fi
   346  }
   347  hash_sha256_verify() {
   348    TARGET=$1
   349    checksums=$2
   350    if [ -z "$checksums" ]; then
   351      log_err "hash_sha256_verify checksum file not specified in arg2"
   352      return 1
   353    fi
   354    BASENAME=${TARGET##*/}
   355    want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
   356    if [ -z "$want" ]; then
   357      log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
   358      return 1
   359    fi
   360    got=$(hash_sha256 "$TARGET")
   361    if [ "$want" != "$got" ]; then
   362      log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
   363      return 1
   364    fi
   365  }
   366  cat /dev/null <<EOF
   367  ------------------------------------------------------------------------
   368  End of functions from https://github.com/client9/shlib
   369  ------------------------------------------------------------------------
   370  EOF
   371  
   372  PROJECT_NAME="confluent-cli"
   373  OWNER=confluentinc
   374  REPO="cli"
   375  BINARY=confluent
   376  FORMAT=tar.gz
   377  OS=$(uname_os)
   378  ARCH=$(uname_arch)
   379  PREFIX="$OWNER/$REPO"
   380  
   381  # use in logging routines
   382  log_prefix() {
   383  	echo "$PREFIX"
   384  }
   385  PLATFORM="${OS}/${ARCH}"
   386  
   387  main() {
   388    uname_os_check
   389    uname_arch_check
   390  
   391    parse_args "$@"
   392  
   393    check_platform
   394  
   395    tag_to_version
   396  
   397    adjust_format
   398  
   399    adjust_os
   400  
   401    if [ ${OS} = "windows" ]; then
   402      FORMAT=zip ;
   403    fi
   404  
   405    log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
   406  
   407    ARCHIVES_S3_PATH=${PROJECT_NAME}/archives/${VERSION#v}
   408    S3_ARCHIVES_URL=${S3_URL}/${ARCHIVES_S3_PATH}
   409    if [ ${RELEASE_TEST} = "true" ]; then
   410      S3_ARCHIVES_URL=${S3_URL}/cli-release-test/${ARCHIVES_S3_PATH}
   411    fi
   412    NAME=${BINARY}_${VERSION}_${OS}_${ARCH}
   413    TARBALL=${NAME}.${FORMAT}
   414    TARBALL_URL=${S3_ARCHIVES_URL}/${TARBALL}
   415    CHECKSUM=${BINARY}_${VERSION}_checksums.txt
   416    CHECKSUM_URL=${S3_ARCHIVES_URL}/${CHECKSUM}
   417  
   418    execute
   419  }
   420  
   421  if [ "${TEST}" != "true" ]; then
   422    main "$@"
   423  fi