From f3b9c34e3b89e8c8b273ead44a0701af937c8fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Sun, 13 Oct 2019 16:00:42 +0200 Subject: [PATCH] externalize netbox download with wget --- .dockerignore | 1 + .gitignore | 1 + Dockerfile | 69 +++++++++++++++++++++++++--------------------- README.md | 11 ++++---- build.sh | 68 ++++++++++++++++++++++++++++----------------- docker-compose.yml | 5 ---- 6 files changed, 88 insertions(+), 67 deletions(-) diff --git a/.dockerignore b/.dockerignore index ce32f24..1b2bacc 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,3 +2,4 @@ .travis.yml build* *.env +.git diff --git a/.gitignore b/.gitignore index 53a4e81..cbaffa8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.sql.gz +.netbox diff --git a/Dockerfile b/Dockerfile index 481613e..dab0723 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ ARG FROM=python:3.7-alpine -FROM ${FROM} as main +FROM ${FROM} as builder RUN apk add --no-cache \ bash \ @@ -8,51 +8,52 @@ RUN apk add --no-cache \ cyrus-sasl-dev \ graphviz \ jpeg-dev \ + libevent-dev \ libffi-dev \ - libxml2-dev \ libxslt-dev \ openldap-dev \ - postgresql-dev \ - ttf-ubuntu-font-family \ - wget + postgresql-dev -RUN pip install \ +WORKDIR /install + +RUN pip install --install-option="--prefix=/install" \ # gunicorn is used for launching netbox gunicorn \ + greenlet \ + eventlet \ # napalm is used for gathering information from network devices napalm \ # ruamel is used in startup_scripts 'ruamel.yaml>=0.15,<0.16' \ -# pinning django to the version required by netbox -# adding it here, to install the correct version of -# django-rq - 'Django>=2.2,<2.3' \ -# django-rq is used for webhooks - django-rq +# django_auth_ldap is required for ldap + django_auth_ldap -ARG BRANCH=master +COPY .netbox/netbox/requirements.txt / +RUN pip install --install-option="--prefix=/install" -r /requirements.txt -WORKDIR /tmp +### +# Main stage +### -# As the requirements don't change very often, -# and as they take some time to compile, -# we try to cache them very agressively. -ARG REQUIREMENTS_URL=https://raw.githubusercontent.com/netbox-community/netbox/$BRANCH/requirements.txt -ADD ${REQUIREMENTS_URL} requirements.txt -RUN pip install -r requirements.txt +ARG FROM +FROM ${FROM} as main -# Cache bust when the upstream branch changes: -# ADD will fetch the file and check if it has changed -# If not, Docker will use the existing build cache. -# If yes, Docker will bust the cache and run every build step from here on. -ARG REF_URL=https://api.github.com/repos/netbox-community/netbox/contents?ref=$BRANCH -ADD ${REF_URL} version.json +RUN apk add --no-cache \ + bash \ + ca-certificates \ + graphviz \ + libevent \ + libffi \ + libjpeg-turbo \ + libressl \ + libxslt \ + postgresql-libs \ + ttf-ubuntu-font-family WORKDIR /opt -ARG URL=https://github.com/netbox-community/netbox/archive/$BRANCH.tar.gz -RUN wget -q -O - "${URL}" | tar xz \ - && mv netbox* netbox +COPY --from=builder /install /usr/local +COPY .netbox/netbox /opt/netbox COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py COPY configuration/gunicorn_config.py /etc/netbox/config/ @@ -73,13 +74,19 @@ LABEL SRC_URL="$URL" ARG NETBOX_DOCKER_PROJECT_VERSION=snapshot LABEL NETBOX_DOCKER_PROJECT_VERSION="$NETBOX_DOCKER_PROJECT_VERSION" +ARG NETBOX_BRANCH=custom_build +LABEL NETBOX_BRANCH="$NETBOX_BRANCH" + ##### -## LDAP specific tasks +## LDAP specific configuration ##### FROM main as ldap -RUN pip install django_auth_ldap +RUN apk add --no-cache \ + libsasl \ + libldap \ + util-linux COPY docker/ldap_config.docker.py /opt/netbox/netbox/netbox/ldap_config.py COPY configuration/ldap_config.py /etc/netbox/config/ldap_config.py diff --git a/README.md b/README.md index 89b39a1..6b3c8b1 100644 --- a/README.md +++ b/README.md @@ -69,18 +69,17 @@ To use this feature, set the environment-variable `VERSION` before launching `do [any tag of the `netboxcommunity/netbox` Docker image on Docker Hub][netbox-dockerhub]. ```bash -export VERSION=v2.2.6 +export VERSION=v2.6.6 docker-compose pull netbox docker-compose up -d ``` -You can also build a specific version of the Netbox image. This time, `VERSION` indicates any valid -[Git Reference][git-ref] declared on [the 'netbox-community/netbox' Github repository][netbox-github]. -Most commonly you will specify a tag or branch name. +You can also build a specific version of the Netbox Docker image yourself. +`VERSION` can be any valid [git ref][git-ref] in that case. ```bash -export VERSION=develop -docker-compose build --no-cache netbox +export VERSION=v2.6.6 +./build.sh $VERSION docker-compose up -d ``` diff --git a/build.sh b/build.sh index 6ebea6f..72428ae 100755 --- a/build.sh +++ b/build.sh @@ -68,6 +68,16 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then fi fi +### +# Determining the build command to use +### +if [ -z "$DRY_RUN" ]; then + DRY="" +else + echo "⚠️ DRY_RUN MODE ON ⚠️" + DRY="echo" +fi + ### # read the project version from the `VERSION` file and trim it # see https://stackoverflow.com/a/3232433/172132 @@ -82,6 +92,26 @@ SRC_REPO="${SRC_REPO-netbox}" BRANCH="${1}" URL="${URL-https://github.com/${SRC_ORG}/${SRC_REPO}/archive/$BRANCH.tar.gz}" +### +# fetching the source +### +if [ "${2}" != "--push-only" ] ; then + echo "🗑️ Preparing" + $DRY rm -rf .netbox + $DRY mkdir .netbox + echo "✅ Done preparing" + + echo "🌐 Downloading netbox from the url '${URL}'" + ( + $DRY cd .netbox + + $DRY wget -qO netbox.tgz "${URL}" && \ + $DRY tar -xzf netbox.tgz && \ + $DRY mv netbox-* netbox + ) + echo "✅ Downloaded netbox" +fi + ### # Determining the value for DOCKERFILE # and checking whether it exists @@ -156,21 +186,12 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do --target "$DOCKER_TARGET" ) - # caching is only ok for version tags, - # but turning the cache off is only required for the - # first build target, usually "main". - case "${TAG}" in - v*) ;; - *) [ "$DOCKER_TARGET" == "${DOCKER_TARGETS[0]}" ] && DOCKER_OPTS+=( --no-cache ) ;; - esac - ### # Composing arguments for `docker build` CLI ### DOCKER_BUILD_ARGS=( --build-arg "NETBOX_DOCKER_PROJECT_VERSION=${NETBOX_DOCKER_PROJECT_VERSION}" - --build-arg "BRANCH=${BRANCH}" - --build-arg "URL=${URL}" + --build-arg "NETBOX_BRANCH=${BRANCH}" --build-arg "DOCKER_ORG=${DOCKER_ORG}" --build-arg "DOCKER_REPO=${DOCKER_REPO}" ) @@ -190,27 +211,17 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do DOCKER_BUILD_ARGS+=( --build-arg "no_proxy=${NO_PROXY}" ) fi - ### - # Determining the build command to use - ### - if [ -z "$DRY_RUN" ]; then - DOCKER_CMD="docker" - else - echo "⚠️ DRY_RUN MODE ON ⚠️" - DOCKER_CMD="echo docker" - fi - ### # Building the docker images, except if `--push-only` is passed ### if [ "${2}" != "--push-only" ] ; then - echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG}' from the url '${URL}'." - $DOCKER_CMD build -t "${TARGET_DOCKER_TAG}" "${DOCKER_BUILD_ARGS[@]}" "${DOCKER_OPTS[@]}" -f "${DOCKERFILE}" . + echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG}'." + $DRY docker build -t "${TARGET_DOCKER_TAG}" "${DOCKER_BUILD_ARGS[@]}" "${DOCKER_OPTS[@]}" -f "${DOCKERFILE}" . echo "✅ Finished building the Docker images '${TARGET_DOCKER_TAG}'" if [ -n "$DOCKER_SHORT_TAG" ]; then echo "🐳 Tagging image '${DOCKER_SHORT_TAG}'." - $DOCKER_CMD tag "${TARGET_DOCKER_TAG}" "${DOCKER_SHORT_TAG}" + $DRY docker tag "${TARGET_DOCKER_TAG}" "${DOCKER_SHORT_TAG}" echo "✅ Tagged image '${DOCKER_SHORT_TAG}'" fi fi @@ -220,13 +231,20 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do ### if [ "${2}" == "--push" ] || [ "${2}" == "--push-only" ] ; then echo "⏫ Pushing '${TARGET_DOCKER_TAG}" - $DOCKER_CMD push "${TARGET_DOCKER_TAG}" + $DRY docker push "${TARGET_DOCKER_TAG}" echo "✅ Finished pushing the Docker image '${TARGET_DOCKER_TAG}'." if [ -n "$DOCKER_SHORT_TAG" ]; then echo "⏫ Pushing '${DOCKER_SHORT_TAG}'" - $DOCKER_CMD push "${DOCKER_SHORT_TAG}" + $DRY docker push "${DOCKER_SHORT_TAG}" echo "✅ Finished pushing the Docker image '${DOCKER_SHORT_TAG}'." fi fi done + +### +# Cleaning up +### +echo "🗑️ Cleaning up" +$DRY rm -rf .netbox +echo "✅ Cleaned up" diff --git a/docker-compose.yml b/docker-compose.yml index f57de21..f70a93f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,6 @@ version: '3.4' services: netbox: &netbox - build: - context: . - target: ${DOCKER_TARGET-main} - args: - - BRANCH=${VERSION-master} image: netboxcommunity/netbox:${VERSION-latest} depends_on: - postgres