python-compute/node_agent/vm/base.py

23 lines
724 B
Python
Raw Normal View History

2023-07-22 23:59:49 +03:00
import libvirt
2023-08-24 22:36:12 +03:00
from .exceptions import VMError, VMNotFound
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
def __init__(self, session: 'LibvirtSession', name: str):
2023-07-22 23:59:49 +03:00
self.domname = name
self.session = session.session # virConnect object
self.config = session.config # ConfigLoader object
self.domain = self._get_domain(name)
def _get_domain(self, name: str) -> libvirt.virDomain:
"""Get virDomain object by name to manipulate with domain."""
try:
domain = self.session.lookupByName(name)
if domain is not None:
return domain
raise VMNotFound(name)
except libvirt.libvirtError as err:
2023-08-24 22:36:12 +03:00
raise VMError(err) from err