diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 10146ded2..000000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,42 +0,0 @@ -# Use the latest 2.1 version of CircleCI pipeline process engine. -# See: https://circleci.com/docs/2.1/configuration-reference -version: 2.1 - -jobs: - govet: - docker: - - image: golang:1.21 - working_directory: /terraform-provider-mysql - steps: - - checkout - - run: - name: Vet - command: | - make vet - integration: - machine: - image: ubuntu-2204:current - docker_layer_caching: true - parameters: - target: - type: string - steps: - - checkout - - run: - name: Install mysql client - command: | - sudo apt-get update - sudo apt-get -f -y install mysql-client - - run: - name: Run test << parameters.target >> - command: | - make << parameters.target >> - -workflows: - all-tests: - jobs: - - integration: - matrix: - parameters: - target: ["testversion5.6", "testversion5.7", "testversion8.0", "testpercona5.7", "testpercona8.0", "testmariadb10.3", "testmariadb10.8", "testmariadb10.10", "testtidb6.1.0", "testtidb7.5.2"] - - govet diff --git a/.dockerignore b/.dockerignore index 9f98cb8c8..0455de207 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,6 @@ # Exclude unnecessary files from Docker build context .git .gitignore -.travis.yml .github *.md *.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index eea327a1e..4f1a82b28 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -224,7 +224,6 @@ jobs: env: GOFLAGS: -mod=vendor TF_ACC: 1 - GOTOOLCHAIN: auto run: | export PATH="${{ github.workspace }}/bin:$PATH" echo "Running ${{ matrix.db_type }} ${{ matrix.db_version }} tests using Makefile target: ${{ matrix.make_target }}" diff --git a/.testcontainers-build-flags.sh b/.testcontainers-build-flags.sh deleted file mode 100755 index 7888a5d24..000000000 --- a/.testcontainers-build-flags.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -# Build flags to suppress warnings from testcontainers dependencies -# Usage: source .testcontainers-build-flags.sh before building - -export CGO_CFLAGS="-Wno-gnu-folding-constant" diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index dd1fddd08..000000000 --- a/.travis.yml +++ /dev/null @@ -1,42 +0,0 @@ -dist: trusty - -sudo: required - -language: go - -go: - - "1.21.x" - -env: - - DB=mysql:5.6 DB_EXTRA='' - - DB=mysql:5.7 DB_EXTRA='' - - DB=mysql:8.0 DB_EXTRA='mysqld --default-authentication-plugin=mysql_native_password' - -services: - - docker - -script: - - test -d /home/travis/gopath/src/github.com/terraform-providers || mv $(dirname $PWD) /home/travis/gopath/src/github.com/terraform-providers - - make test - - make vet - - sudo service mysql stop - - export MYSQL_HOST=127.0.0.1 - - export MYSQL_PORT=3306 - - export MYSQL_USERNAME=root - - export MYSQL_ENDPOINT="${MYSQL_HOST}:${MYSQL_PORT}" - - export MYSQL_PASSWORD='' - - docker pull $DB - - docker run -d -p ${MYSQL_HOST}:${MYSQL_PORT}:${MYSQL_PORT} -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -e MYSQL_ROOT_PASSWORD='' $DB $DB_EXTRA - - docker ps -a - - | - while ! mysql -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USERNAME} -e 'SELECT 1'; do - echo 'Waiting for MySQL...' - sleep 1; - done; - - mysql -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USERNAME} -e "INSTALL PLUGIN mysql_no_login SONAME 'mysql_no_login.so';" - - make testacc - -matrix: - fast_finish: true - allow_failures: - - go: tip diff --git a/Makefile b/Makefile index bc3175b79..e35cda33c 100644 --- a/Makefile +++ b/Makefile @@ -90,13 +90,13 @@ test-sequential: acceptance # Run testcontainers tests with a matrix of all database versions # Usage: make testcontainers-matrix TESTARGS="TestAccUser" testcontainers-matrix: fmtcheck bin/terraform ## Run test matrix across all database versions - @cd $(CURDIR) && PATH="$(CURDIR)/bin:${PATH}" PARALLEL=4 GOTOOLCHAIN=auto TF_ACC=1 go run scripts/test-runner.go $(if $(TESTARGS),$(TESTARGS),WithTestcontainers) + @cd $(CURDIR) && PATH="$(CURDIR)/bin:${PATH}" PARALLEL=4 TF_ACC=1 go run scripts/test-runner.go $(if $(TESTARGS),$(TESTARGS),WithTestcontainers) # Run testcontainers tests for a specific database image # Usage: make testcontainers-image DOCKER_IMAGE=mysql:8.0 # make testcontainers-image DOCKER_IMAGE=tidb:8.5.3 testcontainers-image: fmtcheck bin/terraform ## Run tests for a specific database image (set DOCKER_IMAGE) - @PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers $(TEST) -v $(TESTARGS) -timeout=15m + @PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers $(TEST) -v $(TESTARGS) -timeout=15m bin/terraform: ## Download Terraform binary mkdir -p "$(CURDIR)/bin" @@ -124,13 +124,13 @@ testversion%: ## Run tests against MySQL version (e.g., testversion8.0) [backwar docker rmi mysql:$* 2>/dev/null || true; \ docker manifest rm mysql:$* 2>/dev/null || true; \ docker pull --platform linux/amd64 mysql:$* 2>&1 | grep -v "no match" || true; \ - DOCKER_DEFAULT_PLATFORM=linux/amd64 DOCKER_IMAGE=mysql:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m; \ + DOCKER_DEFAULT_PLATFORM=linux/amd64 DOCKER_IMAGE=mysql:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m; \ else \ - DOCKER_IMAGE=mysql:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m; \ + DOCKER_IMAGE=mysql:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m; \ fi testversion: ## Run tests against MySQL version (set MYSQL_VERSION) - @DOCKER_IMAGE=mysql:$(MYSQL_VERSION) PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m + @DOCKER_IMAGE=mysql:$(MYSQL_VERSION) PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m # Percona test targets - use testcontainers # Preferred format: test-percona-VERSION (e.g., test-percona-8.0) @@ -145,10 +145,10 @@ testpercona%: ## Run tests against Percona version (e.g., testpercona8.0) [backw docker manifest rm percona:$* 2>/dev/null || true; \ docker pull --platform linux/amd64 percona:$* || true; \ fi - @DOCKER_IMAGE=percona:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m + @DOCKER_IMAGE=percona:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m testpercona: ## Run tests against Percona version (set MYSQL_VERSION) - @DOCKER_IMAGE=percona:$(MYSQL_VERSION) PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m + @DOCKER_IMAGE=percona:$(MYSQL_VERSION) PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m testrdsdb%: ## Run tests against RDS MySQL version (requires MYSQL_ENDPOINT env vars) $(MAKE) MYSQL_VERSION=$* MYSQL_USERNAME=${MYSQL_USERNAME} MYSQL_HOST=$(shell echo ${MYSQL_ENDPOINT} | cut -d: -f1) MYSQL_PASSWORD=${MYSQL_PASSWORD} MYSQL_PORT=$(shell echo ${MYSQL_ENDPOINT} | cut -d: -f2) testrdsdb @@ -164,10 +164,10 @@ test-tidb-%: ## Run tests against TiDB version (e.g., test-tidb-8.5.3) @$(MAKE) testtidb$* testtidb%: ## Run tests against TiDB version (e.g., testtidb8.5.3) [backwards compatible] - @DOCKER_IMAGE=tidb:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m + @DOCKER_IMAGE=tidb:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m testtidb: ## Run tests against TiDB version (set MYSQL_VERSION) - @DOCKER_IMAGE=tidb:$(MYSQL_VERSION) PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m + @DOCKER_IMAGE=tidb:$(MYSQL_VERSION) PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m # MariaDB test targets - use testcontainers # Preferred format: test-mariadb-VERSION (e.g., test-mariadb-10.10) @@ -175,10 +175,10 @@ test-mariadb-%: ## Run tests against MariaDB version (e.g., test-mariadb-10.10) @$(MAKE) testmariadb$* testmariadb%: ## Run tests against MariaDB version (e.g., testmariadb10.10) [backwards compatible] - @DOCKER_IMAGE=mariadb:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m + @DOCKER_IMAGE=mariadb:$* PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m testmariadb: ## Run tests against MariaDB version (set MYSQL_VERSION) - @DOCKER_IMAGE=mariadb:$(MYSQL_VERSION) PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 GOTOOLCHAIN=auto go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m + @DOCKER_IMAGE=mariadb:$(MYSQL_VERSION) PATH="$(CURDIR)/bin:${PATH}" TF_ACC=1 go test -tags=testcontainers ./mysql/... -v $(if $(TESTARGS),-run "$(TESTARGS)",) -timeout=30m vet: ## Run go vet @echo "go vet ." @@ -197,10 +197,32 @@ deps: ## Update dependencies and vendor go mod vendor fmtcheck: ## Check Go code formatting - @sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'" + @echo "==> Checking that code complies with gofmt requirements..." + @gofmt_files=$$(gofmt -l `find . -name '*.go' | grep -v vendor`); \ + if [ -n "$$gofmt_files" ]; then \ + echo 'gofmt needs running on the following files:'; \ + echo "$$gofmt_files"; \ + echo "You can use the command: \`make fmt\` to reformat code."; \ + exit 1; \ + fi errcheck: ## Run errcheck - @sh -c "'$(CURDIR)/scripts/errcheck.sh'" + @echo "==> Checking for unchecked errors..." + @if ! which errcheck > /dev/null; then \ + echo "==> Installing errcheck..."; \ + go install github.com/kisielk/errcheck@latest; \ + fi + @err_files=$$(errcheck -ignoretests \ + -ignore 'github.com/hashicorp/terraform/helper/schema:Set' \ + -ignore 'bytes:.*' \ + -ignore 'io:Close|Write' \ + $$(go list ./... | grep -v /vendor/)); \ + if [ -n "$$err_files" ]; then \ + echo 'Unchecked errors found in the following places:'; \ + echo "$$err_files"; \ + echo "Please handle returned errors. You can check directly with \`make errcheck\`"; \ + exit 1; \ + fi vendor-status: ## Show vendor status @govendor status diff --git a/README.md b/README.md index b18e3182f..8e072a0db 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,6 @@ I incorporated changes by winebarrel/terraform-provider-mysql. Because I found out about that repo only after some work, PR cannot be created easily now. Feel free to create a PR with my code to their repo or the other way around. -[![Build Status](https://www.travis-ci.com/petoju/terraform-provider-mysql.svg?branch=master)](https://www.travis-ci.com/petoju/terraform-provider-mysql) Terraform Provider ================== diff --git a/scripts/changelog-links.sh b/scripts/changelog-links.sh deleted file mode 100755 index 5fae45c78..000000000 --- a/scripts/changelog-links.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/bash - -# This script rewrites [GH-nnnn]-style references in the CHANGELOG.md file to -# be Markdown links to the given github issues. -# -# This is run during releases so that the issue references in all of the -# released items are presented as clickable links, but we can just use the -# easy [GH-nnnn] shorthand for quickly adding items to the "Unrelease" section -# while merging things between releases. - -set -e - -if [[ ! -f CHANGELOG.md ]]; then - echo "ERROR: CHANGELOG.md not found in pwd." - echo "Please run this from the root of the terraform provider repository" - exit 1 -fi - -if [[ `uname` == "Darwin" ]]; then - echo "Using BSD sed" - SED="sed -i.bak -E -e" -else - echo "Using GNU sed" - SED="sed -i.bak -r -e" -fi - -PROVIDER_URL="https:\/\/github.com\/terraform-providers\/terraform-provider-mysql\/issues" - -$SED "s/GH-([0-9]+)/\[#\1\]\($PROVIDER_URL\/\1\)/g" -e 's/\[\[#(.+)([0-9])\)]$/(\[#\1\2))/g' CHANGELOG.md - -rm CHANGELOG.md.bak diff --git a/scripts/errcheck.sh b/scripts/errcheck.sh deleted file mode 100755 index 15464f5aa..000000000 --- a/scripts/errcheck.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash - -# Check gofmt -echo "==> Checking for unchecked errors..." - -if ! which errcheck > /dev/null; then - echo "==> Installing errcheck..." - go get -u github.com/kisielk/errcheck -fi - -err_files=$(errcheck -ignoretests \ - -ignore 'github.com/hashicorp/terraform/helper/schema:Set' \ - -ignore 'bytes:.*' \ - -ignore 'io:Close|Write' \ - $(go list ./...| grep -v /vendor/)) - -if [[ -n ${err_files} ]]; then - echo 'Unchecked errors found in the following places:' - echo "${err_files}" - echo "Please handle returned errors. You can check directly with \`make errcheck\`" - exit 1 -fi - -exit 0 diff --git a/scripts/gofmtcheck.sh b/scripts/gofmtcheck.sh deleted file mode 100755 index 1c055815f..000000000 --- a/scripts/gofmtcheck.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash - -# Check gofmt -echo "==> Checking that code complies with gofmt requirements..." -gofmt_files=$(gofmt -l `find . -name '*.go' | grep -v vendor`) -if [[ -n ${gofmt_files} ]]; then - echo 'gofmt needs running on the following files:' - echo "${gofmt_files}" - echo "You can use the command: \`make fmt\` to reformat code." - exit 1 -fi - -exit 0 diff --git a/scripts/gogetcookie.sh b/scripts/gogetcookie.sh deleted file mode 100755 index 26c63a64b..000000000 --- a/scripts/gogetcookie.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -touch ~/.gitcookies -chmod 0600 ~/.gitcookies - -git config --global http.cookiefile ~/.gitcookies - -tr , \\t <<\__END__ >>~/.gitcookies -.googlesource.com,TRUE,/,TRUE,2147483647,o,git-paul.hashicorp.com=1/z7s05EYPudQ9qoe6dMVfmAVwgZopEkZBb1a2mA5QtHE -__END__ diff --git a/scripts/test-runner.go b/scripts/test-runner.go index 1e02c21c4..dd2fb259f 100644 --- a/scripts/test-runner.go +++ b/scripts/test-runner.go @@ -371,7 +371,7 @@ func runTest(job testJob) testResult { dockerImage = "tidb:" + job.image } envVars = append(envVars, "DOCKER_IMAGE="+dockerImage) - envVars = append(envVars, "TF_ACC=1", "GOTOOLCHAIN=auto") + envVars = append(envVars, "TF_ACC=1") // Handle platform-specific issues for older MySQL/Percona versions on ARM64 // MySQL 5.6, 5.7 and Percona 5.7, 8.0 don't have ARM64 builds diff --git a/scripts/tidb-playground.sh b/scripts/tidb-playground.sh deleted file mode 100755 index 607bb0dc7..000000000 --- a/scripts/tidb-playground.sh +++ /dev/null @@ -1,92 +0,0 @@ -#!/usr/bin/env bash - -# TiDB Playground wrapper script for testing -# Uses TiUP playground for faster TiDB cluster startup - -set -e - -VERSION=${MYSQL_VERSION:-7.5.2} -PORT=${MYSQL_PORT:-4000} -MODE=${1:-start} # start or stop - -# Ensure TiUP is in PATH -export PATH=$HOME/.tiup/bin:$PATH - -if ! command -v tiup &> /dev/null; then - echo "TiUP not found. Installing..." - curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh - export PATH=$HOME/.tiup/bin:$PATH -else - # Update TiUP and playground component to ensure latest version - echo "Updating TiUP and playground component..." - tiup update --self || true - tiup update playground || true -fi - -if [ "$MODE" = "start" ]; then - echo "==> Starting TiDB Playground v${VERSION} on port ${PORT}..." - - # Clean up any existing playground instances - pkill -f "tiup playground" || true - sleep 1 - - # Start playground in background - tiup playground ${VERSION} \ - --db 1 \ - --kv 1 \ - --pd 1 \ - --tiflash 0 \ - --without-monitor \ - --host 0.0.0.0 \ - --db.port ${PORT} \ - > /tmp/tidb-playground-${PORT}.log 2>&1 & - - PLAYGROUND_PID=$! - echo $PLAYGROUND_PID > /tmp/tidb-playground-${PORT}.pid - - # Determine timeout based on TiDB version - # Versions 6.1.x and 6.5.x need longer startup time - if [[ "${VERSION}" == 6.1.* ]] || [[ "${VERSION}" == 6.5.* ]]; then - TIMEOUT=240 # 4 minutes for older versions - echo "Using extended timeout (240s) for TiDB ${VERSION}" - else - TIMEOUT=120 # 2 minutes for newer versions - fi - - # Wait for TiDB to be ready - echo "Waiting for TiDB to be ready (max ${TIMEOUT} seconds)..." - for i in $(seq 1 ${TIMEOUT}); do - if mysql -h 127.0.0.1 -P ${PORT} -u root -e 'SELECT 1' >/dev/null 2>&1; then - echo "TiDB is ready!" - exit 0 - fi - sleep 1 - if [ $((i % 10)) -eq 0 ]; then - printf "." - fi - done - - echo "" - echo "ERROR: TiDB failed to start within ${TIMEOUT} seconds" - echo "Last 20 lines of playground log:" - tail -20 /tmp/tidb-playground-${PORT}.log || true - exit 1 - -elif [ "$MODE" = "stop" ]; then - echo "==> Stopping TiDB Playground..." - - # Kill by PID if available - if [ -f /tmp/tidb-playground-${PORT}.pid ]; then - PID=$(cat /tmp/tidb-playground-${PORT}.pid) - kill $PID 2>/dev/null || true - rm /tmp/tidb-playground-${PORT}.pid - fi - - # Kill any remaining tiup playground processes - pkill -f "tiup playground" || true - - # Clean up log file - rm -f /tmp/tidb-playground-${PORT}.log - - echo "TiDB Playground stopped" -fi diff --git a/scripts/tidb-test-cluster.sh b/scripts/tidb-test-cluster.sh deleted file mode 100755 index f6b1e30b8..000000000 --- a/scripts/tidb-test-cluster.sh +++ /dev/null @@ -1,222 +0,0 @@ -#!/usr/bin/env bash - -# This file creates minimal TiDB cluster for tests - -REALPATH=$(which realpath) -if [ -z $REALPATH ]; then - realpath() { - [[ $1 == /* ]] && echo "$1" || echo "$PWD/${1#./}" - } -fi - -# Set up constants -SCRIPT_PATH=$(realpath $(dirname "$0")) -DOCKER=$(which docker) -SCRIPT_INIT=false -DOCKER_NETWORK="mysql_provider_test_network" -RUNNING_CONTAINERS="" -export MYSQL_PORT=${MYSQL_PORT:-4000} -export TAG_VERSION="v${MYSQL_VERSION:-7.5.1}" - -# Sanity checks -if [ -z "$DOCKER" ]; then - echo "Missing docker binary" - exit 2 -fi - -# A better class of script... -set -o errexit # Exit on most errors (see the manual) -set -o errtrace # Make sure any error trap is inherited -set -o nounset # Disallow expansion of unset variables -set -o pipefail # Use last non-zero exit code in a pipeline - -function script_usage() { - cat < ^ TiDB Listen port (default: 4000) - --version ^ TiDB version (default: 7.5.1) - -h|--help ^ Displays this help -EOF -} - -function parse_params() { - local param - while [[ $# -gt 0 ]]; do - param="$1" - case $param in - --port) - shift - if [ -z "$1" ]; then - echo "Missing port" - exit 1 - fi - export MYSQL_PORT=${1:-4000} - shift - ;; - --version) - shift - if [ -z "$1" ]; then - echo "Missing version" - exit 1 - fi - export TAG_VERSION="v${1:-6.1.0}" - shift - ;; - - --init) - export SCRIPT_INIT=true - shift - ;; - --destroy) - if [ -z "$SCRIPT_INIT" ]; then - echo "Can't destroy and init at once" - exit 1 - fi - destroy_cluster - exit 0 - ;; - -h | --help) - script_usage - shift - exit 0 - ;; - *) - echo "Invalid parameter was provided: $param" - script_usage - shift - exit 1 - ;; - esac - done -} - -function destroy_cluster() { - RUNNING_CONTAINERS=$(${DOCKER} ps -a -q -f name=tidb -f name=tikv -f name=pd) - echo "==> Removing any existing TiDB cluster components" - if [ ! -z "$RUNNING_CONTAINERS" ]; then - ${DOCKER} rm -f $RUNNING_CONTAINERS >/dev/null 2>&1 - fi - ${DOCKER} network rm ${DOCKER_NETWORK} >/dev/null 2>&1 || true -} - -function show_docker_logs_and_exit() { - CONTAINERS=$(docker ps -a -q -f name=$1) - if [ ! -z "$CONTAINERS" ]; then - echo "=== Container logs for $1 ===" - echo "$CONTAINERS" | xargs docker logs --details 2>&1 || true - else - echo "No container found for $1 - container may have failed to start" - echo "Checking if Docker image exists..." - docker images pingcap/$1:$TAG_VERSION || echo "Image pingcap/$1:$TAG_VERSION not found locally" - fi - echo "" - echo "Error with $1 component. For debugging use:" - echo "docker ps -a -q -f name=$1 |xargs docker logs" - exit 1 -} - -function run_pd() { - echo "==> Pulling up PD component" - if ! ${DOCKER} run -d --name pd \ - -v /etc/localtime:/etc/localtime:ro \ - -h pd \ - --network "$DOCKER_NETWORK" \ - pingcap/pd:$TAG_VERSION \ - --name="pd" \ - --data-dir="/data" \ - --client-urls="http://0.0.0.0:2379" \ - --advertise-client-urls="http://pd:2379" \ - --peer-urls="http://0.0.0.0:2380" \ - --advertise-peer-urls="http://pd:2380" \ - --initial-cluster="pd=http://pd:2380" 2>&1; then - echo "ERROR: Failed to start PD container" - show_docker_logs_and_exit pd - fi -} - -function run_tikv() { - echo "==> Pulling up TiKV component" - if ! ${DOCKER} run -d --name tikv \ - -v /etc/localtime:/etc/localtime:ro \ - -h tikv \ - --network "$DOCKER_NETWORK" \ - pingcap/tikv:$TAG_VERSION \ - --addr="0.0.0.0:20160" \ - --advertise-addr="tikv:20160" \ - --status-addr="0.0.0.0:20180" \ - --data-dir="/data" \ - --pd="pd:2379" 2>&1; then - echo "ERROR: Failed to start TiKV container" - show_docker_logs_and_exit tikv - fi -} - -function run_tidb() { - local _mysql_port=$1 - echo "==> Pulling up TiDB component" - if ! ${DOCKER} run -d --name tidb \ - -p $_mysql_port:$_mysql_port \ - -v /etc/localtime:/etc/localtime:ro \ - -h tidb \ - --network "$DOCKER_NETWORK" \ - pingcap/tidb:$TAG_VERSION \ - --store=tikv \ - -P $_mysql_port \ - --path="pd:2379" 2>&1; then - echo "ERROR: Failed to start TiDB container" - show_docker_logs_and_exit tidb - fi -} - -function wait_for_tidb() { - local _mysql_port=$1 - local _version=$2 - echo "==> Waiting for TiDB to be ready..." - - # Determine timeout based on TiDB version - # Versions 6.1.x and 6.5.x need longer startup time - if [[ "${_version}" == 6.1.* ]] || [[ "${_version}" == 6.5.* ]]; then - TIMEOUT=240 # 4 minutes for older versions - echo "Using extended timeout (240s) for TiDB ${_version}" - else - TIMEOUT=120 # 2 minutes for newer versions - fi - - # Wait for TiDB to be ready - for i in $(seq 1 ${TIMEOUT}); do - if mysql -h 127.0.0.1 -P ${_mysql_port} -u root -e 'SELECT 1' >/dev/null 2>&1; then - echo "TiDB is ready!" - return 0 - fi - sleep 1 - if [ $((i % 10)) -eq 0 ]; then - printf "." - fi - done - - echo "" - echo "ERROR: TiDB failed to start within ${TIMEOUT} seconds" - echo "Checking container logs..." - docker logs tidb 2>&1 | tail -20 || true - return 1 -} - -function main() { - parse_params "$@" - if [[ "$SCRIPT_INIT" = "true" ]]; then - echo "==> Pulling up TiDB cluster with TiKV and TB components" - destroy_cluster && \ - ${DOCKER} network create ${DOCKER_NETWORK} && \ - run_pd && \ - run_tikv && \ - run_tidb $MYSQL_PORT && \ - wait_for_tidb $MYSQL_PORT $MYSQL_VERSION - - else - script_usage - fi -} - -main "$@"