From 40a12b24e705d7be011de81d0e215935e3628ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=A4der?= Date: Wed, 9 Oct 2019 12:21:11 +0200 Subject: [PATCH] Copied over the relevant parts from the README --- Custom-Startup-Scripts.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Custom-Startup-Scripts.md diff --git a/Custom-Startup-Scripts.md b/Custom-Startup-Scripts.md new file mode 100644 index 0000000..f246fe1 --- /dev/null +++ b/Custom-Startup-Scripts.md @@ -0,0 +1,29 @@ +When using `docker-compose`, all the python scripts present in `/opt/netbox/startup_scripts` will automatically be executed in the context of `./manage.py` after the application boots. + +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) +``` + +## Disable Startup Scripts + +The execution of the startup scripts can be prevented by setting the environment variable `SKIP_STARTUP_SCRIPTS` to `true`, e.g. in the file `env/netbox.env`.