github.com/iotexproject/iotex-core@v1.14.1-rc1/install-injector.sh (about)

     1  #!/bin/sh
     2  
     3  # This install script is intended to download and install the latest available
     4  # release of the ioctl dependency manager for Golang.
     5  #
     6  # It attempts to identify the current platform and an error will be thrown if
     7  # the platform is not supported.
     8  #
     9  # Environment variables:
    10  # - INSTALL_DIRECTORY (optional): defaults to $GOPATH/bin (if $GOPATH exists) 
    11  #   or /usr/local/bin (else)
    12  # - CLI_RELEASE_TAG (optional): defaults to fetching the latest release
    13  #
    14  # You can install using this script:
    15  # $ curl https://raw.githubusercontent.com/iotexproject/iotex-core/master/install-cli.sh | sh
    16  
    17  set -e
    18  
    19  RELEASES_URL="https://github.com/iotexproject/iotex-core/releases"
    20  S3URL="https://s3-ap-southeast-1.amazonaws.com/ioctl"
    21  INSTALL_DIRECTORY='/usr/local/bin'
    22  
    23  downloadJSON() {
    24      url="$2"
    25  
    26      echo "Fetching $url.."
    27      if test -x "$(command -v curl)"; then
    28          response=$(curl -s -L -w 'HTTPSTATUS:%{http_code}' -H 'Accept: application/json' "$url")
    29          body=$(echo "$response" | sed -e 's/HTTPSTATUS\:.*//g')
    30          code=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
    31      elif test -x "$(command -v wget)"; then
    32          temp=$(mktemp)
    33          body=$(wget -q --header='Accept: application/json' -O - --server-response "$url" 2> "$temp")
    34          code=$(awk '/^  HTTP/{print $2}' < "$temp" | tail -1)
    35          rm "$temp"
    36      else
    37          echo "Neither curl nor wget was available to perform http requests."
    38          exit 1
    39      fi
    40      if [ "$code" != 200 ]; then
    41          echo "Request failed with code $code"
    42          exit 1
    43      fi
    44  
    45      eval "$1='$body'"
    46  }
    47  
    48  downloadFile() {
    49      url="$1"
    50      destination="$2"
    51  
    52      echo "Fetching $url.."
    53      if test -x "$(command -v curl)"; then
    54          code=$(curl -s -w '%{http_code}' -L "$url" -o "$destination")
    55      elif test -x "$(command -v wget)"; then
    56          code=$(wget -q -O "$destination" --server-response "$url" 2>&1 | awk '/^  HTTP/{print $2}' | tail -1)
    57      else
    58          echo "Neither curl nor wget was available to perform http requests."
    59          exit 1
    60      fi
    61  
    62      if [ "$code" != 200 ]; then
    63          echo "Request failed with code $code"
    64          exit 1
    65      fi
    66  }
    67  
    68  initArch() {
    69      ARCH=$(uname -m)
    70      case $ARCH in
    71          amd64) ARCH="amd64";;
    72          x86_64) ARCH="amd64";;
    73          i386) ARCH="386";;
    74          ppc64) ARCH="ppc64";;
    75          ppc64le) ARCH="ppc64le";;
    76          s390x) ARCH="s390x";;
    77          armv6*) ARCH="arm";;
    78          armv7*) ARCH="arm";;
    79          aarch64) ARCH="arm64";;
    80          *) echo "Architecture ${ARCH} is not supported by this installation script"; exit 1;;
    81      esac
    82      echo "ARCH = $ARCH"
    83  }
    84  
    85  initOS() {
    86      OS=$(uname | tr '[:upper:]' '[:lower:]')
    87      OS_CYGWIN=0
    88      case "$OS" in
    89          darwin) OS='darwin';;
    90          linux) OS='linux';;
    91          freebsd) OS='freebsd';;
    92          mingw*) OS='windows';;
    93          msys*) OS='windows';;
    94  	cygwin*)
    95  	    OS='windows'
    96  	    OS_CYGWIN=1
    97  	    ;;
    98          *) echo "OS ${OS} is not supported by this installation script"; exit 1;;
    99      esac
   100      echo "OS = $OS"
   101  }
   102  
   103  # identify platform based on uname output
   104  initArch
   105  initOS
   106  INSTALL_NAME="injector"
   107  
   108  # assemble expected release artifact name
   109  if [ "${OS}" != "linux" ] && { [ "${ARCH}" = "ppc64" ] || [ "${ARCH}" = "ppc64le" ];}; then
   110      # ppc64 and ppc64le are only supported on Linux.
   111      echo "${OS}-${ARCH} is not supported by this instalation script"
   112  else
   113      BINARY="${INSTALL_NAME}-${OS}-${ARCH}"
   114  fi
   115  
   116  # add .exe if on windows
   117  if [ "$OS" = "windows" ]; then
   118      BINARY="$BINARY.exe"
   119  fi
   120  
   121  if [ -z "$CLI_RELEASE_TAG" ]; then
   122      downloadJSON LATEST_RELEASE "$RELEASES_URL/latest"
   123      CLI_RELEASE_TAG=$(echo "${LATEST_RELEASE}" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//' )
   124  fi
   125  
   126  if [ "$1" = "unstable" ]; then
   127      BINARY_URL="$S3URL/$BINARY"
   128  
   129  else
   130      # fetch the real release data to make sure it exists before we attempt a download
   131      downloadJSON RELEASE_DATA "$RELEASES_URL/tag/$CLI_RELEASE_TAG"
   132      BINARY_URL="$RELEASES_URL/download/$CLI_RELEASE_TAG/$BINARY"
   133  fi
   134  
   135  DOWNLOAD_FILE=$(mktemp)
   136  
   137  downloadFile "$BINARY_URL" "$DOWNLOAD_FILE"
   138  
   139  echo "Setting executable permissions."
   140  chmod +x "$DOWNLOAD_FILE"
   141  
   142  
   143  if [ "$OS" = "windows" ]; then
   144      INSTALL_NAME="$INSTALL_NAME.exe"
   145      echo "Moving executable to $HOME/$INSTALL_NAME"
   146      mv "$DOWNLOAD_FILE" "$HOME/$INSTALL_NAME"
   147  else
   148      echo "Moving executable to $INSTALL_DIRECTORY/$INSTALL_NAME"
   149      sudo mv "$DOWNLOAD_FILE" "$INSTALL_DIRECTORY/$INSTALL_NAME"
   150  fi
   151  
   152