From ad628fc8a3aa63c83d605830761692ac4ca2e01b Mon Sep 17 00:00:00 2001 From: gitsang Date: Wed, 8 Dec 2021 03:26:12 +0000 Subject: [PATCH] chore: add dockerfile --- .gitignore | 1 + Dockerfile | 13 +++++++++++++ Makefile | 40 ++++++++++++++++++++++++---------------- cmd/ddns.go | 3 +++ pkg/config/config.go | 2 ++ 5 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 Dockerfile diff --git a/.gitignore b/.gitignore index 1b20dd2..518e665 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ # make target /bin /target +/build # Test binary, built with `go test -c` *.test diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..27e7dfa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM ubuntu + +ARG DOCKER_PACKAGE_PATH +ENV DOCKER_PACKAGE_PATH ${DOCKER_PACKAGE_PATH} + +ENV SERVICE_NAME=ddns +ENV SERVICE_HOME=/opt/${SERVICE_NAME} +ENV PATH=$PATH:${SERVICE_HOME}/bin + +COPY ${DOCKER_PACKAGE_PATH} ${SERVICE_HOME} +WORKDIR ${SERVICE_HOME}/bin + +ENTRYPOINT [ "ddns" ] diff --git a/Makefile b/Makefile index be32abc..99eabb6 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,34 @@ +.PHONY: default help build docker -default: build +SERVICE_NAME=ddns +TARGET_PATH=build +DOCKER_REPO=hub.cn.sang.ink +VERSION=$(shell git describe --tags) -build: +default: help - go build -o bin/ddns cmd/ddns.go +help: ## show help -pack: build + @echo -e "Usage: \n\tmake \033[36m[option]\033[0m" + @echo -e "Options:" + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\t\033[36m%-20s\033[0m %s\n", $$1, $$2}' - mkdir -p target/bin target/conf target/log - cp bin/* target/bin/ - cp configs/* target/conf/ -install: +LD_FLAGS=-ldflags "-X ddns/pkg/config.Version=$(VERSION)" - cp bin/ddns /usr/local/bin/ddns - mkdir -p /usr/local/etc/ddns - cp configs/private.yml /usr/local/etc/ddns/ddns.conf - touch /var/log/ddns.log - cp configs/ddns.service /etc/systemd/system/ddns.service - systemctl status ddns +build: ## build target -uninstall: + mkdir -p $(TARGET_PATH) $(TARGET_PATH)/bin $(TARGET_PATH)/conf $(TARGET_PATH)/log + go build $(LD_FLAGS) -o $(TARGET_PATH)/bin/$(SERVICE_NAME) cmd/$(SERVICE_NAME).go + cp configs/template.yml $(TARGET_PATH)/conf - rm -fr /usr/local/bin/ddns /usr/local/etc/ddns /etc/systemd/system/ddns.service + +docker: build ## build docker and push + + docker build -f Dockerfile --no-cache \ + --build-arg DOCKER_PACKAGE_PATH=$(TARGET_PATH) \ + -t $(DOCKER_REPO)/$(SERVICE_NAME):$(VERSION) . + docker tag $(DOCKER_REPO)/$(SERVICE_NAME):$(VERSION) $(DOCKER_REPO)/$(SERVICE_NAME):latest + docker push $(DOCKER_REPO)/$(SERVICE_NAME):$(VERSION) + docker push $(DOCKER_REPO)/$(SERVICE_NAME):latest diff --git a/cmd/ddns.go b/cmd/ddns.go index 0dc2110..ffe2f48 100644 --- a/cmd/ddns.go +++ b/cmd/ddns.go @@ -6,6 +6,7 @@ import ( "flag" log "github.com/gitsang/golog" + "go.uber.org/zap" ) var ( @@ -45,6 +46,8 @@ func main() { log.WithLogFile(logFile), log.WithLogFileCompress(true)) + log.Info("start ddns", zap.String("version", config.Version), + zap.Reflect("config", config.Cfg)) err = service.DdnsStart() if err != nil { panic(err) diff --git a/pkg/config/config.go b/pkg/config/config.go index d0fce8b..1cf9c8e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -8,6 +8,8 @@ import ( "go.uber.org/zap" ) +var Version = "unknown" + type DDNS struct { RRs []string Record struct {