vitess.io/vitess@v0.16.2/bootstrap.sh (about)

     1  #!/bin/bash
     2  # shellcheck disable=SC2164
     3  
     4  # Copyright 2019 The Vitess Authors.
     5  #
     6  # Licensed under the Apache License, Version 2.0 (the "License");
     7  # you may not use this file except in compliance with the License.
     8  # You may obtain a copy of the License at
     9  #
    10  #     http://www.apache.org/licenses/LICENSE-2.0
    11  #
    12  # Unless required by applicable law or agreed to in writing, software
    13  # distributed under the License is distributed on an "AS IS" BASIS,
    14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  # See the License for the specific language governing permissions and
    16  # limitations under the License.
    17  
    18  ### This file is executed by 'make tools'. You do not need to execute it directly.
    19  
    20  source ./dev.env
    21  
    22  # Outline of this file.
    23  # 0. Initialization and helper methods.
    24  # 1. Installation of dependencies.
    25  
    26  BUILD_JAVA=${BUILD_JAVA:-1}
    27  BUILD_CONSUL=${BUILD_CONSUL:-1}
    28  BUILD_CHROME=${BUILD_CHROME:-1}
    29  
    30  VITESS_RESOURCES_DOWNLOAD_BASE_URL="https://github.com/vitessio/vitess-resources/releases/download"
    31  VITESS_RESOURCES_RELEASE="v2.0"
    32  VITESS_RESOURCES_DOWNLOAD_URL="${VITESS_RESOURCES_DOWNLOAD_BASE_URL}/${VITESS_RESOURCES_RELEASE}"
    33  #
    34  # 0. Initialization and helper methods.
    35  #
    36  
    37  [[ "$(dirname "$0")" = "." ]] || fail "bootstrap.sh must be run from its current directory"
    38  
    39  # install_dep is a helper function to generalize the download and installation of dependencies.
    40  #
    41  # If the installation is successful, it puts the installed version string into
    42  # the $dist/.installed_version file. If the version has not changed, bootstrap
    43  # will skip future installations.
    44  install_dep() {
    45    if [[ $# != 4 ]]; then
    46      fail "install_dep function requires exactly 4 parameters (and not $#). Parameters: $*"
    47    fi
    48    local name="$1"
    49    local version="$2"
    50    local dist="$3"
    51    local install_func="$4"
    52  
    53    version_file="$dist/.installed_version"
    54    if [[ -f "$version_file" && "$(cat "$version_file")" == "$version" ]]; then
    55      echo "skipping $name install. remove $dist to force re-install."
    56      return
    57    fi
    58  
    59    echo "<<< Installing $name $version >>>"
    60  
    61    # shellcheck disable=SC2064
    62    trap "fail '$name build failed'; exit 1" ERR
    63  
    64    # Cleanup any existing data and re-create the directory.
    65    rm -rf "$dist"
    66    mkdir -p "$dist"
    67  
    68    # Change $CWD to $dist before calling "install_func".
    69    pushd "$dist" >/dev/null
    70    # -E (same as "set -o errtrace") makes sure that "install_func" inherits the
    71    # trap. If here's an error, the trap will be called which will exit this
    72    # script.
    73    set -E
    74    $install_func "$version" "$dist"
    75    set +E
    76    popd >/dev/null
    77  
    78    trap - ERR
    79  
    80    echo "$version" > "$version_file"
    81  }
    82  
    83  
    84  #
    85  # 1. Installation of dependencies.
    86  #
    87  
    88  # We should not use the arch command, since it is not reliably
    89  # available on macOS or some linuxes:
    90  # https://www.gnu.org/software/coreutils/manual/html_node/arch-invocation.html
    91  get_arch() {
    92    uname -m
    93  }
    94  
    95  # Install protoc.
    96  install_protoc() {
    97    local version="$1"
    98    local dist="$2"
    99  
   100    case $(uname) in
   101      Linux)  local platform=linux;;
   102      Darwin) local platform=osx;;
   103      *) echo "ERROR: unsupported platform for protoc"; exit 1;;
   104    esac
   105  
   106    case $(get_arch) in
   107        aarch64)  local target=aarch_64;;
   108        x86_64)  local target=x86_64;;
   109        arm64) case "$platform" in
   110            osx) local target=aarch_64;;
   111            *) echo "ERROR: unsupported architecture for protoc"; exit 1;;
   112        esac;;
   113        *)   echo "ERROR: unsupported architecture for protoc"; exit 1;;
   114    esac
   115  
   116    # This is how we'd download directly from source:
   117    $VTROOT/tools/wget-retry https://github.com/protocolbuffers/protobuf/releases/download/v$version/protoc-$version-$platform-${target}.zip
   118    #$VTROOT/tools/wget-retry "${VITESS_RESOURCES_DOWNLOAD_URL}/protoc-$version-$platform-${target}.zip"
   119    unzip "protoc-$version-$platform-${target}.zip"
   120  
   121    ln -snf "$dist/bin/protoc" "$VTROOT/bin/protoc"
   122  }
   123  
   124  
   125  # Install Zookeeper.
   126  install_zookeeper() {
   127    local version="$1"
   128    local dist="$2"
   129    zk="zookeeper-$version"
   130    vtzk="vt-zookeeper-$version"
   131    # This is how we'd download directly from source:
   132    # wget "https://dlcdn.apache.org/zookeeper/$zk/apache-$zk.tar.gz"
   133    $VTROOT/tools/wget-retry "${VITESS_RESOURCES_DOWNLOAD_URL}/apache-${zk}.tar.gz"
   134    tar -xzf "$dist/apache-$zk.tar.gz"
   135    mv $dist/apache-$zk $dist/$vtzk
   136    mvn -f $dist/$vtzk/zookeeper-contrib/zookeeper-contrib-fatjar/pom.xml clean install -P fatjar -DskipTests
   137    mkdir -p $dist/$vtzk/lib
   138    cp "$dist/$vtzk/zookeeper-contrib/zookeeper-contrib-fatjar/target/$zk-fatjar.jar" "$dist/$vtzk/lib/$zk-fatjar.jar"
   139    rm -rf "$zk.tar.gz"
   140  }
   141  
   142  
   143  # Download and install etcd, link etcd binary into our root.
   144  install_etcd() {
   145    local version="$1"
   146    local dist="$2"
   147  
   148    case $(uname) in
   149      Linux)  local platform=linux; local ext=tar.gz;;
   150      Darwin) local platform=darwin; local ext=zip;;
   151      *)   echo "ERROR: unsupported platform for etcd"; exit 1;;
   152    esac
   153  
   154    case $(get_arch) in
   155        aarch64)  local target=arm64;;
   156        x86_64)  local target=amd64;;
   157        arm64)  local target=arm64;;
   158        *)   echo "ERROR: unsupported architecture for etcd"; exit 1;;
   159    esac
   160  
   161    file="etcd-${version}-${platform}-${target}.${ext}"
   162  
   163    # This is how we'd download directly from source:
   164    $VTROOT/tools/wget-retry "https://github.com/etcd-io/etcd/releases/download/$version/$file"
   165    #$VTROOT/tools/wget-retry "${VITESS_RESOURCES_DOWNLOAD_URL}/${file}"
   166    if [ "$ext" = "tar.gz" ]; then
   167      tar xzf "$file"
   168    else
   169      unzip "$file"
   170    fi
   171    rm "$file"
   172    ln -snf "$dist/etcd-${version}-${platform}-${target}/etcd" "$VTROOT/bin/etcd"
   173    ln -snf "$dist/etcd-${version}-${platform}-${target}/etcdctl" "$VTROOT/bin/etcdctl"
   174  }
   175  
   176  
   177  # Download and install k3s, link k3s binary into our root
   178  install_k3s() {
   179    local version="$1"
   180    local dist="$2"
   181    case $(uname) in
   182      Linux)  local platform=linux;;
   183      *)   echo "WARNING: unsupported platform. K3s only supports running on Linux, the k8s topology will not be available for local examples."; return;;
   184    esac
   185  
   186    case $(get_arch) in
   187        aarch64)  local target="-arm64";;
   188        x86_64) local target="";;
   189        arm64)  local target="-arm64";;
   190        *)   echo "WARNING: unsupported architecture, the k8s topology will not be available for local examples."; return;;
   191    esac
   192  
   193    file="k3s${target}"
   194  
   195    local dest="$dist/k3s${target}-${version}-${platform}"
   196    # This is how we'd download directly from source:
   197    # download_url=https://github.com/rancher/k3s/releases/download
   198    # wget -O  $dest "$download_url/$version/$file"
   199    $VTROOT/tools/wget-retry -O $dest "${VITESS_RESOURCES_DOWNLOAD_URL}/$file-$version"
   200    chmod +x $dest
   201    ln -snf  $dest "$VTROOT/bin/k3s"
   202  }
   203  
   204  
   205  # Download and install consul, link consul binary into our root.
   206  install_consul() {
   207    local version="$1"
   208    local dist="$2"
   209  
   210    case $(uname) in
   211      Linux)  local platform=linux;;
   212      Darwin) local platform=darwin;;
   213      *)   echo "ERROR: unsupported platform for consul"; exit 1;;
   214    esac
   215  
   216    case $(get_arch) in
   217      aarch64)  local target=arm64;;
   218      x86_64)  local target=amd64;;
   219      arm64)  local target=arm64;;
   220      *)   echo "ERROR: unsupported architecture for consul"; exit 1;;
   221    esac
   222  
   223    # This is how we'd download directly from source:
   224    # download_url=https://releases.hashicorp.com/consul
   225    # wget "${download_url}/${version}/consul_${version}_${platform}_${target}.zip"
   226    $VTROOT/tools/wget-retry "${VITESS_RESOURCES_DOWNLOAD_URL}/consul_${version}_${platform}_${target}.zip"
   227    unzip "consul_${version}_${platform}_${target}.zip"
   228    ln -snf "$dist/consul" "$VTROOT/bin/consul"
   229  }
   230  
   231  
   232  # Download chromedriver
   233  install_chromedriver() {
   234    local version="$1"
   235    local dist="$2"
   236  
   237    case $(uname) in
   238      Linux)  local platform=linux;;
   239      *)   echo "Platform not supported for vtctl-web tests. Skipping chromedriver install."; return;;
   240    esac
   241  
   242    if [ "$(arch)" == "aarch64" ] ; then
   243        os=$(cat /etc/*release | grep "^ID=" | cut -d '=' -f 2)
   244        case $os in
   245          ubuntu|debian)
   246            sudo apt-get update -y && sudo apt install -y --no-install-recommends unzip libglib2.0-0 libnss3 libx11-6
   247          ;;
   248          centos|fedora)
   249            sudo yum update -y && yum install -y libX11 unzip wget
   250          ;;
   251        esac
   252        echo "For Arm64, using prebuilt binary from electron (https://github.com/electron/electron/) of version 76.0.3809.126"
   253        $VTROOT/tools/wget-retry https://github.com/electron/electron/releases/download/v6.0.3/chromedriver-v6.0.3-linux-arm64.zip
   254        unzip -o -q chromedriver-v6.0.3-linux-arm64.zip -d "$dist"
   255        rm chromedriver-v6.0.3-linux-arm64.zip
   256    else
   257        $VTROOT/tools/wget-retry "https://chromedriver.storage.googleapis.com/$version/chromedriver_linux64.zip"
   258        unzip -o -q chromedriver_linux64.zip -d "$dist"
   259        rm chromedriver_linux64.zip
   260    fi
   261  }
   262  
   263  install_all() {
   264    echo "##local system details..."
   265    echo "##platform: $(uname) target:$(get_arch) OS: $os"
   266    # protoc
   267    protoc_ver=21.3
   268    install_dep "protoc" "$protoc_ver" "$VTROOT/dist/vt-protoc-$protoc_ver" install_protoc
   269  
   270    # zk
   271    zk_ver=${ZK_VERSION:-3.8.0}
   272    if [ "$BUILD_JAVA" == 1 ] ; then
   273      install_dep "Zookeeper" "$zk_ver" "$VTROOT/dist" install_zookeeper
   274    fi
   275  
   276    # etcd
   277    install_dep "etcd" "v3.5.6" "$VTROOT/dist/etcd" install_etcd
   278  
   279    # k3s
   280    command -v k3s || install_dep "k3s" "v1.0.0" "$VTROOT/dist/k3s" install_k3s
   281  
   282    # consul
   283    if [ "$BUILD_CONSUL" == 1 ] ; then
   284      install_dep "Consul" "1.11.4" "$VTROOT/dist/consul" install_consul
   285    fi
   286  
   287    # chromedriver
   288    if [ "$BUILD_CHROME" == 1 ] ; then
   289      install_dep "chromedriver" "90.0.4430.24" "$VTROOT/dist/chromedriver" install_chromedriver
   290    fi
   291  
   292    echo
   293    echo "bootstrap finished - run 'make build' to compile"
   294  }
   295  
   296  install_all