Skip to content

Commit f1cc9b5

Browse files
authored
Merge pull request #340 from CllaudiaB/style
Style: apply Ruff for linting and formatting
2 parents cc20d42 + f2e1cfc commit f1cc9b5

32 files changed

+1253
-1224
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13.0"]
12+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
1313
steps:
1414
- name: Check out repository code
1515
uses: actions/checkout@v4

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,9 @@ On a personal note, I use the docker image from [netbox-community/netbox-docker]
299299
# docker-compose pull
300300
# docker-compose up
301301
```
302+
303+
For the linter and code formatting, you need to run:
304+
```
305+
ruff check
306+
ruff format
307+
```

netbox_agent/cli.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
from netbox_agent.virtualmachine import VirtualMachine, is_vm
1313

1414
MANUFACTURERS = {
15-
'Dell Inc.': DellHost,
16-
'HP': HPHost,
17-
'HPE': HPHost,
18-
'Supermicro': SupermicroHost,
19-
'Quanta Cloud Technology Inc.': QCTHost,
20-
'Generic': GenericHost,
15+
"Dell Inc.": DellHost,
16+
"HP": HPHost,
17+
"HPE": HPHost,
18+
"Supermicro": SupermicroHost,
19+
"Quanta Cloud Technology Inc.": QCTHost,
20+
"Generic": GenericHost,
2121
}
2222

2323

@@ -26,25 +26,33 @@ def run(config):
2626

2727
if config.virtual.enabled or is_vm(dmi):
2828
if config.virtual.hypervisor:
29-
raise Exception('This host can\'t be a hypervisor because it\'s a VM')
29+
raise Exception("This host can't be a hypervisor because it's a VM")
3030
if not config.virtual.cluster_name:
31-
raise Exception('virtual.cluster_name parameter is mandatory because it\'s a VM')
31+
raise Exception("virtual.cluster_name parameter is mandatory because it's a VM")
3232
server = VirtualMachine(dmi=dmi)
3333
else:
3434
if config.virtual.hypervisor and not config.virtual.cluster_name:
35-
raise Exception('virtual.cluster_name parameter is mandatory because it\'s a hypervisor')
36-
manufacturer = dmidecode.get_by_type(dmi, 'Chassis')[0].get('Manufacturer')
35+
raise Exception(
36+
"virtual.cluster_name parameter is mandatory because it's a hypervisor"
37+
)
38+
manufacturer = dmidecode.get_by_type(dmi, "Chassis")[0].get("Manufacturer")
3739
try:
3840
server = MANUFACTURERS[manufacturer](dmi=dmi)
3941
except KeyError:
4042
server = GenericHost(dmi=dmi)
4143

42-
if version.parse(nb.version) < version.parse('3.7'):
43-
print('netbox-agent is not compatible with Netbox prior to version 3.7')
44+
if version.parse(nb.version) < version.parse("3.7"):
45+
print("netbox-agent is not compatible with Netbox prior to version 3.7")
4446
return False
4547

46-
if config.register or config.update_all or config.update_network or \
47-
config.update_location or config.update_inventory or config.update_psu:
48+
if (
49+
config.register
50+
or config.update_all
51+
or config.update_network
52+
or config.update_location
53+
or config.update_inventory
54+
or config.update_psu
55+
):
4856
server.netbox_create_or_update(config)
4957
if config.debug:
5058
server.print_debug()
@@ -55,5 +63,5 @@ def main():
5563
return run(config)
5664

5765

58-
if __name__ == '__main__':
66+
if __name__ == "__main__":
5967
sys.exit(main())

netbox_agent/config.py

Lines changed: 121 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -10,91 +10,131 @@
1010
def get_config():
1111
p = jsonargparse.ArgumentParser(
1212
default_config_files=[
13-
'/etc/netbox_agent.yaml',
14-
'~/.config/netbox_agent.yaml',
15-
'~/.netbox_agent.yaml',
13+
"/etc/netbox_agent.yaml",
14+
"~/.config/netbox_agent.yaml",
15+
"~/.netbox_agent.yaml",
1616
],
17-
prog='netbox_agent',
17+
prog="netbox_agent",
1818
description="Netbox agent to run on your infrastructure's servers",
19-
env_prefix='NETBOX_AGENT_',
20-
default_env=True
19+
env_prefix="NETBOX_AGENT_",
20+
default_env=True,
2121
)
22-
p.add_argument('-c', '--config', action=jsonargparse.ActionConfigFile)
22+
p.add_argument("-c", "--config", action=jsonargparse.ActionConfigFile)
2323

24-
p.add_argument('-r', '--register', action='store_true', help='Register server to Netbox')
25-
p.add_argument('-u', '--update-all', action='store_true', help='Update all infos in Netbox')
26-
p.add_argument('-d', '--debug', action='store_true', help='Print debug infos')
27-
p.add_argument('--update-network', action='store_true', help='Update network')
28-
p.add_argument('--update-inventory', action='store_true', help='Update inventory')
29-
p.add_argument('--update-location', action='store_true', help='Update location')
30-
p.add_argument('--update-psu', action='store_true', help='Update PSU')
31-
p.add_argument('--update-hypervisor', action='store_true', help='Update virtualization cluster and virtual machines')
32-
p.add_argument('--update-old-devices', action='store_true',
33-
help='Update serial number of existing (old ?) devices having same name but different serial')
34-
p.add_argument('--purge-old-devices', action='store_true',
35-
help='Purge existing (old ?) devices having same name but different serial')
36-
p.add_argument('--expansion-as-device', action='store_true',
37-
help='Manage blade expansions as external devices')
24+
p.add_argument("-r", "--register", action="store_true", help="Register server to Netbox")
25+
p.add_argument("-u", "--update-all", action="store_true", help="Update all infos in Netbox")
26+
p.add_argument("-d", "--debug", action="store_true", help="Print debug infos")
27+
p.add_argument("--update-network", action="store_true", help="Update network")
28+
p.add_argument("--update-inventory", action="store_true", help="Update inventory")
29+
p.add_argument("--update-location", action="store_true", help="Update location")
30+
p.add_argument("--update-psu", action="store_true", help="Update PSU")
31+
p.add_argument(
32+
"--update-hypervisor",
33+
action="store_true",
34+
help="Update virtualization cluster and virtual machines",
35+
)
36+
p.add_argument(
37+
"--update-old-devices",
38+
action="store_true",
39+
help="Update serial number of existing (old ?) devices having same name but different serial",
40+
)
41+
p.add_argument(
42+
"--purge-old-devices",
43+
action="store_true",
44+
help="Purge existing (old ?) devices having same name but different serial",
45+
)
46+
p.add_argument(
47+
"--expansion-as-device",
48+
action="store_true",
49+
help="Manage blade expansions as external devices",
50+
)
3851

39-
p.add_argument('--log_level', default='debug')
40-
p.add_argument('--netbox.ssl_ca_certs_file', help='SSL CA certificates file')
41-
p.add_argument('--netbox.url', help='Netbox URL')
42-
p.add_argument('--netbox.token', help='Netbox API Token')
43-
p.add_argument('--netbox.ssl_verify', default=True, action='store_true',
44-
help='Disable SSL verification')
45-
p.add_argument('--virtual.enabled', action='store_true', help='Is a virtual machine or not')
46-
p.add_argument('--virtual.cluster_name', help='Cluster name of VM')
47-
p.add_argument('--virtual.hypervisor', action='store_true', help='Is a hypervisor or not')
48-
p.add_argument('--virtual.list_guests_cmd', default=None,
49-
help='Command to output the list of vrtualization guests in the hypervisor separated by whitespace')
50-
p.add_argument('--hostname_cmd', default=None,
51-
help="Command to output hostname, used as Device's name in netbox")
52-
p.add_argument('--device.platform', default=None,
53-
help='Override device platform. Here we use OS distribution.')
54-
p.add_argument('--device.tags', default=r'',
55-
help='tags to use for a host')
56-
p.add_argument('--preserve-tags', action='store_true', help='Append new unique tags, preserve those already present')
57-
p.add_argument('--device.custom_fields', default=r'',
58-
help='custom_fields to use for a host, eg: field1=v1,field2=v2')
59-
p.add_argument('--device.blade_role', default=r'Blade',
60-
help='role to use for a blade server')
61-
p.add_argument('--device.chassis_role', default=r'Server Chassis',
62-
help='role to use for a chassis')
63-
p.add_argument('--device.server_role', default=r'Server',
64-
help='role to use for a server')
65-
p.add_argument('--tenant.driver',
66-
help='tenant driver, ie cmd, file')
67-
p.add_argument('--tenant.driver_file',
68-
help='tenant driver custom driver file path')
69-
p.add_argument('--tenant.regex',
70-
help='tenant regex to extract Netbox tenant slug')
71-
p.add_argument('--datacenter_location.driver',
72-
help='Datacenter location driver, ie: cmd, file')
73-
p.add_argument('--datacenter_location.driver_file',
74-
help='Datacenter location custom driver file path')
75-
p.add_argument('--datacenter_location.regex',
76-
help='Datacenter location regex to extract Netbox DC slug')
77-
p.add_argument('--rack_location.driver', help='Rack location driver, ie: cmd, file')
78-
p.add_argument('--rack_location.driver_file', help='Rack location custom driver file path')
79-
p.add_argument('--rack_location.regex', help='Rack location regex to extract Rack name')
80-
p.add_argument('--slot_location.driver', help='Slot location driver, ie: cmd, file')
81-
p.add_argument('--slot_location.driver_file', help='Slot location custom driver file path')
82-
p.add_argument('--slot_location.regex', help='Slot location regex to extract slot name')
83-
p.add_argument('--network.ignore_interfaces', default=r'(dummy.*|docker.*)',
84-
help='Regex to ignore interfaces')
85-
p.add_argument('--network.ignore_ips', default=r'^(127\.0\.0\..*|fe80.*|::1.*)',
86-
help='Regex to ignore IPs')
87-
p.add_argument('--network.ipmi', default=True, help='Enable gathering IPMI information')
88-
p.add_argument('--network.lldp', help='Enable auto-cabling feature through LLDP infos')
89-
p.add_argument('--inventory', action='store_true',
90-
help='Enable HW inventory (CPU, Memory, RAID Cards, Disks) feature')
91-
p.add_argument('--process-virtual-drives', action='store_true',
92-
help='Process virtual drives information from RAID '
93-
'controllers to fill disk custom_fields')
94-
p.add_argument('--force-disk-refresh', action='store_true',
95-
help='Forces disks detection reprocessing')
96-
p.add_argument('--dump-disks-map',
97-
help='File path to dump physical/virtual disks map')
52+
p.add_argument("--log_level", default="debug")
53+
p.add_argument("--netbox.ssl_ca_certs_file", help="SSL CA certificates file")
54+
p.add_argument("--netbox.url", help="Netbox URL")
55+
p.add_argument("--netbox.token", help="Netbox API Token")
56+
p.add_argument(
57+
"--netbox.ssl_verify", default=True, action="store_true", help="Disable SSL verification"
58+
)
59+
p.add_argument("--virtual.enabled", action="store_true", help="Is a virtual machine or not")
60+
p.add_argument("--virtual.cluster_name", help="Cluster name of VM")
61+
p.add_argument("--virtual.hypervisor", action="store_true", help="Is a hypervisor or not")
62+
p.add_argument(
63+
"--virtual.list_guests_cmd",
64+
default=None,
65+
help="Command to output the list of vrtualization guests in the hypervisor separated by whitespace",
66+
)
67+
p.add_argument(
68+
"--hostname_cmd",
69+
default=None,
70+
help="Command to output hostname, used as Device's name in netbox",
71+
)
72+
p.add_argument(
73+
"--device.platform",
74+
default=None,
75+
help="Override device platform. Here we use OS distribution.",
76+
)
77+
p.add_argument("--device.tags", default=r"", help="tags to use for a host")
78+
p.add_argument(
79+
"--preserve-tags",
80+
action="store_true",
81+
help="Append new unique tags, preserve those already present",
82+
)
83+
p.add_argument(
84+
"--device.custom_fields",
85+
default=r"",
86+
help="custom_fields to use for a host, eg: field1=v1,field2=v2",
87+
)
88+
p.add_argument("--device.blade_role", default=r"Blade", help="role to use for a blade server")
89+
p.add_argument(
90+
"--device.chassis_role", default=r"Server Chassis", help="role to use for a chassis"
91+
)
92+
p.add_argument("--device.server_role", default=r"Server", help="role to use for a server")
93+
p.add_argument("--tenant.driver", help="tenant driver, ie cmd, file")
94+
p.add_argument("--tenant.driver_file", help="tenant driver custom driver file path")
95+
p.add_argument("--tenant.regex", help="tenant regex to extract Netbox tenant slug")
96+
p.add_argument(
97+
"--datacenter_location.driver", help="Datacenter location driver, ie: cmd, file"
98+
)
99+
p.add_argument(
100+
"--datacenter_location.driver_file", help="Datacenter location custom driver file path"
101+
)
102+
p.add_argument(
103+
"--datacenter_location.regex", help="Datacenter location regex to extract Netbox DC slug"
104+
)
105+
p.add_argument("--rack_location.driver", help="Rack location driver, ie: cmd, file")
106+
p.add_argument("--rack_location.driver_file", help="Rack location custom driver file path")
107+
p.add_argument("--rack_location.regex", help="Rack location regex to extract Rack name")
108+
p.add_argument("--slot_location.driver", help="Slot location driver, ie: cmd, file")
109+
p.add_argument("--slot_location.driver_file", help="Slot location custom driver file path")
110+
p.add_argument("--slot_location.regex", help="Slot location regex to extract slot name")
111+
p.add_argument(
112+
"--network.ignore_interfaces",
113+
default=r"(dummy.*|docker.*)",
114+
help="Regex to ignore interfaces",
115+
)
116+
p.add_argument(
117+
"--network.ignore_ips",
118+
default=r"^(127\.0\.0\..*|fe80.*|::1.*)",
119+
help="Regex to ignore IPs",
120+
)
121+
p.add_argument("--network.ipmi", default=True, help="Enable gathering IPMI information")
122+
p.add_argument("--network.lldp", help="Enable auto-cabling feature through LLDP infos")
123+
p.add_argument(
124+
"--inventory",
125+
action="store_true",
126+
help="Enable HW inventory (CPU, Memory, RAID Cards, Disks) feature",
127+
)
128+
p.add_argument(
129+
"--process-virtual-drives",
130+
action="store_true",
131+
help="Process virtual drives information from RAID "
132+
"controllers to fill disk custom_fields",
133+
)
134+
p.add_argument(
135+
"--force-disk-refresh", action="store_true", help="Forces disks detection reprocessing"
136+
)
137+
p.add_argument("--dump-disks-map", help="File path to dump physical/virtual disks map")
98138

99139
options = p.parse_args()
100140
return options
@@ -105,7 +145,7 @@ def get_config():
105145

106146
def get_netbox_instance():
107147
if config.netbox.url is None or config.netbox.token is None:
108-
logging.error('Netbox URL and token are mandatory')
148+
logging.error("Netbox URL and token are mandatory")
109149
sys.exit(1)
110150

111151
nb = pynetbox.api(

0 commit comments

Comments
 (0)