various improvements

This commit is contained in:
ge
2023-11-11 02:28:46 +03:00
parent 0d2a18d1f3
commit bd4e575a7c
23 changed files with 453 additions and 37 deletions

View File

@ -7,7 +7,7 @@ from typing import NamedTuple
import libvirt
from lxml import etree
from compute.exceptions import StoragePoolError
from compute.exceptions import StoragePoolError, VolumeNotFoundError
from .volume import Volume, VolumeConfig
@ -99,13 +99,8 @@ class StoragePool:
vol = self.pool.storageVolLookupByName(name)
return Volume(self.pool, vol)
except libvirt.libvirtError as e:
# TODO @ge: Raise VolumeNotFoundError instead
if (
e.get_error_domain() == libvirt.VIR_FROM_STORAGE
or e.get_error_code() == libvirt.VIR_ERR_NO_STORAGE_VOL
):
log.exception(e.get_error_message())
return None
if e.get_error_code() == libvirt.VIR_ERR_NO_STORAGE_VOL:
raise VolumeNotFoundError(name) from e
log.exception('unexpected error from libvirt')
raise StoragePoolError(e) from e

View File

@ -56,15 +56,17 @@ class DiskConfig:
to compute instances.
"""
disk_type: str
source: str | Path
target: str
path: str
readonly: bool = False
def to_xml(self) -> str:
"""Return XML config for libvirt."""
xml = E.disk(type='file', device='disk')
xml = E.disk(type=self.disk_type, device='disk')
xml.append(E.driver(name='qemu', type='qcow2', cache='writethrough'))
xml.append(E.source(file=self.path))
if self.disk_type == 'file':
xml.append(E.source(file=str(self.source)))
xml.append(E.target(dev=self.target, bus='virtio'))
if self.readonly:
xml.append(E.readonly())