Skip to content

Commit

Permalink
Merge pull request #40 from netboxlabs/feat-OBS-404-reload-netbox-on-…
Browse files Browse the repository at this point in the history
…plugin-change

feat: OBS-404 - reload netbox WSGI on netbox_diode_plugin change
  • Loading branch information
mfiedorowicz authored Feb 19, 2024
2 parents 5e5bbee + 6c0b3a9 commit be2ecb8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions diode-server/docker/docker-compose.netbox.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ services:
volumes:
- ./diode/netbox_diode_plugin:/opt/netbox/netbox/netbox_diode_plugin:rw
- ./netbox/configuration:/etc/netbox/config:z,ro
- ./netbox/launch-netbox.sh:/opt/netbox/launch-netbox.sh:z,ro
- netbox-media-files:/opt/netbox/netbox/media:rw
- netbox-reports-files:/opt/netbox/netbox/reports:rw
- netbox-scripts-files:/opt/netbox/netbox/scripts:rw
Expand Down
1 change: 1 addition & 0 deletions diode-server/docker/netbox/env/netbox.env
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ RELEASE_CHECK_URL=https://api.github.com/repos/netbox-community/netbox/releases
SECRET_KEY='r(m)9nLGnz$(_q3N4z1k(EFsMCjjjzx08x9VhNVcfd%6RF#r!6DE@+V5Zk2X'
SKIP_SUPERUSER=true
WEBHOOKS_ENABLED=true
RELOAD_NETBOX_ON_DIODE_PLUGIN_CHANGE=true
75 changes: 75 additions & 0 deletions diode-server/docker/netbox/launch-netbox.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

UNIT_CONFIG="${UNIT_CONFIG-/etc/unit/nginx-unit.json}"
# Also used in "nginx-unit.json"
UNIT_SOCKET="/opt/unit/unit.sock"

load_configuration() {
MAX_WAIT=10
WAIT_COUNT=0
while [ ! -S $UNIT_SOCKET ]; do
if [ $WAIT_COUNT -ge $MAX_WAIT ]; then
echo "⚠️ No control socket found; configuration will not be loaded."
return 1
fi

WAIT_COUNT=$((WAIT_COUNT + 1))
echo "⏳ Waiting for control socket to be created... (${WAIT_COUNT}/${MAX_WAIT})"

sleep 1
done

# even when the control socket exists, it does not mean unit has finished initialisation
# this curl call will get a reply once unit is fully launched
curl --silent --output /dev/null --request GET --unix-socket $UNIT_SOCKET http://localhost/

echo "⚙️ Applying configuration from $UNIT_CONFIG"

RESP_CODE=$(
curl \
--silent \
--output /dev/null \
--write-out '%{http_code}' \
--request PUT \
--data-binary "@${UNIT_CONFIG}" \
--unix-socket $UNIT_SOCKET \
http://localhost/config
)
if [ "$RESP_CODE" != "200" ]; then
echo "⚠️ Could no load Unit configuration"
kill "$(cat /opt/unit/unit.pid)"
return 1
fi

echo "✅ Unit configuration loaded successfully"
}

reload_netbox() {
if [ "$RELOAD_NETBOX_ON_DIODE_PLUGIN_CHANGE" == "true" ]; then
netbox_diode_plugin_md5=$(find /opt/netbox/netbox/netbox_diode_plugin -type f -name "*.py" -exec md5sum {} + | md5sum | awk '{print $1}')

while true; do
new_md5=$(find /opt/netbox/netbox/netbox_diode_plugin -type f -name "*.py" -exec md5sum {} + | md5sum | awk '{print $1}')
if [ "$netbox_diode_plugin_md5" != "$new_md5" ]; then
echo "🔄 Reloading NetBox"
curl --silent --output /dev/null --unix-socket /opt/unit/unit.sock -X GET http://localhost/control/applications/netbox/restart
netbox_diode_plugin_md5=$new_md5
fi
sleep 1
done
fi
}

load_configuration &

reload_netbox &

exec unitd \
--no-daemon \
--control unix:$UNIT_SOCKET \
--pid /opt/unit/unit.pid \
--log /dev/stdout \
--statedir /opt/unit/state/ \
--tmpdir /opt/unit/tmp/ \
--user unit \
--group root

0 comments on commit be2ecb8

Please sign in to comment.