python-compute/node_agent/vm/main.py

155 lines
4.9 KiB
Python
Raw Normal View History

2023-07-22 23:59:49 +03:00
import logging
import libvirt
from ..exceptions import VMError
2023-07-29 14:29:37 +03:00
from .base import VirtualMachineBase
2023-07-22 23:59:49 +03:00
logger = logging.getLogger(__name__)
2023-07-29 14:29:37 +03:00
class VirtualMachine(VirtualMachineBase):
2023-07-22 23:59:49 +03:00
@property
def name(self):
2023-07-28 01:01:32 +03:00
return self.domname
2023-07-22 23:59:49 +03:00
@property
def status(self) -> str:
"""
Return VM state: 'running', 'shutoff', etc. Reference:
https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainState
"""
2023-07-28 01:01:32 +03:00
try:
# libvirt returns list [state: int, reason: int]
# https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainGetState
state = self.domain.state()[0]
except libvirt.libvirtError as err:
raise VMError(f'Cannot fetch VM status vm={self.domname}: {err}') from err
2023-07-22 23:59:49 +03:00
match state:
case libvirt.VIR_DOMAIN_NOSTATE:
return 'nostate'
case libvirt.VIR_DOMAIN_RUNNING:
return 'running'
case libvirt.VIR_DOMAIN_BLOCKED:
return 'blocked'
case libvirt.VIR_DOMAIN_PAUSED:
return 'paused'
case libvirt.VIR_DOMAIN_SHUTDOWN:
return 'shutdown'
case libvirt.VIR_DOMAIN_SHUTOFF:
return 'shutoff'
case libvirt.VIR_DOMAIN_CRASHED:
return 'crashed'
case libvirt.VIR_DOMAIN_PMSUSPENDED:
return 'pmsuspended'
@property
def is_running(self) -> bool:
"""Return True if VM is running, else return False."""
if self.domain.isActive() != 1:
# inactive (0) or error (-1)
return False
return True
2023-07-28 01:01:32 +03:00
@property
def is_autostart(self) -> bool:
"""Return True if VM autostart is enabled, else return False."""
try:
if self.domain.autostart() == 1:
return True
return False
except libvirt.libvirtError as err:
raise VMError(f'Cannot get autostart status vm={self.domname}: {err}') from err
2023-07-22 23:59:49 +03:00
def start(self) -> None:
"""Start defined VM."""
logger.info('Starting VM: vm=%s', self.domname)
if self.is_running:
logger.debug('VM vm=%s is already started, nothing to do', self.domname)
return
try:
ret = self.domain.create()
except libvirt.libvirtError as err:
2023-07-29 14:29:37 +03:00
raise VMError(f'Cannot start vm={self.domname}: {err}') from err
2023-07-22 23:59:49 +03:00
def shutdown(self, force=False, sigkill=False) -> None:
"""
Send ACPI signal to guest OS to shutdown. OS may ignore this.
Use `force=True` for graceful VM destroy. Add `sigkill=True`
to hard shutdown (may corrupt guest data!).
"""
if sigkill:
flags = libvirt.VIR_DOMAIN_DESTROY_DEFAULT
else:
flags = libvirt.VIR_DOMAIN_DESTROY_GRACEFUL
2023-07-28 01:01:32 +03:00
try:
if force:
self.domain.destroyFlags(flags=flags)
else:
# Normal VM shutdown via ACPI signal, OS may ignore this.
self.domain.shutdown()
except libvirt.libvirtError as err:
2023-07-22 23:59:49 +03:00
raise VMError(
2023-07-28 01:01:32 +03:00
f'Cannot shutdown vm={self.domname} '
f'force={force} sigkill={sigkill}: {err}'
) from err
2023-07-22 23:59:49 +03:00
def reset(self):
"""
2023-07-28 01:01:32 +03:00
Copypaste from libvirt doc:
2023-07-22 23:59:49 +03:00
2023-07-28 01:01:32 +03:00
Reset a domain immediately without any guest OS shutdown.
Reset emulates the power reset button on a machine, where all
hardware sees the RST line set and reinitializes internal state.
2023-07-22 23:59:49 +03:00
2023-07-28 01:01:32 +03:00
Note that there is a risk of data loss caused by reset without any
guest OS shutdown.
2023-07-22 23:59:49 +03:00
"""
2023-07-28 01:01:32 +03:00
try:
2023-07-29 14:29:37 +03:00
self.domain.reset()
2023-07-28 01:01:32 +03:00
except libvirt.libvirtError as err:
raise VMError(f'Cannot reset vm={self.domname}: {err}') from err
2023-07-22 23:59:49 +03:00
def reboot(self) -> None:
"""Send ACPI signal to guest OS to reboot. OS may ignore this."""
2023-07-28 01:01:32 +03:00
try:
self.domain.reboot()
except libvirt.libvirtError as err:
raise VMError(f'Cannot reboot vm={self.domname}: {err}') from err
def autostart(self, enabled: bool) -> None:
"""
Configure VM to be automatically started when the host machine boots.
"""
if enabled:
autostart_flag = 1
else:
autostart_flag = 0
try:
self.domain.setAutostart(autostart_flag)
except libvirt.libvirtError as err:
raise VMError(
f'Cannot set autostart vm={self.domname} '
f'autostart={autostart_flag}: {err}'
) from err
2023-07-22 23:59:49 +03:00
def vcpu_set(self, count: int):
pass
def vram_set(self, count: int):
pass
def ssh_keys_list(self, user: str):
pass
def ssh_keys_add(self, user: str):
pass
def ssh_keys_remove(self, user: str):
pass
def set_user_password(self, user: str):
pass