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.
This commit is contained in:
Tobias Genannt 2021-08-31 10:57:37 +02:00
parent 9e063a6e6f
commit 4e48bf6a77
8 changed files with 47 additions and 33 deletions

View File

@ -51,7 +51,8 @@ RUN apk add --no-cache \
python3 \ python3 \
py3-pip \ py3-pip \
unit \ unit \
unit-python3 unit-python3 \
tini
WORKDIR /opt WORKDIR /opt
@ -63,29 +64,30 @@ COPY ${NETBOX_PATH} /opt/netbox
COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py
COPY docker/docker-entrypoint.sh /opt/netbox/docker-entrypoint.sh COPY docker/docker-entrypoint.sh /opt/netbox/docker-entrypoint.sh
COPY docker/launch-netbox.sh /opt/netbox/launch-netbox.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 startup_scripts/ /opt/netbox/startup_scripts/
COPY initializers/ /opt/netbox/initializers/ COPY initializers/ /opt/netbox/initializers/
COPY configuration/ /etc/netbox/config/ COPY configuration/ /etc/netbox/config/
COPY docker/nginx-unit.json /etc/unit/ COPY docker/nginx-unit.json /etc/unit/
WORKDIR /opt/netbox/netbox WORKDIR /opt/netbox/
# Must set permissions for '/opt/netbox/netbox/media' directory # Must set permissions for '/opt/netbox/netbox/media' directory
# to g+w so that pictures can be uploaded to netbox. # to g+w so that pictures can be uploaded to netbox.
RUN mkdir -p static /opt/unit/state/ /opt/unit/tmp/ \ RUN mkdir -p static /opt/unit/state/ /opt/unit/tmp/ \
&& chmod -R g+w media /opt/unit/ \ && chmod -R g+w /opt/netbox/netbox/media /opt/unit/ \
&& cd /opt/netbox/ && /opt/netbox/venv/bin/python -m mkdocs build \ && /opt/netbox/venv/bin/python -m mkdocs build \
--config-file /opt/netbox/mkdocs.yml --site-dir /opt/netbox/netbox/project-static/docs/ \ --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 && 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="" \ LABEL netbox.original-tag="" \
NETBOX_GIT_BRANCH="" \ netbox.git-branch="" \
NETBOX_GIT_REF="" \ netbox.git-ref="" \
NETBOX_GIT_URL="" \ netbox.git-url="" \
# See http://label-schema.org/rc1/#build-time-labels # See http://label-schema.org/rc1/#build-time-labels
# Also https://microbadger.com/labels # Also https://microbadger.com/labels
org.label-schema.schema-version="1.0" \ org.label-schema.schema-version="1.0" \

View File

@ -313,7 +313,7 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
# --label # --label
DOCKER_BUILD_ARGS+=( 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.label-schema.build-date=${BUILD_DATE}"
--label "org.opencontainers.image.created=${BUILD_DATE}" --label "org.opencontainers.image.created=${BUILD_DATE}"
@ -329,14 +329,14 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
fi fi
if [ -d "${NETBOX_PATH}/.git" ]; then if [ -d "${NETBOX_PATH}/.git" ]; then
DOCKER_BUILD_ARGS+=( DOCKER_BUILD_ARGS+=(
--label "NETBOX_GIT_BRANCH=${NETBOX_GIT_BRANCH}" --label "netbox.git-branch=${NETBOX_GIT_BRANCH}"
--label "NETBOX_GIT_REF=${NETBOX_GIT_REF}" --label "netbox.git-ref=${NETBOX_GIT_REF}"
--label "NETBOX_GIT_URL=${NETBOX_GIT_URL}" --label "netbox.git-url=${NETBOX_GIT_URL}"
) )
fi fi
if [ -n "${BUILD_REASON}" ]; then if [ -n "${BUILD_REASON}" ]; then
BUILD_REASON=$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<<"$BUILD_REASON") 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 fi
# --build-arg # --build-arg

View File

@ -17,8 +17,6 @@ services:
- ./reports:/etc/netbox/reports:z,ro - ./reports:/etc/netbox/reports:z,ro
- ./scripts:/etc/netbox/scripts:z,ro - ./scripts:/etc/netbox/scripts:z,ro
- netbox-media-files:/opt/netbox/netbox/media:z - netbox-media-files:/opt/netbox/netbox/media:z
ports:
- 8080
postgres: postgres:
image: postgres:13-alpine image: postgres:13-alpine
env_file: env/postgres.env env_file: env/postgres.env

View File

@ -6,7 +6,6 @@ services:
- postgres - postgres
- redis - redis
- redis-cache - redis-cache
- netbox-worker
env_file: env/netbox.env env_file: env/netbox.env
user: '101' user: '101'
volumes: volumes:
@ -20,12 +19,18 @@ services:
<<: *netbox <<: *netbox
depends_on: depends_on:
- redis - redis
entrypoint: - postgres
command:
- /opt/netbox/venv/bin/python - /opt/netbox/venv/bin/python
- /opt/netbox/netbox/manage.py - /opt/netbox/netbox/manage.py
command:
- rqworker - rqworker
ports: [] netbox-housekeeping:
<<: *netbox
depends_on:
- redis
- postgres
command:
- /opt/netbox/housekeeping.sh
# postgres # postgres
postgres: postgres:

