2020-04-19 12:28:49 +02:00
|
|
|
import os
|
2024-10-21 12:55:54 +02:00
|
|
|
from pprint import pprint
|
2020-04-19 12:28:49 +02:00
|
|
|
|
|
|
|
import netbox_agent.dmidecode as dmidecode
|
|
|
|
from netbox_agent.config import config
|
|
|
|
from netbox_agent.config import netbox_instance as nb
|
2020-07-01 18:54:58 +02:00
|
|
|
from netbox_agent.location import Tenant
|
2020-04-19 12:28:49 +02:00
|
|
|
from netbox_agent.logging import logging # NOQA
|
2024-10-21 12:55:54 +02:00
|
|
|
from netbox_agent.misc import create_netbox_tags, get_device_platform, get_hostname
|
2020-04-19 12:28:49 +02:00
|
|
|
from netbox_agent.network import VirtualNetwork
|
|
|
|
|
|
|
|
|
|
|
|
def is_vm(dmi):
|
2024-10-21 12:55:54 +02:00
|
|
|
bios = dmidecode.get_by_type(dmi, "BIOS")[0]
|
|
|
|
system = dmidecode.get_by_type(dmi, "System")[0]
|
2023-02-25 01:15:23 +01:00
|
|
|
|
|
|
|
return (
|
2024-10-21 12:55:54 +02:00
|
|
|
"Hyper-V" in bios["Version"]
|
|
|
|
or "Xen" in bios["Version"]
|
|
|
|
or "Google Compute Engine" in system["Product Name"]
|
|
|
|
) or (
|
2023-02-25 01:15:23 +01:00
|
|
|
(
|
2024-10-21 12:55:54 +02:00
|
|
|
"Amazon EC2" in system["Manufacturer"]
|
|
|
|
and not system["Product Name"].endswith(".metal")
|
2023-02-25 01:15:23 +01:00
|
|
|
)
|
2024-10-21 12:55:54 +02:00
|
|
|
or "RHEV Hypervisor" in system["Product Name"]
|
|
|
|
or "QEMU" in system["Manufacturer"]
|
|
|
|
or "VirtualBox" in bios["Version"]
|
|
|
|
or "VMware" in system["Manufacturer"]
|
2023-02-25 01:15:23 +01:00
|
|
|
)
|
2020-04-19 12:28:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
class VirtualMachine(object):
|
|
|
|
def __init__(self, dmi=None):
|
|
|
|
if dmi:
|
|
|
|
self.dmi = dmi
|
|
|
|
else:
|
|
|
|
self.dmi = dmidecode.parse()
|
|
|
|
self.network = None
|
2022-03-31 21:04:32 +02:00
|
|
|
self.device_platform = get_device_platform(config.device.platform)
|
2020-04-19 12:28:49 +02:00
|
|
|
|
2024-10-21 12:55:54 +02:00
|
|
|
self.tags = (
|
|
|
|
list(set(config.device.tags.split(","))) if config.device.tags else []
|
|
|
|
)
|
2024-03-28 12:07:17 +01:00
|
|
|
self.nb_tags = create_netbox_tags(self.tags)
|
2020-07-01 18:54:58 +02:00
|
|
|
|
2020-04-19 12:28:49 +02:00
|
|
|
def get_memory(self):
|
2024-10-21 12:55:54 +02:00
|
|
|
mem_bytes = os.sysconf("SC_PAGE_SIZE") * os.sysconf(
|
|
|
|
"SC_PHYS_PAGES"
|
|
|
|
) # e.g. 4015976448
|
|
|
|
mem_gib = mem_bytes / (1024.0**2) # e.g. 3.74
|
2020-04-19 12:28:49 +02:00
|
|
|
return int(mem_gib)
|
|
|
|
|
|
|
|
def get_vcpus(self):
|
|
|
|
return os.cpu_count()
|
|
|
|
|
|
|
|
def get_netbox_vm(self):
|
|
|
|
hostname = get_hostname(config)
|
2024-10-21 12:55:54 +02:00
|
|
|
vm = nb.virtualization.virtual_machines.get(name=hostname)
|
2020-04-19 12:28:49 +02:00
|
|
|
return vm
|
|
|
|
|
|
|
|
def get_netbox_cluster(self, name):
|
|
|
|
cluster = nb.virtualization.clusters.get(
|
|
|
|
name=name,
|
|
|
|
)
|
|
|
|
return cluster
|
|
|
|
|
2020-07-01 18:54:58 +02:00
|
|
|
def get_netbox_datacenter(self, name):
|
|
|
|
cluster = self.get_netbox_cluster()
|
|
|
|
if cluster.datacenter:
|
|
|
|
return cluster.datacenter
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_tenant(self):
|
|
|
|
tenant = Tenant()
|
|
|
|
return tenant.get()
|
|
|
|
|
|
|
|
def get_netbox_tenant(self):
|
2020-07-11 15:09:44 +02:00
|
|
|
tenant = self.get_tenant()
|
|
|
|
if tenant is None:
|
|
|
|
return None
|
2024-10-21 12:55:54 +02:00
|
|
|
nb_tenant = nb.tenancy.tenants.get(slug=self.get_tenant())
|
2020-07-11 15:09:44 +02:00
|
|
|
return nb_tenant
|
2020-07-01 18:54:58 +02:00
|
|
|
|
2020-04-19 12:28:49 +02:00
|
|
|
def netbox_create_or_update(self, config):
|
2024-10-21 12:55:54 +02:00
|
|
|
logging.debug("It's a virtual machine")
|
2020-04-19 12:28:49 +02:00
|
|
|
created = False
|
|
|
|
updated = 0
|
|
|
|
|
|
|
|
hostname = get_hostname(config)
|
|
|
|
vm = self.get_netbox_vm()
|
|
|
|
|
|
|
|
vcpus = self.get_vcpus()
|
|
|
|
memory = self.get_memory()
|
2020-07-01 18:54:58 +02:00
|
|
|
tenant = self.get_netbox_tenant()
|
2020-04-19 12:28:49 +02:00
|
|
|
if not vm:
|
2024-10-21 12:55:54 +02:00
|
|
|
logging.debug("Creating Virtual machine..")
|
2020-04-19 12:28:49 +02:00
|
|
|
cluster = self.get_netbox_cluster(config.virtual.cluster_name)
|
|
|
|
|
|
|
|
vm = nb.virtualization.virtual_machines.create(
|
|
|
|
name=hostname,
|
|
|
|
cluster=cluster.id,
|
2022-11-03 14:56:38 +01:00
|
|
|
platform=self.device_platform.id,
|
2020-04-19 12:28:49 +02:00
|
|
|
vcpus=vcpus,
|
|
|
|
memory=memory,
|
2020-07-01 18:54:58 +02:00
|
|
|
tenant=tenant.id if tenant else None,
|
2024-10-21 12:55:54 +02:00
|
|
|
tags=[{"name": x} for x in self.tags],
|
2020-04-19 12:28:49 +02:00
|
|
|
)
|
|
|
|
created = True
|
|
|
|
|
|
|
|
self.network = VirtualNetwork(server=self)
|
|
|
|
self.network.create_or_update_netbox_network_cards()
|
|
|
|
|
2020-07-01 18:54:58 +02:00
|
|
|
if not created:
|
|
|
|
if vm.vcpus != vcpus:
|
|
|
|
vm.vcpus = vcpus
|
|
|
|
updated += 1
|
|
|
|
if vm.memory != memory:
|
|
|
|
vm.memory = memory
|
|
|
|
updated += 1
|
2024-03-28 12:07:17 +01:00
|
|
|
|
|
|
|
vm_tags = sorted(set([x.name for x in vm.tags]))
|
|
|
|
tags = sorted(set(self.tags))
|
|
|
|
if vm_tags != tags:
|
|
|
|
new_tags_ids = [x.id for x in self.nb_tags]
|
|
|
|
if not config.preserve_tags:
|
|
|
|
vm.tags = new_tags_ids
|
|
|
|
else:
|
|
|
|
vm_tags_ids = [x.id for x in vm.tags]
|
|
|
|
vm.tags = sorted(set(new_tags_ids + vm_tags_ids))
|
2020-07-01 18:54:58 +02:00
|
|
|
updated += 1
|
2024-03-28 12:07:17 +01:00
|
|
|
|
2022-03-31 21:04:32 +02:00
|
|
|
if vm.platform != self.device_platform:
|
|
|
|
vm.platform = self.device_platform
|
|
|
|
updated += 1
|
2020-04-19 12:28:49 +02:00
|
|
|
|
|
|
|
if updated:
|
|
|
|
vm.save()
|
2023-08-21 20:38:41 +02:00
|
|
|
|
|
|
|
def print_debug(self):
|
|
|
|
self.network = VirtualNetwork(server=self)
|
2024-10-21 12:55:54 +02:00
|
|
|
print("Cluster:", self.get_netbox_cluster(config.virtual.cluster_name))
|
|
|
|
print("Platform:", self.device_platform)
|
|
|
|
print("VM:", self.get_netbox_vm())
|
|
|
|
print("vCPU:", self.get_vcpus())
|
|
|
|
print("Memory:", f"{self.get_memory()} MB")
|
|
|
|
print(
|
|
|
|
"NIC:",
|
|
|
|
)
|
2023-08-21 20:38:41 +02:00
|
|
|
pprint(self.network.get_network_cards())
|
|
|
|
pass
|