externalize netbox download with wget

This commit is contained in:
Christian Mäder 2019-10-13 16:00:42 +02:00
parent ab4b8720d1
commit f3b9c34e3b
6 changed files with 88 additions and 67 deletions

View File

@ -2,3 +2,4 @@
.travis.yml .travis.yml
build* build*
*.env *.env
.git

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.sql.gz *.sql.gz
.netbox

View File

@ -1,5 +1,5 @@
ARG FROM=python:3.7-alpine ARG FROM=python:3.7-alpine
FROM ${FROM} as main FROM ${FROM} as builder
RUN apk add --no-cache \ RUN apk add --no-cache \
bash \ bash \
@ -8,51 +8,52 @@ RUN apk add --no-cache \
cyrus-sasl-dev \ cyrus-sasl-dev \
graphviz \ graphviz \
jpeg-dev \ jpeg-dev \
libevent-dev \
libffi-dev \ libffi-dev \
libxml2-dev \
libxslt-dev \ libxslt-dev \
openldap-dev \ openldap-dev \
postgresql-dev \ postgresql-dev
ttf-ubuntu-font-family \
wget
RUN pip install \ WORKDIR /install
RUN pip install --install-option="--prefix=/install" \
# gunicorn is used for launching netbox # gunicorn is used for launching netbox
gunicorn \ gunicorn \
greenlet \
eventlet \
# napalm is used for gathering information from network devices # napalm is used for gathering information from network devices
napalm \ napalm \
# ruamel is used in startup_scripts # ruamel is used in startup_scripts
'ruamel.yaml>=0.15,<0.16' \ 'ruamel.yaml>=0.15,<0.16' \
# pinning django to the version required by netbox # django_auth_ldap is required for ldap
# adding it here, to install the correct version of django_auth_ldap
# django-rq
'Django>=2.2,<2.3' \
# django-rq is used for webhooks
django-rq
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, ARG FROM
# and as they take some time to compile, FROM ${FROM} as main
# 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
# Cache bust when the upstream branch changes: RUN apk add --no-cache \
# ADD will fetch the file and check if it has changed bash \
# If not, Docker will use the existing build cache. ca-certificates \
# If yes, Docker will bust the cache and run every build step from here on. graphviz \
ARG REF_URL=https://api.github.com/repos/netbox-community/netbox/contents?ref=$BRANCH libevent \
ADD ${REF_URL} version.json libffi \
libjpeg-turbo \
libressl \
libxslt \
postgresql-libs \
ttf-ubuntu-font-family
WORKDIR /opt WORKDIR /opt
ARG URL=https://github.com/netbox-community/netbox/archive/$BRANCH.tar.gz COPY --from=builder /install /usr/local
RUN wget -q -O - "${URL}" | tar xz \ COPY .netbox/netbox /opt/netbox
&& mv netbox* netbox
COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py
COPY configuration/gunicorn_config.py /etc/netbox/config/ COPY configuration/gunicorn_config.py /etc/netbox/config/
@ -73,13 +74,19 @@ LABEL SRC_URL="$URL"
ARG NETBOX_DOCKER_PROJECT_VERSION=snapshot ARG NETBOX_DOCKER_PROJECT_VERSION=snapshot
LABEL NETBOX_DOCKER_PROJECT_VERSION="$NETBOX_DOCKER_PROJECT_VERSION" 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 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 docker/ldap_config.docker.py /opt/netbox/netbox/netbox/ldap_config.py
COPY configuration/ldap_config.py /etc/netbox/config/ldap_config.py COPY configuration/ldap_config.py /etc/netbox/config/ldap_config.py

View File

@ -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]. [any tag of the `netboxcommunity/netbox` Docker image on Docker Hub][netbox-dockerhub].
```bash ```bash
export VERSION=v2.2.6 export VERSION=v2.6.6
docker-compose pull netbox docker-compose pull netbox
docker-compose up -d docker-compose up -d
``` ```
You can also build a specific version of the Netbox image. This time, `VERSION` indicates any valid You can also build a specific version of the Netbox Docker image yourself.
[Git Reference][git-ref] declared on [the 'netbox-community/netbox' Github repository][netbox-github]. `VERSION` can be any valid [git ref][git-ref] in that case.
Most commonly you will specify a tag or branch name.
```bash ```bash
export VERSION=develop export VERSION=v2.6.6
docker-compose build --no-cache netbox ./build.sh $VERSION
docker-compose up -d docker-compose up -d
``` ```

