From 8d47abe8864a45d685e890f74c02b86ff8afd9b8 Mon Sep 17 00:00:00 2001 From: xhe Date: Thu, 1 Sep 2022 12:05:26 +0800 Subject: [PATCH] docker: add docker support (#70) --- .dockerignore | 1 + .gitignore | 1 + Makefile | 6 +++++ docker/Dockerfile | 11 +++++++++ docker/apk-fastest-mirror.sh | 48 ++++++++++++++++++++++++++++++++++++ go.mod | 8 +++--- 6 files changed, 71 insertions(+), 4 deletions(-) create mode 120000 .dockerignore create mode 100644 docker/Dockerfile create mode 100644 docker/apk-fastest-mirror.sh diff --git a/.dockerignore b/.dockerignore new file mode 120000 index 00000000..3e4e48b0 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.gitignore \ No newline at end of file diff --git a/.gitignore b/.gitignore index daad7805..511f4f7a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ vendor work .vscode/ .coverage* +.git diff --git a/Makefile b/Makefile index a48112fb..62e93916 100644 --- a/Makefile +++ b/Makefile @@ -21,8 +21,11 @@ GOFLAGS ?= -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags '${BUILD_TAGS}' ifeq ("$(WITH_RACE)", "1") GOFLAGS = $(GOFLAGS) -race endif +IMAGE_TAG ?= latest EXECUTABLE_TARGETS := $(patsubst cmd/%,cmd_%,$(wildcard cmd/*)) +.PHONY: cmd_% build test docker + default: cmd cmd: $(EXECUTABLE_TARGETS) @@ -40,3 +43,6 @@ test: go tool cover -func=.coverage.out -o .coverage.func tail -1 .coverage.func go tool cover -html=.coverage.out -o .coverage.html + +docker: + docker build -t "tiproxy:${IMAGE_TAG}" -f docker/Dockerfile . diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 00000000..a42f7126 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,11 @@ +FROM alpine:latest + +EXPOSE 3080 +EXPOSE 3081 +EXPOSE 6000 + +ADD ./ proxy +RUN sh ./proxy/docker/apk-fastest-mirror.sh +RUN apk add git make go +RUN cd proxy && make build && cp bin/* /bin/ && cp -a conf /etc/proxy && cd .. && rm -rf proxy +RUN apk del git make go diff --git a/docker/apk-fastest-mirror.sh b/docker/apk-fastest-mirror.sh new file mode 100644 index 00000000..81414890 --- /dev/null +++ b/docker/apk-fastest-mirror.sh @@ -0,0 +1,48 @@ +#!/bin/sh +# shellcheck disable=SC3043 + +get_hostname_url() { + local n + n="${1#*://}" + echo "${n%%/*}" +} + +time_cmd() { + local proc start end + + proc=$(cut -d ' ' -f1 /proc/uptime) + start="$(echo "$proc" | cut -d . -f1)$(echo "$proc" | cut -d . -f2)" + "$@" >/dev/null 2>&1 || return + proc=$(cut -d ' ' -f1 /proc/uptime) + end="$(echo "$proc" | cut -d . -f1)$(echo "$proc" | cut -d . -f2)" + echo $((end - start)) +} + +DATA="" +MIRRORS=$(wget -qO- "http://rsync.alpinelinux.org/alpine/MIRRORS.txt") +DST=/etc/apk/mirrors.txt + +#find best +for URL in $MIRRORS; do + TIME=$(time_cmd wget -T 1 -t 1 -q "${URL%/}" -O /dev/null) + + if [ -n "$TIME" ]; then + echo "$(get_hostname_url "$URL") was $TIME" + DATA="$DATA$TIME $URL\n" + if [ "$TIME" -lt "50" ]; then + break + fi + fi + +done + +if printf '%b' "$DATA" | sort -n | tail -n +2 > $DST; then + echo file $DST created +fi + +BEST=$(head -n1 $DST | cut -d ' ' -f2) +echo "Best mirror is: $BEST" + +sed -i -r 's#^http.+/(.+/main)#'"${BEST%/}"'/\1#' /etc/apk/repositories +sed -i -r 's#^http.+/(.+/community)#'"${BEST%/}"'/\1#' /etc/apk/repositories +sed -i -r 's#^http.+/(.+/testing)#'"${BEST%/}"'/\1#' /etc/apk/repositories diff --git a/go.mod b/go.mod index 8ff1bd80..1a9c315e 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/go-mysql-org/go-mysql v1.6.0 github.com/goccy/go-yaml v1.9.5 github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c + github.com/pingcap/log v1.1.0 github.com/pingcap/tidb v1.1.0-beta.0.20220819110652-8b5b724d8a93 github.com/pingcap/tidb/parser v0.0.0-20220819110652-8b5b724d8a93 github.com/prometheus/client_golang v1.12.2 @@ -20,7 +21,10 @@ require ( go.etcd.io/etcd/server/v3 v3.5.2 go.uber.org/atomic v1.9.0 go.uber.org/zap v1.21.0 + golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e google.golang.org/grpc v1.45.0 + gopkg.in/yaml.v2 v2.4.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -69,7 +73,6 @@ require ( github.com/pelletier/go-toml/v2 v2.0.1 // indirect github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3 // indirect github.com/pingcap/kvproto v0.0.0-20220804022843-f006036b1277 // indirect - github.com/pingcap/log v1.1.0 // indirect github.com/pingcap/tipb v0.0.0-20220718022156-3e2483c20a9e // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect @@ -110,7 +113,6 @@ require ( go.opentelemetry.io/proto/otlp v0.7.0 // indirect go.uber.org/multierr v1.8.0 // indirect golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect - golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect @@ -120,7 +122,5 @@ require ( google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect sigs.k8s.io/yaml v1.2.0 // indirect )