From a0344b703f98e22339cbcadfde85b92944b28bc3 Mon Sep 17 00:00:00 2001 From: ge Date: Sat, 29 Jul 2023 15:35:36 +0300 Subject: [PATCH] refactor --- node_agent/__init__.py | 3 +-- node_agent/config.py | 6 ++++-- node_agent/main.py | 5 ++++- node_agent/utils/vmctl.py | 3 +-- node_agent/utils/vmexec.py | 3 +-- node_agent/vm/__init__.py | 3 ++- node_agent/vm/base.py | 2 +- node_agent/{ => vm}/exceptions.py | 12 ++---------- node_agent/vm/ga.py | 2 +- node_agent/vm/main.py | 4 ++-- 10 files changed, 19 insertions(+), 24 deletions(-) rename node_agent/{ => vm}/exceptions.py (66%) diff --git a/node_agent/__init__.py b/node_agent/__init__.py index c8d207e..8aae7b9 100644 --- a/node_agent/__init__.py +++ b/node_agent/__init__.py @@ -1,4 +1,3 @@ from .main import LibvirtSession -from .vm import VirtualMachine, QemuAgent from .config import ConfigLoader -from .exceptions import * +from .vm import * diff --git a/node_agent/config.py b/node_agent/config.py index e7b78b0..aad7de8 100644 --- a/node_agent/config.py +++ b/node_agent/config.py @@ -3,13 +3,15 @@ import tomllib from pathlib import Path from collections import UserDict -from .exceptions import ConfigLoadError - NODEAGENT_CONFIG_FILE = os.getenv('NODEAGENT_CONFIG_FILE') NODEAGENT_DEFAULT_CONFIG_FILE = '/etc/node-agent/config.toml' +class ConfigLoadError(Exception): + """Bad config file syntax, unreachable file or bad data.""" + + class ConfigLoader(UserDict): def __init__(self, file: Path | None = None): if file is None: diff --git a/node_agent/main.py b/node_agent/main.py index 9ff169e..798976c 100644 --- a/node_agent/main.py +++ b/node_agent/main.py @@ -4,7 +4,10 @@ from contextlib import AbstractContextManager import libvirt from .config import ConfigLoader -from .exceptions import LibvirtSessionError + + +class LibvirtSessionError(Exception): + """Something went wrong while connecting to libvirt.""" class LibvirtSession(AbstractContextManager): diff --git a/node_agent/utils/vmctl.py b/node_agent/utils/vmctl.py index b3e8682..b1053cd 100644 --- a/node_agent/utils/vmctl.py +++ b/node_agent/utils/vmctl.py @@ -21,8 +21,7 @@ import libvirt from docopt import docopt from ..main import LibvirtSession -from ..vm import VirtualMachine -from ..exceptions import VMError, VMNotFound +from ..vm import VirtualMachine, VMError, VMNotFound logger = logging.getLogger(__name__) diff --git a/node_agent/utils/vmexec.py b/node_agent/utils/vmexec.py index f8b4e1e..9de4c62 100644 --- a/node_agent/utils/vmexec.py +++ b/node_agent/utils/vmexec.py @@ -17,8 +17,7 @@ import logging from docopt import docopt from ..main import LibvirtSession -from ..vm import QemuAgent -from ..exceptions import QemuAgentError, VMNotFound +from ..vm import QemuAgent, QemuAgentError, VMNotFound logger = logging.getLogger(__name__) diff --git a/node_agent/vm/__init__.py b/node_agent/vm/__init__.py index 3feeb85..63731fb 100644 --- a/node_agent/vm/__init__.py +++ b/node_agent/vm/__init__.py @@ -1,2 +1,3 @@ from .main import VirtualMachine -from .ga import QemuAgent +from .ga import QemuAgent, QemuAgentError +from .exceptions import * diff --git a/node_agent/vm/base.py b/node_agent/vm/base.py index db26a68..81f1126 100644 --- a/node_agent/vm/base.py +++ b/node_agent/vm/base.py @@ -1,7 +1,7 @@ import libvirt from ..main import LibvirtSession -from ..exceptions import VMNotFound +from .exceptions import VMNotFound class VirtualMachineBase: diff --git a/node_agent/exceptions.py b/node_agent/vm/exceptions.py similarity index 66% rename from node_agent/exceptions.py rename to node_agent/vm/exceptions.py index e50b223..b88b8be 100644 --- a/node_agent/exceptions.py +++ b/node_agent/vm/exceptions.py @@ -1,9 +1,5 @@ -class ConfigLoadError(Exception): - """Bad config file syntax, unreachable file or bad data.""" - - -class LibvirtSessionError(Exception): - """Something went wrong while connecting to libvirt.""" +class QemuAgentError(Exception): + """Mostly QEMU Guest Agent is not responding.""" class VMError(Exception): @@ -15,7 +11,3 @@ class VMNotFound(Exception): self.domain = domain self.message = message.format(domain=domain) super().__init__(self.message) - - -class QemuAgentError(Exception): - """Mostly QEMU Guest Agent is not responding.""" diff --git a/node_agent/vm/ga.py b/node_agent/vm/ga.py index 93865c5..a262c63 100644 --- a/node_agent/vm/ga.py +++ b/node_agent/vm/ga.py @@ -7,8 +7,8 @@ import libvirt import libvirt_qemu from ..main import LibvirtSession -from ..exceptions import QemuAgentError from .base import VirtualMachineBase +from .exceptions import QemuAgentError logger = logging.getLogger(__name__) diff --git a/node_agent/vm/main.py b/node_agent/vm/main.py index 9e12fe4..23fc94a 100644 --- a/node_agent/vm/main.py +++ b/node_agent/vm/main.py @@ -2,8 +2,8 @@ import logging import libvirt -from ..exceptions import VMError from .base import VirtualMachineBase +from .exceptions import VMError logger = logging.getLogger(__name__) @@ -70,7 +70,7 @@ class VirtualMachine(VirtualMachineBase): logger.debug('VM vm=%s is already started, nothing to do', self.domname) return try: - ret = self.domain.create() + self.domain.create() except libvirt.libvirtError as err: raise VMError(f'Cannot start vm={self.domname}: {err}') from err