hostapps/hostapps/update.py

127 lines
4.1 KiB
Python
Raw Permalink Normal View History

2022-05-28 14:14:08 -05:00
"""Install desktop entries."""
import ipaddress
2023-10-18 20:26:59 -05:00
import json
2022-05-28 14:14:08 -05:00
import os
import pathlib
2023-12-07 19:47:17 -06:00
import re
2022-05-28 14:14:08 -05:00
import click
import pynetbox
@click.command()
def main():
data = json.load(open(f"/run/user/{os.getuid()}/secrets/netbox.json"))
nb = pynetbox.api(
data["url"],
token=data["token"],
threading=True,
)
2024-05-14 14:44:33 -05:00
appdir = pathlib.Path("~/dev/nixos-desktops/hostapps.json").expanduser().resolve()
2022-05-28 14:14:08 -05:00
data = {}
cisco = nb.dcim.manufacturers.get(name="Cisco")
2024-05-14 14:44:33 -05:00
if cisco is None:
return
role_ids = []
for name in ["Network", "Network Core", "Network Edge", "Network WAN"]:
role = nb.dcim.device_roles.get(name=name)
if role is not None:
role_ids.append(role.id)
2022-05-28 14:14:08 -05:00
for device in nb.dcim.devices.filter(
manufacturer_id=cisco.id,
2024-05-14 14:44:33 -05:00
role_id=role_ids,
2022-05-28 14:14:08 -05:00
status="active",
2023-10-18 20:31:33 -05:00
has_primary_ip="true",
2022-05-28 14:14:08 -05:00
):
if device.virtual_chassis is not None:
name = device.virtual_chassis.name
else:
name = device.name
2024-05-16 10:12:31 -05:00
address = None
if device.primary_ip4 is not None:
address = str(ipaddress.ip_interface(device.primary_ip4.address).ip)
if address is None and device.primary_ip6 is not None:
address = str(ipaddress.ip_interface(device.primary_ip6.address).ip)
if address is None:
print(f"{device.name} does not have a primary IP address")
continue
2022-05-28 14:14:08 -05:00
services = nb.ipam.services.filter(device_id=device.id)
for service in services:
if service.name.lower() in ["ssh", "telnet"]:
for port in service.ports:
data[f"{name}:{service.name.lower()}:{port}"] = {
"name": name,
"type": service.name.lower(),
"comment": " ".join(name.split("-")),
2024-05-16 10:12:31 -05:00
"address": address,
2022-05-28 14:14:08 -05:00
"port": port,
"username": "jcollie",
2023-10-30 22:26:24 -05:00
"manufacturer": device.device_type.manufacturer.name,
"model": device.device_type.model,
"part_number": device.device_type.part_number,
2023-12-07 19:47:17 -06:00
"class": re.sub(r"[^a-zA-Z0-9_]", "_", name),
2022-05-28 14:14:08 -05:00
}
2024-05-16 10:12:31 -05:00
print(name, address)
2022-05-28 14:14:08 -05:00
schneider = nb.dcim.manufacturers.get(name="Schneider")
2024-05-14 14:44:33 -05:00
if schneider is None:
return
role_ids = []
for name in ["Power Protection", "Power Distribution"]:
role = nb.dcim.device_roles.get(name=name)
if role is not None:
role_ids.append(role.id)
2022-05-28 14:14:08 -05:00
for device in nb.dcim.devices.filter(
manufacturer_id=schneider.id,
2024-05-14 14:44:33 -05:00
role_id=role_ids,
2022-05-28 14:14:08 -05:00
status="active",
has_primary_ip="true",
):
name = device.name
2024-05-16 10:12:31 -05:00
address = None
if device.primary_ip4 is not None:
address = str(ipaddress.ip_interface(device.primary_ip4.address).ip)
if address is None and device.primary_ip6 is not None:
address = str(ipaddress.ip_interface(device.primary_ip6.address).ip)
if address is None:
print(f"{device.name} does not have a primary IP address")
continue
2022-05-28 14:14:08 -05:00
services = nb.ipam.services.filter(device_id=device.id)
for service in services:
if service.name.lower() in ["ssh", "telnet"]:
for port in service.ports:
data[f"{name}:{service.name.lower()}:{port}"] = {
"name": name,
"type": service.name.lower(),
"comment": " ".join(name.split("-")),
2024-05-16 10:12:31 -05:00
"address": address,
2022-05-28 14:14:08 -05:00
"port": port,
"username": "apc",
2023-10-30 22:26:24 -05:00
"manufacturer": device.device_type.manufacturer.name,
"model": device.device_type.model,
"part_number": device.device_type.part_number,
2023-12-07 19:47:17 -06:00
"class": re.sub(r"[^a-zA-Z0-9_]", "_", name),
2022-05-28 14:14:08 -05:00
}
2024-05-16 10:12:31 -05:00
print(name, address)
2022-05-28 14:14:08 -05:00
open(appdir, "w").write(json.dumps(data, indent=2))
if __name__ == "__main__":
main()