python-compute/node_agent/vm/base.py

32 lines
942 B
Python
Raw Normal View History

2023-07-22 23:59:49 +03:00
import libvirt
2023-08-27 23:42:56 +03:00
from .exceptions import VMError
2023-07-22 23:59:49 +03:00
2023-07-29 14:29:37 +03:00
class VirtualMachineBase:
2023-08-24 22:36:12 +03:00
2023-08-27 23:42:56 +03:00
def __init__(self, domain: libvirt.virDomain):
self.domain = domain
self.domain_name = self._get_domain_name()
self.domain_info = self._get_domain_info()
2023-07-22 23:59:49 +03:00
2023-08-27 23:42:56 +03:00
def _get_domain_name(self):
2023-07-22 23:59:49 +03:00
try:
2023-08-27 23:42:56 +03:00
return self.domain.name()
2023-07-22 23:59:49 +03:00
except libvirt.libvirtError as err:
2023-08-27 23:42:56 +03:00
raise VMError(f'Cannot get domain name: {err}') from err
def _get_domain_info(self):
# https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainInfo
try:
info = self.domain.info()
return {
'state': info[0],
'max_memory': info[1],
'memory': info[2],
'nproc': info[3],
'cputime': info[4]
}
except libvirt.libvirtError as err:
raise VMError(f'Cannot get domain info: {err}') from err