Skip to content

Commit

Permalink
initialize netbox on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
mmguero committed Sep 20, 2022
1 parent 7c0d37a commit 41a1706
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Dockerfiles/netbox.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ ENV PGROUP "boxer"
ENV PUSER_PRIV_DROP true

ARG BASE_PATH=assets
ARG NETBOX_DEFAULT_SITE=Malcolm

ENV BASE_PATH $BASE_PATH
ENV NETBOX_DEFAULT_SITE $NETBOX_DEFAULT_SITE

RUN apt-get -q update && \
apt-get -y -q --no-install-recommends upgrade && \
Expand All @@ -41,7 +44,7 @@ RUN apt-get -q update && \
groupadd --gid ${DEFAULT_GID} ${PUSER} && \
useradd -m --uid ${DEFAULT_UID} --gid ${DEFAULT_GID} ${PUSER} && \
usermod -a -G tty ${PUSER} && \
mkdir -p /opt/unit /etc/supervisor.d && \
mkdir -p /opt/unit && \
chown -R $PUSER:$PGROUP /etc/netbox /opt/unit /opt/netbox && \
if [ -n "${BASE_PATH}" ] && [ "${BASE_PATH}" != "netbox" ]; then \
mkdir /opt/netbox/netbox/$BASE_PATH && \
Expand Down
1 change: 1 addition & 0 deletions docker-compose-standalone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ x-filebeat-variables: &filebeat-variables
x-netbox-variables: &netbox-variables
# Parameters related to NetBox (and supporting tools). Note that other more specific parameters
# can also be configured in the env_file files for netbox* services
NETBOX_DEFAULT_SITE : 'Malcolm'
NETBOX_DISABLED : &netboxdisabled 'true'
NETBOX_POSTGRES_DISABLED : *netboxdisabled
NETBOX_REDIS_DISABLED : *netboxdisabled
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ x-filebeat-variables: &filebeat-variables
x-netbox-variables: &netbox-variables
# Parameters related to NetBox (and supporting tools). Note that other more specific parameters
# can also be configured in the env_file files for netbox* services
NETBOX_DEFAULT_SITE : 'Malcolm'
NETBOX_DISABLED : &netboxdisabled 'true'
NETBOX_POSTGRES_DISABLED : *netboxdisabled
NETBOX_REDIS_DISABLED : *netboxdisabled
Expand Down
34 changes: 32 additions & 2 deletions netbox/scripts/netbox_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import pynetbox
import sys
import time

from slugify import slugify

Expand Down Expand Up @@ -70,7 +71,26 @@ def main():
add_help=False,
usage='{} <arguments>'.format(script_name),
)
parser.add_argument('--verbose', '-v', action='count', default=1, help='Increase verbosity (e.g., -v, -vv, etc.)')
parser.add_argument(
'--verbose',
'-v',
action='count',
default=1,
help='Increase verbosity (e.g., -v, -vv, etc.)',
)
parser.add_argument(
'--wait',
dest='wait',
action='store_true',
help='Wait for connection first',
)
parser.add_argument(
'--no-wait',
dest='wait',
action='store_false',
help='Do not wait for connection (error if connection fails)',
)
parser.set_defaults(wait=True)
parser.add_argument(
'-u',
'--url',
Expand All @@ -95,7 +115,7 @@ def main():
dest='netboxSites',
nargs='*',
type=str,
default=[],
default=[os.getenv('NETBOX_DEFAULT_SITE', 'default')],
required=False,
help="Site(s) to create",
)
Expand All @@ -122,6 +142,16 @@ def main():
token=args.netboxToken,
)

# wait for a good connection
while args.wait:
try:
sitesConnTest = [x.name for x in nb.dcim.sites.all()]
break
except Exception as e:
logging.info(f"{type(e).__name__}: {e}")
logging.debug("retrying in a few seconds...")
time.sleep(5)

###### GROUPS ################################################################################################
# list existing groups
groupsPreExisting = [x.name for x in nb.users.groups.all()]
Expand Down
22 changes: 18 additions & 4 deletions netbox/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface
serverurl=unix:///dev/shm/supervisor.sock

[group:netbox]
programs=main,worker,housekeeping
programs=main,worker,housekeeping,initialization

[program:main]
command=/opt/netbox/launch-netbox.sh
Expand All @@ -31,6 +31,23 @@ stdout_logfile_maxbytes=0
stdout_logfile_backups=0
redirect_stderr=true

[program:initialization]
command=/usr/bin/python3 /usr/local/bin/netbox_init.py
--wait
--url "http://localhost:8080/assets"
--token "%(ENV_SUPERUSER_API_TOKEN)s"
--site "%(ENV_NETBOX_DEFAULT_SITE)s"
autostart=true
autorestart=false
startsecs=0
startretries=0
stopasgroup=true
killasgroup=true
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
stdout_logfile_backups=0
redirect_stderr=true

[program:worker]
command=/opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py rqworker
autostart=true
Expand Down Expand Up @@ -58,6 +75,3 @@ stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
stdout_logfile_backups=0
redirect_stderr=true

[include]
files = /etc/supervisor.d/*.conf

0 comments on commit 41a1706

Please sign in to comment.