-
-
Save candlerb/195ecd0fcc6aca6f7218d6e312dd0ee1 to your computer and use it in GitHub Desktop.
#!/opt/netbox/venv/bin/python | |
import django | |
import os | |
import sys | |
sys.path.append('/opt/netbox/netbox') | |
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'netbox.settings') | |
django.setup() | |
from dcim.models import Device, ConsolePort, ConsoleServerPort, PowerPort, PowerOutlet, Interface, RearPort, FrontPort, DeviceBay | |
from django.db import transaction | |
transaction.set_autocommit(False) | |
for device in Device.objects.all(): | |
# Based on Device.save() | |
ConsolePort.objects.bulk_create( | |
[ConsolePort(device=device, name=template.name) for template in | |
device.device_type.consoleporttemplates.all() | |
if template.name not in {i.name for i in device.consoleports.all()}] | |
) | |
ConsoleServerPort.objects.bulk_create( | |
[ConsoleServerPort(device=device, name=template.name) for template in | |
device.device_type.consoleserverporttemplates.all() | |
if template.name not in {i.name for i in device.consoleserverports.all()}] | |
) | |
PowerPort.objects.bulk_create( | |
[PowerPort(device=device, name=template.name) for template in | |
device.device_type.powerporttemplates.all() | |
if template.name not in {i.name for i in device.powerports.all()}] | |
) | |
PowerOutlet.objects.bulk_create( | |
[PowerOutlet(device=device, name=template.name) for template in | |
device.device_type.poweroutlettemplates.all() | |
if template.name not in {i.name for i in device.poweroutlets.all()}] | |
) | |
Interface.objects.bulk_create( | |
[Interface(device=device, name=template.name, type=template.type, | |
mgmt_only=template.mgmt_only) for template in device.device_type.interfacetemplates.all() | |
if template.name not in {i.name for i in device.interfaces.all()}] | |
) | |
RearPort.objects.bulk_create([ | |
RearPort( | |
device=device, | |
name=template.name, | |
type=template.type, | |
positions=template.positions | |
) for template in device.device_type.rearporttemplates.all() | |
if template.name not in {i.name for i in device.rearports.all()} | |
]) | |
FrontPort.objects.bulk_create([ | |
FrontPort( | |
device=device, | |
name=template.name, | |
type=template.type, | |
rear_port=RearPort.objects.get(device=device, name=template.rear_port.name), | |
rear_port_position=template.rear_port_position, | |
) for template in device.device_type.frontporttemplates.all() | |
if template.name not in {i.name for i in device.frontports.all()} | |
]) | |
DeviceBay.objects.bulk_create( | |
[DeviceBay(device=device, name=template.name) for template in | |
device.device_type.devicebaytemplates.all() | |
if template.name not in {i.name for i in device.devicebays.all()}] | |
) | |
#transaction.rollback() | |
transaction.commit() |
Also, try running it like this:
PYTHONPATH=/opt/netbox/netbox /opt/netbox/venv/bin/python add-device-ports.py
(it might be that the wrong version of python3 has been picked up)
EDIT: I've adjusted the script so it can be run directly with chmod +x add-device-ports.py
then ./add-device-ports.py
Ah, i see...
So you run the script from CLI, ok.
From the CLI -the script is running and gets the job done.
But i can't place it in /scripts folder, can I?
Because when i do so, when i press Scripts in the GUI, the script is instantly executed (i checked twice), and yet after that GUI shows "No script found".
This script is not indented to be used from GUI, am i right?
Correct, this has not been modified to be a Netbox "custom script" which can be directly integrated into the web UI.
Correct, this has not been modified to be a Netbox "custom script" which can be directly integrated into the web UI.
OK. Great script tho. Saved me from a lot of hassle.
Do you plan to write one for UI? Would be great.
Yes, this is done, and contributed to the public "reports" repository. See:
- https://github.com/netbox-community/reports/blob/master/scripts/add_device_type_components.py
- https://github.com/netbox-community/reports/blob/master/reports/dcim-reports/missing_device_type_components.py
(for safety the update script asks you to select the device(s) to update, rather than blindly updating all devices)
Firstly, thank you for creating this script. I know it's a few years old, but I'm hoping that it's still expected to work. I'm using the one from https://github.com/netbox-community/reports/blob/master/scripts/add_device_type_components.py
When I use it to update some Arista switches. I originally created manually without all their components, since then I've imported a devcie style that has everything, when swapping the device type, as we know, the components don't auto add. Then I came across your script, I get this error, do you know ho wI can fix it please?
The data model for module bays was changed in Netbox v4.1.0, and now includes a "level" hierarchy parameter. I proposed (but didn't test) a change here:
netbox-community/netbox#8189 (reply in thread)
That may be a starting point for working on a fix. (I don't use module bays myself)
Thanks for the prompt response,
I also don't use module bays normally, when I went and looked at the device, it had 2 module bays for the PSU, so I deleted the module bays and added 2 power ports, this stopped the error, but unfortunately it doesn't import the Power Ports or the console connections that are on the device. The network interfaces are added, just nothing else. Assuming theres more changes in 4 that the script doesn't know about yet?
It should handle power ports and console ports from the device type. Are you using the latest version of this script?
The latest version is in git:
https://github.com/netbox-community/customizations/blob/master/scripts/add_device_type_components.py
But there is an outstanding pull request which hasn't been applied:
netbox-community/customizations#101
Yes, using latest version from there. There are no errors, it just doesn't add the ports for some reason. No debug error, so I can't say why it's not doing it unfortunately. Just added your updated save part instead of the bulk code, same thing, doesn't add the console or the power ports unfortunately. admittedly I'm more interested in the network interfaces, so I'm over the moon that that's worked, I can't see why the other ports don't add though.
@theAmberLion: can you show the full backtrace? In configuration.py either set EMAIL and ADMINS (in which case it will be emailed to you) or set DEBUG=True and copy-paste from the screen (as text).
I can't see how
device
can be undefined anywhere inside the for loop.