Skip to content

Commit

Permalink
apply Ruff for linting and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
clbu committed Dec 11, 2024
1 parent da9ff0d commit 7df5a6b
Show file tree
Hide file tree
Showing 28 changed files with 1,208 additions and 1,204 deletions.
32 changes: 19 additions & 13 deletions netbox_agent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from netbox_agent.virtualmachine import VirtualMachine, is_vm

MANUFACTURERS = {
'Dell Inc.': DellHost,
'HP': HPHost,
'HPE': HPHost,
'Supermicro': SupermicroHost,
'Quanta Cloud Technology Inc.': QCTHost,
'Generic': GenericHost,
"Dell Inc.": DellHost,
"HP": HPHost,
"HPE": HPHost,
"Supermicro": SupermicroHost,
"Quanta Cloud Technology Inc.": QCTHost,
"Generic": GenericHost,
}


Expand All @@ -25,21 +25,27 @@ def run(config):

if config.virtual.enabled or is_vm(dmi):
if not config.virtual.cluster_name:
raise Exception('virtual.cluster_name parameter is mandatory because it\'s a VM')
raise Exception("virtual.cluster_name parameter is mandatory because it's a VM")
server = VirtualMachine(dmi=dmi)
else:
manufacturer = dmidecode.get_by_type(dmi, 'Chassis')[0].get('Manufacturer')
manufacturer = dmidecode.get_by_type(dmi, "Chassis")[0].get("Manufacturer")
try:
server = MANUFACTURERS[manufacturer](dmi=dmi)
except KeyError:
server = GenericHost(dmi=dmi)

if version.parse(nb.version) < version.parse('3.7'):
print('netbox-agent is not compatible with Netbox prior to version 3.7')
if version.parse(nb.version) < version.parse("3.7"):
print("netbox-agent is not compatible with Netbox prior to version 3.7")
return False

if config.register or config.update_all or config.update_network or \
config.update_location or config.update_inventory or config.update_psu:
if (
config.register
or config.update_all
or config.update_network
or config.update_location
or config.update_inventory
or config.update_psu
):
server.netbox_create_or_update(config)
if config.debug:
server.print_debug()
Expand All @@ -50,5 +56,5 @@ def main():
return run(config)


if __name__ == '__main__':
if __name__ == "__main__":
main()
187 changes: 110 additions & 77 deletions netbox_agent/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,87 +10,120 @@
def get_config():
p = jsonargparse.ArgumentParser(
default_config_files=[
'/etc/netbox_agent.yaml',
'~/.config/netbox_agent.yaml',
'~/.netbox_agent.yaml',
"/etc/netbox_agent.yaml",
"~/.config/netbox_agent.yaml",
"~/.netbox_agent.yaml",
],
prog='netbox_agent',
prog="netbox_agent",
description="Netbox agent to run on your infrastructure's servers",
env_prefix='NETBOX_AGENT_',
default_env=True
env_prefix="NETBOX_AGENT_",
default_env=True,
)
p.add_argument('-c', '--config', action=jsonargparse.ActionConfigFile)
p.add_argument("-c", "--config", action=jsonargparse.ActionConfigFile)

p.add_argument('-r', '--register', action='store_true', help='Register server to Netbox')
p.add_argument('-u', '--update-all', action='store_true', help='Update all infos in Netbox')
p.add_argument('-d', '--debug', action='store_true', help='Print debug infos')
p.add_argument('--update-network', action='store_true', help='Update network')
p.add_argument('--update-inventory', action='store_true', help='Update inventory')
p.add_argument('--update-location', action='store_true', help='Update location')
p.add_argument('--update-psu', action='store_true', help='Update PSU')
p.add_argument('--update-old-devices', action='store_true',
help='Update serial number of existing (old ?) devices having same name but different serial')
p.add_argument('--purge-old-devices', action='store_true',
help='Purge existing (old ?) devices having same name but different serial')
p.add_argument('--expansion-as-device', action='store_true',
help='Manage blade expansions as external devices')
p.add_argument("-r", "--register", action="store_true", help="Register server to Netbox")
p.add_argument("-u", "--update-all", action="store_true", help="Update all infos in Netbox")
p.add_argument("-d", "--debug", action="store_true", help="Print debug infos")
p.add_argument("--update-network", action="store_true", help="Update network")
p.add_argument("--update-inventory", action="store_true", help="Update inventory")
p.add_argument("--update-location", action="store_true", help="Update location")
p.add_argument("--update-psu", action="store_true", help="Update PSU")
p.add_argument(
"--update-old-devices",
action="store_true",
help="Update serial number of existing (old ?) devices having same name but different serial",
)
p.add_argument(
"--purge-old-devices",
action="store_true",
help="Purge existing (old ?) devices having same name but different serial",
)
p.add_argument(
"--expansion-as-device",
action="store_true",
help="Manage blade expansions as external devices",
)

