From 7c44d733a91cfe09badebf6d2e8ea96a551fbada Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Sun, 10 Apr 2022 08:08:05 +0300 Subject: aiohelpers --- kvmd/aiohelpers.py | 50 +++++++++++++++++++++++++++++++++++++ kvmd/plugins/msd/otg/__init__.py | 5 ++-- kvmd/plugins/msd/otg/helpers.py | 54 ---------------------------------------- 3 files changed, 53 insertions(+), 56 deletions(-) create mode 100644 kvmd/aiohelpers.py delete mode 100644 kvmd/plugins/msd/otg/helpers.py (limited to 'kvmd') diff --git a/kvmd/aiohelpers.py b/kvmd/aiohelpers.py new file mode 100644 index 00000000..6357764c --- /dev/null +++ b/kvmd/aiohelpers.py @@ -0,0 +1,50 @@ +# ========================================================================== # +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +# ========================================================================== # + + +import subprocess + +from typing import List + +from .logging import get_logger + +from . import tools +from . import aioproc + + +# ===== +async def remount(name: str, base_cmd: List[str], rw: bool) -> bool: + logger = get_logger(1) + mode = ("rw" if rw else "ro") + cmd = [ + part.format(mode=mode) + for part in base_cmd + ] + logger.info("Remounting %s storage to %s: %s ...", name, mode.upper(), cmd) + try: + proc = await aioproc.log_process(cmd, logger) + if proc.returncode != 0: + assert proc.returncode is not None + raise subprocess.CalledProcessError(proc.returncode, cmd) + except Exception as err: + logger.error("Can't remount %s storage: %s", name, tools.efmt(err)) + return False + return True diff --git a/kvmd/plugins/msd/otg/__init__.py b/kvmd/plugins/msd/otg/__init__.py index dcf70557..0b81391d 100644 --- a/kvmd/plugins/msd/otg/__init__.py +++ b/kvmd/plugins/msd/otg/__init__.py @@ -46,6 +46,7 @@ from ....validators.os import valid_printable_filename from ....validators.os import valid_command from .... import aiotools +from .... import aiohelpers from .. import MsdError from .. import MsdIsBusyError @@ -59,7 +60,6 @@ from .. import BaseMsd from .. import MsdImageWriter from . import fs -from . import helpers from .drive import Drive @@ -538,4 +538,5 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes # ===== async def __remount_storage(self, rw: bool) -> None: - await helpers.remount_storage(self.__remount_cmd, rw) + if not (await aiohelpers.remount("MSD", self.__remount_cmd, rw)): + raise MsdError("Can't execute remount helper") diff --git a/kvmd/plugins/msd/otg/helpers.py b/kvmd/plugins/msd/otg/helpers.py deleted file mode 100644 index 2d64bd0c..00000000 --- a/kvmd/plugins/msd/otg/helpers.py +++ /dev/null @@ -1,54 +0,0 @@ -# ========================================================================== # -# # -# KVMD - The main PiKVM daemon. # -# # -# Copyright (C) 2018-2022 Maxim Devaev # -# # -# This program is free software: you can redistribute it and/or modify # -# it under the terms of the GNU General Public License as published by # -# the Free Software Foundation, either version 3 of the License, or # -# (at your option) any later version. # -# # -# This program is distributed in the hope that it will be useful, # -# but WITHOUT ANY WARRANTY; without even the implied warranty of # -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # -# GNU General Public License for more details. # -# # -# You should have received a copy of the GNU General Public License # -# along with this program. If not, see . # -# # -# ========================================================================== # - - -from typing import List - -from ....logging import get_logger - -from .... import aioproc - -from .. import MsdError - - -# ===== -async def remount_storage(base_cmd: List[str], rw: bool) -> None: - logger = get_logger(0) - mode = ("rw" if rw else "ro") - cmd = [ - part.format(mode=mode) - for part in base_cmd - ] - logger.info("Remounting internal storage to %s ...", mode.upper()) - try: - await _run_helper(cmd) - except Exception: - logger.error("Can't remount internal storage") - raise - - -# ===== -async def _run_helper(cmd: List[str]) -> None: - logger = get_logger(0) - logger.info("Executing helper %s ...", cmd) - proc = await aioproc.log_process(cmd, logger) - if proc.returncode != 0: - raise MsdError(f"Error while helper execution: pid={proc.pid}; retcode={proc.returncode}") -- cgit v1.2.3