Updates to scripts after refactor
- Run black to reformat all the scripts
- Update makefile test targets, pylint, and fix some of the issues found
- Update pxeconfig script for refactored nbhelper
- Add start of inventory script
Change-Id: I5f426ac2da840dc72f07f8a6844e199e47d49135
diff --git a/scripts/nbhelper/device.py b/scripts/nbhelper/device.py
index 94b03df..32a2075 100644
--- a/scripts/nbhelper/device.py
+++ b/scripts/nbhelper/device.py
@@ -4,12 +4,11 @@
# SPDX-License-Identifier: Apache-2.0
# device.py
-#
+import sys
import netaddr
-from .utils import logger, clean_name_dns
-from .network import Prefix
+from .utils import logger
from .container import DeviceContainer, VirtualMachineContainer, PrefixContainer
@@ -40,7 +39,7 @@
objects = dict()
def __init__(self, data):
- from .utils import netboxapi, netbox_config
+ from .utils import netboxapi
self.data = data
self.nbapi = netboxapi
@@ -49,6 +48,7 @@
self.id = self.data.id
self.tenant = None
self.primary_ip = None
+ self.primary_iface = None
# In Netbox, we use FQDN as the Device name, but in the script,
# we use the first segment to be the name of device.
@@ -108,8 +108,12 @@
# ipam.ip_addresses doesn't have primary tag,
# the primary tag is only available is only in the Device.
# So we need to compare address to check which one is primary ip
- if address.address == self.primary_ip.address:
- interface["isPrimary"] = True
+ try:
+ if address.address == self.primary_ip.address:
+ interface["isPrimary"] = True
+ self.primary_iface = interface
+ except AttributeError:
+ logger.error("Error with primary address for device %s", self.fullname)
# mgmt_only = False is a hack for VirtualMachine type
if self.__class__ == VirtualMachine:
@@ -181,7 +185,7 @@
self.netplan_config["ethernets"].setdefault(intfName, {})
self.netplan_config["ethernets"][intfName].setdefault(
"addresses", []
- ).append(address)
+ ).extend(interface["addresses"])
# If the current selected device is a Server
elif isinstance(self, Device) and self.data.device_role.name == "Server":
@@ -233,7 +237,9 @@
for dest_addr in destination.split(","):
# If interface address is in destination subnet, we don't need this route
- if netaddr.IPNetwork(address).ip in netaddr.IPNetwork(dest_addr):
+ if netaddr.IPNetwork(address).ip in netaddr.IPNetwork(
+ dest_addr
+ ):
continue
new_route = {
@@ -298,7 +304,8 @@
}
)
- # Only management server needs to be configured the whitelist netrange of internal interface
+ # Only management server needs to be configured the whitelist netrange of
+ # internal interface
if self.data.device_role.name == "Router":
ret["interface_subnets"] = dict()
@@ -329,8 +336,13 @@
if prefix.subnet not in ret["interface_subnets"][intfName]:
ret["interface_subnets"][intfName].append(prefix.subnet)
for neighbor in prefix.neighbor:
- if neighbor.subnet not in ret["interface_subnets"][intfName]:
- ret["interface_subnets"][intfName].append(neighbor.subnet)
+ if (
+ neighbor.subnet
+ not in ret["interface_subnets"][intfName]
+ ):
+ ret["interface_subnets"][intfName].append(
+ neighbor.subnet
+ )
for prefix in PrefixContainer().all():
@@ -361,8 +373,6 @@
if self.extra_config:
return self.extra_config
- primary_ip = self.data.primary_ip.address if self.data.primary_ip else None
-
service_names = list(map(lambda x: x.name, self.services))
if "dns" in service_names: