No module named "extras.reports" when i run a script #19069
-
Hi, Is it a known bug? i could not find nothing in the issues. Should i open a new bug? Here is my script class DHCPExporter(Script):
class Meta:
name = "DHCP Hosts Reservation Exporter"
description = "This custom script can export info abuto host reservations for ISC KEA DHCP server"
commit_default = True
export_path = StringVar(
description="Base path for the dhcp file export. The exporter will create a subdirectory 'netbox-dhcp-exporter' below this path",
default="/opt/netbox/shared/exports",
)
default_view_name = StringVar(
description="Default view name for zones without a view",
default="_default",
)
remove_existing_data = BooleanVar(
description="Clean up existing data before exporting",
default=True,
)
dhcp_template = '''\
"hostname","mac_address","ipv4_address","dhcp4_subnet_id"
{% for record in records -%}
"{{ record.name }}","{{ record.ipam_ip_address.assigned_object.mac_address }}","{{ record.ip_address }}", {{ zone.custom_field_data['dhcp4_subnet_id'] }}
{% endfor %}\
'''
jinja_env = Environment(loader=DictLoader({"dhcp_file": dhcp_template}), autoescape=True)
template = jinja_env.get_template("dhcp_file")
def run(self, data, commit):
views = View.objects.all()
export_path = Path(data["export_path"]) / "netbox-dhcp-exporter"
if data["remove_existing_data"] and export_path.exists():
self.log_info(f"Deleting the old export path {export_path}")
try:
rm_tree(export_path)
except OSError as exc:
self.log_failure(f"Could not remove the old export tree: {exc}")
return
try:
export_path.mkdir(parents=False, exist_ok=True)
except OSError as exc:
self.log_failure(f"Could not create the export path {exc}")
return
self.log_info(f"Trovate views nr: '{len(views)}'")
for view in views:
zones = Zone.objects.filter(view=view, status="active")
if len(zones):
self.log_info(f"Exporting zones for view '{view.name}'")
self.export_dhcp(zones, view.name, export_path)
zones = Zone.objects.filter(view__isnull=True, status="active")
if len(zones):
self.log_info("Exporting zones without a view")
self.export_dhcp(zones, data["default_view_name"], export_path)
def export_dhcp(self, zones, view_name, export_path):
view_path = export_path / view_name
try:
view_path.mkdir(parents=True, exist_ok=True)
except OSError as exc:
self.log_failure(f"Could not create directory {view_path}: {exc}")
return
for zone in zones:
if zone.tags.first() is not None:
if 'forward-zone' == zone.tags.first().slug:
self.log_info(f"Exporting Forward zone {zone}")
else:
self.log_info(f"Skipping Reverse zone {zone}")
continue
else:
self.log_info(f"Skipping untagged zone {zone}")
continue
records = Record.objects.filter(zone=zone, ipam_ip_address__isnull=False, status="active")
zone_data = self.template.render({"zone": zone, "records": records})
dhcp_file_path = view_path / f"{zone.name}_dhcp.csv"
try:
if len(records)>0:
dhcp_file = open(dhcp_file_path, "wb")
dhcp_file.write(zone_data.encode("UTF-8"))
dhcp_file.close()
self.log_info(f"Written {len(records)} records")
except OSError as exc:
self.log_failure(f"Could not create zone file {dhcp_file_path}: {exc}")
continues |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I even tried to run the script from command line as suggested here #15352 (comment) |
Beta Was this translation helpful? Give feedback.
Well, after a lot of search and attempts, the simplest solution seems to work.
Inside netbox dir i've run
probably it was an old file. Sounds strange because i always create a new dir and a new virtualenv every time... and i migrate from a 4.2.3 version...
but this is it...