liab-deployments/liab_deployments/operations/user_systemd.py
Michael Hanke a41475dd6c
feat: credential_file operation
This factors out functionality previously implemented inside the Git
credential tools. It is now usable for systemd-compatible credential
files.
2026-09-30 13:08:35 +02:00

150 lines
3.7 KiB
Python

from io import StringIO
from os import environ
from pyinfra import host
from pyinfra.api import (
DeployError,
operation,
)
from pyinfra.operations import (
files,
systemd,
)
@operation()
def credential_file(
path: str,
env_var: str,
user: str | None = None,
group: str | None = None,
):
"""Retrieve a credential from an env var to deposit for LoadCredential
The credential file is placed at `path`, and contains the content of the
given `env_var`.
If this environment variable is not set, the operation will still
check of the target credential file exists with correct access mode.
"""
cred = environ.get(env_var)
if not cred:
info = host.get_fact(files.File, path)
if info is None:
msg = f'No credential found at {path!r}, set {env_var!r} to deposit one.'
raise DeployError(msg)
if info['mode'] != 600:
msg = 'Unexpected permissions for credential file' \
f' {dict((k, v) for k, v in info.items() if k in ("mode",))}' \
f' set {env_var!r} to fix.'
raise DeployError(msg)
return
yield from files.put._inner(
dest=path,
src=StringIO(cred),
user=user,
group=group,
mode='0600',
)
@operation()
def service_unit(
user_name: str,
user_home: str,
unit_name: str,
unit_spec: str,
):
"""Ensure the availability of a user-space systemd service unit"""
unit_dir = f'{user_home}/.config/systemd/user'
unit_file = f'{unit_dir}/{unit_name}.service'
yield from files.directory._inner(
path=f'{user_home}/.config',
present=True,
user=user_name,
)
yield from files.directory._inner(
path=f'{user_home}/.config/systemd',
present=True,
user=user_name,
)
yield from files.directory._inner(
path=f'{user_home}/.config/user',
present=True,
user=user_name,
)
yield from files.directory._inner(
path=unit_dir,
present=True,
user=user_name,
)
yield from files.file._inner(
path=unit_file,
present=True,
user=user_name,
create_remote_dir=True,
)
yield from files.block._inner(
path=unit_file,
content=unit_spec,
present=True,
try_prevent_shell_expansion=True,
)
def container_unit(
user_name: str,
user_home: str,
unit_name: str,
unit_spec: str,
):
"""Ensure the availability of a user-space systemd container unit"""
files.directory(
path=f'{user_home}/.config/containers/systemd',
present=True,
_sudo_user=user_name,
)
files.block(
name=f'{unit_name!r} container unit',
path=f'{user_home}/.config/containers/systemd/{unit_name}.container',
content=unit_spec,
present=True,
try_prevent_shell_expansion=True,
_sudo_user=user_name,
)
# TODO: this does not do anything that systemd.service() could not
# do as well -- remove
def run_service(
user_name: str,
user_id: int,
unit_name: str,
enabled: bool = True,
restarted: bool = True,
):
"""Run a user-space systemd service"""
# not all "services" end with '.service', for example, 'podman.socket'
#service = f'{unit_name}.service'
service = unit_name
systemd.service(
name=f'Launch {user_name} > {service}',
service=service,
running=True,
restarted=restarted,
enabled=enabled,
daemon_reload=True,
user_mode=True,
_sudo_user=user_name,
_env={
'XDG_RUNTIME_DIR': f'/run/user/{user_id}',
},
)