1
0
Fork 0
mirror of https://github.com/Luzifer/dns.git synced 2024-11-09 22:50:06 +00:00

Rebuild to use crond and pre-compiled binary

The build process of the binary patched with the cron runner was too
instable as coredns is not properly vendoring their dependencies. This
way the binary is stable (provided by coredns) and the cron is taken
over by alpine crond.

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-03-04 13:41:25 +01:00
parent caea3c7f13
commit ee90549179
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
5 changed files with 48 additions and 83 deletions

View file

@ -1,27 +1,14 @@
FROM golang:alpine
ARG COREDNS_VERSION=v1.0.5
ADD ./build.sh /usr/local/bin/build.sh
ADD ./cron_generate.go /src/cron_generate.go
RUN set -ex \
&& apk --no-cache add git bash \
&& bash /usr/local/bin/build.sh
FROM alpine
LABEL maintainer Knut Ahlers <knut@ahlers.me>
COPY --from=0 /go/bin/coredns /usr/local/bin/
ADD ./requirements.txt /src/requirements.txt
RUN set -ex \
&& apk --no-cache add python3 bind-tools \
&& pip3 install -r /src/requirements.txt
ADD . /src
WORKDIR /src
RUN set -ex \
&& apk --no-cache add bash \
&& /src/build.sh
EXPOSE 53/udp 53
VOLUME ["/src/zones"]
@ -29,5 +16,5 @@ VOLUME ["/src/zones"]
HEALTHCHECK --interval=30s --timeout=5s \
CMD dig +short @localhost health.server.test TXT || exit 1
ENTRYPOINT ["/usr/local/bin/coredns"]
CMD ["--"]
ENTRYPOINT ["/src/docker-entrypoint.sh"]
CMD ["coredns"]

45
build.sh Normal file → Executable file
View file

@ -1,28 +1,33 @@
#!/bin/bash
set -euxo pipefail
# Download sourcecode
mkdir -p /go/src/github.com/coredns
git clone https://github.com/coredns/coredns.git /go/src/github.com/coredns/coredns
# Install build utilities
apk --no-cache add curl
# Ensure version pinning
cd /go/src/github.com/coredns/coredns
git reset --hard ${COREDNS_VERSION}
# Install dependencies
apk --no-cache add python3 bind-tools
# Copy cron drop-in
cp /src/cron_generate.go .
# Get latest versions of tools using latestver
COREDNS_VERSION=$(curl -sSfL 'https://lv.luzifer.io/catalog-api/coredns/latest.txt?p=version')
DUMB_INIT_VERSION=$(curl -sSfL 'https://lv.luzifer.io/catalog-api/dumb-init/latest.txt?p=version')
# Get dependencies and build
go get -d -v
[ -z "${COREDNS_VERSION}" ] && { exit 1; }
[ -z "${DUMB_INIT_VERSION}" ] && { exit 1; }
# Force downgrades not being pinned
CWD=$(pwd)
cd ${GOPATH}/src/github.com/mholt/caddy && git checkout -q v0.10.10
cd ${GOPATH}/src/github.com/miekg/dns && git checkout -q v1.0.4
cd ${GOPATH}/src/github.com/prometheus/client_golang && git checkout -q v0.8.0
cd ${GOPATH}/src/golang.org/x/net && git checkout -q release-branch.go1.9
cd ${GOPATH}/src/golang.org/x/text && git checkout -q e19ae1496984b1c655b8044a65c0300a3c878dd3
cd "${CWD}"
# Install tools
curl -sSfL https://github.com/coredns/coredns/releases/download/v${COREDNS_VERSION}/coredns_${COREDNS_VERSION}_linux_amd64.tgz | \
tar -x -z -C /usr/local/bin
# Do the compile
go install
curl -sSfLo /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v${DUMB_INIT_VERSION}/dumb-init_${DUMB_INIT_VERSION}_amd64
chmod +x /usr/local/bin/dumb-init
# Install requirements for python3 scripts
pip3 install -r /src/requirements.txt
# Create cron to update zones periodically
echo "* * * * * run-parts /etc/periodic/1min" >> /var/spool/cron/crontabs/root
mkdir -p /etc/periodic/1min
ln -s /src/zonefile_cron /etc/periodic/1min/zonefile_cron
# Cleanup
apk --no-cache del curl

View file

@ -1,44 +0,0 @@
package main
import (
"context"
"os/exec"
"time"
"github.com/Sirupsen/logrus"
"github.com/robfig/cron"
)
func init() {
c := cron.New()
c.AddFunc("0 * * * * *", generateZonefiles)
c.Start()
go generateZonefiles()
}
func generateZonefiles() {
logger := logrus.WithFields(logrus.Fields{
"fkt": "generateZonefiles",
})
var (
iw = logger.WriterLevel(logrus.InfoLevel)
ew = logger.WriterLevel(logrus.ErrorLevel)
)
defer iw.Close()
defer ew.Close()
ctx, cancel := context.WithTimeout(context.Background(), 59*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "/usr/bin/python3", "generateZonefiles.py")
cmd.Stdout = iw
cmd.Stderr = ew
cmd.Dir = "/src"
if err := cmd.Run(); err != nil {
logger.WithError(err).Error("Command execution failed")
}
}

12
docker-entrypoint.sh Executable file
View file

@ -0,0 +1,12 @@
#!/usr/local/bin/dumb-init /bin/bash
set -euxo pipefail
if [ "${1:-}" = 'coredns' ]; then
# Start crond in the background
crond
# Start coredns
exec "$@"
fi
exec "$@"

5
zonefile_cron Executable file
View file

@ -0,0 +1,5 @@
#!/bin/bash
set -euxo pipefail
cd /src
exec /usr/bin/python3 generateZonefiles.py