Skip to content

Commit

Permalink
docker: add docker support (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Sep 1, 2022
1 parent d3ebbdf commit 8d47abe
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 4 deletions.
1 change: 1 addition & 0 deletions .dockerignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ vendor
work
.vscode/
.coverage*
.git
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 .
11 changes: 11 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions docker/apk-fastest-mirror.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
)

0 comments on commit 8d47abe

Please sign in to comment.