github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/tests/up.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  set -eo pipefail
     4  
     5  IMAGE_TAG="nightly"
     6  while [[ $# -gt 0 ]]
     7  do
     8      key="$1"
     9  
    10      case $key in
    11          --pull-images)
    12              PULL_DOCKER_IMAGES=1
    13              shift
    14              ;;
    15          --tag)
    16              IMAGE_TAG=$2
    17              shift
    18              shift
    19              ;;
    20          --cleanup-docker)
    21              CLEANUP_DOCKER=1
    22              shift
    23              ;;
    24          --cleanup-data)
    25              CLEANUP_DATA=1
    26              shift
    27              ;;
    28          --cleanup-all)
    29              CLEANUP_ALL=1
    30              shift
    31              ;;
    32          --bind-bin)
    33              BIND_BIN=1
    34              shift
    35              ;;
    36          --help)
    37              HELP=1
    38              shift
    39              ;;
    40          *)
    41              HELP=1
    42              break
    43              ;;
    44      esac
    45  done
    46  
    47  if [ "$HELP" ]; then
    48      echo "Usage: $0 [OPTIONS]"
    49      echo "OPTIONS:"
    50      echo "  --help                 Display this message"
    51      echo "  --pull-images          Update docker images used in br tests"
    52      echo "  --tag (TAG)            Specify images tag used in br tests"
    53      echo "  --cleanup-docker       Clean up br tests Docker containers"
    54      echo "  --cleanup-data         Clean up persistent data"
    55      echo "  --cleanup-all          Clean up all data inlcuding Docker images, containers and persistent data"
    56      echo "  --bind-bin             Bind br/bin directory"
    57      exit 0
    58  fi
    59  
    60  host_tmp=/tmp/br_tests
    61  host_bash_history=$host_tmp/bash_history
    62  
    63  # Persist tests data and bash history
    64  mkdir -p $host_tmp
    65  touch $host_bash_history || true
    66  function cleanup_data() {
    67      rm -rf $host_tmp || { echo try "sudo rm -rf $host_tmp"? ; exit 1; }
    68  }
    69  if [ "$CLEANUP_DATA" ]; then
    70      cleanup_data
    71      exit 0
    72  fi
    73  
    74  # Clean up docker images and containers.
    75  docker_repo=br_tests
    76  function cleanup_docker_containers() {
    77      containers=$(docker container ps --all --filter="ancestor=$docker_repo:$IMAGE_TAG" -q)
    78      if [ "$containers" ]; then
    79          docker stop $containers
    80          docker rm $containers
    81      fi
    82  }
    83  function cleanup_docker_images() {
    84      images=$(docker images --filter="reference=$docker_repo:$IMAGE_TAG" -q)
    85      if [ "$images" ]; then
    86          docker rmi $images
    87      fi
    88  }
    89  if [ "$CLEANUP_DOCKER" ]; then
    90      cleanup_docker_containers
    91      exit 0
    92  fi
    93  
    94  if [ "$CLEANUP_ALL" ]; then
    95      cleanup_data
    96      cleanup_docker_containers
    97      cleanup_docker_images
    98      exit 0
    99  fi
   100  
   101  if [ "$PULL_DOCKER_IMAGES" ]; then
   102      for image in "pingcap/tidb" "pingcap/tikv" "pingcap/pd" "pingcap/ticdc" "pingcap/tiflash" "pingcap/tidb-lightning"; do
   103          docker pull $image:$IMAGE_TAG
   104          docker tag $image:$IMAGE_TAG $image:$IMAGE_TAG.$docker_repo
   105      done
   106  fi
   107  
   108  docker build -t $docker_repo:$IMAGE_TAG - << EOF
   109  FROM pingcap/tidb:$IMAGE_TAG.$docker_repo           AS tidb-builder
   110  FROM pingcap/tikv:$IMAGE_TAG.$docker_repo           AS tikv-builder
   111  FROM pingcap/pd:$IMAGE_TAG.$docker_repo             AS pd-builder
   112  FROM pingcap/ticdc:$IMAGE_TAG.$docker_repo          AS ticdc-builder
   113  FROM pingcap/tiflash:$IMAGE_TAG.$docker_repo        AS tiflash-builder
   114  FROM pingcap/tidb-lightning:$IMAGE_TAG.$docker_repo AS lightning-builder
   115  FROM pingcap/br:v4.0.8                              AS br408-builder
   116  FROM minio/minio                                    AS minio-builder
   117  FROM minio/mc                                       AS mc-builder
   118  FROM fsouza/fake-gcs-server                         AS gcs-builder
   119  
   120  FROM golang:1.16.4-buster as ycsb-builder
   121  WORKDIR /go/src/github.com/pingcap/
   122  RUN git clone https://github.com/pingcap/go-ycsb.git && \
   123      cd go-ycsb && \
   124      make && \
   125      cp bin/go-ycsb /go-ycsb
   126  
   127  FROM golang:1.16.4-buster
   128  
   129  RUN apt-get update && apt-get install -y --no-install-recommends \
   130      git \
   131      curl \
   132      wget \
   133      openssl \
   134      lsof \
   135      psmisc \
   136      vim \
   137      less \
   138      jq \
   139      default-mysql-client
   140  
   141  RUN mkdir -p /br/bin
   142  COPY --from=tidb-builder      /tidb-server                   /br/bin/tidb-server
   143  COPY --from=tikv-builder      /tikv-server                   /br/bin/tikv-server
   144  COPY --from=pd-builder        /pd-server                     /br/bin/pd-server
   145  COPY --from=pd-builder        /pd-ctl                        /br/bin/pd-ctl
   146  COPY --from=ticdc-builder     /cdc                           /br/bin/cdc
   147  COPY --from=br408-builder     /br                            /br/bin/brv4.0.8
   148  COPY --from=ycsb-builder      /go-ycsb                       /br/bin/go-ycsb
   149  COPY --from=tiflash-builder   /tiflash/tiflash               /br/bin/tiflash
   150  COPY --from=tiflash-builder   /tiflash/libtiflash_proxy.so   /br/bin/libtiflash_proxy.so
   151  COPY --from=tiflash-builder   /tiflash/flash_cluster_manager /br/bin/flash_cluster_manager
   152  COPY --from=lightning-builder /tikv-importer                 /br/bin/tikv-importer
   153  COPY --from=minio-builder     /usr/bin/minio                 /br/bin/minio
   154  COPY --from=mc-builder        /usr/bin/mc                    /br/bin/mc
   155  COPY --from=gcs-builder       /bin/fake-gcs-server           /br/bin/fake-gcs-server
   156  
   157  WORKDIR /br
   158  
   159  # Required by tiflash
   160  ENV LD_LIBRARY_PATH=/br/bin
   161  
   162  ENTRYPOINT ["/bin/bash"]
   163  EOF
   164  
   165  # Start an existing container or create and run a new container.
   166  exist_container=$(docker container ps --all -q --filter="ancestor=$docker_repo:$IMAGE_TAG" --filter="status=exited" | head -n 1)
   167  if [ "$exist_container" ]; then
   168      docker start $exist_container
   169      echo "Attach exsiting container: $exist_container"
   170      exec docker attach $exist_container
   171  else
   172      volume_args=
   173      for f in `ls -a`; do
   174          if [ $f = "." ] || [ $f = ".." ]; then
   175              continue
   176          fi
   177          if [ $f = "bin" ] && [ ! "$BIND_BIN" ]; then
   178              continue
   179          fi
   180          volume_args="$volume_args -v `pwd`/$f:/br/$f"
   181      done
   182      echo "Run a new container"
   183      exec docker run -it \
   184          -v $host_tmp:/tmp/br/tests \
   185          -v $host_bash_history:/root/.bash_history \
   186          $volume_args \
   187          $docker_repo:$IMAGE_TAG
   188  fi