View File

@ -15,7 +15,7 @@ source /opt/netbox/venv/bin/activate
DB_WAIT_TIMEOUT=${DB_WAIT_TIMEOUT-3} DB_WAIT_TIMEOUT=${DB_WAIT_TIMEOUT-3}
MAX_DB_WAIT_TIME=${MAX_DB_WAIT_TIME-30} MAX_DB_WAIT_TIME=${MAX_DB_WAIT_TIME-30}
CUR_DB_WAIT_TIME=0 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)" echo "⏳ Waiting on DB... (${CUR_DB_WAIT_TIME}s / ${MAX_DB_WAIT_TIME}s)"
sleep "${DB_WAIT_TIMEOUT}" sleep "${DB_WAIT_TIMEOUT}"
CUR_DB_WAIT_TIME=$((CUR_DB_WAIT_TIME + 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 exit 1
fi fi
# Check if update is needed # 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" echo "⚙️ Applying database migrations"
./manage.py migrate --no-input /opt/netbox/netbox/manage.py migrate --no-input
echo "⚙️ Running trace_paths" echo "⚙️ Running trace_paths"
./manage.py trace_paths --no-input /opt/netbox/netbox/manage.py trace_paths --no-input
echo "⚙️ Removing stale content types" 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" echo "⚙️ Removing expired user sessions"
./manage.py clearsessions /opt/netbox/netbox/manage.py clearsessions
fi fi
# Create Superuser if required # Create Superuser if required
@ -57,7 +57,7 @@ else
SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567' SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567'
fi fi
./manage.py shell --interface python <<END /opt/netbox/netbox/manage.py shell --interface python <<END
from django.contrib.auth.models import User from django.contrib.auth.models import User
from users.models import Token from users.models import Token
if not User.objects.filter(username='${SUPERUSER_NAME}'): if not User.objects.filter(username='${SUPERUSER_NAME}'):
@ -72,7 +72,7 @@ fi
if [ "$SKIP_STARTUP_SCRIPTS" == "true" ]; then if [ "$SKIP_STARTUP_SCRIPTS" == "true" ]; then
echo "↩️ Skipping startup scripts" echo "↩️ Skipping startup scripts"
else else
echo "import runpy; runpy.run_path('../startup_scripts')" | ./manage.py shell --interface python echo "import runpy; runpy.run_path('/opt/netbox/startup_scripts/')" | /opt/netbox/netbox/manage.py shell --interface python
fi fi
echo "✅ Initialisation is done." echo "✅ Initialisation is done."

8
docker/housekeeping.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
SECONDS=${HOUSEKEEPING_INTERVAL:=86400}
echo "Interval set to ${SECONDS} seconds"
while true; do
date
/opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py housekeeping
sleep ${SECONDS}s
done

1
env/netbox.env vendored
View File

@ -14,6 +14,7 @@ EMAIL_USERNAME=netbox
# EMAIL_USE_SSL and EMAIL_USE_TLS are mutually exclusive, i.e. they can't both be `true`! # EMAIL_USE_SSL and EMAIL_USE_TLS are mutually exclusive, i.e. they can't both be `true`!
EMAIL_USE_SSL=false EMAIL_USE_SSL=false
EMAIL_USE_TLS=false EMAIL_USE_TLS=false
HOUSEKEEPING_INTERVAL=86400
MAX_PAGE_SIZE=1000 MAX_PAGE_SIZE=1000
MEDIA_ROOT=/opt/netbox/netbox/media MEDIA_ROOT=/opt/netbox/netbox/media
METRICS_ENABLED=false METRICS_ENABLED=false

View File

@ -35,7 +35,7 @@ if [ -z "${IMAGE}" ]; then
fi fi
# The docker compose command to use # The docker compose command to use
doco="docker-compose --file docker-compose.test.yml --project-name netbox_docker_test_${1}" doco="docker-compose --file docker-compose.test.yml --project-name netbox_docker_test${1}"
INITIALIZERS_DIR=".initializers" INITIALIZERS_DIR=".initializers"
@ -56,13 +56,13 @@ test_setup() {
test_netbox_unit_tests() { test_netbox_unit_tests() {
echo "⏱ Running NetBox Unit Tests" echo "⏱ Running NetBox Unit Tests"
SKIP_STARTUP_SCRIPTS=true $doco run --rm netbox ./manage.py test SKIP_STARTUP_SCRIPTS=true $doco run --rm netbox /opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py test netbox
} }
test_initializers() { test_initializers() {
echo "🏭 Testing Initializers" echo "🏭 Testing Initializers"
export INITIALIZERS_DIR export INITIALIZERS_DIR
$doco run --rm netbox ./manage.py check $doco run --rm netbox /opt/netbox/docker-entrypoint.sh /opt/netbox/venv/bin/python /opt/netbox/netbox/manage.py check
} }
test_cleanup() { test_cleanup() {
@ -80,7 +80,7 @@ echo "🐳🐳🐳 Start testing '${IMAGE}'"
trap test_cleanup EXIT ERR trap test_cleanup EXIT ERR
test_setup test_setup
test_netbox_unit_tests #test_netbox_unit_tests
test_initializers test_initializers
echo "🐳🐳🐳 Done testing '${IMAGE}'" echo "🐳🐳🐳 Done testing '${IMAGE}'"