p.add_argument('--log_level', default='debug')
p.add_argument('--netbox.ssl_ca_certs_file', help='SSL CA certificates file')
p.add_argument('--netbox.url', help='Netbox URL')
p.add_argument('--netbox.token', help='Netbox API Token')
p.add_argument('--netbox.ssl_verify', default=True, action='store_true',
help='Disable SSL verification')
p.add_argument('--virtual.enabled', action='store_true', help='Is a virtual machine or not')
p.add_argument('--virtual.cluster_name', help='Cluster name of VM')
p.add_argument('--hostname_cmd', default=None,
help="Command to output hostname, used as Device's name in netbox")
p.add_argument('--device.platform', default=None,
help='Override device platform. Here we use OS distribution.')
p.add_argument('--device.tags', default=r'',
help='tags to use for a host')
p.add_argument('--preserve-tags', action='store_true', help='Append new unique tags, preserve those already present')
p.add_argument('--device.custom_fields', default=r'',
help='custom_fields to use for a host, eg: field1=v1,field2=v2')
p.add_argument('--device.blade_role', default=r'Blade',
help='role to use for a blade server')
p.add_argument('--device.chassis_role', default=r'Server Chassis',
help='role to use for a chassis')
p.add_argument('--device.server_role', default=r'Server',
help='role to use for a server')
p.add_argument('--tenant.driver',
help='tenant driver, ie cmd, file')
p.add_argument('--tenant.driver_file',
help='tenant driver custom driver file path')
p.add_argument('--tenant.regex',
help='tenant regex to extract Netbox tenant slug')
p.add_argument('--datacenter_location.driver',
help='Datacenter location driver, ie: cmd, file')
p.add_argument('--datacenter_location.driver_file',
help='Datacenter location custom driver file path')
p.add_argument('--datacenter_location.regex',
help='Datacenter location regex to extract Netbox DC slug')
p.add_argument('--rack_location.driver', help='Rack location driver, ie: cmd, file')
p.add_argument('--rack_location.driver_file', help='Rack location custom driver file path')
p.add_argument('--rack_location.regex', help='Rack location regex to extract Rack name')
p.add_argument('--slot_location.driver', help='Slot location driver, ie: cmd, file')
p.add_argument('--slot_location.driver_file', help='Slot location custom driver file path')
p.add_argument('--slot_location.regex', help='Slot location regex to extract slot name')
p.add_argument('--network.ignore_interfaces', default=r'(dummy.*|docker.*)',
help='Regex to ignore interfaces')
p.add_argument('--network.ignore_ips', default=r'^(127\.0\.0\..*|fe80.*|::1.*)',
help='Regex to ignore IPs')
p.add_argument('--network.ipmi', default=True, help='Enable gathering IPMI information')
p.add_argument('--network.lldp', help='Enable auto-cabling feature through LLDP infos')
p.add_argument('--inventory', action='store_true',
help='Enable HW inventory (CPU, Memory, RAID Cards, Disks) feature')
p.add_argument('--process-virtual-drives', action='store_true',
help='Process virtual drives information from RAID '
'controllers to fill disk custom_fields')
p.add_argument('--force-disk-refresh', action='store_true',
help='Forces disks detection reprocessing')
p.add_argument('--dump-disks-map',
help='File path to dump physical/virtual disks map')
p.add_argument("--log_level", default="debug")
p.add_argument("--netbox.ssl_ca_certs_file", help="SSL CA certificates file")
p.add_argument("--netbox.url", help="Netbox URL")
p.add_argument("--netbox.token", help="Netbox API Token")
p.add_argument(
"--netbox.ssl_verify", default=True, action="store_true", help="Disable SSL verification"
)
p.add_argument("--virtual.enabled", action="store_true", help="Is a virtual machine or not")
p.add_argument("--virtual.cluster_name", help="Cluster name of VM")
p.add_argument(
"--hostname_cmd",
default=None,
help="Command to output hostname, used as Device's name in netbox",
)
p.add_argument(
"--device.platform",
default=None,
help="Override device platform. Here we use OS distribution.",
)
p.add_argument("--device.tags", default=r"", help="tags to use for a host")
p.add_argument(
"--preserve-tags",
action="store_true",
help="Append new unique tags, preserve those already present",
)
p.add_argument(
"--device.custom_fields",
default=r"",
help="custom_fields to use for a host, eg: field1=v1,field2=v2",
)
p.add_argument("--device.blade_role", default=r"Blade", help="role to use for a blade server")
p.add_argument(
"--device.chassis_role", default=r"Server Chassis", help="role to use for a chassis"
)
p.add_argument("--device.server_role", default=r"Server", help="role to use for a server")
p.add_argument("--tenant.driver", help="tenant driver, ie cmd, file")
p.add_argument("--tenant.driver_file", help="tenant driver custom driver file path")
p.add_argument("--tenant.regex", help="tenant regex to extract Netbox tenant slug")
p.add_argument(
"--datacenter_location.driver", help="Datacenter location driver, ie: cmd, file"
)
p.add_argument(
"--datacenter_location.driver_file", help="Datacenter location custom driver file path"
)
p.add_argument(
"--datacenter_location.regex", help="Datacenter location regex to extract Netbox DC slug"
)
p.add_argument("--rack_location.driver", help="Rack location driver, ie: cmd, file")
p.add_argument("--rack_location.driver_file", help="Rack location custom driver file path")
p.add_argument("--rack_location.regex", help="Rack location regex to extract Rack name")
p.add_argument("--slot_location.driver", help="Slot location driver, ie: cmd, file")
p.add_argument("--slot_location.driver_file", help="Slot location custom driver file path")
p.add_argument("--slot_location.regex", help="Slot location regex to extract slot name")
p.add_argument(
"--network.ignore_interfaces",
default=r"(dummy.*|docker.*)",
help="Regex to ignore interfaces",
)
p.add_argument(
"--network.ignore_ips",
default=r"^(127\.0\.0\..*|fe80.*|::1.*)",
help="Regex to ignore IPs",
)
p.add_argument("--network.ipmi", default=True, help="Enable gathering IPMI information")
p.add_argument("--network.lldp", help="Enable auto-cabling feature through LLDP infos")
p.add_argument(
"--inventory",
action="store_true",
help="Enable HW inventory (CPU, Memory, RAID Cards, Disks) feature",
)
p.add_argument(
"--process-virtual-drives",
action="store_true",
help="Process virtual drives information from RAID "
"controllers to fill disk custom_fields",
)
p.add_argument(
"--force-disk-refresh", action="store_true", help="Forces disks detection reprocessing"
)
p.add_argument("--dump-disks-map", help="File path to dump physical/virtual disks map")

options = p.parse_args()
return options
Expand All @@ -101,7 +134,7 @@ def get_config():

def get_netbox_instance():
if config.netbox.url is None or config.netbox.token is None:
logging.error('Netbox URL and token are mandatory')
logging.error("Netbox URL and token are mandatory")
sys.exit(1)

nb = pynetbox.api(
Expand Down
Loading

0 comments on commit 7df5a6b

Please sign in to comment.