Skip to content

Commit

Permalink
chore: Optimize cli by reducing HTTP calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lperdereau committed Dec 24, 2024
1 parent 8a83370 commit b02f170
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
27 changes: 24 additions & 3 deletions src/pvecontrol/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self, name, host, user, password, config, verify_ssl=False):
self.api = ProxmoxAPI(host, user=user, password=password, verify_ssl=verify_ssl)
self.name = name
self.config = config
self._tasks = []
self._initstatus()

def _initstatus(self):
Expand All @@ -22,7 +23,15 @@ def _initstatus(self):

self.nodes = []
for node in self.api.nodes.get():
self.nodes.append(PVENode(self.api, node["node"], node["status"], node))
self.nodes.append(
PVENode(
self.api,
node["node"],
node["status"],
node_resources=self.get_node_ressources(node["node"]),
kwargs=node,
)
)

self.storages = []
for storage in self.get_resources_storages():
Expand All @@ -34,14 +43,23 @@ def _initstatus(self):
"resources": self.api.cluster.ha.resources.get(),
}

self.tasks = []
@property
def tasks(self):
if self._tasks:
return self._tasks

for task in self.api.cluster.tasks.get():
logging.debug("Get task informations: %s", (str(task)))
self.tasks.append(PVETask(self.api, task["upid"]))
self._tasks.append(PVETask(self.api, task["upid"]))
return self._tasks

def refresh(self):
self._initstatus()

# force tasks refesh
self._tasks = []
_ = self.tasks

def __str__(self):
output = f"Proxmox VE Cluster {self.name}\n"
output += f" Status: {self.status}\n"
Expand Down Expand Up @@ -79,6 +97,9 @@ def is_healthy(self):
def get_resources_nodes(self):
return [resource for resource in self.resources if resource["type"] == "node"]

def get_node_ressources(self, node_name):
return [resource for resource in self.resources if resource["node"] == node_name]

def get_vm(self, vm_id):
if isinstance(vm_id, str):
vm_id = int(vm_id)
Expand Down
19 changes: 8 additions & 11 deletions src/pvecontrol/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ class PVENode:

_api = None

def __init__(self, api, node, status, kwargs=None):
def __init__(self, api, node, status, node_resources, kwargs=None):
if not kwargs:
kwargs = {}

self.node = node
self.resources = node_resources
self.status = NodeStatus[status.upper()]
self._api = api
self.cpu = kwargs.get("cpu", 0)
Expand Down Expand Up @@ -51,30 +52,26 @@ def __str__(self):
def _init_vms(self):
self.vms = []
if self.status == NodeStatus.ONLINE:
self.vms = [
PVEVm(self._api, self.node, vm["vmid"], vm["status"], vm)
for vm in self._api.nodes(self.node).qemu.get()
]
self.vms = [PVEVm(self._api, self.node, vm["vmid"], vm["status"], vm) for vm in self.get_resources_vms()]

def _init_allocatedmem(self):
"""Compute the amount of memory allocated to running VMs"""
self.allocatedmem = 0
for vm in self.vms:
if vm.status != VmStatus.RUNNING:
continue
# This is in MB in configuration
self.allocatedmem += int(vm.config["memory"]) * 1024 * 1024
self.allocatedmem += vm.maxmem

def _init_allocatedcpu(self):
"""Compute the amount of cpu allocated to running VMs"""
self.allocatedcpu = 0
for vm in self.vms:
if vm.status != VmStatus.RUNNING:
continue
if "sockets" in vm.config:
self.allocatedcpu += vm.config["sockets"] * vm.config["cores"]
else:
self.allocatedcpu += vm.config["cores"]
self.allocatedcpu += vm.cpus

def get_resources_vms(self):
return [resource for resource in self.resources if resource["type"] == "qemu"]

# def __contains__(self, item):
# """Check if a VM is running on this node"""
Expand Down
11 changes: 9 additions & 2 deletions src/pvecontrol/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ def __init__(self, api, node, vmid, status, kwargs=None):

self.name = kwargs.get("name", "")
self.lock = kwargs.get("lock", "")
self.cpus = kwargs.get("cpus", 0)
self.cpus = kwargs.get("maxcpu", 0)
self.maxdisk = kwargs.get("maxdisk", 0)
self.maxmem = kwargs.get("maxmem", 0)
self.uptime = kwargs.get("uptime", 0)
self.tags = kwargs.get("tags", "")
self.template = kwargs.get("template", 0)

self.config = self._api.nodes(self.node).qemu(vmid).config.get()
self._config = None

@property
def config(self):
if not self._config:
self._config = self._api.nodes(self.node).qemu(self.vmid).config.get()

return self._config

def __str__(self):
str_keys = [
Expand Down

0 comments on commit b02f170

Please sign in to comment.