GO := CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go

.PHONY: build run fmt clean

default: build buildSuccess

build:
	$(GO) build -trimpath -o bin/dm_test_infra main.go

buildSuccess:
	@echo Build DM testcase successfully!

clean:
	go clean -i ./...
	rm -rf .env
	rm -rf output
	rm -rf kubeconfig.yml
	rm -rf bin

create-testbed: install-tcctl
	tcctl testbed create -f testbed/basic.yaml

create-testbed-mariadb: install-tcctl
	tcctl testbed create -f testbed/mariadb.yaml

create-testbed-sharding: install-tcctl
	tcctl testbed create -f testbed/sharding.yaml

create-testbed-openapi: install-tcctl
	tcctl testbed create -f testbed/openapi.yaml

create-testbed-tiup: install-tcctl
	tcctl testbed create -f testbed/tiup.yaml

destroy-testbed:
	tcctl testbed delete

debug-run:
	KUBECONFIG=kubeconfig.yml MYSQL_PROXY=socks5h://rms-staging.pingcap.net:30081 go run main.go --workload testcases/$(CASE)/workload.yaml --path testcases/$(CASE)/path.yaml

case-image:
	GOOS=linux GOARCH=amd64 make build
	docker build --platform=linux/amd64 -t hub.pingcap.net/dm/test-infra:$(CASE) .
	docker image push hub.pingcap.net/dm/test-infra:$(CASE)

case:
	tcctl apply -f testcases/$(CASE)/case.yaml

upstream-1-image:
	docker build -f image/mysql/Dockerfile.upstream1 -t hub.pingcap.net/dm/mysql:5.7-dm-gtid-server-id-1 .
	docker image push hub.pingcap.net/dm/mysql:5.7-dm-gtid-server-id-1

upstream-2-image:
	docker build -f image/mysql/Dockerfile.upstream2 -t hub.pingcap.net/dm/mysql:5.7-dm-gtid-server-id-2 .
	docker image push hub.pingcap.net/dm/mysql:5.7-dm-gtid-server-id-2

upstream-1-mariadb-image:
	docker build -f image/mariadb/Dockerfile.upstream1 -t hub.pingcap.net/dm/mariadb:10.1.2-dm-gtid-server-id-1 .
	docker image push hub.pingcap.net/dm/mariadb:10.1.2-dm-gtid-server-id-1

upstream-2-mariadb-image:
	docker build -f image/mariadb/Dockerfile.upstream2 -t hub.pingcap.net/dm/mariadb:10.1.2-dm-gtid-server-id-1 .
	docker image push hub.pingcap.net/dm/mariadb:10.1.2-dm-gtid-server-id-2

fmt: groupimports
	go fmt ./...

groupimports: install-goimports
	goimports -w -l -local github.com/pingcap/test-infra .

install-goimports:
ifeq (, $(shell which goimports))
	@{ \
	set -e ;\
	TMP_DIR=$$(mktemp -d) ;\
	cd $$TMP_DIR ;\
	GO111MODULE=on go get golang.org/x/tools/cmd/goimports ;\
	rm -rf $$TMP_DIR ;\
	}
endif

install-tcctl:
ifeq (, $(shell which tcctl))
	curl http://fileserver.pingcap.net/download/pingcap/qa/test-infra/tcctl-install.sh | bash
endif

kube-dm:
	KUBECONFIG=kubeconfig.yml kubectl exec -it dm-dm-master-0 -- bash

kube-dmctl:
	KUBECONFIG=kubeconfig.yml kubectl exec -it dm-dm-master-0 -- /dmctl --master-addr=127.0.0.1:8261

kube-upstream1:
	KUBECONFIG=kubeconfig.yml kubectl exec -it upstream1-0 -- bash

kube-workload:
	KUBECONFIG=kubeconfig.yml kubectl exec -it workload-0 -- bash

kube-sync-diff:
	KUBECONFIG=kubeconfig.yml kubectl exec -it syncdiff-0 -- sh
worker0-log:
	KUBECONFIG=kubeconfig.yml kubectl logs dm-dm-worker-0

