From c584ff60f77914c50f6f2ae1f0da81c912610b97 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sun, 16 Apr 2023 00:10:13 +0200 Subject: [PATCH] current try --- plugins/module_utils/module.py | 5 ++++- plugins/modules/systemd_link.py | 12 ++++++++---- plugins/modules/systemd_mount.py | 13 ++++++++----- plugins/modules/systemd_network.py | 12 ++++++++---- update_doc | 10 +++++----- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/plugins/module_utils/module.py b/plugins/module_utils/module.py index ebc20de..92ea4bf 100644 --- a/plugins/module_utils/module.py +++ b/plugins/module_utils/module.py @@ -2,7 +2,10 @@ import pathlib from typing import Any, Callable, ClassVar, Dict, Optional, TypeVar, NoReturn, overload import ansible.module_utils.basic as basic -from ansible.module_utils.generic import _sdict +try: + from ansible.module_utils.generic import _sdict +except ImportError: + from plugins.module_utils.generic import _sdict __all__ = ( "AnsibleModule", diff --git a/plugins/modules/systemd_link.py b/plugins/modules/systemd_link.py index 49d240f..4386521 100644 --- a/plugins/modules/systemd_link.py +++ b/plugins/modules/systemd_link.py @@ -2,9 +2,13 @@ import pathlib from typing import List -from ansible.module_utils.generic import SYSTEMD_NETWORK_CONFIG as SYSTEMD_PATH -from ansible.module_utils.generic import Types -from ansible.module_utils.module import SystemdUnitModule +try: + from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types + from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule +except ImportError: + from plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types + from plugins.module_utils.module import SystemdUnitModule + class Module(SystemdUnitModule): """generates an systemd-networkd link""" @@ -44,7 +48,7 @@ class Module(SystemdUnitModule): self.get("name", "") or self.get("mac", "") or self.get("permanentmac", "") ) newname = newname.replace(":", "").replace("/", "-").lower() - self.unitfile = SYSTEMD_PATH.joinpath("50-" + newname).with_suffix(".link") + self.unitfile = SYSTEMD_NETWORK_CONFIG.joinpath("50-" + newname).with_suffix(".link") def unit(self) -> str: if self.__unit is None: diff --git a/plugins/modules/systemd_mount.py b/plugins/modules/systemd_mount.py index bf5a248..61bda2e 100644 --- a/plugins/modules/systemd_mount.py +++ b/plugins/modules/systemd_mount.py @@ -2,11 +2,14 @@ import pathlib from typing import List, Optional -from ansible.module_utils.generic import SYSTEMD_SERVICE_CONFIG as SYSTEMD_PATH -from ansible.module_utils.generic import Types -from ansible.module_utils.module import SystemdUnitModule +try: + from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types + from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule +except ImportError: + from plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types + from plugins.module_utils.module import SystemdUnitModule -SYSTEMD_PATH = pathlib.Path("/etc/systemd/system") +SYSTEMD_SERVICE_CONFIG = pathlib.Path("/etc/systemd/system") OPTION_MAPPING = dict( required_by="RequiredBy", @@ -50,7 +53,7 @@ class Module(SystemdUnitModule): def prepare(self): self.mountdir = pathlib.Path(self.params["where"]) - self.unitfile = SYSTEMD_PATH.joinpath( + self.unitfile = SYSTEMD_SERVICE_CONFIG.joinpath( self.mountdir.relative_to("/").as_posix().replace("/", "-") ).with_suffix(".mount") self.__unit = None diff --git a/plugins/modules/systemd_network.py b/plugins/modules/systemd_network.py index 13e3cad..95e6541 100644 --- a/plugins/modules/systemd_network.py +++ b/plugins/modules/systemd_network.py @@ -2,9 +2,13 @@ import pathlib from typing import List, Union -from ansible.module_utils.generic import SYSTEMD_NETWORK_CONFIG as SYSTEMD_PATH -from ansible.module_utils.generic import Types -from ansible.module_utils.module import SystemdUnitModule +try: + from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types + from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule +except ImportError: + from plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types + from plugins.module_utils.module import SystemdUnitModule + def boolconvert(b: Union[bool, str]) -> str: if b is True: @@ -37,7 +41,7 @@ class Module(SystemdUnitModule): ) def prepare(self): - self.unitfile = SYSTEMD_PATH.joinpath(self.get("name")).with_suffix(".network") + self.unitfile = SYSTEMD_NETWORK_CONFIG.joinpath(self.get("name")).with_suffix(".network") self.__unit = None def unit(self) -> str: diff --git a/update_doc b/update_doc index 5cd22e7..7fd5223 100755 --- a/update_doc +++ b/update_doc @@ -3,12 +3,9 @@ import re import pathlib import importlib import sys -import ansible.module_utils -mindocstring = "DOCUMENTATION = ''''''" -sys.path.append("plugins/modules") sys.path.append(".") - +mindocstring = "DOCUMENTATION = ''''''" moduledir = pathlib.Path("plugins/modules") regex = re.compile("DOCUMENTATION *= *r?(?P\"{3}|'{3})(---)?.*?(?P=quote)", re.MULTILINE | re.DOTALL) @@ -16,11 +13,14 @@ if __name__ == "__main__": for modfile in moduledir.iterdir(): if modfile.name in ( "__init__.py", "__pycache__"): continue - mod = importlib.import_module("plugins.modules."+modfile.stem) + mod = importlib.import_module(".".join((modfile.parts[:-1])+(modfile.stem,))) if hasattr(mod, "Module"): module = mod.Module elif hasattr(mod, "__module_name__"): module = getattr(mod, mod.__module_name__) + else: + print("Error loading Module of File: {}. No Module or __module_name__ defined".format(modfile)) + continue moddoc = module.doc() moddata = modfile.read_text() match = regex.search(moddata)