diff --git a/python/vyos/compose_config.py b/python/vyos/compose_config.py index b1c277bce..79a8718c5 100644 --- a/python/vyos/compose_config.py +++ b/python/vyos/compose_config.py @@ -1,88 +1,88 @@ # Copyright 2024 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library 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 # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this library. If not, see <http://www.gnu.org/licenses/>. """This module allows iterating over function calls to modify an existing config. """ import traceback from pathlib import Path from typing import TypeAlias, Union, Callable from vyos.configtree import ConfigTree from vyos.configtree import deep_copy as ct_deep_copy -from vyos.utils.system import load_as_module +from vyos.utils.system import load_as_module_source ConfigObj: TypeAlias = Union[str, ConfigTree] class ComposeConfigError(Exception): """Raised when an error occurs modifying a config object. """ class ComposeConfig: """Apply function to config tree: for iteration over functions or files. """ def __init__(self, config_obj: ConfigObj, checkpoint_file=None): if isinstance(config_obj, ConfigTree): self.config_tree = config_obj else: self.config_tree = ConfigTree(config_obj) self.checkpoint = self.config_tree self.checkpoint_file = checkpoint_file def apply_func(self, func: Callable): """Apply the function to the config tree. """ if not callable(func): raise ComposeConfigError(f'{func.__name__} is not callable') if self.checkpoint_file is not None: self.checkpoint = ct_deep_copy(self.config_tree) try: func(self.config_tree) except Exception as e: if self.checkpoint_file is not None: self.config_tree = self.checkpoint raise ComposeConfigError(e) from e def apply_file(self, func_file: str, func_name: str): """Apply named function from file. """ try: mod_name = Path(func_file).stem.replace('-', '_') - mod = load_as_module(mod_name, func_file) + mod = load_as_module_source(mod_name, func_file) func = getattr(mod, func_name) except Exception as e: raise ComposeConfigError(f'Error with {func_file}: {e}') from e try: self.apply_func(func) except ComposeConfigError as e: msg = str(e) tb = f'{traceback.format_exc()}' raise ComposeConfigError(f'Error in {func_file}: {msg}\n{tb}') from e def to_string(self, with_version=False) -> str: """Return the rendered config tree. """ return self.config_tree.to_string(no_version=not with_version) def write(self, config_file: str, with_version=False): """Write the config tree to a file. """ config_str = self.to_string(with_version=with_version) Path(config_file).write_text(config_str) diff --git a/python/vyos/utils/system.py b/python/vyos/utils/system.py index f427032a4..fca93d118 100644 --- a/python/vyos/utils/system.py +++ b/python/vyos/utils/system.py @@ -1,130 +1,141 @@ # Copyright 2023-2024 VyOS maintainers and contributors <maintainers@vyos.io> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library 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 # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. import os from subprocess import run def sysctl_read(name: str) -> str: """Read and return current value of sysctl() option Args: name (str): sysctl key name Returns: str: sysctl key value """ tmp = run(['sysctl', '-nb', name], capture_output=True) return tmp.stdout.decode() def sysctl_write(name: str, value: str | int) -> bool: """Change value via sysctl() Args: name (str): sysctl key name value (str | int): sysctl key value Returns: bool: True if changed, False otherwise """ # convert other types to string before comparison if not isinstance(value, str): value = str(value) # do not change anything if a value is already configured if sysctl_read(name) == value: return True # return False if sysctl call failed if run(['sysctl', '-wq', f'{name}={value}']).returncode != 0: return False # compare old and new values # sysctl may apply value, but its actual value will be # different from requested if sysctl_read(name) == value: return True # False in other cases return False def sysctl_apply(sysctl_dict: dict[str, str], revert: bool = True) -> bool: """Apply sysctl values. Args: sysctl_dict (dict[str, str]): dictionary with sysctl keys with values revert (bool, optional): Revert to original values if new were not applied. Defaults to True. Returns: bool: True if all params configured properly, False in other cases """ # get current values sysctl_original: dict[str, str] = {} for key_name in sysctl_dict.keys(): sysctl_original[key_name] = sysctl_read(key_name) # apply new values and revert in case one of them was not applied for key_name, value in sysctl_dict.items(): if not sysctl_write(key_name, value): if revert: sysctl_apply(sysctl_original, revert=False) return False # everything applied return True def find_device_file(device): """ Recurively search /dev for the given device file and return its full path. If no device file was found 'None' is returned """ from fnmatch import fnmatch for root, dirs, files in os.walk('/dev'): for basename in files: if fnmatch(basename, device): return os.path.join(root, basename) return None def load_as_module(name: str, path: str): import importlib.util spec = importlib.util.spec_from_file_location(name, path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod +def load_as_module_source(name: str, path: str): + """ Necessary modification of load_as_module for files without *.py + extension """ + import importlib.util + from importlib.machinery import SourceFileLoader + + loader = SourceFileLoader(name, path) + spec = importlib.util.spec_from_loader(name, loader) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + def get_uptime_seconds(): """ Returns system uptime in seconds """ from re import search from vyos.utils.file import read_file data = read_file("/proc/uptime") seconds = search(r"([0-9\.]+)\s", data).group(1) res = int(float(seconds)) return res def get_load_averages(): """ Returns load averages for 1, 5, and 15 minutes as a dict """ from re import search from vyos.utils.file import read_file from vyos.utils.cpu import get_core_count data = read_file("/proc/loadavg") matches = search(r"\s*(?P<one>[0-9\.]+)\s+(?P<five>[0-9\.]+)\s+(?P<fifteen>[0-9\.]+)\s*", data) core_count = get_core_count() res = {} res[1] = float(matches["one"]) / core_count res[5] = float(matches["five"]) / core_count res[15] = float(matches["fifteen"]) / core_count return res -