Skip to content

Commit

Permalink
main/runit: Improve logic around shoelaces mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
the-maldridge committed Feb 2, 2023
1 parent dac1e14 commit 5d5d4c9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 && \
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 17 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 5 additions & 3 deletions runit/netbox-dnsmasq-dhcp/run
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5d5d4c9

Please sign in to comment.