Add a startup script to automate cabling process (#42)

* 📌 Update cacheops, filter, and Django consumption

* add cabling script

* clean up

* fix typo

* fix typo

* fix typo

* add cable initializer

* set to false by default

Co-authored-by: KylerBurke <Kontazler@gmail.com>
This commit is contained in:
Hoanh An 2020-08-10 15:49:07 -04:00 committed by GitHub
parent f7ffcb0c12
commit 32ff0aef47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1 @@
cable_interfaces: false

View File

@ -0,0 +1,50 @@
"""
Automate cabling process so that all Customer Network Locker's 1-2 interfaces go to Access Switch's EFR1, and 3-4 ones go to EFR2.
This script should only be run once and in dev environment. Mostly used for integration testing purposes.
"""
from dcim.models import Cable
from ruamel.yaml import YAML
from pathlib import Path
import sys
def get_devices(role):
from dcim.models import Device, DeviceRole
return Device.objects.all().filter(device_role=DeviceRole.objects.get(slug=role).id)
file = Path('/opt/netbox/initializers/cables.yml')
if not file.is_file():
sys.exit()
with file.open('r') as stream:
yaml = YAML(typ='safe')
config = yaml.load(stream)
if config.get('cable_interfaces'):
lockers = get_devices('customer-network-locker')
switches = get_devices('access-switch')
i = 0
for locker in lockers:
# break if i is larger than switch interface's size.
if i > len(switches[0].vc_interfaces)-1:
break
i12 = locker.vc_interfaces[0]
i34 = locker.vc_interfaces[1]
efr1 = switches[0].vc_interfaces[i]
efr2 = switches[1].vc_interfaces[i]
c1 = Cable.objects.create(termination_a=i12, termination_b=efr1)
print('🔌 Created cable for {} and {}'.format(i12.name, efr1.name))
c2 = Cable.objects.create(termination_a=i34, termination_b=efr2)
print('🔌 Created cable for {} and {}'.format(i34.name, efr2.name))
i += 1