netbox-agent/netbox_agent/cli.py

61 lines
1.7 KiB
Python
Raw Normal View History

2021-05-18 13:59:04 +02:00
from packaging import version
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
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
from netbox_agent.virtualmachine import VirtualMachine, is_vm
2023-02-28 00:56:03 +01:00
MANUFACTURERS = {
2020-02-02 20:08:56 +01:00
'Dell Inc.': DellHost,
'HP': HPHost,
'HPE': HPHost,
'Supermicro': SupermicroHost,
'Quanta Cloud Technology Inc.': QCTHost,
'Generic': GenericHost,
2023-02-28 00:56:03 +01:00
'Virtual Machine': VirtualMachine,
2020-02-02 20:08:56 +01:00
}
2023-02-28 00:56:03 +01:00
def main():
dmi = dmidecode.parse()
if version.parse(nb.version) < version.parse('2.9'):
2023-02-28 00:56:03 +01:00
raise SystemExit('netbox-agent requires Netbox version 2.9 or higher')
2023-02-28 00:56:03 +01:00
if is_vm(dmi):
if not config.virtual.cluster_name:
raise SystemExit(
'The `virtual.cluster_name` parameter/configuration value must'
'be set for virtualized systems.'
)
kind = 'Virtual'
else:
chassis = dmidecode.get_by_type(dmi, 'Chassis')
kind = chassis[0].get('Manufacturer')
server_class = MANUFACTURERS.get(kind, GenericHost)
server = server_class(dmi)
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)
2023-02-28 00:56:03 +01:00
2019-09-03 13:16:37 +02:00
if config.debug:
server.print_debug()
if __name__ == '__main__':
main()