diff --git a/Dockerfile b/Dockerfile index ca6fa57..6774c23 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file +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"] \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..65af44d --- /dev/null +++ b/build.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +docker build --rm -t llehouerou/dnsupdater:latest . +docker push llehouerou/dnsupdater \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1e1833b --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..240be25 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index be3b59e..4771306 100644 --- a/main.go +++ b/main.go @@ -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")