-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathcli.py
67 lines (57 loc) · 2.06 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import sys
from packaging import version
import netbox_agent.dmidecode as dmidecode
from netbox_agent.config import config
from netbox_agent.config import netbox_instance as nb
from netbox_agent.logging import logging # NOQA
from netbox_agent.vendors.dell import DellHost
from netbox_agent.vendors.generic import GenericHost
from netbox_agent.vendors.hp import HPHost
from netbox_agent.vendors.qct import QCTHost
from netbox_agent.vendors.supermicro import SupermicroHost
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,
}
def run(config):
dmi = dmidecode.parse()
if config.virtual.enabled or is_vm(dmi):
if config.virtual.hypervisor:
raise Exception("This host can't be a hypervisor because it's a VM")
if not config.virtual.cluster_name:
raise Exception("virtual.cluster_name parameter is mandatory because it's a VM")
server = VirtualMachine(dmi=dmi)
else:
if config.virtual.hypervisor and not config.virtual.cluster_name:
raise Exception(
"virtual.cluster_name parameter is mandatory because it's a hypervisor"
)
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")
return 1
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()
return 0
def main():
return run(config)
if __name__ == "__main__":
sys.exit(main())