2021-05-18 13:59:04 +02:00
|
|
|
from packaging import version
|
2024-10-21 12:55:54 +02:00
|
|
|
|
2019-08-04 00:00:22 +02:00
|
|
|
import netbox_agent.dmidecode as dmidecode
|
2021-05-18 13:59:04 +02:00
|
|
|
from netbox_agent.config import config
|
|
|
|
from netbox_agent.config import netbox_instance as nb
|
2020-02-02 20:08:56 +01:00
|
|
|
from netbox_agent.logging import logging # NOQA
|
|
|
|
from netbox_agent.vendors.dell import DellHost
|
2020-02-03 00:38:23 +01:00
|
|
|
from netbox_agent.vendors.generic import GenericHost
|
2019-08-26 15:38:44 +02:00
|
|
|
from netbox_agent.vendors.hp import HPHost
|
2019-08-26 16:39:36 +02:00
|
|
|
from netbox_agent.vendors.qct import QCTHost
|
2019-08-26 15:38:44 +02:00
|
|
|
from netbox_agent.vendors.supermicro import SupermicroHost
|
2020-04-19 12:28:49 +02:00
|
|
|
from netbox_agent.virtualmachine import VirtualMachine, is_vm
|
2019-08-04 00:00:22 +02:00
|
|
|
|
|
|
|
MANUFACTURERS = {
|
2024-10-21 12:55:54 +02:00
|
|
|
"Dell Inc.": DellHost,
|
|
|
|
"HP": HPHost,
|
|
|
|
"HPE": HPHost,
|
|
|
|
"Supermicro": SupermicroHost,
|
|
|
|
"Quanta Cloud Technology Inc.": QCTHost,
|
|
|
|
"Generic": GenericHost,
|
2020-02-02 20:08:56 +01:00
|
|
|
}
|
2019-08-04 00:00:22 +02:00
|
|
|
|
|
|
|
|
2019-09-03 13:16:37 +02:00
|
|
|
def run(config):
|
2020-02-16 20:24:40 +01:00
|
|
|
dmi = dmidecode.parse()
|
2020-02-03 00:38:23 +01:00
|
|
|
|
2020-04-19 12:28:49 +02:00
|
|
|
if config.virtual.enabled or is_vm(dmi):
|
|
|
|
if not config.virtual.cluster_name:
|
2024-10-21 12:55:54 +02:00
|
|
|
raise Exception(
|
|
|
|
"virtual.cluster_name parameter is mandatory because it's a VM"
|
|
|
|
)
|
2020-04-19 12:28:49 +02:00
|
|
|
server = VirtualMachine(dmi=dmi)
|
|
|
|
else:
|
2024-10-21 12:55:54 +02:00
|
|
|
manufacturer = dmidecode.get_by_type(dmi, "Chassis")[0].get("Manufacturer")
|
2020-04-19 12:28:49 +02:00
|
|
|
try:
|
|
|
|
server = MANUFACTURERS[manufacturer](dmi=dmi)
|
|
|
|
except KeyError:
|
|
|
|
server = GenericHost(dmi=dmi)
|
2019-09-03 13:16:37 +02:00
|
|
|
|
2024-10-21 12:55:54 +02:00
|
|
|
if version.parse(nb.version) < version.parse("3.7"):
|
|
|
|
print("netbox-agent is not compatible with Netbox prior to version 3.7")
|
2021-05-12 19:40:36 +02:00
|
|
|
return False
|
|
|
|
|
2024-10-21 12:55:54 +02:00
|
|
|
if (
|
|
|
|
config.register
|
|
|
|
or config.update_all
|
|
|
|
or config.update_network
|
|
|
|
or config.update_location
|
|
|
|
or config.update_inventory
|
|
|
|
or config.update_psu
|
|
|
|
):
|
2022-02-21 18:12:16 +01:00
|
|
|
server.netbox_create_or_update(config)
|
2019-09-03 13:16:37 +02:00
|
|
|
if config.debug:
|
2019-08-04 00:00:22 +02:00
|
|
|
server.print_debug()
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-08-21 20:40:10 +02:00
|
|
|
return 0 if run(config) else 1
|
2019-08-04 00:00:22 +02:00
|
|
|
|
|
|
|
|
2024-10-21 12:55:54 +02:00
|
|
|
if __name__ == "__main__":
|
2019-08-04 00:00:22 +02:00
|
|
|
main()
|