Skip to content

Commit

Permalink
chore: add dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsang committed Dec 8, 2021
1 parent f5dec38 commit ad628fc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# make target
/bin
/target
/build

# Test binary, built with `go test -c`
*.test
Expand Down
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
40 changes: 24 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions cmd/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"

log "github.com/gitsang/golog"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"go.uber.org/zap"
)

var Version = "unknown"

type DDNS struct {
RRs []string
Record struct {
Expand Down

0 comments on commit ad628fc

Please sign in to comment.