From 818266ace1449183aefd06377b8bbb7285b4a916 Mon Sep 17 00:00:00 2001 From: ryanmerolle Date: Wed, 30 Dec 2020 19:10:46 -0500 Subject: [PATCH] added services startup_script --- initializers/services.yml | 10 ++++++++++ startup_scripts/320_services.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 initializers/services.yml create mode 100644 startup_scripts/320_services.py diff --git a/initializers/services.yml b/initializers/services.yml new file mode 100644 index 0000000..49b081c --- /dev/null +++ b/initializers/services.yml @@ -0,0 +1,10 @@ +# - name: SSH +# protocol: TCP +# ports: +# - 22 +# virtual_machine: virtual machine 1 +# - name: FTP +# protocol: TCP +# ports: +# - 21 +# device: server01 diff --git a/startup_scripts/320_services.py b/startup_scripts/320_services.py new file mode 100644 index 0000000..800f215 --- /dev/null +++ b/startup_scripts/320_services.py @@ -0,0 +1,29 @@ +from ipam.models import Service +from dcim.models import Device +from virtualization.models import VirtualMachine +from startup_script_utils import load_yaml +import sys + +services = load_yaml('/opt/netbox/initializers/services.yml') + +if services is None: + sys.exit() + +optional_assocs = { + 'device': (Device, 'name'), + 'virtual_machine': (VirtualMachine, 'name') +} + +for params in services: + + 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) + + service, created = Service.objects.get_or_create(**params) + + if created: + print("🧰 Created Service", service.name)