Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

systemd.service operation: support masking/unmasking #1256

Open
bauen1 opened this issue Dec 19, 2024 · 2 comments
Open

systemd.service operation: support masking/unmasking #1256

bauen1 opened this issue Dec 19, 2024 · 2 comments

Comments

@bauen1
Copy link
Contributor

bauen1 commented Dec 19, 2024

Is your feature request related to a problem? Please describe

I have some systemd units that I need to stop and mask, to prevent them from being started at all.

Currently I do something like this:

def systemd_unit_enabled_state(unit: str) -> str | None:
    enabled_state = host.get_fact(Command, command=f"systemctl is-enabled {unit} || true")
    return enabled_state


def mydeploy() -> None:
    if systemd_unit_enabled_state("myunit.service") != "masked":
        server.shell(commands=['systemctl mask myunit.service'],
                     _sudo=True)

Describe the solution you'd like

Probably adding a masked argument to the systemd.service operation.
I suspect this might require improving some of the things going on behind the scenes, specifically querying unit status.

@xvello
Copy link

xvello commented Dec 28, 2024

The systemctl mask command creates a symlink to /dev/null (and it's a documented behaviour), so you can use this custom operation that will apply faster:

def mask_unit(unit_name: str) -> OperationMeta:
    return files.link(f"/etc/systemd/system/{unit_name}", "/dev/null", name=f"Mask {unit_name}")

Still worth adding to the built-in operation though.

@bauen1
Copy link
Contributor Author

bauen1 commented Dec 31, 2024

Right, I forgot about that, now I have the following:

def systemd_unit_enabled_state(unit: str) -> str | None:
    enabled_state = host.get_fact(Command, command=f"systemctl is-enabled {unit} || true")
    return enabled_state

@operation()
def systemd_mask_unit(unit_name: str) -> Generator:
    if systemd_unit_enabled_state(unit_name) == "masked":
        host.noop(f"unit '{unit_name}' is already masked")
    else:
        yield from files.link(f"/etc/systemd/system/{unit_name}", "/dev/null")

I don't think I will have to time to work on integrating it, but with the "directly symlink it" approach it should be much easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants