setup go modules

build in docker
build shell script
This commit is contained in:
Laurent Le Houerou 2019-06-01 21:31:30 +04:00
parent 41dcf6226d
commit 14a45cb1de
5 changed files with 51 additions and 24 deletions

View File

@ -1,19 +1,11 @@
#
# Alpine image to get some needed data
#
FROM alpine:latest as alpine
RUN apk add --no-cache \
ca-certificates \
tzdata
FROM golang as builder
WORKDIR /tmp/dnsupdater
COPY . .
RUN go get .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o dnsupdater .
#
# Image
#
FROM scratch
# copy files from other containers
COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=alpine /usr/share/zoneinfo /usr/share/zoneinfo
COPY main /
ENTRYPOINT ["/main"]
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /tmp/dnsupdater/dnsupdater .
ENTRYPOINT ["/dnsupdater"]

4
build.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/sh
docker build --rm -t llehouerou/dnsupdater:latest .
docker push llehouerou/dnsupdater

9
go.mod Normal file
View File

@ -0,0 +1,9 @@
module gogs.lehouerou.net/Laurent/dnsupdater
go 1.12
require (
github.com/robfig/cron v1.1.0 // indirect
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/urfave/cli v1.20.0 // indirect
)

13
go.sum Normal file
View File

@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.1.0 h1:jk4/Hud3TTdcrJgUOBgsqrZBarcxl6ADIjSC2iniwLY=
github.com/robfig/cron v1.1.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

23
main.go
View File

@ -97,8 +97,8 @@ func start(c *cli.Context) error {
tryLockSem := make(chan bool, 1)
tryLockSem <- true
cron := cron.New()
err := cron.AddFunc(
cr := cron.New()
err := cr.AddFunc(
scheduleSpec,
func() {
select {
@ -112,7 +112,7 @@ func start(c *cli.Context) error {
log.Debug("Skipped another update already running.")
}
nextRuns := cron.Entries()
nextRuns := cr.Entries()
if len(nextRuns) > 0 {
log.Debug("Scheduled next run: " + nextRuns[0].Next.String())
}
@ -122,8 +122,8 @@ func start(c *cli.Context) error {
return err
}
log.Info("First run: " + cron.Entries()[0].Schedule.Next(time.Now()).String())
cron.Start()
log.Info("First run: " + cr.Entries()[0].Schedule.Next(time.Now()).String())
cr.Start()
// Graceful shut-down on SIGINT/SIGTERM
interrupt := make(chan os.Signal, 1)
@ -131,7 +131,7 @@ func start(c *cli.Context) error {
signal.Notify(interrupt, syscall.SIGTERM)
<-interrupt
cron.Stop()
cr.Stop()
log.Info("Waiting for running update to be finished...")
<-tryLockSem
os.Exit(1)
@ -167,7 +167,10 @@ func update(c *cli.Context) error {
Info("Ip has changed %s -> %s", currentIp, newIp)
for _, domain := range configuration.Domains {
SetCurrentIp(newIp, domain, configuration)
err = SetCurrentIp(newIp, domain, configuration)
if err != nil {
return err
}
}
SendStatusMail(fmt.Sprintf(`Home IP has changed : %s -> %s
@ -211,6 +214,9 @@ func GetCurrentIp(configuration Configuration) (string, error) {
url := "https://dns.api.gandi.net/api/v5/domains/lehouerou.net/records/@/A"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Set("X-Api-Key", configuration.GandiKey)
client := &http.Client{}
resp, err := client.Do(req)
@ -236,6 +242,9 @@ func SetCurrentIp(newip string, domain string, configuration Configuration) erro
fmt.Println("json:", str)
var jsonStr = []byte(str)
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
if err != nil {
return err
}
req.Header.Set("X-Api-Key", configuration.GandiKey)
req.Header.Set("Content-Type", "application/json")