View File

@ -68,6 +68,16 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
fi fi
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 # read the project version from the `VERSION` file and trim it
# see https://stackoverflow.com/a/3232433/172132 # see https://stackoverflow.com/a/3232433/172132
@ -82,6 +92,26 @@ SRC_REPO="${SRC_REPO-netbox}"
BRANCH="${1}" BRANCH="${1}"
URL="${URL-https://github.com/${SRC_ORG}/${SRC_REPO}/archive/$BRANCH.tar.gz}" 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 # Determining the value for DOCKERFILE
# and checking whether it exists # and checking whether it exists
@ -156,21 +186,12 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
--target "$DOCKER_TARGET" --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 # Composing arguments for `docker build` CLI
### ###
DOCKER_BUILD_ARGS=( DOCKER_BUILD_ARGS=(
--build-arg "NETBOX_DOCKER_PROJECT_VERSION=${NETBOX_DOCKER_PROJECT_VERSION}" --build-arg "NETBOX_DOCKER_PROJECT_VERSION=${NETBOX_DOCKER_PROJECT_VERSION}"
--build-arg "BRANCH=${BRANCH}" --build-arg "NETBOX_BRANCH=${BRANCH}"
--build-arg "URL=${URL}"
--build-arg "DOCKER_ORG=${DOCKER_ORG}" --build-arg "DOCKER_ORG=${DOCKER_ORG}"
--build-arg "DOCKER_REPO=${DOCKER_REPO}" --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}" ) DOCKER_BUILD_ARGS+=( --build-arg "no_proxy=${NO_PROXY}" )
fi 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 # Building the docker images, except if `--push-only` is passed
### ###
if [ "${2}" != "--push-only" ] ; then if [ "${2}" != "--push-only" ] ; then
echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG}' from the url '${URL}'." echo "🐳 Building the Docker image '${TARGET_DOCKER_TAG}'."
$DOCKER_CMD build -t "${TARGET_DOCKER_TAG}" "${DOCKER_BUILD_ARGS[@]}" "${DOCKER_OPTS[@]}" -f "${DOCKERFILE}" . $DRY docker build -t "${TARGET_DOCKER_TAG}" "${DOCKER_BUILD_ARGS[@]}" "${DOCKER_OPTS[@]}" -f "${DOCKERFILE}" .
echo "✅ Finished building the Docker images '${TARGET_DOCKER_TAG}'" echo "✅ Finished building the Docker images '${TARGET_DOCKER_TAG}'"
if [ -n "$DOCKER_SHORT_TAG" ]; then if [ -n "$DOCKER_SHORT_TAG" ]; then
echo "🐳 Tagging image '${DOCKER_SHORT_TAG}'." 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}'" echo "✅ Tagged image '${DOCKER_SHORT_TAG}'"
fi fi
fi fi
@ -220,13 +231,20 @@ for DOCKER_TARGET in "${DOCKER_TARGETS[@]}"; do
### ###
if [ "${2}" == "--push" ] || [ "${2}" == "--push-only" ] ; then if [ "${2}" == "--push" ] || [ "${2}" == "--push-only" ] ; then
echo "⏫ Pushing '${TARGET_DOCKER_TAG}" 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}'." echo "✅ Finished pushing the Docker image '${TARGET_DOCKER_TAG}'."
if [ -n "$DOCKER_SHORT_TAG" ]; then if [ -n "$DOCKER_SHORT_TAG" ]; then
echo "⏫ Pushing '${DOCKER_SHORT_TAG}'" 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}'." echo "✅ Finished pushing the Docker image '${DOCKER_SHORT_TAG}'."
fi fi
fi fi
done done
###
# Cleaning up
###
echo "🗑️ Cleaning up"
$DRY rm -rf .netbox
echo "✅ Cleaned up"

View File

@ -1,11 +1,6 @@
version: '3.4' version: '3.4'
services: services:
netbox: &netbox netbox: &netbox
build:
context: .
target: ${DOCKER_TARGET-main}
args:
- BRANCH=${VERSION-master}
image: netboxcommunity/netbox:${VERSION-latest} image: netboxcommunity/netbox:${VERSION-latest}
depends_on: depends_on:
- postgres - postgres