2023-04-14 22:58:51 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import pathlib
|
|
|
|
from typing import List, Optional
|
2023-04-15 17:30:04 +00:00
|
|
|
|
2024-02-11 13:35:00 +00:00
|
|
|
try:
|
2024-02-11 13:39:52 +00:00
|
|
|
from ansible_module.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, modspec
|
|
|
|
from ansible_module.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable
|
2024-02-11 13:35:00 +00:00
|
|
|
except ImportError:
|
2024-02-11 19:59:50 +00:00
|
|
|
from ansible_collections.sebastian.base.plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, modspec
|
|
|
|
from ansible_collections.sebastian.base.plugins.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable
|
2023-04-14 22:58:51 +00:00
|
|
|
|
2023-04-15 22:10:13 +00:00
|
|
|
SYSTEMD_SERVICE_CONFIG = pathlib.Path("/etc/systemd/system")
|
2023-04-14 22:58:51 +00:00
|
|
|
|
|
|
|
OPTION_MAPPING = dict(
|
|
|
|
required_by="RequiredBy",
|
|
|
|
wanted_by="WantedBy",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-04-20 22:19:35 +00:00
|
|
|
@installable
|
2024-03-08 23:13:24 +00:00
|
|
|
class Module(SystemdUnitModule, SystemdReloadMixin): # type: ignore[misc]
|
2023-04-14 22:58:51 +00:00
|
|
|
"""Creates an systemd mount"""
|
|
|
|
|
2023-04-20 20:09:58 +00:00
|
|
|
name = "mount"
|
2023-04-23 08:32:42 +00:00
|
|
|
module_spec = modspec(
|
2023-04-14 22:58:51 +00:00
|
|
|
argument_spec=dict(
|
2023-04-20 22:17:36 +00:00
|
|
|
fs=Types.str(required=True, help="The filesystem that is used for the mount"),
|
|
|
|
where=Types.path(required=True, help="The Path where the filesystem is mounted to"),
|
|
|
|
what=Types.str(required=True, help="The device or an string that will be mounted"),
|
2023-04-14 22:58:51 +00:00
|
|
|
state=Types.str(
|
|
|
|
choices=("present", "absent"),
|
|
|
|
default="present",
|
|
|
|
help="the state the mount is",
|
|
|
|
),
|
|
|
|
options=Types.list(elements=str, help="The options for the mount"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def prepare(self):
|
|
|
|
self.mountdir = pathlib.Path(self.params["where"])
|
2023-04-20 22:17:36 +00:00
|
|
|
self.unitfile = SYSTEMD_SERVICE_CONFIG.joinpath(self.mountdir.relative_to("/").as_posix().replace("/", "-")).with_suffix(".mount")
|
2023-04-14 22:58:51 +00:00
|
|
|
self.__unit = None
|
2023-04-20 22:21:55 +00:00
|
|
|
if self.get("description", False) is False:
|
2023-05-01 08:29:21 +00:00
|
|
|
self.params["description"] = "Mount for {}".format(self.mountdir.as_posix())
|
2023-04-14 22:58:51 +00:00
|
|
|
|
|
|
|
def unit(self) -> str:
|
|
|
|
if self.__unit is None:
|
2024-03-08 23:13:24 +00:00
|
|
|
self.__unit = self._unit(
|
|
|
|
self.header(),
|
|
|
|
self.mount(),
|
|
|
|
self.install(), # type: ignore[misc,call-arg]
|
2023-04-14 22:58:51 +00:00
|
|
|
)
|
|
|
|
return self.__unit
|
|
|
|
|
|
|
|
def header(self) -> str:
|
2024-03-08 23:13:24 +00:00
|
|
|
description = self.get("description", "Mount for {}".format(self.get("where")))
|
|
|
|
return f"[Unit]\nDescription={description}\n"
|
2023-04-14 22:58:51 +00:00
|
|
|
|
|
|
|
def mount(self) -> str:
|
2024-03-08 23:13:24 +00:00
|
|
|
options = []
|
|
|
|
options.append("Where={}\n".format(self.get("where")))
|
|
|
|
options.append("What={}\n".format(self.get("what")))
|
|
|
|
options.append("Type={}\n".format(self.get("fs")))
|
2023-04-14 22:58:51 +00:00
|
|
|
if self.get("options", False):
|
2024-03-08 23:13:24 +00:00
|
|
|
options.append("Options={}\n".format(",".join(self.get("options"))))
|
|
|
|
return "[Mount]\n" + "".join(options)
|
2023-04-14 22:58:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = """---
|
|
|
|
description:
|
|
|
|
- Creates an systemd mount
|
2023-04-20 20:09:58 +00:00
|
|
|
module: mount
|
2023-04-14 22:58:51 +00:00
|
|
|
options:
|
2023-04-20 22:19:35 +00:00
|
|
|
after:
|
|
|
|
default: []
|
|
|
|
description:
|
|
|
|
- list of units that this unit wants to be started after this unit
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
type: list
|
|
|
|
before:
|
|
|
|
default: []
|
|
|
|
description:
|
|
|
|
- list of units that this unit needs to be started before this unit.
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
type: list
|
2023-04-14 22:58:51 +00:00
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- An description for programs that access systemd
|
|
|
|
required: false
|
|
|
|
type: str
|
2023-04-20 22:19:35 +00:00
|
|
|
documentation:
|
|
|
|
default: []
|
|
|
|
description:
|
|
|
|
- Paths where documentation can be found
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
type: list
|
2023-04-14 22:58:51 +00:00
|
|
|
fs:
|
|
|
|
description:
|
|
|
|
- The filesystem that is used for the mount
|
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
options:
|
|
|
|
default: []
|
|
|
|
description:
|
|
|
|
- The options for the mount
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
type: list
|
2023-04-20 22:19:35 +00:00
|
|
|
partof:
|
|
|
|
default: []
|
|
|
|
description:
|
|
|
|
- list of units that this unit is part of.
|
|
|
|
- If the restart this unit does it too, but if this restarts it does not affect
|
|
|
|
the other units.
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
type: list
|
2023-04-14 22:58:51 +00:00
|
|
|
required_by:
|
|
|
|
default: []
|
|
|
|
description:
|
|
|
|
- systemd units that require this mount
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
type: list
|
2023-04-20 22:19:35 +00:00
|
|
|
requires:
|
|
|
|
default: []
|
|
|
|
description:
|
|
|
|
- list of units that this unit requires. If it fails or can't be started this
|
|
|
|
unit fails. without before/after this is started at the same time
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
type: list
|
2023-04-14 22:58:51 +00:00
|
|
|
state:
|
|
|
|
choices:
|
|
|
|
- present
|
|
|
|
- absent
|
|
|
|
default: present
|
|
|
|
description:
|
|
|
|
- the state the mount is
|
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
wanted_by:
|
|
|
|
default: []
|
|
|
|
description:
|
|
|
|
- systemd units that want the mount, but not explicitly require it. Commonly used
|
|
|
|
for target if not service explicitly require it.
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
type: list
|
2023-04-20 22:19:35 +00:00
|
|
|
wants:
|
|
|
|
default: []
|
|
|
|
description:
|
|
|
|
- list of units that this unit wants. If it fails or can't be started it does
|
|
|
|
not affect this unit
|
|
|
|
elements: str
|
|
|
|
required: false
|
|
|
|
type: list
|
2023-04-14 22:58:51 +00:00
|
|
|
what:
|
|
|
|
description:
|
|
|
|
- The device or an string that will be mounted
|
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
where:
|
|
|
|
description:
|
|
|
|
- The Path where the filesystem is mounted to
|
|
|
|
required: true
|
|
|
|
type: path
|
|
|
|
short_description: Creates an systemd mount
|
|
|
|
"""
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Module()()
|