diff --git a/Dockerfiles/netbox.Dockerfile b/Dockerfiles/netbox.Dockerfile index 63e5acb01..9d63b1437 100644 --- a/Dockerfiles/netbox.Dockerfile +++ b/Dockerfiles/netbox.Dockerfile @@ -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 && \ @@ -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 && \ diff --git a/docker-compose-standalone.yml b/docker-compose-standalone.yml index df5cda14b..44dfc1494 100644 --- a/docker-compose-standalone.yml +++ b/docker-compose-standalone.yml @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index 81d8bd6d3..5d975584a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/netbox/scripts/netbox_init.py b/netbox/scripts/netbox_init.py index ad6093313..b4d18733b 100755 --- a/netbox/scripts/netbox_init.py +++ b/netbox/scripts/netbox_init.py @@ -7,6 +7,7 @@ import os import pynetbox import sys +import time from slugify import slugify @@ -70,7 +71,26 @@ def main(): add_help=False, usage='{} '.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', @@ -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", ) @@ -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()] diff --git a/netbox/supervisord.conf b/netbox/supervisord.conf index fc8b91a25..79c33a1ef 100644 --- a/netbox/supervisord.conf +++ b/netbox/supervisord.conf @@ -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 @@ -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 @@ -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