def discover(self, pod_id, context): """Discover all Pod host resources.""" # Connect to the Pod and make sure it is valid. client = yield self.get_client(pod_id, context) if not client.has_api_extension("virtual-machines"): raise LXDPodError( "Please upgrade your LXD host to 3.19+ for virtual machine support." ) resources = yield deferToThread(lambda: client.resources) mac_addresses = [] for card in resources["network"]["cards"]: for port in card["ports"]: mac_addresses.append(port["address"]) # After the region creates the Pod object it will sync LXD commissioning # data for all hardware information. discovered_pod = DiscoveredPod( # client.host_info["environment"]["architectures"] reports all the # architectures the host CPU supports, not the architectures LXD # supports. On x86_64 LXD reports [x86_64, i686] however LXD does # not currently support VMs on i686. The LXD API currently does not # have a way to query which architectures are usable for VMs. The # safest bet is to just use the kernel_architecture. architectures=[ kernel_to_debian_architecture( client.host_info["environment"]["kernel_architecture"]) ], name=client.host_info["environment"]["server_name"], mac_addresses=mac_addresses, capabilities=[ Capabilities.COMPOSABLE, Capabilities.DYNAMIC_LOCAL_STORAGE, Capabilities.OVER_COMMIT, Capabilities.STORAGE_POOLS, ], ) # Check that we have at least one storage pool. # If not, user should be warned that they need to create one. storage_pools = yield deferToThread(client.storage_pools.all) if not storage_pools: raise LXDPodError( "No storage pools exists. Please create a storage pool in LXD." ) # Discover Storage Pools. pools = [] storage_pools = yield deferToThread(client.storage_pools.all) local_storage = 0 for storage_pool in storage_pools: discovered_storage_pool = self.get_discovered_pod_storage_pool( storage_pool) local_storage += discovered_storage_pool.storage pools.append(discovered_storage_pool) discovered_pod.storage_pools = pools discovered_pod.local_storage = local_storage # Discover VMs. machines = [] virtual_machines = yield deferToThread(client.virtual_machines.all) for virtual_machine in virtual_machines: discovered_machine = yield self.get_discovered_machine( client, virtual_machine, storage_pools=discovered_pod.storage_pools, ) discovered_machine.cpu_speed = lxd_cpu_speed(resources) machines.append(discovered_machine) discovered_pod.machines = machines # Return the DiscoveredPod. return discovered_pod
def discover(self, pod_id: int, context: dict): """Discover all Pod host resources.""" # Connect to the Pod and make sure it is valid. client = self._get_client(pod_id, context) if not client.has_api_extension("virtual-machines"): raise LXDPodError( "Please upgrade your LXD host to 3.19+ for virtual machine support." ) self._ensure_project(client) # get MACs for host interfaces. "unknown" interfaces are considered too # to match ethernets in containers networks_state = [ net.state() for net in client.networks.all() if net.type in ("unknown", "physical") ] mac_addresses = list( {state.hwaddr for state in networks_state if state.hwaddr}) environment = client.host_info["environment"] # After the region creates the Pod object it will sync LXD commissioning # data for all hardware information. discovered_pod = DiscoveredPod( # client.host_info["environment"]["architectures"] reports all the # architectures the host CPU supports, not the architectures LXD # supports. On x86_64 LXD reports [x86_64, i686] however LXD does # not currently support VMs on i686. The LXD API currently does not # have a way to query which architectures are usable for VMs. The # safest bet is to just use the kernel_architecture. architectures=[ kernel_to_debian_architecture( environment["kernel_architecture"]) ], name=environment["server_name"], version=environment["server_version"], mac_addresses=mac_addresses, capabilities=[ Capabilities.COMPOSABLE, Capabilities.DYNAMIC_LOCAL_STORAGE, Capabilities.OVER_COMMIT, Capabilities.STORAGE_POOLS, ], ) # Check that we have at least one storage pool. # If not, user should be warned that they need to create one. storage_pools = client.storage_pools.all() if not storage_pools: raise LXDPodError( "No storage pools exists. Please create a storage pool in LXD." ) # Discover Storage Pools. pools = [] local_storage = 0 for storage_pool in storage_pools: discovered_storage_pool = self._get_discovered_pod_storage_pool( storage_pool) local_storage += discovered_storage_pool.storage pools.append(discovered_storage_pool) discovered_pod.storage_pools = pools discovered_pod.local_storage = local_storage host_cpu_speed = lxd_cpu_speed(client.resources) # Discover VMs. projects = [project.name for project in client.projects.all()] machines = [] for project in projects: project_cli = self._get_client(pod_id, context, project=project) for virtual_machine in project_cli.virtual_machines.all(): discovered_machine = self._get_discovered_machine( project_cli, virtual_machine, storage_pools=discovered_pod.storage_pools, ) discovered_machine.cpu_speed = host_cpu_speed machines.append(discovered_machine) discovered_pod.machines = machines # Return the DiscoveredPod. return discovered_pod