github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/hack/install_cli_docker.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (C) 2022-2023 ApeCloud Co., Ltd
     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  : ${CLI_INSTALL_DIR:="/usr/local/bin"}
    18  : ${CLI_BREW_INSTALL_DIR:="/opt/homebrew/bin"}
    19  
    20  # sudo is required to copy binary to CLI_INSTALL_DIR for linux
    21  : ${USE_SUDO:="false"}
    22  
    23  # Http request CLI
    24  HTTP_REQUEST_CLI=curl
    25  
    26  # cli filename
    27  CLI_FILENAME=kbcli
    28  
    29  CLI_FILE="${CLI_INSTALL_DIR}/${CLI_FILENAME}"
    30  CLI_BREW_FILE="${CLI_BREW_INSTALL_DIR}/${CLI_FILENAME}"
    31  
    32  getSystemInfo() {
    33      ARCH=$(uname -m)
    34      case $ARCH in
    35          armv7*) ARCH="arm";;
    36          aarch64) ARCH="arm64";;
    37          x86_64) ARCH="amd64";;
    38      esac
    39  
    40      OS=$(echo $(uname) | tr '[:upper:]' '[:lower:]')
    41  
    42      # Most linux distro needs root permission to copy the file to /usr/local/bin
    43      if [ "$OS" == "linux" ] || [ "$OS" == "darwin" ]; then
    44          if [ "$CLI_INSTALL_DIR" == "/usr/local/bin" ]; then
    45              USE_SUDO="true"
    46          fi
    47      fi
    48  }
    49  
    50  verifySupported() {
    51      local supported=(darwin-amd64 darwin-arm64 linux-amd64 linux-arm linux-arm64)
    52      local current_osarch="${OS}-${ARCH}"
    53  
    54      for osarch in "${supported[@]}"; do
    55          if [ "$osarch" == "$current_osarch" ]; then
    56              echo "Your system is ${OS}_${ARCH}"
    57              return
    58          fi
    59      done
    60  
    61      echo "No prebuilt binary for ${current_osarch}"
    62      exit 1
    63  }
    64  
    65  runAsRoot() {
    66      local CMD="$*"
    67  
    68      if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then
    69          CMD="sudo $CMD"
    70      fi
    71  
    72      $CMD
    73  }
    74  
    75  checkHttpRequestCLI() {
    76      if type "curl" >/dev/null; then
    77          HTTP_REQUEST_CLI=curl
    78      elif type "wget" >/dev/null; then
    79          HTTP_REQUEST_CLI=wget
    80      else
    81          echo "Either curl or wget is required"
    82          exit 1
    83      fi
    84  }
    85  
    86  checkExistingCli() {
    87      if [ -f "$CLI_FILE" ]; then
    88          echo -e "\nkbcli is detected: $CLI_FILE"
    89          echo -e "\nPlease uninstall first"
    90          exit 1
    91      elif [ -f "$CLI_BREW_FILE" ]; then
    92          echo -e "\nkbcli is detected: $CLI_BREW_FILE"
    93          echo -e "\nPlease uninstall first"
    94          exit 1
    95      else
    96          echo -e "Installing kbcli ...\n"
    97      fi
    98  }
    99  
   100  downloadDockerImage() {
   101      LATEST_RELEASE_TAG=$1
   102      # Create the temp directory
   103      CLI_TMP_ROOT=$(mktemp -dt kbcli-install-XXXXXX)
   104      # pull image and run
   105      echo -e "Pulling kbcli image..."
   106      docker run --name kbcli -d docker.io/apecloud/kbcli:${LATEST_RELEASE_TAG} sh &>/dev/null
   107      # copy kbcli to /tmp-xxx/kbcli
   108      docker cp kbcli:/kbcli.${OS}.${ARCH} ${CLI_TMP_ROOT}/${CLI_FILENAME} 2>&1 >/dev/null
   109      # remove docker
   110      docker rm kbcli 2>&1 >/dev/null
   111  }
   112  
   113  installFile() {
   114      local tmp_root_kbcli="$CLI_TMP_ROOT/$CLI_FILENAME"
   115  
   116      if [ ! -f "$tmp_root_kbcli" ]; then
   117          echo "Failed to pull kbcli."
   118          exit 1
   119      fi
   120  
   121      chmod o+x "$tmp_root_kbcli"
   122      runAsRoot cp "$tmp_root_kbcli" "$CLI_INSTALL_DIR"
   123  
   124      if [ $? -eq 0 ] && [ -f "$CLI_FILE" ]; then
   125          echo "kbcli installed successfully."
   126          kbcli version
   127          echo -e "Make sure your docker service is running and begin your journey with kbcli:\n"
   128          echo -e "\t$CLI_FILENAME playground init"
   129      else
   130          echo "Failed to install $CLI_FILENAME"
   131          exit 1
   132      fi
   133  }
   134  
   135  fail_trap() {
   136      result=$?
   137      if [ "$result" != "0" ]; then
   138          echo "Failed to install kbcli"
   139          echo "Go to https://kubeblocks.io for more support."
   140      fi
   141      cleanup
   142      exit $result
   143  }
   144  
   145  cleanup() {
   146      if [[ -d "${CLI_TMP_ROOT:-}" ]]; then
   147          rm -rf "$CLI_TMP_ROOT"
   148      fi
   149  }
   150  
   151  installCompleted() {
   152      echo -e "\nFor more information on how to get started, please visit:"
   153      echo "  https://kubeblocks.io"
   154  }
   155  
   156  # -----------------------------------------------------------------------------
   157  # main
   158  # -----------------------------------------------------------------------------
   159  trap "fail_trap" EXIT
   160  
   161  getSystemInfo
   162  verifySupported
   163  checkExistingCli
   164  checkHttpRequestCLI
   165  
   166  if [ -z "$1" ]; then
   167      echo "Getting the latest kbcli..."
   168      ret_val="v0.1.0"
   169  elif [[ $1 == v* ]]; then
   170      ret_val=$1
   171  else
   172      ret_val=v$1
   173  fi
   174  
   175  downloadDockerImage $ret_val
   176  installFile
   177  cleanup
   178  installCompleted