From 5d5d4c930d3254b05b37fa41ec34e91ceab74849 Mon Sep 17 00:00:00 2001 From: Michael Aldridge Date: Thu, 2 Feb 2023 13:04:07 -0600 Subject: [PATCH] main/runit: Improve logic around shoelaces mappings --- Containerfile | 4 +++- README.md | 3 +++ main.go | 23 +++++++++++++++++------ runit/netbox-dnsmasq-dhcp/run | 8 +++++--- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Containerfile b/Containerfile index 2fe15b4..796883b 100644 --- a/Containerfile +++ b/Containerfile @@ -17,6 +17,8 @@ WORKDIR / COPY --from=build /netbox-dhcp-hosts /usr/local/bin/netbox-dhcp-hosts COPY runit/dnsmasq /etc/service/dnsmasq COPY runit/netbox-dnsmasq-dhcp /etc/service/netbox-dnsmasq-dhcp +ENV DNSMASQ_HOSTSFILE=/run/dhcp-hosts.next +ENV REFRESH_INTERVAL=600 RUN apk update && \ apk add tini runit dnsmasq && \ rm -rf /var/cache/apk && \ @@ -33,7 +35,7 @@ RUN apk add git && \ FROM base as shoelaces WORKDIR / -ENV SHOELACES_MAPFILE=/var/lib/shoelaces/mappings.yaml +ENV SHOELACES_MAPFILE=/var/lib/shoelaces/mappings.yaml.next COPY --from=shoelaces_build /shoelaces/shoelaces /usr/local/bin/shoelaces COPY --from=shoelaces_build /shoelaces/web /usr/share/shoelaces/web COPY runit/shoelaces /etc/service/shoelaces diff --git a/README.md b/README.md index 9769749..4384d6f 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ in the environment: * `DNSMASQ_TEMPLATE` - A go template expression for the dhcp-hosts file. Defaults to a suitable configuration for IPv4. The default template is `{{JoinStrings .HWAddr ","}},{{.Addr}}`. + * `DNSMASQ_HOSTSFILE` - A file to write out the dhcp hosts + configuration to. This must match wherever you configure your + dnsmasq config file to search for dhcp hosts. * `SHOELACES_MAPFILE` - A file to write out shoelaces mappings too. Must be named `mappings.yaml` and at the path expected by shoelaces. Only relevant in images that contain shoelaces. diff --git a/main.go b/main.go index 68fcb11..6092283 100644 --- a/main.go +++ b/main.go @@ -132,20 +132,31 @@ func main() { } } - for _, host := range hosts { - if err := hostTmpl.Execute(os.Stdout, host); err != nil { - log.Println("Error executing template", err) + if os.Getenv("DNSMASQ_HOSTSFILE") != "" { + f, err := os.Create(os.Getenv("DNSMASQ_HOSTSFILE")) + if err != nil { + log.Println("Error writing out hosts file", err) + os.Exit(1) + } + defer f.Close() + + for _, host := range hosts { + if err := hostTmpl.Execute(f, host); err != nil { + log.Println("Error executing template", err) + } } } if os.Getenv("SHOELACES_MAPFILE") != "" { - bytes, err := json.Marshal(ShoelacesNetworkMap{NetworkMaps: shoenets}) + f, err := os.Create(os.Getenv("SHOELACES_MAPFILE")) if err != nil { - log.Println("Error marshalling shoelaces mappings", err) + log.Println("Error opening shoelaces map file", err) os.Exit(1) } + defer f.Close() - if err := os.WriteFile(os.Getenv("SHOELACES_MAPFILE"), bytes, 0644); err != nil { + enc := json.NewEncoder(f) + if err := enc.Encode(ShoelacesNetworkMap{NetworkMaps: shoenets}); err != nil { log.Println("Error writing shoelaces mappings", err) os.Exit(1) } diff --git a/runit/netbox-dnsmasq-dhcp/run b/runit/netbox-dnsmasq-dhcp/run index 8a6b023..79c0a6e 100755 --- a/runit/netbox-dnsmasq-dhcp/run +++ b/runit/netbox-dnsmasq-dhcp/run @@ -1,17 +1,19 @@ #!/bin/sh while true ; do - if /usr/local/bin/netbox-dhcp-hosts | sort > /run/dhcp-hosts.next ; then + if /usr/local/bin/netbox-dhcp-hosts ; then if ! diff /run/dhcp-hosts /run/dhcp-hosts.next ; then echo "Updated host mappings, reloading services" mv /run/dhcp-hosts.next /run/dhcp-hosts - pkill -SIGHUP dnsmasq + fi + if ! diff /var/lib/shoelaces/mappings.yaml /var/lib/shoelaces/mappings.yaml.next ; then if [ -n "$SHOELACES_TAG_PREFIX" ] ; then + mv /var/lib/shoelaces/mappings.yaml.next /var/lib/shoelaces/mappings.yaml sv restart shoelaces fi fi fi - sleep 600 + sleep "$REFRESH_INTERVAL" done