From 0ea826346d139a8374eecef881da1f1c59180912 Mon Sep 17 00:00:00 2001 From: Rowan Ruseler Date: Mon, 16 Oct 2017 12:33:13 +0200 Subject: [PATCH 01/30] Project requirements --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index e1ab147..daadeab 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,13 @@ This repository houses the components needed to build NetBox as a Docker container. Images built using this code are released to [Docker Hub](https://hub.docker.com/r/ninech/netbox) every night. +### Dependencies + +This project depends on: + +* [docker/docker-ce] >= 1.13.0 +* [docker/compose] >= 1.10.0 + ## Quickstart To get NetBox up and running: @@ -74,3 +81,6 @@ $ docker-compose -f docker-compose.test.yml run --rm app This repository is currently maintained and funded by [nine](https://nine.ch). [![logo of the company 'nine'](https://logo.apps.at-nine.ch/Dmqied_eSaoBMQwk3vVgn4UIgDo=/trim/500x0/logo_claim.png)](https://www.nine.ch) + +[docker/docker-ce]: https://github.com/docker/docker-ce +[docker/compose]: https://github.com/docker/compose From 6c285297364283d4e20450a9046b8551338a0c84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Mon, 16 Oct 2017 13:09:17 +0200 Subject: [PATCH 02/30] Re-phrased the dependencies section --- README.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index daadeab..0c07e71 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,6 @@ This repository houses the components needed to build NetBox as a Docker container. Images built using this code are released to [Docker Hub](https://hub.docker.com/r/ninech/netbox) every night. -### Dependencies - -This project depends on: - -* [docker/docker-ce] >= 1.13.0 -* [docker/compose] >= 1.10.0 - ## Quickstart To get NetBox up and running: @@ -41,6 +34,15 @@ Default credentials: * Username: **admin** * Password: **admin** +## Dependencies + +This project relies only on Docker and docker-compose. But we + +* The *Docker version* must be at least `1.13.0`. +* The *docker-compose version* must be at least `1.10.0`. + +To ensure this, compare the output of `docker --version` and `docker-compose --version` with the requirements above. + ## Configuration You can configure the app using environment variables. These are defined in `netbox.env`. @@ -81,6 +83,3 @@ $ docker-compose -f docker-compose.test.yml run --rm app This repository is currently maintained and funded by [nine](https://nine.ch). [![logo of the company 'nine'](https://logo.apps.at-nine.ch/Dmqied_eSaoBMQwk3vVgn4UIgDo=/trim/500x0/logo_claim.png)](https://www.nine.ch) - -[docker/docker-ce]: https://github.com/docker/docker-ce -[docker/compose]: https://github.com/docker/compose From ec8bc17289d7da1b6934315866752288c28cab9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Mon, 16 Oct 2017 13:13:12 +0200 Subject: [PATCH 03/30] Fix typo. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c07e71..7ade7ce 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Default credentials: ## Dependencies -This project relies only on Docker and docker-compose. But we +This project relies only on *Docker* and *docker-compose* meeting this requirements: * The *Docker version* must be at least `1.13.0`. * The *docker-compose version* must be at least `1.10.0`. From 843342d972a0d638a711d1967173264df530ce3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Mon, 16 Oct 2017 13:33:12 +0200 Subject: [PATCH 04/30] Document the use of the build variable --- README.md | 27 +++++++++++++++++++++++++++ docker-compose.yml | 4 ++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7ade7ce..8cbef06 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,33 @@ To ensure this, compare the output of `docker --version` and `docker-compose --v You can configure the app using environment variables. These are defined in `netbox.env`. +## Version + +The `docker-compose.yml` file is prepared to run a specific version of Netbox. +To use this feature, set the environment-variable `VERSION` before launching `docker-compose`, as shown below. +`VERSION` may be set to the name of +[any tag of the `ninech/netbox` Docker image](https://hub.docker.com/r/ninech/netbox/tags/). + +``` +$ export VERSION=v2.2.1 +$ 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](https://git-scm.com/book/en/v2/Git-Internals-Git-References) declared on +[the Netbox Github repository](https://github.com/digitalocean/netbox/releases). +Most commonly you will specify a tag name or a branch name. + +``` +$ export VERSION=develop +$ docker-compose build --no-cache netbox +$ docker-compose up -d +``` + +Hint: If you're building a specific version by tag name, the `--no-cache` argument is not strictly necessary. +This can increase the build speed if you're just adjusting the config, for example. + ## Rebuilding & Publishing images `./build.sh` is used to rebuild the Docker image: diff --git a/docker-compose.yml b/docker-compose.yml index 117925f..d34b591 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,8 +4,8 @@ services: build: context: . args: - - BRANCH=${BRANCH-master} - image: ninech/netbox:${BRANCH-latest} + - BRANCH=${VERSION-master} + image: ninech/netbox:${VERSION-latest} depends_on: - postgres env_file: netbox.env From d3dd6a2f6997eebafd085a1375091060b8b55a46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Wed, 29 Nov 2017 10:55:43 +0100 Subject: [PATCH 05/30] =?UTF-8?q?=F0=9F=90=9E=20Adds=20most=20common=20hos?= =?UTF-8?q?tnames=20to=20ALLOWED=5FHOSTS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `nginx.netboxdocker.docker` and `netboxdocker.docker` are required if our ['reception'][1] thing is used to access the container. Fixes #21 [1]: https://github.com/ninech/reception --- netbox.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox.env b/netbox.env index 4bc812f..3e0c7e1 100644 --- a/netbox.env +++ b/netbox.env @@ -1,7 +1,7 @@ SUPERUSER_NAME=admin SUPERUSER_EMAIL=admin@example.com SUPERUSER_PASSWORD=admin -ALLOWED_HOSTS=localhost +ALLOWED_HOSTS=localhost 0.0.0.0 127.0.0.1 [::1] netboxdocker.docker nginx.netboxdocker.docker DB_NAME=netbox DB_USER=netbox DB_PASSWORD=J5brHrAXFLQSif0K From 7b69fd8a1cf405596969b1ab9a8468827b1b4953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Wed, 29 Nov 2017 15:08:55 +0100 Subject: [PATCH 06/30] =?UTF-8?q?=E2=9C=A8=20Creates=20a=20token=20for=20a?= =?UTF-8?q?dmin=20by=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/docker-entrypoint.sh | 30 ++++++++++++++++++++---------- netbox.env | 3 ++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index f830bd5..f58b9bf 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -1,30 +1,40 @@ #!/bin/bash set -e -# run db migrations (retry on error) +# wait shortly and then run db migrations (retry on error) while ! ./manage.py migrate 2>&1; do - sleep 5 + echo "⏳ Waiting on DB..." + sleep 3 done # create superuser silently -if [[ -z ${SUPERUSER_NAME} || -z ${SUPERUSER_EMAIL} || -z ${SUPERUSER_PASSWORD} ]]; then - SUPERUSER_NAME='admin' - SUPERUSER_EMAIL='admin@example.com' - SUPERUSER_PASSWORD='admin' - echo "Using defaults: Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}, Password: ${SUPERUSER_PASSWORD}" +if [[ -z ${SUPERUSER_NAME} ]]; then + SUPERUSER_NAME='admin' fi +if [[ -z ${SUPERUSER_EMAIL} ]]; then + SUPERUSER_EMAIL='admin@example.com' +fi +if [[ -z ${SUPERUSER_PASSWORD} ]]; then + SUPERUSER_PASSWORD='admin' +fi +if [[ -z ${SUPERUSER_API_TOKEN} ]]; then + SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567' +fi + +echo "💡 Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}, Password: ${SUPERUSER_PASSWORD}, Token: ${SUPERUSER_API_TOKEN}" ./manage.py shell --plain << END from django.contrib.auth.models import User +from users.models import Token if not User.objects.filter(username='${SUPERUSER_NAME}'): - User.objects.create_superuser('${SUPERUSER_NAME}', '${SUPERUSER_EMAIL}', '${SUPERUSER_PASSWORD}') + u=User.objects.create_superuser('${SUPERUSER_NAME}', '${SUPERUSER_EMAIL}', '${SUPERUSER_PASSWORD}') + Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}') END # copy static files ./manage.py collectstatic --no-input -echo "✅ Initialisation is done. Launching CMD:" -echo "exec ${@}" +echo "✅ Initialisation is done." # launch whatever is passed by docker via RUN exec ${@} diff --git a/netbox.env b/netbox.env index 3e0c7e1..9bed821 100644 --- a/netbox.env +++ b/netbox.env @@ -1,7 +1,8 @@ SUPERUSER_NAME=admin SUPERUSER_EMAIL=admin@example.com SUPERUSER_PASSWORD=admin -ALLOWED_HOSTS=localhost 0.0.0.0 127.0.0.1 [::1] netboxdocker.docker nginx.netboxdocker.docker +SUPERUSER_API_TOKEN=0123456789abcdef0123456789abcdef01234567 +ALLOWED_HOSTS=localhost 0.0.0.0 127.0.0.1 [::1] netbox nginx netboxdocker.docker nginx.netboxdocker.docker DB_NAME=netbox DB_USER=netbox DB_PASSWORD=J5brHrAXFLQSif0K From e4ddfc925ca439beaed64385cf7bc28bff6a16b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Wed, 29 Nov 2017 15:13:55 +0100 Subject: [PATCH 07/30] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Some=20README=20impr?= =?UTF-8?q?ovements=20in=20the=20process?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8cbef06..c141d6d 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ To get NetBox up and running: ``` $ git clone -b master https://github.com/ninech/netbox-docker.git $ cd netbox-docker +$ docker-compose pull $ docker-compose up -d ``` @@ -29,10 +30,14 @@ $ open "http://$(docker-compose port nginx 80)/" $ xdg-open "http://$(docker-compose port nginx 80)/" &>/dev/null & ``` +Alternatively, use something like [Reception](https://github.com/ninech/reception) to +connect to _docker-compose_ projects. + Default credentials: * Username: **admin** * Password: **admin** +* API Token: **0123456789abcdef0123456789abcdef01234567** ## Dependencies @@ -55,7 +60,7 @@ To use this feature, set the environment-variable `VERSION` before launching `do [any tag of the `ninech/netbox` Docker image](https://hub.docker.com/r/ninech/netbox/tags/). ``` -$ export VERSION=v2.2.1 +$ export VERSION=v2.2.6 $ docker-compose pull netbox $ docker-compose up -d ``` From 2aa582f3ce85cf1860b62a8908ea46a6c8e01d5f Mon Sep 17 00:00:00 2001 From: nathbooth Date: Fri, 1 Dec 2017 09:27:30 +0000 Subject: [PATCH 08/30] Added ttf-ubuntu-font-family The present build does not include a font-family causing Graphviz to render text as blank squares, including the ttf ubuntu font-family means text is rendered correctly --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 8e3a1cd..df564cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ RUN apk add --no-cache \ ca-certificates \ cyrus-sasl-dev \ graphviz \ + ttf-ubuntu-font-family \ jpeg-dev \ libffi-dev \ libxml2-dev \ From 1f114dfd3497d17027a72aa1dd303026e06bb1e9 Mon Sep 17 00:00:00 2001 From: Viktor Fogelberg Date: Fri, 1 Dec 2017 12:44:37 +0100 Subject: [PATCH 09/30] Enable uploading of images Set client_max_body_size to 10M so that we can upload images --- docker/nginx.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/docker/nginx.conf b/docker/nginx.conf index 5f3207c..e736490 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -12,6 +12,7 @@ http { keepalive_timeout 65; gzip on; server_tokens off; + client_max_body_size 10M; server { listen 80; From f96ddf1377c3fc1e635bc4fc87cd743bae8af1ae Mon Sep 17 00:00:00 2001 From: Viktor Fogelberg Date: Fri, 1 Dec 2017 12:40:55 +0100 Subject: [PATCH 10/30] Allow setting ENFORCE_GLOBAL_UNIQUE in env --- docker/configuration.docker.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docker/configuration.docker.py b/docker/configuration.docker.py index 56f9da3..7ad16f4 100644 --- a/docker/configuration.docker.py +++ b/docker/configuration.docker.py @@ -77,3 +77,9 @@ TIME_FORMAT = os.environ.get('TIME_FORMAT', 'g:i a') SHORT_TIME_FORMAT = os.environ.get('SHORT_TIME_FORMAT', 'H:i:s') DATETIME_FORMAT = os.environ.get('DATETIME_FORMAT', 'N j, Y g:i a') SHORT_DATETIME_FORMAT = os.environ.get('SHORT_DATETIME_FORMAT', 'Y-m-d H:i') + +# Enforcement of unique IP space can be toggled on a per-VRF basis. +# To enforce unique IP space within the global table (all prefixes and IP addresses not assigned to a VRF), +# set ENFORCE_GLOBAL_UNIQUE to True. +ENFORCE_GLOBAL_UNIQUE = os.environ.get('ENFORCE_GLOBAL_UNIQUE') + From 6420f0b0d5131dab08cfb0198dbb21c40a85bded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Thu, 14 Dec 2017 09:36:05 +0100 Subject: [PATCH 11/30] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Added=20an=20issue?= =?UTF-8?q?=5Ftemplate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With which I hope that people provide some basic information about their project right away instead only on request. --- .github/issue_template.md | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 .github/issue_template.md diff --git a/.github/issue_template.md b/.github/issue_template.md new file mode 100644 index 0000000..489b1ca --- /dev/null +++ b/.github/issue_template.md @@ -0,0 +1,47 @@ + + +## Current Behavior + + +... + +## Expected Behavior + + +... + +## Debug Information + + +The output of `docker-compose version`: `XXXXX` +The output of `docker version`: `XXXXX` +The output of `git rev-parse HEAD`: `XXXXX` +The command you used to start the project: `XXXXX` + +The output of `docker-compose logs netbox`: + + +``` +LOG LOG LOG +``` + + +The output of `docker-compose logs nginx`: + +``` +LOG LOG LOG +``` From bd9298e66818c742e50e5aecb1dbfed1c37e4d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Wed, 13 Dec 2017 15:50:30 +0100 Subject: [PATCH 12/30] =?UTF-8?q?=E2=9D=87=EF=B8=8F=20Make=20the=20default?= =?UTF-8?q?=20configuration=20cluster=20ready?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This changes the default configuration to be better prepared for usage with container management platforms, such as Docker Swarm, Kubernetes or OpenShift. Closes #27. --- .gitignore | 1 + README.md | 126 ++++++++++++++++++++++++++++++--- docker-compose.test.yml | 35 +++++---- docker-compose.yml | 8 ++- docker/configuration.docker.py | 93 +++++++++++++++++++++--- docker/docker-entrypoint.sh | 20 ++++-- docker/nginx.conf | 22 +++--- netbox.env | 20 +++--- 8 files changed, 257 insertions(+), 68 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53a4e81 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.sql.gz diff --git a/README.md b/README.md index c141d6d..ebdb7c5 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,16 @@ # netbox-docker -[![Build Status](https://travis-ci.org/ninech/netbox-docker.svg?branch=master)](https://travis-ci.org/ninech/netbox-docker) +[![Build Status](https://travis-ci.org/ninech/netbox-docker.svg?branch=master)][travis] -This repository houses the components needed to build NetBox as a Docker container. -Images built using this code are released to [Docker Hub](https://hub.docker.com/r/ninech/netbox) every night. +This repository houses the components needed to build Netbox as a Docker container. +Images built using this code are released to [Docker Hub][netbox-dockerhub] every night. + +[travis]: https://travis-ci.org/ninech/netbox-docker +[netbox-dockerhub]: https://hub.docker.com/r/ninech/netbox/tags/ ## Quickstart -To get NetBox up and running: +To get Netbox up and running: ``` $ git clone -b master https://github.com/ninech/netbox-docker.git @@ -30,7 +33,7 @@ $ open "http://$(docker-compose port nginx 80)/" $ xdg-open "http://$(docker-compose port nginx 80)/" &>/dev/null & ``` -Alternatively, use something like [Reception](https://github.com/ninech/reception) to +Alternatively, use something like [Reception][docker-reception] to connect to _docker-compose_ projects. Default credentials: @@ -39,6 +42,8 @@ Default credentials: * Password: **admin** * API Token: **0123456789abcdef0123456789abcdef01234567** +[docker-reception]: https://github.com/ninech/reception + ## Dependencies This project relies only on *Docker* and *docker-compose* meeting this requirements: @@ -51,13 +56,74 @@ To ensure this, compare the output of `docker --version` and `docker-compose --v ## Configuration You can configure the app using environment variables. These are defined in `netbox.env`. +Read [Environment Variables in Compose][compose-env] to understand about the various possibilities to overwrite these variables. +(The easiest solution being simply adjusting that file.) + +To find all possible variables, have a look at the [configuration.docker.py][docker-config] and [docker-entrypoint.sh][entrypoint] files. +Generally, the environment variables are called the same as their respective Netbox configuration variables. +Variables which are arrays are usually composed by putting all the values into the same environment variables with the values separated by a whitespace ("` `"). +For example defining `ALLOWED_HOSTS=localhost ::1 127.0.0.1` would allows access to Netbox through `http://localhost`, `http://[::1]` and `http://127.0.0.1`. + +[compose-env]: https://docs.docker.com/compose/environment-variables/ + +### Production + +The default settings are optimized for (local) development environments. +You should therefore adjust the configuration for production setups, at least the following variables: + +* `ALLOWED_HOSTS`: Add all URLs that lead to your netbox instance. +* `DB_*`: Use a persistent database. +* `EMAIL_*`: Use your own mailserver. +* `MAX_PAGE_SIZE`: Use the recommended default of 1000. +* `SUPERUSER_*`: Only define those variables during the initial setup, and drop them once the DB is set up. + +### Running on Docker Swarm / Kubernetes / OpenShift + +You may run this image in a cluster such as Docker Swarm, Kubernetes or OpenShift, but this is advanced level. + +In this case, we encourage you to statically configure Netbox by starting from [Netbox's example config file][default-config], and mounting it into your container using the mechanism provided by your container platform (i.e. [Docker Swarm configs][swarm-config], [Kubernetes secrets][k8s-secrets], [OpenShift configmaps][openshift-config]). + +But if you rather continue to configure your application through environment variables, you may continue to use [the built-in configuration file][docker-config]. +We discourage storing secrets in environment variables, as environment variable are passed on to all sub-processes and may leak easily into other systems, e.g. error collecting tools that often collect all environment variables whenever an error occurs. + +Therefore we *strongly advise* to make use of the secrets mechanism provided by your container platform (i.e. [Docker Swarm secrets][swarm-secrets], [Kubernetes secrets][k8s-secrets], [OpenShift secrets][openshift-secrets]). +[The configuration file][docker-config] and [the entrypoint script][entrypoint] try to load the following secrets from the respective files. +If a secret is defined by an environment variable and in the respective file at the same time, then the value from the environment variable is used. + +* `SUPERUSER_PASSWORD`: `/run/secrets/superuser_password` +* `SUPERUSER_API_TOKEN`: `/run/secrets/superuser_api_token` +* `DB_PASSWORD`: `/run/secrets/db_password` +* `SECRET_KEY`: `/run/secrets/secret_key` +* `EMAIL_PASSWORD`: `/run/secrets/email_password` +* `NAPALM_PASSWORD`: `/run/secrets/napalm_password` + +Please also consider [the advice about running Netbox in production](#production) above! + +[docker-config]: https://github.com/ninech/netbox-docker/blob/master/docker/configuration.docker.py +[default-config]: https://github.com/digitalocean/netbox/blob/develop/netbox/netbox/configuration.example.py +[entrypoint]: https://github.com/ninech/netbox-docker/blob/master/docker/docker-entrypoint.sh +[swarm-config]: https://docs.docker.com/engine/swarm/configs/ +[swarm-secrets]: https://docs.docker.com/engine/swarm/secrets/ +[openshift-config]: https://docs.openshift.org/latest/dev_guide/configmaps.html +[openshift-secrets]: https://docs.openshift.org/latest/dev_guide/secrets.html +[k8s-secrets]: https://kubernetes.io/docs/concepts/configuration/secret/ + +#### A Note On OpenShift + +OpenShift usually is configured with specific restriction regarding root users. +[Special care][openshift-root] has to be taken when building images for OpenShift. +The Docker Image that may be built using this project (and which is available on Docker Hub) might not yet run without further customization on OpenShift. +If you have this running on OpenShift, it would be nice if you could open a PR with the changes you needed to make. +Or if you didn't do any changes and it just worked, that you could confirm this so that we can remove this notice. + +[openshift-root]: https://docs.openshift.org/latest/creating_images/guidelines.html#openshift-specific-guidelines ## Version The `docker-compose.yml` file is prepared to run a specific version of Netbox. To use this feature, set the environment-variable `VERSION` before launching `docker-compose`, as shown below. `VERSION` may be set to the name of -[any tag of the `ninech/netbox` Docker image](https://hub.docker.com/r/ninech/netbox/tags/). +[any tag of the `ninech/netbox` Docker image on Docker Hub][netbox-dockerhub]. ``` $ export VERSION=v2.2.6 @@ -66,9 +132,8 @@ $ docker-compose up -d ``` You can also build a specific version of the Netbox image. This time, `VERSION` indicates any valid -[Git Reference](https://git-scm.com/book/en/v2/Git-Internals-Git-References) declared on -[the Netbox Github repository](https://github.com/digitalocean/netbox/releases). -Most commonly you will specify a tag name or a branch name. +[Git Reference][git-ref] declared on [the 'digitalocean/netbox' Github repository][netbox-github]. +Most commonly you will specify a tag or branch name. ``` $ export VERSION=develop @@ -79,6 +144,47 @@ $ docker-compose up -d Hint: If you're building a specific version by tag name, the `--no-cache` argument is not strictly necessary. This can increase the build speed if you're just adjusting the config, for example. +[git-ref]: https://git-scm.com/book/en/v2/Git-Internals-Git-References +[netbox-github]: https://github.com/digitalocean/netbox/releases + +## Troubleshooting + +This section is a collection of some common issues and how to resolve them. +If your issue is not here, look through [the existing issues][issues] and eventually create a new issue. + +[issues]: (https://github.com/ninech/netbox-docker/issues) + +### Docker Compose basics + +* You can see all running containers belonging to this project using `docker-compose ps`. +* You can see the logs by running `docker-compose logs -f`. + Running `docker-compose logs -f netbox` will just show the logs for netbox. +* You can stop everything using `docker-compose stop`. +* You can clean up everything using `docker-compose down -v --remove-orphans`. **This will also remove any related data.** +* You can enter the shell of the running Netbox container using `docker-compose exec netbox /bin/bash`. Now you have access to `./manage.py`, e.g. to reset a password. +* To access the database run `docker-compose exec postgres sh -c 'psql -U $POSTGRES_USER $POSTGRES_DB'` +* To create a database backup run `docker-compose exec postgres sh -c 'pg_dump -U $POSTGRES_USER $POSTGRES_DB' | gzip > db_dump.sql.gz` +* To restore that database backup run `gunzip -c db_dump.sql.gz | docker exec -i $(docker-compose ps -q postgres) sh -c 'psql -U $POSTGRES_USER $POSTGRES_DB'`. + +### Getting a "Bad Request (400)" + +> When connecting to the Netbox instance, I get a "Bad Request (400)" error. + +This usually happens when the `ALLOWED_HOSTS` variable is not set correctly. + +### How to upgrade + +> How do I update to a newer version? + +It should be sufficient to pull the latest image from Docker Hub, stopping the container and starting it up again: + +```bash +docker-compose pull netbox +docker-compose stop netbox +docker-compose rm -f netbox +docker-compose up -d netbox +``` + ## Rebuilding & Publishing images `./build.sh` is used to rebuild the Docker image: @@ -104,7 +210,7 @@ You can use the following ENV variables to customize the build: ## Tests -To run the bundled test, use the `docker-compose.test.yml` file. +To run the test coming with Netbox, use the `docker-compose.test.yml` file as such: ``` $ docker-compose -f docker-compose.test.yml run --rm app diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 978de00..9177dd2 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -1,23 +1,22 @@ version: '3' services: app: - build: - context: . - args: - - BRANCH=${BRANCH-master} - image: ninech/netbox:${BRANCH-latest} - depends_on: - - postgres - env_file: netbox.env - command: - - ./manage.py - - test + build: + context: . + args: + - BRANCH=${BRANCH-master} + image: ninech/netbox:${BRANCH-latest} + depends_on: + - postgres + env_file: netbox.env + command: + - ./manage.py + - test postgres: - image: postgres:9.6-alpine - env_file: postgres.env - + image: postgres:9.6-alpine + env_file: postgres.env volumes: - netbox-static-files: - driver: local - netbox-nginx-config: - driver: local + netbox-static-files: + driver: local + netbox-nginx-config: + driver: local diff --git a/docker-compose.yml b/docker-compose.yml index d34b591..f76a7b3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,13 +12,15 @@ services: volumes: - netbox-nginx-config:/etc/netbox-nginx/ - netbox-static-files:/opt/netbox/netbox/static + - netbox-media-files:/opt/netbox/netbox/media + - netbox-report-files:/opt/netbox/netbox/reports nginx: image: nginx:1.11-alpine command: nginx -g 'daemon off;' -c /etc/netbox-nginx/nginx.conf depends_on: - netbox ports: - - 80 + - 8080 volumes: - netbox-static-files:/opt/netbox/netbox/static - netbox-nginx-config:/etc/netbox-nginx/ @@ -31,3 +33,7 @@ volumes: driver: local netbox-nginx-config: driver: local + netbox-media-files: + driver: local + netbox-report-files: + driver: local diff --git a/docker/configuration.docker.py b/docker/configuration.docker.py index 7ad16f4..f973809 100644 --- a/docker/configuration.docker.py +++ b/docker/configuration.docker.py @@ -1,4 +1,21 @@ import os +import socket + +# For reference see http://netbox.readthedocs.io/en/latest/configuration/mandatory-settings/ +# Based on https://github.com/digitalocean/netbox/blob/develop/netbox/netbox/configuration.example.py + +# Read secret from file +def read_secret(secret_name): + try: + f = open('/run/secrets/' + secret_name, 'r', encoding='utf-8') + except EnvironmentError: + return '' + else: + with f: + return f.readline().strip() + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + ######################### # # # Required settings # @@ -9,13 +26,14 @@ import os # access to the server via any other hostnames. The first FQDN in the list will be treated as the preferred name. # # Example: ALLOWED_HOSTS = ['netbox.example.com', 'netbox.internal.local'] -ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(' ') +ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', socket.gethostname()).split(' ') # PostgreSQL database configuration. DATABASE = { 'NAME': os.environ.get('DB_NAME', 'netbox'), # Database name 'USER': os.environ.get('DB_USER', ''), # PostgreSQL username - 'PASSWORD': os.environ.get('DB_PASSWORD', ''), # PostgreSQL password + 'PASSWORD': os.environ.get('DB_PASSWORD', read_secret('db_password')), + # PostgreSQL password 'HOST': os.environ.get('DB_HOST', 'localhost'), # Database server 'PORT': os.environ.get('DB_PORT', ''), # Database port (leave blank for default) } @@ -24,7 +42,7 @@ DATABASE = { # For optimal security, SECRET_KEY should be at least 50 characters in length and contain a mix of letters, numbers, and # symbols. NetBox will not run without this defined. For more information, see # https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-SECRET_KEY -SECRET_KEY = os.environ.get('SECRET_KEY', '') +SECRET_KEY = os.environ.get('SECRET_KEY', read_secret('secret_key')) ######################### # # @@ -38,16 +56,51 @@ ADMINS = [ # ['John Doe', 'jdoe@example.com'], ] +# Optionally display a persistent banner at the top and/or bottom of every page. HTML is allowed. To display the same +# content in both banners, define BANNER_TOP and set BANNER_BOTTOM = BANNER_TOP. +BANNER_TOP = os.environ.get('BANNER_TOP', '') +BANNER_BOTTOM = os.environ.get('BANNER_BOTTOM', '') + +# Text to include on the login page above the login form. HTML is allowed. +BANNER_LOGIN = os.environ.get('BANNER_LOGIN', '') + +# Base URL path if accessing NetBox within a directory. For example, if installed at http://example.com/netbox/, set: +# BASE_PATH = 'netbox/' +BASE_PATH = os.environ.get('BASE_PATH', '') + +# API Cross-Origin Resource Sharing (CORS) settings. If CORS_ORIGIN_ALLOW_ALL is set to True, all origins will be +# allowed. Otherwise, define a list of allowed origins using either CORS_ORIGIN_WHITELIST or +# CORS_ORIGIN_REGEX_WHITELIST. For more information, see https://github.com/ottoyiu/django-cors-headers +CORS_ORIGIN_ALLOW_ALL = os.environ.get('CORS_ORIGIN_ALLOW_ALL', False) +CORS_ORIGIN_WHITELIST = os.environ.get('CORS_ORIGIN_WHITELIST', '').split(' ') +CORS_ORIGIN_REGEX_WHITELIST = [ + # r'^(https?://)?(\w+\.)?example\.com$', +] + +# Set to True to enable server debugging. WARNING: Debugging introduces a substantial performance penalty and may reveal +# sensitive information about your installation. Only enable debugging while performing testing. Never enable debugging +# on a production system. +DEBUG = os.environ.get('DEBUG', False) + # Email settings EMAIL = { 'SERVER': os.environ.get('EMAIL_SERVER', 'localhost'), 'PORT': os.environ.get('EMAIL_PORT', 25), 'USERNAME': os.environ.get('EMAIL_USERNAME', ''), - 'PASSWORD': os.environ.get('EMAIL_PASSWORD', ''), + 'PASSWORD': os.environ.get('EMAIL_PASSWORD', read_secret('email_password')), 'TIMEOUT': os.environ.get('EMAIL_TIMEOUT', 10), # seconds 'FROM_EMAIL': os.environ.get('EMAIL_FROM', ''), } +# Enforcement of unique IP space can be toggled on a per-VRF basis. +# To enforce unique IP space within the global table (all prefixes and IP addresses not assigned to a VRF), +# set ENFORCE_GLOBAL_UNIQUE to True. +ENFORCE_GLOBAL_UNIQUE = os.environ.get('ENFORCE_GLOBAL_UNIQUE', False) + +# Enable custom logging. Please see the Django documentation for detailed guidance on configuring custom logs: +# https://docs.djangoproject.com/en/1.11/topics/logging/ +LOGGING = {} + # Setting this to True will permit only authenticated users to access any part of NetBox. By default, anonymous users # are permitted to access most data in NetBox (excluding secrets) but not make any changes. LOGIN_REQUIRED = os.environ.get('LOGIN_REQUIRED', False) @@ -59,13 +112,37 @@ BASE_PATH = os.environ.get('BASE_PATH', '') # Setting this to True will display a "maintenance mode" banner at the top of every page. MAINTENANCE_MODE = os.environ.get('MAINTENANCE_MODE', False) +# An API consumer can request an arbitrary number of objects =by appending the "limit" parameter to the URL (e.g. +# "?limit=1000"). This setting defines the maximum limit. Setting it to 0 or None will allow an API consumer to request +# all objects by specifying "?limit=0". +MAX_PAGE_SIZE = int(os.environ.get('MAX_PAGE_SIZE', 1000)) + +# The file path where uploaded media such as image attachments are stored. A trailing slash is not needed. Note that +# the default value of this setting is derived from the installed location. +MEDIA_ROOT = os.environ.get('MEDIA_ROOT', os.path.join(BASE_DIR, 'media')) + # Credentials that NetBox will use to access live devices. NAPALM_USERNAME = os.environ.get('NAPALM_USERNAME', '') -NAPALM_PASSWORD = os.environ.get('NAPALM_PASSWORD', '') +NAPALM_PASSWORD = os.environ.get('NAPALM_PASSWORD', read_secret('napalm_password')) + +# NAPALM timeout (in seconds). (Default: 30) +NAPALM_TIMEOUT = os.environ.get('NAPALM_TIMEOUT', 30) + +# NAPALM optional arguments (see http://napalm.readthedocs.io/en/latest/support/#optional-arguments). Arguments must +# be provided as a dictionary. +NAPALM_ARGS = {} # Determine how many objects to display per page within a list. (Default: 50) PAGINATE_COUNT = os.environ.get('PAGINATE_COUNT', 50) +# When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to +# prefer IPv4 instead. +PREFER_IPV4 = os.environ.get('PREFER_IPV4', False) + +# The file path where custom reports will be stored. A trailing slash is not needed. Note that the default value of +# this setting is derived from the installed location. +REPORTS_ROOT = os.environ.get('REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')) + # Time zone (default: UTC) TIME_ZONE = os.environ.get('TIME_ZONE', 'UTC') @@ -77,9 +154,3 @@ TIME_FORMAT = os.environ.get('TIME_FORMAT', 'g:i a') SHORT_TIME_FORMAT = os.environ.get('SHORT_TIME_FORMAT', 'H:i:s') DATETIME_FORMAT = os.environ.get('DATETIME_FORMAT', 'N j, Y g:i a') SHORT_DATETIME_FORMAT = os.environ.get('SHORT_DATETIME_FORMAT', 'Y-m-d H:i') - -# Enforcement of unique IP space can be toggled on a per-VRF basis. -# To enforce unique IP space within the global table (all prefixes and IP addresses not assigned to a VRF), -# set ENFORCE_GLOBAL_UNIQUE to True. -ENFORCE_GLOBAL_UNIQUE = os.environ.get('ENFORCE_GLOBAL_UNIQUE') - diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index f58b9bf..f03b760 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -8,17 +8,25 @@ while ! ./manage.py migrate 2>&1; do done # create superuser silently -if [[ -z ${SUPERUSER_NAME} ]]; then +if [ -z ${SUPERUSER_NAME+x} ]; then SUPERUSER_NAME='admin' fi -if [[ -z ${SUPERUSER_EMAIL} ]]; then +if [ -z ${SUPERUSER_EMAIL+x} ]; then SUPERUSER_EMAIL='admin@example.com' fi -if [[ -z ${SUPERUSER_PASSWORD} ]]; then - SUPERUSER_PASSWORD='admin' +if [ -z ${SUPERUSER_PASSWORD+x} ]; then + if [ -f "/run/secrets/superuser_password" ]; then + SUPERUSER_PASSWORD="$(< /run/secrets/superuser_password)" + else + SUPERUSER_PASSWORD='admin' + fi fi -if [[ -z ${SUPERUSER_API_TOKEN} ]]; then - SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567' +if [ -z ${SUPERUSER_API_TOKEN+x} ]; then + if [ -f "/run/secrets/superuser_api_token" ]; then + SUPERUSER_API_TOKEN="$(< /run/secrets/superuser_api_token)" + else + SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567' + fi fi echo "💡 Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}, Password: ${SUPERUSER_PASSWORD}, Token: ${SUPERUSER_API_TOKEN}" diff --git a/docker/nginx.conf b/docker/nginx.conf index e736490..02efd01 100644 --- a/docker/nginx.conf +++ b/docker/nginx.conf @@ -1,25 +1,23 @@ worker_processes 1; events { - worker_connections 1024; + worker_connections 1024; } http { - include /etc/nginx/mime.types; - default_type application/octet-stream; - sendfile on; - tcp_nopush on; - keepalive_timeout 65; - gzip on; - server_tokens off; + include /etc/nginx/mime.types; + default_type application/octet-stream; + sendfile on; + tcp_nopush on; + keepalive_timeout 65; + gzip on; + server_tokens off; client_max_body_size 10M; server { - listen 80; - + listen 8080; server_name localhost; - - access_log off; + access_log off; location /static/ { alias /opt/netbox/netbox/static/; diff --git a/netbox.env b/netbox.env index 9bed821..72bdb4a 100644 --- a/netbox.env +++ b/netbox.env @@ -1,18 +1,18 @@ -SUPERUSER_NAME=admin -SUPERUSER_EMAIL=admin@example.com -SUPERUSER_PASSWORD=admin -SUPERUSER_API_TOKEN=0123456789abcdef0123456789abcdef01234567 ALLOWED_HOSTS=localhost 0.0.0.0 127.0.0.1 [::1] netbox nginx netboxdocker.docker nginx.netboxdocker.docker DB_NAME=netbox DB_USER=netbox DB_PASSWORD=J5brHrAXFLQSif0K DB_HOST=postgres -SECRET_KEY=r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj EMAIL_SERVER=localhost EMAIL_PORT=25 -EMAIL_USERNAME=foo -EMAIL_PASSWORD=bar -EMAIL_TIMEOUT=10 +EMAIL_USERNAME=netbox +EMAIL_PASSWORD= +EMAIL_TIMEOUT=5 EMAIL_FROM=netbox@bar.com -NETBOX_USERNAME=guest -NETBOX_PASSWORD=guest +NAPALM_TIMEOUT=5 +MAX_PAGE_SIZE=0 +SECRET_KEY=r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj +SUPERUSER_NAME=admin +SUPERUSER_EMAIL=admin@example.com +SUPERUSER_PASSWORD=admin +SUPERUSER_API_TOKEN=0123456789abcdef0123456789abcdef01234567 From 1f143dd6900985479f1ed3a8ac5cd2fca993dda1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Thu, 14 Dec 2017 17:58:43 +0100 Subject: [PATCH 13/30] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Kubernetes=20has=20C?= =?UTF-8?q?onfigMaps=20as=20well?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ebdb7c5..d508e49 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ You should therefore adjust the configuration for production setups, at least th You may run this image in a cluster such as Docker Swarm, Kubernetes or OpenShift, but this is advanced level. -In this case, we encourage you to statically configure Netbox by starting from [Netbox's example config file][default-config], and mounting it into your container using the mechanism provided by your container platform (i.e. [Docker Swarm configs][swarm-config], [Kubernetes secrets][k8s-secrets], [OpenShift configmaps][openshift-config]). +In this case, we encourage you to statically configure Netbox by starting from [Netbox's example config file][default-config], and mounting it into your container using the mechanism provided by your container platform (i.e. [Docker Swarm configs][swarm-config], [Kubernetes ConfigMap][k8s-config], [OpenShift ConfigMaps][openshift-config]). But if you rather continue to configure your application through environment variables, you may continue to use [the built-in configuration file][docker-config]. We discourage storing secrets in environment variables, as environment variable are passed on to all sub-processes and may leak easily into other systems, e.g. error collecting tools that often collect all environment variables whenever an error occurs. @@ -107,6 +107,7 @@ Please also consider [the advice about running Netbox in production](#production [openshift-config]: https://docs.openshift.org/latest/dev_guide/configmaps.html [openshift-secrets]: https://docs.openshift.org/latest/dev_guide/secrets.html [k8s-secrets]: https://kubernetes.io/docs/concepts/configuration/secret/ +[k8s-config]: https://kubernetes.io/docs/tasks/configure-pod-container/configmap/ #### A Note On OpenShift From 76a7b79db412b908b08939c43b635395835a0936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Thu, 14 Dec 2017 18:00:11 +0100 Subject: [PATCH 14/30] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Another=20typo=20fix?= =?UTF-8?q?ed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d508e49..f53c8a7 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ Please also consider [the advice about running Netbox in production](#production #### A Note On OpenShift -OpenShift usually is configured with specific restriction regarding root users. +OpenShift usually is configured with specific restrictions regarding root users. [Special care][openshift-root] has to be taken when building images for OpenShift. The Docker Image that may be built using this project (and which is available on Docker Hub) might not yet run without further customization on OpenShift. If you have this running on OpenShift, it would be nice if you could open a PR with the changes you needed to make. From 1bfbd4add45e80194623f6a317b0c871ebaa41dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Tue, 19 Dec 2017 10:24:35 +0100 Subject: [PATCH 15/30] =?UTF-8?q?=F0=9F=90=B3=20Build=20the=20develop-2.3?= =?UTF-8?q?=20branch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 702d2a0..43e64aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,6 +22,7 @@ after_success: - if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then ./build.sh master --push; ./build.sh develop --push; + ./build.sh develop-2.3 --push; ./build-latest.sh --push; PRERELEASE=true ./build-latest.sh --push; fi From fce8a23d74a38165e3e4e1df6c6d5d8fe1e0e319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Tue, 26 Dec 2017 23:03:02 +0100 Subject: [PATCH 16/30] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Fixes=20port=20for?= =?UTF-8?q?=20nginx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #30 --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f53c8a7..aea506f 100644 --- a/README.md +++ b/README.md @@ -20,17 +20,17 @@ $ docker-compose up -d ``` The application will be available after a few minutes. -Use `docker-compose port nginx 80` to find out where to connect to. +Use `docker-compose port nginx 8080` to find out where to connect to. ``` -$ echo "http://$(docker-compose port nginx 80)/" +$ echo "http://$(docker-compose port nginx 8080)/" http://0.0.0.0:32768/ # Open netbox in your default browser on macOS: -$ open "http://$(docker-compose port nginx 80)/" +$ open "http://$(docker-compose port nginx 8080)/" # Open netbox in your default browser on (most) linuxes: -$ xdg-open "http://$(docker-compose port nginx 80)/" &>/dev/null & +$ xdg-open "http://$(docker-compose port nginx 8080)/" &>/dev/null & ``` Alternatively, use something like [Reception][docker-reception] to From d0b252becd2c02efa19a03e4893ec73242caabbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Tue, 26 Dec 2017 23:06:19 +0100 Subject: [PATCH 17/30] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20More=20port=20fixes?= =?UTF-8?q?=20in=20the=20README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to #30 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aea506f..9b7889a 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Read [Environment Variables in Compose][compose-env] to understand about the var To find all possible variables, have a look at the [configuration.docker.py][docker-config] and [docker-entrypoint.sh][entrypoint] files. Generally, the environment variables are called the same as their respective Netbox configuration variables. Variables which are arrays are usually composed by putting all the values into the same environment variables with the values separated by a whitespace ("` `"). -For example defining `ALLOWED_HOSTS=localhost ::1 127.0.0.1` would allows access to Netbox through `http://localhost`, `http://[::1]` and `http://127.0.0.1`. +For example defining `ALLOWED_HOSTS=localhost ::1 127.0.0.1` would allows access to Netbox through `http://localhost:8080`, `http://[::1]:8080` and `http://127.0.0.1:8080`. [compose-env]: https://docs.docker.com/compose/environment-variables/ From 54835bd72bb8bec1ccd60ee04cc0d474aa026f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Tue, 26 Dec 2017 23:08:10 +0100 Subject: [PATCH 18/30] =?UTF-8?q?9=EF=B8=8F=E2=83=A3=20Updated=20sponsorin?= =?UTF-8?q?g=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b7889a..e563b8b 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,6 @@ $ docker-compose -f docker-compose.test.yml run --rm app ## About -This repository is currently maintained and funded by [nine](https://nine.ch). +This repository is currently maintained and funded by [nine](https://nine.ch), your cloud navigators. [![logo of the company 'nine'](https://logo.apps.at-nine.ch/Dmqied_eSaoBMQwk3vVgn4UIgDo=/trim/500x0/logo_claim.png)](https://www.nine.ch) From 52e653dd303a98b3fd9693863581095407ec3a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Tue, 26 Dec 2017 23:08:10 +0100 Subject: [PATCH 19/30] =?UTF-8?q?9=EF=B8=8F=E2=83=A3=20Updated=20sponsorin?= =?UTF-8?q?g=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e563b8b..20f4a87 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,6 @@ $ docker-compose -f docker-compose.test.yml run --rm app ## About -This repository is currently maintained and funded by [nine](https://nine.ch), your cloud navigators. +This repository is currently maintained and funded by [nine](https://nine.ch), your cloud navigator. [![logo of the company 'nine'](https://logo.apps.at-nine.ch/Dmqied_eSaoBMQwk3vVgn4UIgDo=/trim/500x0/logo_claim.png)](https://www.nine.ch) From e398390630dacdd869ec2308faef013fbd719720 Mon Sep 17 00:00:00 2001 From: Brady Lamprecht Date: Wed, 10 Jan 2018 12:29:41 -0700 Subject: [PATCH 20/30] Adding a named volume for 'postgresql-data'. --- docker-compose.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index f76a7b3..dfc1fca 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,6 +27,8 @@ services: postgres: image: postgres:9.6-alpine env_file: postgres.env + volumes: + - netbox-postgres-data:/var/lib/postgresql/data volumes: netbox-static-files: @@ -37,3 +39,6 @@ volumes: driver: local netbox-report-files: driver: local + netbox-postgres-data: + driver: local + From a68d09e815274ca9be9e0edea0474b6344389058 Mon Sep 17 00:00:00 2001 From: Brady Lamprecht Date: Wed, 10 Jan 2018 12:31:25 -0700 Subject: [PATCH 21/30] Adding a named volume for 'postgresql-data'. --- docker-compose.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index dfc1fca..97e803d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -41,4 +41,3 @@ volumes: driver: local netbox-postgres-data: driver: local - From ce15326e7ebd540cea8e17692cda63c874f9c0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Mon, 15 Jan 2018 11:24:48 +0100 Subject: [PATCH 22/30] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Explain=20how=20to?= =?UTF-8?q?=20do=20a=20better=20backup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #35 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 20f4a87..de93f74 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ If your issue is not here, look through [the existing issues][issues] and eventu * You can clean up everything using `docker-compose down -v --remove-orphans`. **This will also remove any related data.** * You can enter the shell of the running Netbox container using `docker-compose exec netbox /bin/bash`. Now you have access to `./manage.py`, e.g. to reset a password. * To access the database run `docker-compose exec postgres sh -c 'psql -U $POSTGRES_USER $POSTGRES_DB'` -* To create a database backup run `docker-compose exec postgres sh -c 'pg_dump -U $POSTGRES_USER $POSTGRES_DB' | gzip > db_dump.sql.gz` +* To create a database backup run `docker-compose exec postgres sh -c 'pg_dump -cU $POSTGRES_USER $POSTGRES_DB' | gzip > db_dump.sql.gz` * To restore that database backup run `gunzip -c db_dump.sql.gz | docker exec -i $(docker-compose ps -q postgres) sh -c 'psql -U $POSTGRES_USER $POSTGRES_DB'`. ### Getting a "Bad Request (400)" From 55c31ae80398b7ae58ff1454e9ea10208454da4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Mon, 15 Jan 2018 11:33:40 +0100 Subject: [PATCH 23/30] =?UTF-8?q?=F0=9F=90=9E=20Convert=20numbers=20from?= =?UTF-8?q?=20ENV=20to=20int?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #36 --- docker/configuration.docker.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docker/configuration.docker.py b/docker/configuration.docker.py index f973809..5eba733 100644 --- a/docker/configuration.docker.py +++ b/docker/configuration.docker.py @@ -85,10 +85,10 @@ DEBUG = os.environ.get('DEBUG', False) # Email settings EMAIL = { 'SERVER': os.environ.get('EMAIL_SERVER', 'localhost'), - 'PORT': os.environ.get('EMAIL_PORT', 25), + 'PORT': int(os.environ.get('EMAIL_PORT', 25)), 'USERNAME': os.environ.get('EMAIL_USERNAME', ''), 'PASSWORD': os.environ.get('EMAIL_PASSWORD', read_secret('email_password')), - 'TIMEOUT': os.environ.get('EMAIL_TIMEOUT', 10), # seconds + 'TIMEOUT': int(os.environ.get('EMAIL_TIMEOUT', 10)), # seconds 'FROM_EMAIL': os.environ.get('EMAIL_FROM', ''), } @@ -126,14 +126,14 @@ NAPALM_USERNAME = os.environ.get('NAPALM_USERNAME', '') NAPALM_PASSWORD = os.environ.get('NAPALM_PASSWORD', read_secret('napalm_password')) # NAPALM timeout (in seconds). (Default: 30) -NAPALM_TIMEOUT = os.environ.get('NAPALM_TIMEOUT', 30) +NAPALM_TIMEOUT = int(os.environ.get('NAPALM_TIMEOUT', 30)) # NAPALM optional arguments (see http://napalm.readthedocs.io/en/latest/support/#optional-arguments). Arguments must # be provided as a dictionary. NAPALM_ARGS = {} # Determine how many objects to display per page within a list. (Default: 50) -PAGINATE_COUNT = os.environ.get('PAGINATE_COUNT', 50) +PAGINATE_COUNT = int(os.environ.get('PAGINATE_COUNT', 50)) # When determining the primary IP address for a device, IPv6 is preferred over IPv4 by default. Set this to True to # prefer IPv4 instead. From 8e98b8d870349c4d338a76d8c80d5a7eb14cf424 Mon Sep 17 00:00:00 2001 From: David Dieulivol Date: Wed, 24 Jan 2018 09:21:55 +0100 Subject: [PATCH 24/30] :sparkles: Load custom fields when creating the container. --- README.md | 28 ++++++++++++++++++++++++++++ docker-compose.yml | 1 + docker/docker-entrypoint.sh | 4 ++++ docker/startup_scripts/.gitkeep | 0 4 files changed, 33 insertions(+) create mode 100644 docker/startup_scripts/.gitkeep diff --git a/README.md b/README.md index de93f74..36b4e14 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,34 @@ For example defining `ALLOWED_HOSTS=localhost ::1 127.0.0.1` would allows access [compose-env]: https://docs.docker.com/compose/environment-variables/ +## Adding Netbox Custom Fields + +When using `docker-compose`, all the python scripts present in `docker/startup_scripts` will automatically be executed after the application boots. + +That mechanism can be used for many things, and in particular to load Netbox custom fields: + +```python +# docker/startup_scripts/load_custom_fields.py +from django.contrib.contenttypes.models import ContentType +from extras.models import CF_TYPE_TEXT, CustomField + +from dcim.models import Device +from dcim.models import DeviceType + +device = ContentType.objects.get_for_model(Device) +device_type = ContentType.objects.get_for_model(DeviceType) + +my_custom_field, created = CustomField.objects.get_or_create( + type=CF_TYPE_TEXT, + name='my_custom_field', + description='My own custom field' +) + +if created: + my_custom_field.obj_type.add(device) + my_custom_field.obj_type.add(device_type) +``` + ### Production The default settings are optimized for (local) development environments. diff --git a/docker-compose.yml b/docker-compose.yml index 97e803d..34e4572 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,7 @@ services: - postgres env_file: netbox.env volumes: + - ./docker/startup_scripts:/opt/netbox/netbox/startup_scripts - netbox-nginx-config:/etc/netbox-nginx/ - netbox-static-files:/opt/netbox/netbox/static - netbox-media-files:/opt/netbox/netbox/media diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index f03b760..ff22cad 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -39,6 +39,10 @@ if not User.objects.filter(username='${SUPERUSER_NAME}'): Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}') END +for script in $(ls startup_scripts/*.py 2> /dev/null); do + ./manage.py shell --plain < "${script}" +done + # copy static files ./manage.py collectstatic --no-input diff --git a/docker/startup_scripts/.gitkeep b/docker/startup_scripts/.gitkeep new file mode 100644 index 0000000..e69de29 From 57e0c3466104343a23d11dc1058d3278190d057d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=A4der?= Date: Mon, 29 Jan 2018 11:41:38 +0100 Subject: [PATCH 25/30] Fix formatting in README.d --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 36b4e14..5aba037 100644 --- a/README.md +++ b/README.md @@ -66,9 +66,9 @@ For example defining `ALLOWED_HOSTS=localhost ::1 127.0.0.1` would allows access [compose-env]: https://docs.docker.com/compose/environment-variables/ -## Adding Netbox Custom Fields +### Custom Initialisation Code (e.g. Automatically Setting Up Custom Fields) -When using `docker-compose`, all the python scripts present in `docker/startup_scripts` will automatically be executed after the application boots. +When using `docker-compose`, all the python scripts present in `docker/startup_scripts` will automatically be executed after the application boots in the context of `./manage.py`. That mechanism can be used for many things, and in particular to load Netbox custom fields: From 96d414b46dd329230cd7022437d6ee86e2c1ad6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Tue, 30 Jan 2018 11:22:43 +0100 Subject: [PATCH 26/30] =?UTF-8?q?=E2=9D=87=EF=B8=8F=20Don't=20build=20what?= =?UTF-8?q?'s=20already=20there?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change introduces an API call to hub.docker.com to check if the version we're about to build was already built & pushed. This avoids superfluos builds. --- build-latest.sh | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/build-latest.sh b/build-latest.sh index 4a37237..97782f5 100755 --- a/build-latest.sh +++ b/build-latest.sh @@ -1,12 +1,15 @@ #!/bin/bash -URL_RELEASES=https://api.github.com/repos/digitalocean/netbox/releases +ORIGINAL_GITHUB_REPO="digitalocean/netbox" +GITHUB_REPO="${GITHUB_REPO-$ORIGINAL_GITHUB_REPO}" +URL_RELEASES="https://api.github.com/repos/${GITHUB_REPO}/releases" JQ_LATEST="group_by(.prerelease) | .[] | sort_by(.published_at) | reverse | .[0] | select(.prerelease==${PRERELEASE-false}) | .tag_name" CURL_OPTS="-s" +CURL="curl ${CURL_OPTS}" -VERSION=$(curl $CURL_OPTS "${URL_RELEASES}" | jq -r "${JQ_LATEST}") +VERSION=$($CURL "${URL_RELEASES}" | jq -r "${JQ_LATEST}") # Check if the prerelease version is actually higher than stable version if [ "${PRERELEASE}" == "true" ]; then @@ -25,4 +28,18 @@ if [ "${PRERELEASE}" == "true" ]; then fi fi -./build.sh "${VERSION}" $@ +# Check if that version is not already available on docker hub: +ORIGINAL_DOCKERHUB_REPO="ninech/netbox" +DOCKERHUB_REPO="${DOCKERHUB_REPO-$ORIGINAL_DOCKERHUB_REPO}" +URL_DOCKERHUB_TOKEN="https://auth.docker.io/token?service=registry.docker.io&scope=repository:${DOCKERHUB_REPO}:pull" +BEARER_TOKEN="$($CURL "${URL_DOCKERHUB_TOKEN}" | jq -r .token)" + +URL_DOCKERHUB_TAG="https://registry.hub.docker.com/v2/${DOCKERHUB_REPO}/tags/list" +AUTHORIZATION_HEADER="Authorization: Bearer ${BEARER_TOKEN}" +ALREADY_BUILT="$($CURL -H "${AUTHORIZATION_HEADER}" "${URL_DOCKERHUB_TAG}" | jq -e ".tags | any(.==\"${VERSION}\")")" + +if [ "$ALREADY_BUILT" == "false" ]; then + ./build.sh "${VERSION}" $@ +else + echo "✅ ${VERSION} already exists on https://hub.docker.com/r/${DOCKERHUB_REPO}" +fi From 2e819bf094c74e79e7d8696aa3361dd7a921ca8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Tue, 30 Jan 2018 11:45:53 +0100 Subject: [PATCH 27/30] =?UTF-8?q?=E2=9D=87=EF=B8=8F=20Automatically=20buil?= =?UTF-8?q?d=20all=20branches?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit .. which are not version branches. --- .travis.yml | 4 +--- build-branches.sh | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100755 build-branches.sh diff --git a/.travis.yml b/.travis.yml index 43e64aa..41381e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,9 +20,7 @@ after_script: after_success: - docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD" - if [ "$TRAVIS_BRANCH" = "master" -a "$TRAVIS_PULL_REQUEST" = "false" ]; then - ./build.sh master --push; - ./build.sh develop --push; - ./build.sh develop-2.3 --push; + ./build-branches.sh --push; ./build-latest.sh --push; PRERELEASE=true ./build-latest.sh --push; fi diff --git a/build-branches.sh b/build-branches.sh new file mode 100755 index 0000000..0b32ae9 --- /dev/null +++ b/build-branches.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +ORIGINAL_GITHUB_REPO="digitalocean/netbox" +GITHUB_REPO="${GITHUB_REPO-$ORIGINAL_GITHUB_REPO}" +URL_RELEASES="https://api.github.com/repos/${GITHUB_REPO}/branches" + +CURL_OPTS="-s" +CURL="curl ${CURL_OPTS}" + +BRANCHES=$($CURL "${URL_RELEASES}" | jq -r 'map(.name) | .[] | scan("^[^v].+")') + +for BRANCH in $BRANCHES; do + ./build.sh "${BRANCH}" $@ +done From dca64f55f55eaa9a7351ee759e981f766436e897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Tue, 30 Jan 2018 14:48:40 +0100 Subject: [PATCH 28/30] =?UTF-8?q?=E2=9D=87=EF=B8=8F=20Squash=20the=20image?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 9141d0a..15ebbf5 100755 --- a/build.sh +++ b/build.sh @@ -43,7 +43,7 @@ else fi echo "🐳 Building the Docker image '${DOCKER_REPO}/netbox:${TAG}' from the branch '${BRANCH}'." -docker build -t "${DOCKER_REPO}/netbox:${TAG}" --build-arg "BRANCH=${BRANCH}" --build-arg "URL=${URL}" --pull ${CACHE} . +docker build --squash -t "${DOCKER_REPO}/netbox:${TAG}" --build-arg "BRANCH=${BRANCH}" --build-arg "URL=${URL}" --pull ${CACHE} . echo "✅ Finished building the Docker images '${DOCKER_REPO}/netbox:${TAG}'" if [ "${2}" == "--push" ] ; then From d2ec32fefe46fe61307df832491b31fac7b5ae7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Tue, 30 Jan 2018 16:00:05 +0100 Subject: [PATCH 29/30] =?UTF-8?q?=E2=9C=8B=20Travis=20docker=20can't=20do?= =?UTF-8?q?=20`squash`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 15ebbf5..9141d0a 100755 --- a/build.sh +++ b/build.sh @@ -43,7 +43,7 @@ else fi echo "🐳 Building the Docker image '${DOCKER_REPO}/netbox:${TAG}' from the branch '${BRANCH}'." -docker build --squash -t "${DOCKER_REPO}/netbox:${TAG}" --build-arg "BRANCH=${BRANCH}" --build-arg "URL=${URL}" --pull ${CACHE} . +docker build -t "${DOCKER_REPO}/netbox:${TAG}" --build-arg "BRANCH=${BRANCH}" --build-arg "URL=${URL}" --pull ${CACHE} . echo "✅ Finished building the Docker images '${DOCKER_REPO}/netbox:${TAG}'" if [ "${2}" == "--push" ] ; then From fe05ca5c7ce999eca9f8baa5e4a3431826a2dbb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Fri, 2 Feb 2018 12:48:38 +0100 Subject: [PATCH 30/30] =?UTF-8?q?=E2=9D=87=EF=B8=8F=20Adds=20new=20options?= =?UTF-8?q?=20to=20the=20build=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 ++ build.sh | 75 ++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/Dockerfile b/Dockerfile index df564cd..02cdc08 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,3 +40,5 @@ ENTRYPOINT [ "/docker-entrypoint.sh" ] VOLUME ["/etc/netbox-nginx/"] CMD ["gunicorn", "--log-level debug", "-c /opt/netbox/gunicorn_config.py", "netbox.wsgi"] + +LABEL SRC_URL="$URL" diff --git a/build.sh b/build.sh index 9141d0a..e8443bc 100755 --- a/build.sh +++ b/build.sh @@ -8,16 +8,33 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then echo " --push Pushes built Docker image to docker hub." echo "" echo "You can use the following ENV variables to customize the build:" + echo " DOCKER_OPTS Add parameters to Docker." + echo " Default:" + echo " When starts with 'v': \"\"" + echo " Else: \"--no-cache\"" echo " BRANCH The branch to build." echo " Also used for tagging the image." - echo " DOCKER_REPO The Docker registry (i.e. hub.docker.com/r/DOCKER_REPO/netbox) " + echo " TAG The version part of the docker tag." + echo " Default:" + echo " When =master: latest" + echo " When =develop: snapshot" + echo " Else: same as " + echo " DOCKER_ORG The Docker registry (i.e. hub.docker.com/r//) " echo " Also used for tagging the image." echo " Default: ninech" - echo " SRC_REPO Which fork of netbox to use (i.e. github.com//netbox)." + echo " DOCKER_REPO The Docker registry (i.e. hub.docker.com/r//) " + echo " Also used for tagging the image." + echo " Default: netbox" + echo " DOCKER_TAG The name of the tag which is applied to the image." + echo " Useful for pushing into another registry than hub.docker.com." + echo " Default: /:" + echo " SRC_ORG Which fork of netbox to use (i.e. github.com//)." echo " Default: digitalocean" + echo " SRC_REPO The name of the netbox for to use (i.e. github.com//)." + echo " Default: netbox" echo " URL Where to fetch the package from." echo " Must be a tar.gz file of the source code." - echo " Default: https://github.com/\${SRC_REPO}/netbox/archive/\$BRANCH.tar.gz" + echo " Default: https://github.com///archive/\$BRANCH.tar.gz" if [ "${1}x" == "x" ]; then exit 1 @@ -26,28 +43,42 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then fi fi -SRC_REPO="${SRC_REPO-digitalocean}" -DOCKER_REPO="${DOCKER_REPO-ninech}" +# variables for fetching the source +SRC_ORG="${SRC_ORG-digitalocean}" +SRC_REPO="${SRC_REPO-netbox}" BRANCH="${1}" -URL="${URL-https://github.com/${SRC_REPO}/netbox/archive/$BRANCH.tar.gz}" +URL="${URL-https://github.com/${SRC_ORG}/${SRC_REPO}/archive/$BRANCH.tar.gz}" -if [ "${BRANCH}" == "master" ]; then - TAG="${TAG-latest}" - CACHE="--no-cache" -elif [ "${BRANCH}" == "develop" ]; then - TAG="${TAG-snapshot}" - CACHE="--no-cache" -else - TAG="${TAG-$BRANCH}" - CACHE="" -fi +# variables for tagging the docker image +DOCKER_ORG="${DOCKER_ORG-ninech}" +DOCKER_REPO="${DOCKER_REPO-netbox}" +case "${BRANCH}" in + master) + TAG="${TAG-latest}";; + develop) + TAG="${TAG-snapshot}";; + *) + TAG="${TAG-$BRANCH}";; +esac +DOCKER_TAG="${DOCKER_TAG-${DOCKER_ORG}/${DOCKER_REPO}:${TAG}}" -echo "🐳 Building the Docker image '${DOCKER_REPO}/netbox:${TAG}' from the branch '${BRANCH}'." -docker build -t "${DOCKER_REPO}/netbox:${TAG}" --build-arg "BRANCH=${BRANCH}" --build-arg "URL=${URL}" --pull ${CACHE} . -echo "✅ Finished building the Docker images '${DOCKER_REPO}/netbox:${TAG}'" +# caching is only ok for version tags +case "${TAG}" in + v*) + CACHE="${CACHE-}";; + *) + CACHE="${CACHE---no-cache}";; +esac + +# Docker options +DOCKER_OPTS="${DOCKER_OPTS-$CACHE}" + +echo "🐳 Building the Docker image '${DOCKER_TAG}' from the url '${URL}'." +docker build -t "${DOCKER_TAG}" --build-arg "BRANCH=${BRANCH}" --build-arg "URL=${URL}" --pull ${DOCKER_OPTS} . +echo "✅ Finished building the Docker images '${DOCKER_TAG}'" if [ "${2}" == "--push" ] ; then - echo "⏫ Pushing '${DOCKER_REPO}/netbox:${BRANCH}" - docker push "${DOCKER_REPO}/netbox:${TAG}" - echo "✅ Finished pushing the Docker image '${DOCKER_REPO}/netbox:${TAG}'." + echo "⏫ Pushing '${DOCKER_TAG}" + docker push "${DOCKER_TAG}" + echo "✅ Finished pushing the Docker image '${DOCKER_TAG}'." fi