This commit is contained in:
ge 2023-07-29 15:35:36 +03:00
parent 3ac1cf5404
commit a0344b703f
10 changed files with 19 additions and 24 deletions

View File

@ -1,4 +1,3 @@
from .main import LibvirtSession from .main import LibvirtSession
from .vm import VirtualMachine, QemuAgent
from .config import ConfigLoader from .config import ConfigLoader
from .exceptions import * from .vm import *

View File

@ -3,13 +3,15 @@ import tomllib
from pathlib import Path from pathlib import Path
from collections import UserDict from collections import UserDict
from .exceptions import ConfigLoadError
NODEAGENT_CONFIG_FILE = os.getenv('NODEAGENT_CONFIG_FILE') NODEAGENT_CONFIG_FILE = os.getenv('NODEAGENT_CONFIG_FILE')
NODEAGENT_DEFAULT_CONFIG_FILE = '/etc/node-agent/config.toml' NODEAGENT_DEFAULT_CONFIG_FILE = '/etc/node-agent/config.toml'
class ConfigLoadError(Exception):
"""Bad config file syntax, unreachable file or bad data."""
class ConfigLoader(UserDict): class ConfigLoader(UserDict):
def __init__(self, file: Path | None = None): def __init__(self, file: Path | None = None):
if file is None: if file is None:

View File

@ -4,7 +4,10 @@ from contextlib import AbstractContextManager
import libvirt import libvirt
from .config import ConfigLoader from .config import ConfigLoader
from .exceptions import LibvirtSessionError
class LibvirtSessionError(Exception):
"""Something went wrong while connecting to libvirt."""
class LibvirtSession(AbstractContextManager): class LibvirtSession(AbstractContextManager):

View File

@ -21,8 +21,7 @@ import libvirt
from docopt import docopt from docopt import docopt
from ..main import LibvirtSession from ..main import LibvirtSession
from ..vm import VirtualMachine from ..vm import VirtualMachine, VMError, VMNotFound
from ..exceptions import VMError, VMNotFound
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -17,8 +17,7 @@ import logging
from docopt import docopt from docopt import docopt
from ..main import LibvirtSession from ..main import LibvirtSession
from ..vm import QemuAgent from ..vm import QemuAgent, QemuAgentError, VMNotFound
from ..exceptions import QemuAgentError, VMNotFound
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -1,2 +1,3 @@
from .main import VirtualMachine from .main import VirtualMachine
from .ga import QemuAgent from .ga import QemuAgent, QemuAgentError
from .exceptions import *

View File

@ -1,7 +1,7 @@
import libvirt import libvirt
from ..main import LibvirtSession from ..main import LibvirtSession
from ..exceptions import VMNotFound from .exceptions import VMNotFound
class VirtualMachineBase: class VirtualMachineBase:

View File

@ -1,9 +1,5 @@
class ConfigLoadError(Exception): class QemuAgentError(Exception):
"""Bad config file syntax, unreachable file or bad data.""" """Mostly QEMU Guest Agent is not responding."""
class LibvirtSessionError(Exception):
"""Something went wrong while connecting to libvirt."""
class VMError(Exception): class VMError(Exception):
@ -15,7 +11,3 @@ class VMNotFound(Exception):
self.domain = domain self.domain = domain
self.message = message.format(domain=domain) self.message = message.format(domain=domain)
super().__init__(self.message) super().__init__(self.message)
class QemuAgentError(Exception):
"""Mostly QEMU Guest Agent is not responding."""

View File

@ -7,8 +7,8 @@ import libvirt
import libvirt_qemu import libvirt_qemu
from ..main import LibvirtSession from ..main import LibvirtSession
from ..exceptions import QemuAgentError
from .base import VirtualMachineBase from .base import VirtualMachineBase
from .exceptions import QemuAgentError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -2,8 +2,8 @@ import logging
import libvirt import libvirt
from ..exceptions import VMError
from .base import VirtualMachineBase from .base import VirtualMachineBase
from .exceptions import VMError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -70,7 +70,7 @@ class VirtualMachine(VirtualMachineBase):
logger.debug('VM vm=%s is already started, nothing to do', self.domname) logger.debug('VM vm=%s is already started, nothing to do', self.domname)
return return
try: try:
ret = self.domain.create() self.domain.create()
except libvirt.libvirtError as err: except libvirt.libvirtError as err:
raise VMError(f'Cannot start vm={self.domname}: {err}') from err raise VMError(f'Cannot start vm={self.domname}: {err}') from err