From 4e48bf6a77b937408a05788d23f753233f3f2dd9 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Tue, 31 Aug 2021 10:57:37 +0200 Subject: [PATCH] Added container for Netbox housekeeping command Adds an additional container in which the new "housekeeping" command from Netbox v3.0.0 is run. "tini" is now used as PID 1 to ensure correct process management in all Netbox containers. --- Dockerfile | 22 ++++++++++++---------- build.sh | 10 +++++----- docker-compose.test.yml | 2 -- docker-compose.yml | 13 +++++++++---- docker/docker-entrypoint.sh | 16 ++++++++-------- docker/housekeeping.sh | 8 ++++++++ env/netbox.env | 1 + test.sh | 8 ++++---- 8 files changed, 47 insertions(+), 33 deletions(-) create mode 100755 docker/housekeeping.sh diff --git a/Dockerfile b/Dockerfile index c636a04..2c74741 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,7 +51,8 @@ RUN apk add --no-cache \ python3 \ py3-pip \ unit \ - unit-python3 + unit-python3 \ + tini WORKDIR /opt @@ -63,29 +64,30 @@ COPY ${NETBOX_PATH} /opt/netbox COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py COPY docker/docker-entrypoint.sh /opt/netbox/docker-entrypoint.sh COPY docker/launch-netbox.sh /opt/netbox/launch-netbox.sh +COPY docker/housekeeping.sh /opt/netbox/housekeeping.sh COPY startup_scripts/ /opt/netbox/startup_scripts/ COPY initializers/ /opt/netbox/initializers/ COPY configuration/ /etc/netbox/config/ COPY docker/nginx-unit.json /etc/unit/ -WORKDIR /opt/netbox/netbox +WORKDIR /opt/netbox/ # Must set permissions for '/opt/netbox/netbox/media' directory # to g+w so that pictures can be uploaded to netbox. RUN mkdir -p static /opt/unit/state/ /opt/unit/tmp/ \ - && chmod -R g+w media /opt/unit/ \ - && cd /opt/netbox/ && /opt/netbox/venv/bin/python -m mkdocs build \ + && chmod -R g+w /opt/netbox/netbox/media /opt/unit/ \ + && /opt/netbox/venv/bin/python -m mkdocs build \ --config-file /opt/netbox/mkdocs.yml --site-dir /opt/netbox/netbox/project-static/docs/ \ && SECRET_KEY="dummy" /opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py collectstatic --no-input -ENTRYPOINT [ "/opt/netbox/docker-entrypoint.sh" ] +ENTRYPOINT [ "/sbin/tini", "--" ] -CMD [ "/opt/netbox/launch-netbox.sh" ] +CMD [ "/opt/netbox/docker-entrypoint.sh", "/opt/netbox/launch-netbox.sh" ] -LABEL ORIGINAL_TAG="" \ - NETBOX_GIT_BRANCH="" \ - NETBOX_GIT_REF="" \ - NETBOX_GIT_URL="" \ +LABEL netbox.original-tag="" \ + netbox.git-branch="" \ + netbox.git-ref="" \ + netbox.git-url="" \ # See http://label-schema.org/rc1/#build-time-labels # Also https://microbadger.com/labels org.label-schema.schema-version="1.0" \ diff --git a/build.sh b/build.sh index e980411..090bc79 100755 --- a/build.sh +++ b/build.sh @@ -313,7 +313,7 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do # --label DOCKER_BUILD_ARGS+=( - --label "ORIGINAL_TAG=${TARGET_DOCKER_TAG}" + --label "netbox.original-tag=${TARGET_DOCKER_TAG}" --label "org.label-schema.build-date=${BUILD_DATE}" --label "org.opencontainers.image.created=${BUILD_DATE}" @@ -329,14 +329,14 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do fi if [ -d "${NETBOX_PATH}/.git" ]; then DOCKER_BUILD_ARGS+=( - --label "NETBOX_GIT_BRANCH=${NETBOX_GIT_BRANCH}" - --label "NETBOX_GIT_REF=${NETBOX_GIT_REF}" - --label "NETBOX_GIT_URL=${NETBOX_GIT_URL}" + --label "netbox.git-branch=${NETBOX_GIT_BRANCH}" + --label "netbox.git-ref=${NETBOX_GIT_REF}" + --label "netbox.git-url=${NETBOX_GIT_URL}" ) fi if [ -n "${BUILD_REASON}" ]; then BUILD_REASON=$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<<"$BUILD_REASON") - DOCKER_BUILD_ARGS+=(--label "BUILD_REASON=${BUILD_REASON}") + DOCKER_BUILD_ARGS+=(--label "netbox.build-reason=${BUILD_REASON}") fi # --build-arg diff --git a/docker-compose.test.yml b/docker-compose.test.yml index df681d2..1ca0903 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -17,8 +17,6 @@ services: - ./reports:/etc/netbox/reports:z,ro - ./scripts:/etc/netbox/scripts:z,ro - netbox-media-files:/opt/netbox/netbox/media:z - ports: - - 8080 postgres: image: postgres:13-alpine env_file: env/postgres.env diff --git a/docker-compose.yml b/docker-compose.yml index e38aa0c..49c1e3d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,6 @@ services: - postgres - redis - redis-cache - - netbox-worker env_file: env/netbox.env user: '101' volumes: @@ -20,12 +19,18 @@ services: <<: *netbox depends_on: - redis - entrypoint: + - postgres + command: - /opt/netbox/venv/bin/python - /opt/netbox/netbox/manage.py - command: - rqworker - ports: [] + netbox-housekeeping: + <<: *netbox + depends_on: + - redis + - postgres + command: + - /opt/netbox/housekeeping.sh # postgres postgres: diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index 1610d8f..ecce068 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -15,7 +15,7 @@ source /opt/netbox/venv/bin/activate DB_WAIT_TIMEOUT=${DB_WAIT_TIMEOUT-3} MAX_DB_WAIT_TIME=${MAX_DB_WAIT_TIME-30} CUR_DB_WAIT_TIME=0 -while ! ./manage.py showmigrations >/dev/null 2>&1 && [ "${CUR_DB_WAIT_TIME}" -lt "${MAX_DB_WAIT_TIME}" ]; do +while ! /opt/netbox/netbox/manage.py showmigrations >/dev/null 2>&1 && [ "${CUR_DB_WAIT_TIME}" -lt "${MAX_DB_WAIT_TIME}" ]; do echo "⏳ Waiting on DB... (${CUR_DB_WAIT_TIME}s / ${MAX_DB_WAIT_TIME}s)" sleep "${DB_WAIT_TIMEOUT}" CUR_DB_WAIT_TIME=$((CUR_DB_WAIT_TIME + DB_WAIT_TIMEOUT)) @@ -25,15 +25,15 @@ if [ "${CUR_DB_WAIT_TIME}" -ge "${MAX_DB_WAIT_TIME}" ]; then exit 1 fi # Check if update is needed -if ! ./manage.py migrate --check >/dev/null 2>&1; then +if ! /opt/netbox/netbox/manage.py migrate --check >/dev/null 2>&1; then echo "⚙️ Applying database migrations" - ./manage.py migrate --no-input + /opt/netbox/netbox/manage.py migrate --no-input echo "⚙️ Running trace_paths" - ./manage.py trace_paths --no-input + /opt/netbox/netbox/manage.py trace_paths --no-input echo "⚙️ Removing stale content types" - ./manage.py remove_stale_contenttypes --no-input + /opt/netbox/netbox/manage.py remove_stale_contenttypes --no-input echo "⚙️ Removing expired user sessions" - ./manage.py clearsessions + /opt/netbox/netbox/manage.py clearsessions fi # Create Superuser if required @@ -57,7 +57,7 @@ else SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567' fi - ./manage.py shell --interface python <