Compare commits
69 Commits
Author | SHA1 | Date | |
---|---|---|---|
b790796f9b | |||
5ae5977717 | |||
e894cdaa06 | |||
5defc38294 | |||
61c0a9b519 | |||
992a8f1d5f | |||
2d25907cba | |||
8a40c6e0a3 | |||
ed8e339bfe | |||
4b1514f8d3 | |||
2044f685cf | |||
86de0d850b | |||
4e1ac2392d | |||
723d4744a4 | |||
821d6c8672 | |||
0b5214d247 | |||
aca448d180 | |||
6051092a59 | |||
03b52f9074 | |||
ce9158eb07 | |||
74000645dc | |||
ef0755b9cf | |||
7f22d21d5d | |||
e349c239fe | |||
28ba0e56e7 | |||
b0287e344e | |||
e6e7647f32 | |||
3e1f688f78 | |||
135d29e06f | |||
4cb5b9f20d | |||
5a4e9d7a14 | |||
89b9c22252 | |||
dc3db2d9fd | |||
0270fc2dca | |||
aed5126279 | |||
9c45e4d5e1 | |||
dfd30f5eae | |||
a5537b18a4 | |||
9601cb1e54 | |||
54ac5dc2ab | |||
a086c2fc22 | |||
e33a2d2c0a | |||
821fb5f36e | |||
7362e275b0 | |||
6c3db3deff | |||
2f8ea89d54 | |||
7008c03bcb | |||
8f39034014 | |||
98833e20c8 | |||
a5e51dfb47 | |||
936d868582 | |||
96924736df | |||
5107fb7c6b | |||
9eef398a9d | |||
e83db27b91 | |||
4419646732 | |||
286bfa0248 | |||
e768cb5b87 | |||
e45773c29f | |||
220370ed76 | |||
b49e6fac14 | |||
5beb3093db | |||
df16a431ca | |||
585e411cde | |||
e17baea76c | |||
8c95f32b0c | |||
d4b394c999 | |||
9c259adc88 | |||
582064f4cd |
32
Dockerfile
32
Dockerfile
@ -1,4 +1,4 @@
|
||||
FROM python:3.6-alpine3.9
|
||||
FROM python:3.7-alpine3.10
|
||||
|
||||
RUN apk add --no-cache \
|
||||
bash \
|
||||
@ -21,28 +21,42 @@ RUN pip install \
|
||||
# napalm is used for gathering information from network devices
|
||||
napalm \
|
||||
# ruamel is used in startup_scripts
|
||||
ruamel.yaml \
|
||||
'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>=1.11,<2.1' \
|
||||
'Django>=2.2,<2.3' \
|
||||
# django-rq is used for webhooks
|
||||
django-rq
|
||||
|
||||
ARG BRANCH=master
|
||||
|
||||
WORKDIR /tmp
|
||||
|
||||
# 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
|
||||
|
||||
# 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
|
||||
|
||||
WORKDIR /opt
|
||||
|
||||
ARG BRANCH=master
|
||||
ARG URL=https://github.com/digitalocean/netbox/archive/$BRANCH.tar.gz
|
||||
ARG URL=https://github.com/netbox-community/netbox/archive/$BRANCH.tar.gz
|
||||
RUN wget -q -O - "${URL}" | tar xz \
|
||||
&& mv netbox* netbox
|
||||
|
||||
WORKDIR /opt/netbox
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
COPY docker/configuration.docker.py /opt/netbox/netbox/netbox/configuration.py
|
||||
COPY configuration/gunicorn_config.py /etc/netbox/config/
|
||||
COPY docker/nginx.conf /etc/netbox-nginx/nginx.conf
|
||||
COPY docker/docker-entrypoint.sh docker-entrypoint.sh
|
||||
COPY docker/docker-entrypoint.sh /opt/netbox/docker-entrypoint.sh
|
||||
COPY startup_scripts/ /opt/netbox/startup_scripts/
|
||||
COPY initializers/ /opt/netbox/initializers/
|
||||
COPY configuration/configuration.py /etc/netbox/config/configuration.py
|
||||
|
345
README.md
345
README.md
@ -1,9 +1,9 @@
|
||||
# netbox-docker
|
||||
|
||||
[The Github repository](netbox-docker-github) houses the components needed to build Netbox as a Docker container.
|
||||
Images are built using this code are released to [Docker Hub][netbox-dockerhub] every night.
|
||||
Images are built using this code and are released to [Docker Hub][netbox-dockerhub] once a day.
|
||||
|
||||
Questions? Before opening an issue on Github, please join the [Network To Code][ntc-slack] Slack and ask for help in our `#netbox-docker` channel.
|
||||
Do you have any questions? Before opening an issue on Github, please join the [Network To Code][ntc-slack] Slack and ask for help in our `#netbox-docker` channel.
|
||||
|
||||
[netbox-dockerhub]: https://hub.docker.com/r/netboxcommunity/netbox/tags/
|
||||
[netbox-docker-github]: https://github.com/netbox-community/netbox-docker/
|
||||
@ -13,17 +13,17 @@ Questions? Before opening an issue on Github, please join the [Network To Code][
|
||||
|
||||
To get Netbox up and running:
|
||||
|
||||
```
|
||||
$ git clone -b master https://github.com/netbox-community/netbox-docker.git
|
||||
$ cd netbox-docker
|
||||
$ docker-compose pull
|
||||
$ docker-compose up -d
|
||||
```bash
|
||||
git clone -b master https://github.com/netbox-community/netbox-docker.git
|
||||
cd netbox-docker
|
||||
docker-compose pull
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
The application will be available after a few minutes.
|
||||
Use `docker-compose port nginx 8080` to find out where to connect to.
|
||||
|
||||
```
|
||||
```bash
|
||||
$ echo "http://$(docker-compose port nginx 8080)/"
|
||||
http://0.0.0.0:32768/
|
||||
|
||||
@ -53,172 +53,12 @@ This project relies only on *Docker* and *docker-compose* meeting this requireme
|
||||
|
||||
To ensure this, compare the output of `docker --version` and `docker-compose --version` with the requirements above.
|
||||
|
||||
## Configuration
|
||||
## Reference Documentation
|
||||
|
||||
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.)
|
||||
Please refer [to the wiki][wiki] for further information on how to use this Netbox Docker image properly.
|
||||
It covers advanced topics such as using secret files, deployment to Kubernetes as well as NAPALM and LDAP configuration.
|
||||
|
||||
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:8080`, `http://[::1]:8080` and `http://127.0.0.1:8080`.
|
||||
|
||||
[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, space separated. E.g. `ALLOWED_HOSTS=netbox.mycorp.com server042.mycorp.com 2a02:123::42 10.0.0.42 localhost ::1 127.0.0.1` (It's good advice to always allow localhost connections for easy debugging, i.e. `localhost ::1 127.0.0.1`.)
|
||||
* `DB_*`: Use your own persistent database. Don't use the default passwords!
|
||||
* `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. Don't use the default passwords!
|
||||
* `REDIS_*`: Use your own persistent redis. Don't use the default passwords!
|
||||
|
||||
### 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 in the directory `/etc/netbox/config/` 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.
|
||||
|
||||
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`
|
||||
* `REDIS_PASSWORD`: `/run/secrets/redis_password`
|
||||
|
||||
Please also consider [the advice about running Netbox in production](#production) above!
|
||||
|
||||
[docker-config]: https://github.com/netbox-community/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/netbox-community/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/
|
||||
[k8s-config]: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
|
||||
|
||||
### NAPALM Configuration
|
||||
|
||||
Since v2.1.0 NAPALM has been tightly integrated into Netbox.
|
||||
NAPALM allows Netbox to fetch live data from devices and return it to a requester via its REST API.
|
||||
To learn more about what NAPALM is and how it works, please see the documentation from the [libary itself][napalm-doc] or the documentation from [Netbox][netbox-napalm-doc] on how it is integrated.
|
||||
|
||||
To enable this functionality, simply complete the following lines in `netbox.env` (or appropriate secrets mechanism) :
|
||||
|
||||
* `NAPALM_USERNAME`: A common username that can be utilized for connecting to network devices in your environment.
|
||||
* `NAPALM_PASSWORD`: The password to use in combintation with the username to connect to network devices.
|
||||
* `NAPALM_TIMEOUT`: A value to use for when an attempt to connect to a device will timeout if no response has been recieved.
|
||||
|
||||
However, if you don't need this functionality, leave these blank.
|
||||
|
||||
[napalm-doc]: http://napalm.readthedocs.io/en/latest/index.html
|
||||
[netbox-napalm-doc]: https://netbox.readthedocs.io/en/latest/configuration/optional-settings/#napalm_username
|
||||
|
||||
### Customizable Reporting
|
||||
|
||||
Netbox includes [customized reporting][netbox-reports-doc] that allows the user to write Python code and determine the validity of the data within Netbox.
|
||||
The `REPORTS_ROOT` variable is setup as a mapped directory within this Docker container to `/reports/` and includes the example directly from the documentation for `devices.py`.
|
||||
However, it has been renamed to `devices.py.example` which prevents Netbox from recognizing it as a valid report.
|
||||
This was done to avoid unnessary issues from being opened when the default does not work for someone's expectations.
|
||||
|
||||
To re-enable this default report, simply rename `devices.py.example` to `devices.py` and browse within the WebUI to `/extras/reports/`.
|
||||
You can also dynamically add any other report to this same directory and Netbox will be able to see it without restarting the container.
|
||||
|
||||
[netbox-reports-doc]: https://netbox.readthedocs.io/en/stable/additional-features/reports/
|
||||
|
||||
### Custom Initialization Code (e.g. Automatically Setting Up Custom Fields)
|
||||
|
||||
When using `docker-compose`, all the python scripts present in `/opt/netbox/startup_scripts` will automatically be executed after the application boots in the context of `./manage.py`.
|
||||
|
||||
That mechanism can be used for many things, e.g. to create 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)
|
||||
```
|
||||
|
||||
#### Initializers
|
||||
|
||||
Initializers are built-in startup scripts for defining Netbox custom fields, groups, users and many other resources.
|
||||
All you need to do is to mount you own `initializers` folder ([see `docker-compose.yml`][netbox-docker-compose]).
|
||||
Look at the [`initializers` folder][netbox-docker-initializers] to learn how the files must look like.
|
||||
|
||||
Here's an example for defining a custom field:
|
||||
|
||||
```yaml
|
||||
# initializers/custom_fields.yml
|
||||
text_field:
|
||||
type: text
|
||||
label: Custom Text
|
||||
description: Enter text in a text field.
|
||||
required: false
|
||||
filter_logic: loose
|
||||
weight: 0
|
||||
on_objects:
|
||||
- dcim.models.Device
|
||||
- dcim.models.Rack
|
||||
- ipam.models.IPAddress
|
||||
- ipam.models.Prefix
|
||||
- tenancy.models.Tenant
|
||||
- virtualization.models.VirtualMachine
|
||||
```
|
||||
|
||||
[netbox-docker-initializers]: https://github.com/netbox-community/netbox-docker/tree/master/initializers
|
||||
[netbox-docker-compose]: https://github.com/netbox-community/netbox-docker/blob/master/docker-compose.yml
|
||||
|
||||
##### Available Groups for User/Group initializers
|
||||
|
||||
To get an up-to-date list about all the available permissions, run the following command.
|
||||
|
||||
```bash
|
||||
# Make sure the 'netbox' container is already running! If unsure, run `docker-compose up -d`
|
||||
echo "from django.contrib.auth.models import Permission\nfor p in Permission.objects.all():\n print(p.codename);" | docker-compose exec -T netbox ./manage.py shell
|
||||
```
|
||||
|
||||
#### Custom Docker Image
|
||||
|
||||
You can also build your own Netbox Docker image containing your own startup scripts, custom fields, users and groups
|
||||
like this:
|
||||
|
||||
```
|
||||
ARG VERSION=latest
|
||||
FROM netboxcommunity/netbox:$VERSION
|
||||
|
||||
COPY startup_scripts/ /opt/netbox/startup_scripts/
|
||||
COPY initializers/ /opt/netbox/initializers/
|
||||
```
|
||||
[wiki]: https://github.com/netbox-community/netbox-docker/wiki/
|
||||
|
||||
## Netbox Version
|
||||
|
||||
@ -227,169 +67,38 @@ To use this feature, set the environment-variable `VERSION` before launching `do
|
||||
`VERSION` may be set to the name of
|
||||
[any tag of the `netboxcommunity/netbox` Docker image on Docker Hub][netbox-dockerhub].
|
||||
|
||||
```
|
||||
$ export VERSION=v2.2.6
|
||||
$ docker-compose pull netbox
|
||||
$ docker-compose up -d
|
||||
```bash
|
||||
export VERSION=v2.2.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 'digitalocean/netbox' Github repository][netbox-github].
|
||||
[Git Reference][git-ref] declared on [the 'netbox-community/netbox' Github repository][netbox-github].
|
||||
Most commonly you will specify a tag or branch name.
|
||||
|
||||
```
|
||||
$ export VERSION=develop
|
||||
$ docker-compose build --no-cache netbox
|
||||
$ docker-compose up -d
|
||||
```bash
|
||||
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.
|
||||
|
||||
[git-ref]: https://git-scm.com/book/en/v2/Git-Internals-Git-References
|
||||
[netbox-github]: https://github.com/digitalocean/netbox/releases
|
||||
[netbox-github]: https://github.com/netbox-community/netbox/releases
|
||||
|
||||
### LDAP enabled variant
|
||||
|
||||
The images tagged with "-ldap" contain anything necessary to authenticate against an LDAP or Active Directory server.
|
||||
The default configuration `ldap_config.py` is prepared for use with an Active Directory server.
|
||||
Custom values can be injected using environment variables, similar to the main configuration mechanisms.
|
||||
|
||||
## 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/netbox-community/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 -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'`.
|
||||
|
||||
### Nginx doesn't start
|
||||
|
||||
As a first step, stop your docker-compose setup.
|
||||
Then locate the `netbox-nginx-config` volume and remove it:
|
||||
|
||||
```bash
|
||||
# Stop your local netbox-docker installation
|
||||
$ docker-compose down
|
||||
|
||||
# Find the volume
|
||||
$ docker volume ls | grep netbox-nginx-config
|
||||
local netbox-docker_netbox-nginx-config
|
||||
|
||||
# Remove the volume
|
||||
$ docker volume rm netbox-docker_netbox-nginx-config
|
||||
netbox-docker_netbox-nginx-config
|
||||
```
|
||||
|
||||
Now start everything up again.
|
||||
|
||||
If this didn't help, try to see if there's anything in the logs indicating why nginx doesn't start:
|
||||
|
||||
```bash
|
||||
$ docker-compose logs -f nginx
|
||||
```
|
||||
|
||||
### 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 of netbox?
|
||||
|
||||
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 netbox-worker
|
||||
docker-compose rm -f netbox netbox-worker
|
||||
docker-compose up -d netbox netbox-worker
|
||||
```
|
||||
|
||||
### Webhooks don't work
|
||||
|
||||
First make sure that the webhooks feature is enabled in your Netbox configuration and that a redis host is defined.
|
||||
Check `netbox.env` if the following variables are defined:
|
||||
|
||||
```
|
||||
WEBHOOKS_ENABLED=true
|
||||
REDIS_HOST=redis
|
||||
```
|
||||
|
||||
Then make sure that the `redis` container and at least one `netbox-worker` are running.
|
||||
|
||||
```
|
||||
# check the container status
|
||||
$ docker-compose ps
|
||||
|
||||
Name Command State Ports
|
||||
--------------------------------------------------------------------------------------------------------
|
||||
netbox-docker_netbox-worker_1 /opt/netbox/docker-entrypo ... Up
|
||||
netbox-docker_netbox_1 /opt/netbox/docker-entrypo ... Up
|
||||
netbox-docker_nginx_1 nginx -c /etc/netbox-nginx ... Up 80/tcp, 0.0.0.0:32776->8080/tcp
|
||||
netbox-docker_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp
|
||||
netbox-docker_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
|
||||
|
||||
# connect to redis and send PING command:
|
||||
$ docker-compose run --rm -T redis sh -c 'redis-cli -h redis -a $REDIS_PASSWORD ping'
|
||||
Warning: Using a password with '-a' option on the command line interface may not be safe.
|
||||
PONG
|
||||
```
|
||||
|
||||
If `redis` and the `netbox-worker` are not available, make sure you have updated your `docker-compose.yml` file!
|
||||
|
||||
Everything's up and running? Then check the log of `netbox-worker` and/or `redis`:
|
||||
|
||||
```bash
|
||||
docker-compose logs -f netbox-worker
|
||||
docker-compose logs -f redis
|
||||
```
|
||||
|
||||
Still no clue? You can connect to the `redis` container and have it report any command that is currently executed on the server:
|
||||
|
||||
```bash
|
||||
docker-compose run --rm -T redis sh -c 'redis-cli -h redis -a $REDIS_PASSWORD monitor'
|
||||
|
||||
# Hit CTRL-C a few times to leave
|
||||
```
|
||||
|
||||
If you don't see anything happening after you triggered a webhook, double-check the configuration of the `netbox` and the `netbox-worker` containers and also check the configuration of your webhook in the admin interface of Netbox.
|
||||
|
||||
### Breaking Changes
|
||||
## Breaking Changes
|
||||
|
||||
From time to time it might become necessary to re-engineer the structure of this setup.
|
||||
Things like the `docker-compose.yml` file or your Kubernetes or OpenShift configurations have to be adjusted as a consequence.
|
||||
Since April 2018 each image built from this repo contains a `NETBOX_DOCKER_PROJECT_VERSION` label.
|
||||
You can check the label of your local image by running `docker inspect netboxcommunity/netbox:v2.3.1 --format "{{json .ContainerConfig.Labels}}"`.
|
||||
Compare the version with the list below to check whether a breaking change was introduced with that version.
|
||||
|
||||
The following is a list of breaking changes of the `netbox-docker` project:
|
||||
Please read [the release notes][releases] carefully when updating to a new image version.
|
||||
|
||||
* 0.8.0: Alpine linux was upgraded to 3.9 [#126][126]
|
||||
* 0.7.0: The value of the `MAX_PAGE_SIZE` environment variable was changed to `1000`, which is the default of Netbox.
|
||||
* 0.6.0: The naming of the default startup_scripts were changed.
|
||||
If you overwrite them, you may need to adjust these scripts.
|
||||
* 0.5.0: Alpine was updated to 3.8, `*.env` moved to `/env` folder
|
||||
* 0.4.0: In order to use Netbox webhooks you need to add Redis and a netbox-worker to your docker-compose.yml.
|
||||
* 0.3.0: Field `filterable: <boolean` was replaced with field `filter_logic: loose/exact/disabled`. It will default to `CF_FILTER_LOOSE=loose` when not defined.
|
||||
* 0.2.0: Re-organized paths: `/etc/netbox -> /etc/netbox/config` and `/etc/reports -> /etc/netbox/reports`. Fixes [#54][54].
|
||||
* 0.1.0: Introduction of the `NETBOX_DOCKER_PROJECT_VERSION`. (Not a breaking change per se.)
|
||||
|
||||
[54]: https://github.com/netbox-community/netbox-docker/issues/54
|
||||
[126]: https://github.com/netbox-community/netbox-docker/pull/126
|
||||
[releases]: https://github.com/netbox-community/netbox-docker/releases
|
||||
|
||||
## Rebuilding & Publishing images
|
||||
|
||||
@ -406,8 +115,8 @@ New Docker images are built and published every 24h on the [Docker Build Infrast
|
||||
|
||||
To run the tests coming with Netbox, use the `docker-compose.yml` file as such:
|
||||
|
||||
```
|
||||
$ docker-compose run netbox ./manage.py test
|
||||
```bash
|
||||
docker-compose run netbox ./manage.py test
|
||||
```
|
||||
|
||||
## About
|
||||
|
@ -11,7 +11,7 @@ else
|
||||
GITHUB_OAUTH_PARAMS=""
|
||||
fi
|
||||
|
||||
ORIGINAL_GITHUB_REPO="${SRC_ORG-digitalocean}/${SRC_REPO-netbox}"
|
||||
ORIGINAL_GITHUB_REPO="${SRC_ORG-netbox-community}/${SRC_REPO-netbox}"
|
||||
GITHUB_REPO="${GITHUB_REPO-$ORIGINAL_GITHUB_REPO}"
|
||||
URL_RELEASES="https://api.github.com/repos/${GITHUB_REPO}/branches?${GITHUB_OAUTH_PARAMS}"
|
||||
|
||||
|
@ -11,7 +11,7 @@ else
|
||||
GITHUB_OAUTH_PARAMS=""
|
||||
fi
|
||||
|
||||
ORIGINAL_GITHUB_REPO="digitalocean/netbox"
|
||||
ORIGINAL_GITHUB_REPO="netbox-community/netbox"
|
||||
GITHUB_REPO="${GITHUB_REPO-$ORIGINAL_GITHUB_REPO}"
|
||||
URL_RELEASES="https://api.github.com/repos/${GITHUB_REPO}/releases?${GITHUB_OAUTH_PARAMS}"
|
||||
|
||||
|
63
build.sh
63
build.sh
@ -34,8 +34,11 @@ if [ "${1}x" == "x" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
|
||||
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: <DOCKER_ORG>/<DOCKER_REPO>:<BRANCH>"
|
||||
echo " DOCKER_SHORT_TAG The name of the short tag which is applied to the image."
|
||||
echo " This is used to tag all patch releases to their containing version e.g. v2.5.1 -> v2.5"
|
||||
echo " Default: <DOCKER_ORG>/<DOCKER_REPO>:\$MAJOR.\$MINOR"
|
||||
echo " SRC_ORG Which fork of netbox to use (i.e. github.com/<SRC_ORG>/<SRC_REPO>)."
|
||||
echo " Default: digitalocean"
|
||||
echo " Default: netbox-community"
|
||||
echo " SRC_REPO The name of the netbox for to use (i.e. github.com/<SRC_ORG>/<SRC_REPO>)."
|
||||
echo " Default: netbox"
|
||||
echo " URL Where to fetch the package from."
|
||||
@ -78,11 +81,30 @@ fi
|
||||
NETBOX_DOCKER_PROJECT_VERSION="${NETBOX_DOCKER_PROJECT_VERSION-$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' VERSION)}"
|
||||
|
||||
# variables for fetching the source
|
||||
SRC_ORG="${SRC_ORG-digitalocean}"
|
||||
SRC_ORG="${SRC_ORG-netbox-community}"
|
||||
SRC_REPO="${SRC_REPO-netbox}"
|
||||
BRANCH="${1}"
|
||||
URL="${URL-https://github.com/${SRC_ORG}/${SRC_REPO}/archive/$BRANCH.tar.gz}"
|
||||
|
||||
# Checking which VARIANT to build
|
||||
VARIANT="${VARIANT-main}"
|
||||
if [ "$VARIANT" == "main" ]; then
|
||||
DOCKERFILE="Dockerfile"
|
||||
else
|
||||
DOCKERFILE="Dockerfile.${VARIANT}"
|
||||
fi
|
||||
|
||||
# Fail fast
|
||||
if [ ! -f "${DOCKERFILE}" ]; then
|
||||
echo "🚨 The Dockerfile ${DOCKERFILE} for variant '${VARIANT}' doesn't exist."
|
||||
|
||||
if [ -z "$DEBUG" ]; then
|
||||
exit 1
|
||||
else
|
||||
echo "⚠️ Would exit here with code '1', but DEBUG is enabled."
|
||||
fi
|
||||
fi
|
||||
|
||||
# variables for tagging the docker image
|
||||
DOCKER_ORG="${DOCKER_ORG-netboxcommunity}"
|
||||
DOCKER_REPO="${DOCKER_REPO-netbox}"
|
||||
@ -94,25 +116,20 @@ case "${BRANCH}" in
|
||||
*)
|
||||
TAG="${TAG-$BRANCH}";;
|
||||
esac
|
||||
|
||||
DOCKER_TAG="${DOCKER_TAG-${DOCKER_ORG}/${DOCKER_REPO}:${TAG}}"
|
||||
|
||||
# Checking which VARIANT to build
|
||||
VARIANT="${VARIANT-main}"
|
||||
if [ "$VARIANT" == "main" ]; then
|
||||
DOCKERFILE="Dockerfile"
|
||||
else
|
||||
DOCKERFILE="Dockerfile.${VARIANT}"
|
||||
if [ "$VARIANT" != "main" ]; then
|
||||
DOCKER_TAG="${DOCKER_TAG}-${VARIANT}"
|
||||
fi
|
||||
|
||||
# Fail fast
|
||||
if [ ! -f "${DOCKERFILE}" ]; then
|
||||
echo "🚨 The Dockerfile ${DOCKERFILE} for variant '${VARIANT}' doesn't exist."
|
||||
if [[ "${TAG}" =~ ^v([0-9]+)\.([0-9]+)\.[0-9]+$ ]]; then
|
||||
MAJOR=${BASH_REMATCH[1]}
|
||||
MINOR=${BASH_REMATCH[2]}
|
||||
|
||||
if [ -z "$DEBUG" ]; then
|
||||
exit 1
|
||||
else
|
||||
echo "⚠️ Would exit here with code '1', but DEBUG is enabled."
|
||||
fi
|
||||
DOCKER_SHORT_TAG="${DOCKER_SHORT_TAG-${DOCKER_ORG}/${DOCKER_REPO}:v${MAJOR}.${MINOR}}"
|
||||
|
||||
if [ "$VARIANT" != "main" ]; then
|
||||
DOCKER_SHORT_TAG="${DOCKER_SHORT_TAG}-${VARIANT}"
|
||||
fi
|
||||
fi
|
||||
|
||||
@ -159,10 +176,22 @@ if [ "${2}" != "--push-only" ] ; then
|
||||
echo "🐳 Building the Docker image '${DOCKER_TAG}' from the url '${URL}'."
|
||||
$DOCKER_CMD build -t "${DOCKER_TAG}" "${DOCKER_BUILD_ARGS[@]}" "${DOCKER_OPTS[@]}" -f "${DOCKERFILE}" .
|
||||
echo "✅ Finished building the Docker images '${DOCKER_TAG}'"
|
||||
|
||||
if [ -n "$DOCKER_SHORT_TAG" ]; then
|
||||
echo "🐳 Tagging image '${DOCKER_SHORT_TAG}'."
|
||||
$DOCKER_CMD tag "${DOCKER_TAG}" "${DOCKER_SHORT_TAG}"
|
||||
echo "✅ Tagged image '${DOCKER_SHORT_TAG}'"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "${2}" == "--push" ] || [ "${2}" == "--push-only" ] ; then
|
||||
echo "⏫ Pushing '${DOCKER_TAG}"
|
||||
$DOCKER_CMD push "${DOCKER_TAG}"
|
||||
echo "✅ Finished pushing the Docker image '${DOCKER_TAG}'."
|
||||
|
||||
if [ -n "$DOCKER_SHORT_TAG" ]; then
|
||||
echo "⏫ Pushing '${DOCKER_SHORT_TAG}'"
|
||||
$DOCKER_CMD push "${DOCKER_SHORT_TAG}"
|
||||
echo "✅ Finished pushing the Docker image '${DOCKER_SHORT_TAG}'."
|
||||
fi
|
||||
fi
|
||||
|
@ -1,8 +1,9 @@
|
||||
import os
|
||||
import re
|
||||
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
|
||||
# Based on https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/configuration.example.py
|
||||
|
||||
# Read secret from file
|
||||
def read_secret(secret_name):
|
||||
@ -44,6 +45,17 @@ DATABASE = {
|
||||
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-SECRET_KEY
|
||||
SECRET_KEY = os.environ.get('SECRET_KEY', read_secret('secret_key'))
|
||||
|
||||
# Redis database settings. The Redis database is used for caching and background processing such as webhooks
|
||||
REDIS = {
|
||||
'HOST': os.environ.get('REDIS_HOST', 'localhost'),
|
||||
'PORT': int(os.environ.get('REDIS_PORT', 6379)),
|
||||
'PASSWORD': os.environ.get('REDIS_PASSWORD', read_secret('redis_password')),
|
||||
'DATABASE': os.environ.get('REDIS_DATABASE', '0'),
|
||||
'CACHE_DATABASE': os.environ.get('REDIS_CACHE_DATABASE', '1'),
|
||||
'DEFAULT_TIMEOUT': os.environ.get('REDIS_TIMEOUT', '300'),
|
||||
'SSL': os.environ.get('REDIS_SSL', 'False').lower() == 'true',
|
||||
}
|
||||
|
||||
#########################
|
||||
# #
|
||||
# Optional settings #
|
||||
@ -68,14 +80,18 @@ BANNER_LOGIN = os.environ.get('BANNER_LOGIN', '')
|
||||
# BASE_PATH = 'netbox/'
|
||||
BASE_PATH = os.environ.get('BASE_PATH', '')
|
||||
|
||||
# Cache timeout in seconds. Set to 0 to dissable caching. Defaults to 900 (15 minutes)
|
||||
CACHE_TIMEOUT = int(os.environ.get('CACHE_TIMEOUT', 900))
|
||||
|
||||
# Maximum number of days to retain logged changes. Set to 0 to retain changes indefinitely. (Default: 90)
|
||||
CHANGELOG_RETENTION = int(os.environ.get('CHANGELOG_RETENTION', 90))
|
||||
|
||||
# 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').lower() == 'true'
|
||||
CORS_ORIGIN_WHITELIST = os.environ.get('CORS_ORIGIN_WHITELIST', '').split(' ')
|
||||
CORS_ORIGIN_REGEX_WHITELIST = [
|
||||
# r'^(https?://)?(\w+\.)?example\.com$',
|
||||
]
|
||||
CORS_ORIGIN_WHITELIST = list(filter(None, os.environ.get('CORS_ORIGIN_WHITELIST', 'https://localhost').split(' ')))
|
||||
CORS_ORIGIN_REGEX_WHITELIST = [re.compile(r) for r in list(filter(None, os.environ.get('CORS_ORIGIN_REGEX_WHITELIST', '').split(' ')))]
|
||||
|
||||
# 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
|
||||
@ -97,6 +113,10 @@ EMAIL = {
|
||||
# set ENFORCE_GLOBAL_UNIQUE to True.
|
||||
ENFORCE_GLOBAL_UNIQUE = os.environ.get('ENFORCE_GLOBAL_UNIQUE', 'False').lower() == 'true'
|
||||
|
||||
# Exempt certain models from the enforcement of view permissions. Models listed here will be viewable by all users and
|
||||
# by anonymous users. List models in the form `<app>.<model>`. Add '*' to this list to exempt all models.
|
||||
EXEMPT_VIEW_PERMISSIONS = list(filter(None, os.environ.get('EXEMPT_VIEW_PERMISSIONS', '').split(' ')))
|
||||
|
||||
# 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 = {}
|
||||
@ -117,6 +137,9 @@ MAX_PAGE_SIZE = int(os.environ.get('MAX_PAGE_SIZE', 1000))
|
||||
# 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'))
|
||||
|
||||
# Expose Prometheus monitoring metrics at the HTTP endpoint '/metrics'
|
||||
METRICS_ENABLED = os.environ.get('METRICS_ENABLED', 'False').lower() == 'true'
|
||||
|
||||
# Credentials that NetBox will use to access live devices.
|
||||
NAPALM_USERNAME = os.environ.get('NAPALM_USERNAME', '')
|
||||
NAPALM_PASSWORD = os.environ.get('NAPALM_PASSWORD', read_secret('napalm_password'))
|
||||
@ -134,20 +157,6 @@ 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.
|
||||
PREFER_IPV4 = os.environ.get('PREFER_IPV4', 'False').lower() == 'true'
|
||||
|
||||
# The Webhook event backend is disabled by default. Set this to True to enable it. Note that this requires a Redis
|
||||
# database be configured and accessible by NetBox (see `REDIS` below).
|
||||
WEBHOOKS_ENABLED = os.environ.get('WEBHOOKS_ENABLED', 'False').lower() == 'true'
|
||||
|
||||
# Redis database settings (optional). A Redis database is required only if the webhooks backend is enabled.
|
||||
REDIS = {
|
||||
'HOST': os.environ.get('REDIS_HOST', 'localhost'),
|
||||
'PORT': os.environ.get('REDIS_PORT', 6379),
|
||||
'PASSWORD': os.environ.get('REDIS_PASSWORD', read_secret('redis_password')),
|
||||
'DATABASE': os.environ.get('REDIS_DATABASE', '0'),
|
||||
'DEFAULT_TIMEOUT': os.environ.get('REDIS_TIMEOUT', '300'),
|
||||
}
|
||||
|
||||
# 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', '/etc/netbox/reports')
|
||||
@ -155,6 +164,10 @@ REPORTS_ROOT = os.environ.get('REPORTS_ROOT', '/etc/netbox/reports')
|
||||
# Time zone (default: UTC)
|
||||
TIME_ZONE = os.environ.get('TIME_ZONE', 'UTC')
|
||||
|
||||
# The Webhook event backend is disabled by default. Set this to True to enable it. Note that this requires a Redis
|
||||
# database be configured and accessible by NetBox (see `REDIS` below).
|
||||
WEBHOOKS_ENABLED = os.environ.get('WEBHOOKS_ENABLED', 'False').lower() == 'true'
|
||||
|
||||
# Date/time formatting. See the following link for supported formats:
|
||||
# https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date
|
||||
DATE_FORMAT = os.environ.get('DATE_FORMAT', 'N j, Y')
|
||||
|
@ -1,7 +1,26 @@
|
||||
import ldap
|
||||
import os
|
||||
|
||||
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType
|
||||
from django_auth_ldap.config import LDAPSearch
|
||||
from importlib import import_module
|
||||
|
||||
# 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()
|
||||
|
||||
# Import and return the group type based on string name
|
||||
def import_group_type(group_type_name):
|
||||
mod = import_module('django_auth_ldap.config')
|
||||
try:
|
||||
return getattr(mod, group_type_name)()
|
||||
except:
|
||||
return None
|
||||
|
||||
# Server URI
|
||||
AUTH_LDAP_SERVER_URI = os.environ.get('AUTH_LDAP_SERVER_URI', '')
|
||||
@ -13,7 +32,7 @@ AUTH_LDAP_CONNECTION_OPTIONS = {
|
||||
|
||||
# Set the DN and password for the NetBox service account.
|
||||
AUTH_LDAP_BIND_DN = os.environ.get('AUTH_LDAP_BIND_DN', '')
|
||||
AUTH_LDAP_BIND_PASSWORD = os.environ.get('AUTH_LDAP_BIND_PASSWORD', '')
|
||||
AUTH_LDAP_BIND_PASSWORD = os.environ.get('AUTH_LDAP_BIND_PASSWORD', read_secret('auth_ldap_bind_password'))
|
||||
|
||||
# Set a string template that describes any user’s distinguished name based on the username.
|
||||
AUTH_LDAP_USER_DN_TEMPLATE = os.environ.get('AUTH_LDAP_USER_DN_TEMPLATE', None)
|
||||
@ -35,7 +54,7 @@ AUTH_LDAP_GROUP_SEARCH_BASEDN = os.environ.get('AUTH_LDAP_GROUP_SEARCH_BASEDN',
|
||||
AUTH_LDAP_GROUP_SEARCH_CLASS = os.environ.get('AUTH_LDAP_GROUP_SEARCH_CLASS', 'group')
|
||||
AUTH_LDAP_GROUP_SEARCH = LDAPSearch(AUTH_LDAP_GROUP_SEARCH_BASEDN, ldap.SCOPE_SUBTREE,
|
||||
"(objectClass=" + AUTH_LDAP_GROUP_SEARCH_CLASS + ")")
|
||||
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType()
|
||||
AUTH_LDAP_GROUP_TYPE = import_group_type(os.environ.get('AUTH_LDAP_GROUP_TYPE', 'GroupOfNamesType'))
|
||||
|
||||
# Define a group required to login.
|
||||
AUTH_LDAP_REQUIRE_GROUP = os.environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', '')
|
||||
|
@ -12,13 +12,13 @@ services:
|
||||
- netbox-worker
|
||||
env_file: env/netbox.env
|
||||
volumes:
|
||||
- ./startup_scripts:/opt/netbox/startup_scripts:ro
|
||||
- ./initializers:/opt/netbox/initializers:ro
|
||||
- ./configuration:/etc/netbox/config:ro
|
||||
- ./reports:/etc/netbox/reports:ro
|
||||
- netbox-nginx-config:/etc/netbox-nginx/
|
||||
- netbox-static-files:/opt/netbox/netbox/static
|
||||
- netbox-media-files:/opt/netbox/netbox/media
|
||||
- ./startup_scripts:/opt/netbox/startup_scripts:z,ro
|
||||
- ./initializers:/opt/netbox/initializers:z,ro
|
||||
- ./configuration:/etc/netbox/config:z,ro
|
||||
- ./reports:/etc/netbox/reports:z,ro
|
||||
- netbox-nginx-config:/etc/netbox-nginx:z
|
||||
- netbox-static-files:/opt/netbox/netbox/static:z
|
||||
- netbox-media-files:/opt/netbox/netbox/media:z
|
||||
netbox-worker:
|
||||
<<: *netbox
|
||||
depends_on:
|
||||
|
@ -39,10 +39,14 @@ if not User.objects.filter(username='${SUPERUSER_NAME}'):
|
||||
Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}')
|
||||
END
|
||||
|
||||
for script in /opt/netbox/startup_scripts/*.py; do
|
||||
echo "⚙️ Executing '$script'"
|
||||
./manage.py shell --interface python < "${script}"
|
||||
done
|
||||
if [ "$SKIP_STARTUP_SCRIPTS" == "true" ]; then
|
||||
echo "☇ Skipping startup scripts"
|
||||
else
|
||||
for script in /opt/netbox/startup_scripts/*.py; do
|
||||
echo "⚙️ Executing '$script'"
|
||||
./manage.py shell --interface python < "${script}"
|
||||
done
|
||||
fi
|
||||
|
||||
# copy static files
|
||||
./manage.py collectstatic --no-input
|
||||
|
4
env/netbox.env
vendored
4
env/netbox.env
vendored
@ -1,3 +1,4 @@
|
||||
CORS_ORIGIN_ALLOW_ALL=True
|
||||
DB_NAME=netbox
|
||||
DB_USER=netbox
|
||||
DB_PASSWORD=J5brHrAXFLQSif0K
|
||||
@ -15,6 +16,9 @@ NAPALM_TIMEOUT=10
|
||||
MAX_PAGE_SIZE=1000
|
||||
REDIS_HOST=redis
|
||||
REDIS_PASSWORD=H733Kdjndks81
|
||||
REDIS_DATABASE=0
|
||||
REDIS_CACHE_DATABASE=1
|
||||
REDIS_SSL=false
|
||||
SECRET_KEY=r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj
|
||||
SUPERUSER_NAME=admin
|
||||
SUPERUSER_EMAIL=admin@example.com
|
||||
|
6
initializers/aggregates.yml
Normal file
6
initializers/aggregates.yml
Normal file
@ -0,0 +1,6 @@
|
||||
# - prefix: 10.0.0.0/16
|
||||
# rir: RFC1918
|
||||
# - prefix: fd00:ccdd::/32
|
||||
# rir: RFC4193 ULA
|
||||
# - prefix: 2001:db8::/32
|
||||
# rir: RFC3849
|
2
initializers/cluster_types.yml
Normal file
2
initializers/cluster_types.yml
Normal file
@ -0,0 +1,2 @@
|
||||
# - name: Hyper-V
|
||||
# slug: hyper-v
|
5
initializers/clusters.yml
Normal file
5
initializers/clusters.yml
Normal file
@ -0,0 +1,5 @@
|
||||
# - name: cluster1
|
||||
# type: Hyper-V
|
||||
# - name: cluster2
|
||||
# type: Hyper-V
|
||||
# site: SING 1
|
8
initializers/dcim_interfaces.yml
Normal file
8
initializers/dcim_interfaces.yml
Normal file
@ -0,0 +1,8 @@
|
||||
# - device: server01
|
||||
# enabled: true
|
||||
# type: Virtual
|
||||
# name: to-server02
|
||||
# - device: server02
|
||||
# enabled: true
|
||||
# type: Virtual
|
||||
# name: to-server01
|
@ -17,7 +17,7 @@
|
||||
# custom_fields:
|
||||
# text_field: Description
|
||||
# - model: Other
|
||||
# manufacturer: NoName
|
||||
# manufacturer: No Name
|
||||
# slug: other
|
||||
# custom_fields:
|
||||
# text_field: Description
|
||||
|
26
initializers/ip_addresses.yml
Normal file
26
initializers/ip_addresses.yml
Normal file
@ -0,0 +1,26 @@
|
||||
# - address: 10.1.1.1/24
|
||||
# device: server01
|
||||
# interface: to-server02
|
||||
# status: Active
|
||||
# vrf: vrf1
|
||||
# - address: 2001:db8:a000:1::1/64
|
||||
# device: server01
|
||||
# interface: to-server02
|
||||
# status: Active
|
||||
# vrf: vrf1
|
||||
# - address: 10.1.1.2/24
|
||||
# device: server02
|
||||
# interface: to-server01
|
||||
# status: Active
|
||||
# - address: 2001:db8:a000:1::2/64
|
||||
# device: server02
|
||||
# interface: to-server01
|
||||
# status: Active
|
||||
# - address: 10.1.1.10/24
|
||||
# description: reserved IP
|
||||
# status: Reserved
|
||||
# tenant: tenant1
|
||||
# - address: 2001:db8:a000:1::10/64
|
||||
# description: reserved IP
|
||||
# status: Reserved
|
||||
# tenant: tenant1
|
@ -2,5 +2,5 @@
|
||||
# slug: manufacturer-1
|
||||
# - name: Manufacturer 2
|
||||
# slug: manufacturer-2
|
||||
# - name: NoName
|
||||
# slug: noname
|
||||
# - name: No Name
|
||||
# slug: no-name
|
||||
|
@ -1,19 +1,15 @@
|
||||
# # Allowed rpc clients are: juniper-junos, cisco-ios, opengear
|
||||
# - name: Platform 1
|
||||
# slug: platform-1
|
||||
# manufacturer: Manufacturer 1
|
||||
# napalm_driver: driver1
|
||||
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
|
||||
# rpc_client: juniper-junos
|
||||
# - name: Platform 2
|
||||
# slug: platform-2
|
||||
# manufacturer: Manufacturer 2
|
||||
# napalm_driver: driver2
|
||||
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
|
||||
# rpc_client: opengear
|
||||
# - name: Platform 3
|
||||
# slug: platform-3
|
||||
# manufacturer: NoName
|
||||
# manufacturer: No Name
|
||||
# napalm_driver: driver3
|
||||
# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}"
|
||||
# rpc_client: juniper-junos
|
||||
|
2
initializers/prefix_vlan_roles.yml
Normal file
2
initializers/prefix_vlan_roles.yml
Normal file
@ -0,0 +1,2 @@
|
||||
# - name: Main Management
|
||||
# slug: main-management
|
20
initializers/prefixes.yml
Normal file
20
initializers/prefixes.yml
Normal file
@ -0,0 +1,20 @@
|
||||
# - description: prefix1
|
||||
# prefix: 10.1.1.0/24
|
||||
# site: AMS 1
|
||||
# status: Active
|
||||
# tenant: tenant1
|
||||
# vlan: vlan1
|
||||
# - description: prefix2
|
||||
# prefix: 10.1.2.0/24
|
||||
# site: AMS 2
|
||||
# status: Active
|
||||
# tenant: tenant2
|
||||
# vlan: vlan2
|
||||
# is_pool: true
|
||||
# vrf: vrf2
|
||||
# - description: ipv6 prefix1
|
||||
# prefix: 2001:db8:a000:1::/64
|
||||
# site: AMS 2
|
||||
# status: Active
|
||||
# tenant: tenant2
|
||||
# vlan: vlan2
|
9
initializers/rirs.yml
Normal file
9
initializers/rirs.yml
Normal file
@ -0,0 +1,9 @@
|
||||
# - is_private: true
|
||||
# name: RFC1918
|
||||
# slug: rfc1918
|
||||
# - is_private: true
|
||||
# name: RFC4193 ULA
|
||||
# slug: rfc4193-ula
|
||||
# - is_private: true
|
||||
# name: RFC3849
|
||||
# slug: rfc3849
|
4
initializers/tenant_groups.yml
Normal file
4
initializers/tenant_groups.yml
Normal file
@ -0,0 +1,4 @@
|
||||
# - name: Tenant Group 1
|
||||
# slug: tenant-group-1
|
||||
# - name: Tenant Group 2
|
||||
# slug: tenant-group-2
|
5
initializers/tenants.yml
Normal file
5
initializers/tenants.yml
Normal file
@ -0,0 +1,5 @@
|
||||
# - name: tenant1
|
||||
# slug: tenant1
|
||||
# - name: tenant2
|
||||
# slug: tenant2
|
||||
# group: Tenant Group 2
|
18
initializers/virtual_machines.yml
Normal file
18
initializers/virtual_machines.yml
Normal file
@ -0,0 +1,18 @@
|
||||
# - cluster: cluster1
|
||||
# comments: VM1
|
||||
# disk: 200
|
||||
# memory: 4096
|
||||
# name: virtual machine 1
|
||||
# platform: Platform 2
|
||||
# status: Active
|
||||
# tenant: tenant1
|
||||
# vcpus: 8
|
||||
# - cluster: cluster1
|
||||
# comments: VM2
|
||||
# disk: 100
|
||||
# memory: 2048
|
||||
# name: virtual machine 2
|
||||
# platform: Platform 2
|
||||
# status: Active
|
||||
# tenant: tenant1
|
||||
# vcpus: 8
|
12
initializers/virtualization_interfaces.yml
Normal file
12
initializers/virtualization_interfaces.yml
Normal file
@ -0,0 +1,12 @@
|
||||
# - description: Network Interface 1
|
||||
# enabled: true
|
||||
# mac_address: 00:77:77:77:77:77
|
||||
# mtu: 1500
|
||||
# name: Network Interface 1
|
||||
# virtual_machine: virtual machine 1
|
||||
# - description: Network Interface 2
|
||||
# enabled: true
|
||||
# mac_address: 00:55:55:55:55:55
|
||||
# mtu: 1500
|
||||
# name: Network Interface 2
|
||||
# virtual_machine: virtual machine 1
|
6
initializers/vlan_groups.yml
Normal file
6
initializers/vlan_groups.yml
Normal file
@ -0,0 +1,6 @@
|
||||
# - name: VLAN group 1
|
||||
# site: AMS 1
|
||||
# slug: vlan-group-1
|
||||
# - name: VLAN group 2
|
||||
# site: AMS 1
|
||||
# slug: vlan-group-2
|
11
initializers/vlans.yml
Normal file
11
initializers/vlans.yml
Normal file
@ -0,0 +1,11 @@
|
||||
# - name: vlan1
|
||||
# site: AMS 1
|
||||
# status: Active
|
||||
# vid: 5
|
||||
# role: Main Management
|
||||
# description: VLAN 5 for MGMT
|
||||
# - group: VLAN group 2
|
||||
# name: vlan2
|
||||
# site: AMS 1
|
||||
# status: Active
|
||||
# vid: 1300
|
8
initializers/vrfs.yml
Normal file
8
initializers/vrfs.yml
Normal file
@ -0,0 +1,8 @@
|
||||
# - enforce_unique: true
|
||||
# name: vrf1
|
||||
# tenant: tenant1
|
||||
# description: main VRF
|
||||
# - enforce_unique: true
|
||||
# name: vrf2
|
||||
# rd: "6500:6500"
|
||||
# tenant: tenant2
|
@ -29,6 +29,6 @@ with file.open('r') as stream:
|
||||
if user_permissions:
|
||||
user.user_permissions.clear()
|
||||
for permission_codename in user_details.get('permissions', []):
|
||||
permission = Permission.objects.get(codename=permission_codename)
|
||||
user.user_permissions.add(permission)
|
||||
for permission in Permission.objects.filter(codename=permission_codename):
|
||||
user.user_permissions.add(permission)
|
||||
user.save()
|
||||
|
@ -27,7 +27,6 @@ with file.open('r') as stream:
|
||||
group_permissions = group_details.get('permissions', [])
|
||||
if group_permissions:
|
||||
group.permissions.clear()
|
||||
print("Permissions:", group.permissions.all())
|
||||
for permission_codename in group_details.get('permissions', []):
|
||||
permission = Permission.objects.get(codename=permission_codename)
|
||||
group.permissions.add(permission)
|
||||
for permission in Permission.objects.filter(codename=permission_codename):
|
||||
group.permissions.add(permission)
|
||||
|
19
startup_scripts/110_tenant_groups.py
Normal file
19
startup_scripts/110_tenant_groups.py
Normal file
@ -0,0 +1,19 @@
|
||||
from tenancy.models import TenantGroup
|
||||
from ruamel.yaml import YAML
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/tenant_groups.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
tenant_groups = yaml.load(stream)
|
||||
|
||||
if tenant_groups is not None:
|
||||
for params in tenant_groups:
|
||||
tenant_group, created = TenantGroup.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
print("🔳 Created Tenant Group", tenant_group.name)
|
45
startup_scripts/120_tenants.py
Normal file
45
startup_scripts/120_tenants.py
Normal file
@ -0,0 +1,45 @@
|
||||
from tenancy.models import Tenant, TenantGroup
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/tenants.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
tenants = yaml.load(stream)
|
||||
|
||||
optional_assocs = {
|
||||
'group': (TenantGroup, 'name')
|
||||
}
|
||||
|
||||
if tenants is not None:
|
||||
for params in tenants:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
tenant, created = Tenant.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=tenant,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
tenant.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("👩💻 Created Tenant", tenant.name)
|
@ -53,6 +53,7 @@ with file.open('r') as stream:
|
||||
for rack_face in RACK_FACE_CHOICES:
|
||||
if params['face'] in rack_face:
|
||||
params['face'] = rack_face[0]
|
||||
break
|
||||
|
||||
device, created = Device.objects.get_or_create(**params)
|
||||
|
19
startup_scripts/140_cluster_types.py
Normal file
19
startup_scripts/140_cluster_types.py
Normal file
@ -0,0 +1,19 @@
|
||||
from virtualization.models import ClusterType
|
||||
from ruamel.yaml import YAML
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/cluster_types.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
cluster_types = yaml.load(stream)
|
||||
|
||||
if cluster_types is not None:
|
||||
for params in cluster_types:
|
||||
cluster_type, created = ClusterType.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
print("🧰 Created Cluster Type", cluster_type.name)
|
19
startup_scripts/150_rirs.py
Normal file
19
startup_scripts/150_rirs.py
Normal file
@ -0,0 +1,19 @@
|
||||
from ipam.models import RIR
|
||||
from ruamel.yaml import YAML
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/rirs.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
rirs = yaml.load(stream)
|
||||
|
||||
if rirs is not None:
|
||||
for params in rirs:
|
||||
rir, created = RIR.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
print("🗺️ Created RIR", rir.name)
|
46
startup_scripts/160_aggregates.py
Normal file
46
startup_scripts/160_aggregates.py
Normal file
@ -0,0 +1,46 @@
|
||||
from ipam.models import Aggregate, RIR
|
||||
from ruamel.yaml import YAML
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
|
||||
from netaddr import IPNetwork
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/aggregates.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
aggregates = yaml.load(stream)
|
||||
|
||||
required_assocs = {
|
||||
'rir': (RIR, 'name')
|
||||
}
|
||||
|
||||
if aggregates is not None:
|
||||
for params in aggregates:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
params['prefix'] = IPNetwork(params['prefix'])
|
||||
|
||||
for assoc, details in required_assocs.items():
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
aggregate, created = Aggregate.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=aggregate,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
aggregate.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("🗞️ Created Aggregate", aggregate.prefix)
|
57
startup_scripts/170_clusters.py
Normal file
57
startup_scripts/170_clusters.py
Normal file
@ -0,0 +1,57 @@
|
||||
from dcim.models import Site
|
||||
from virtualization.models import Cluster, ClusterType, ClusterGroup
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/clusters.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
clusters = yaml.load(stream)
|
||||
|
||||
required_assocs = {
|
||||
'type': (ClusterType, 'name')
|
||||
}
|
||||
|
||||
optional_assocs = {
|
||||
'site': (Site, 'name'),
|
||||
'group': (ClusterGroup, 'name')
|
||||
}
|
||||
|
||||
if clusters is not None:
|
||||
for params in clusters:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
|
||||
for assoc, details in required_assocs.items():
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
cluster, created = Cluster.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=cluster,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
cluster.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("🗄️ Created cluster", cluster.name)
|
46
startup_scripts/180_vrfs.py
Normal file
46
startup_scripts/180_vrfs.py
Normal file
@ -0,0 +1,46 @@
|
||||
from ipam.models import VRF
|
||||
from tenancy.models import Tenant
|
||||
from ruamel.yaml import YAML
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/vrfs.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
vrfs = yaml.load(stream)
|
||||
|
||||
optional_assocs = {
|
||||
'tenant': (Tenant, 'name')
|
||||
}
|
||||
|
||||
if vrfs is not None:
|
||||
for params in vrfs:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
vrf, created = VRF.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=vrf,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
vrf.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("📦 Created VRF", vrf.name)
|
19
startup_scripts/190_prefix_vlan_roles.py
Normal file
19
startup_scripts/190_prefix_vlan_roles.py
Normal file
@ -0,0 +1,19 @@
|
||||
from ipam.models import Role
|
||||
from ruamel.yaml import YAML
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/prefix_vlan_roles.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
roles = yaml.load(stream)
|
||||
|
||||
if roles is not None:
|
||||
for params in roles:
|
||||
role, created = Role.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
print("⛹️ Created Prefix/VLAN Role", role.name)
|
46
startup_scripts/200_vlan_groups.py
Normal file
46
startup_scripts/200_vlan_groups.py
Normal file
@ -0,0 +1,46 @@
|
||||
from dcim.models import Site
|
||||
from ipam.models import VLANGroup
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/vlan_groups.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
vlan_groups = yaml.load(stream)
|
||||
|
||||
optional_assocs = {
|
||||
'site': (Site, 'name')
|
||||
}
|
||||
|
||||
if vlan_groups is not None:
|
||||
for params in vlan_groups:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
vlan_group, created = VLANGroup.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=vlan_group,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
vlan_group.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("🏘️ Created VLAN Group", vlan_group.name)
|
58
startup_scripts/210_vlans.py
Normal file
58
startup_scripts/210_vlans.py
Normal file
@ -0,0 +1,58 @@
|
||||
from dcim.models import Site
|
||||
from ipam.models import VLAN, VLANGroup, Role
|
||||
from ipam.constants import VLAN_STATUS_CHOICES
|
||||
from tenancy.models import Tenant, TenantGroup
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/vlans.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
vlans = yaml.load(stream)
|
||||
|
||||
optional_assocs = {
|
||||
'site': (Site, 'name'),
|
||||
'tenant': (Tenant, 'name'),
|
||||
'tenant_group': (TenantGroup, 'name'),
|
||||
'group': (VLANGroup, 'name'),
|
||||
'role': (Role, 'name')
|
||||
}
|
||||
|
||||
if vlans is not None:
|
||||
for params in vlans:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
if 'status' in params:
|
||||
for vlan_status in VLAN_STATUS_CHOICES:
|
||||
if params['status'] in vlan_status:
|
||||
params['status'] = vlan_status[0]
|
||||
break
|
||||
|
||||
vlan, created = VLAN.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=vlan,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
vlan.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("🏠 Created VLAN", vlan.name)
|
61
startup_scripts/220_prefixes.py
Normal file
61
startup_scripts/220_prefixes.py
Normal file
@ -0,0 +1,61 @@
|
||||
from dcim.models import Site
|
||||
from ipam.models import Prefix, VLAN, Role, VRF
|
||||
from ipam.constants import PREFIX_STATUS_CHOICES
|
||||
from tenancy.models import Tenant, TenantGroup
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from netaddr import IPNetwork
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/prefixes.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
prefixes = yaml.load(stream)
|
||||
|
||||
optional_assocs = {
|
||||
'site': (Site, 'name'),
|
||||
'tenant': (Tenant, 'name'),
|
||||
'tenant_group': (TenantGroup, 'name'),
|
||||
'vlan': (VLAN, 'name'),
|
||||
'role': (Role, 'name'),
|
||||
'vrf': (VRF, 'name')
|
||||
}
|
||||
|
||||
if prefixes is not None:
|
||||
for params in prefixes:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
params['prefix'] = IPNetwork(params['prefix'])
|
||||
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
if 'status' in params:
|
||||
for prefix_status in PREFIX_STATUS_CHOICES:
|
||||
if params['status'] in prefix_status:
|
||||
params['status'] = prefix_status[0]
|
||||
break
|
||||
|
||||
prefix, created = Prefix.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=prefix,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
prefix.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("📌 Created Prefix", prefix.prefix)
|
66
startup_scripts/230_virtual_machines.py
Normal file
66
startup_scripts/230_virtual_machines.py
Normal file
@ -0,0 +1,66 @@
|
||||
from dcim.models import Site, Platform, DeviceRole
|
||||
from virtualization.models import Cluster, VirtualMachine
|
||||
from virtualization.constants import VM_STATUS_CHOICES
|
||||
from tenancy.models import Tenant
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/virtual_machines.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
virtual_machines = yaml.load(stream)
|
||||
|
||||
required_assocs = {
|
||||
'cluster': (Cluster, 'name')
|
||||
}
|
||||
|
||||
optional_assocs = {
|
||||
'tenant': (Tenant, 'name'),
|
||||
'platform': (Platform, 'name'),
|
||||
'role': (DeviceRole, 'name')
|
||||
}
|
||||
|
||||
if virtual_machines is not None:
|
||||
for params in virtual_machines:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
|
||||
for assoc, details in required_assocs.items():
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
if 'status' in params:
|
||||
for vm_status in VM_STATUS_CHOICES:
|
||||
if params['status'] in vm_status:
|
||||
params['status'] = vm_status[0]
|
||||
break
|
||||
|
||||
virtual_machine, created = VirtualMachine.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=virtual_machine,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
virtual_machine.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("🖥️ Created virtual machine", virtual_machine.name)
|
45
startup_scripts/240_virtualization_interfaces.py
Normal file
45
startup_scripts/240_virtualization_interfaces.py
Normal file
@ -0,0 +1,45 @@
|
||||
from dcim.models import Interface
|
||||
from virtualization.models import VirtualMachine
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/virtualization_interfaces.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
interfaces = yaml.load(stream)
|
||||
|
||||
required_assocs = {
|
||||
'virtual_machine': (VirtualMachine, 'name')
|
||||
}
|
||||
|
||||
if interfaces is not None:
|
||||
for params in interfaces:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
|
||||
for assoc, details in required_assocs.items():
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
interface, created = Interface.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=interface,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
interface.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("🧷 Created interface", interface.name, interface.virtual_machine.name)
|
55
startup_scripts/250_dcim_interfaces.py
Normal file
55
startup_scripts/250_dcim_interfaces.py
Normal file
@ -0,0 +1,55 @@
|
||||
from dcim.models import Interface, Device
|
||||
from dcim.constants import IFACE_TYPE_CHOICES
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/dcim_interfaces.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
interfaces = yaml.load(stream)
|
||||
|
||||
required_assocs = {
|
||||
'device': (Device, 'name')
|
||||
}
|
||||
|
||||
if interfaces is not None:
|
||||
for params in interfaces:
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
|
||||
for assoc, details in required_assocs.items():
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
if 'type' in params:
|
||||
for outer_list in IFACE_TYPE_CHOICES:
|
||||
for type_choices in outer_list[1]:
|
||||
if params['type'] in type_choices:
|
||||
params['type'] = type_choices[0]
|
||||
break
|
||||
else:
|
||||
continue
|
||||
break
|
||||
|
||||
interface, created = Interface.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=interface,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
interface.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("🧷 Created interface", interface.name, interface.device.name)
|
72
startup_scripts/260_ip_addresses.py
Normal file
72
startup_scripts/260_ip_addresses.py
Normal file
@ -0,0 +1,72 @@
|
||||
from ipam.models import IPAddress, VRF
|
||||
from ipam.constants import IPADDRESS_STATUS_CHOICES
|
||||
from dcim.models import Device, Interface
|
||||
from virtualization.models import VirtualMachine
|
||||
from tenancy.models import Tenant
|
||||
from extras.models import CustomField, CustomFieldValue
|
||||
from ruamel.yaml import YAML
|
||||
|
||||
from netaddr import IPNetwork
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/ip_addresses.yml')
|
||||
if not file.is_file():
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml = YAML(typ='safe')
|
||||
ip_addresses = yaml.load(stream)
|
||||
|
||||
optional_assocs = {
|
||||
'tenant': (Tenant, 'name'),
|
||||
'vrf': (VRF, 'name'),
|
||||
'interface': (Interface, 'name')
|
||||
}
|
||||
|
||||
if ip_addresses is not None:
|
||||
for params in ip_addresses:
|
||||
vm = params.pop('virtual_machine', None)
|
||||
device = params.pop('device', None)
|
||||
custom_fields = params.pop('custom_fields', None)
|
||||
params['address'] = IPNetwork(params['address'])
|
||||
|
||||
if vm and device:
|
||||
print("IP Address can only specify one of the following: virtual_machine or device.")
|
||||
sys.exit()
|
||||
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
if assoc == 'interface':
|
||||
if vm:
|
||||
vm_id = VirtualMachine.objects.get(name=vm).id
|
||||
query = { field: params.pop(assoc), "virtual_machine_id": vm_id }
|
||||
elif device:
|
||||
dev_id = Device.objects.get(name=device).id
|
||||
query = { field: params.pop(assoc), "device_id": dev_id }
|
||||
else:
|
||||
query = { field: params.pop(assoc) }
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
if 'status' in params:
|
||||
for ip_status in IPADDRESS_STATUS_CHOICES:
|
||||
if params['status'] in ip_status:
|
||||
params['status'] = ip_status[0]
|
||||
break
|
||||
|
||||
ip_address, created = IPAddress.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
if custom_fields is not None:
|
||||
for cf_name, cf_value in custom_fields.items():
|
||||
custom_field = CustomField.objects.get(name=cf_name)
|
||||
custom_field_value = CustomFieldValue.objects.create(
|
||||
field=custom_field,
|
||||
obj=ip_address,
|
||||
value=cf_value
|
||||
)
|
||||
|
||||
ip_address.custom_field_values.add(custom_field_value)
|
||||
|
||||
print("🧬 Created IP Address", ip_address.address)
|
Reference in New Issue
Block a user