forked from lab-in-a-box/liab-deployments
This allwos for use with "container units" that only have transient service units.
63 lines
1.5 KiB
Python
63 lines
1.5 KiB
Python
from pyinfra.operations import (
|
|
files,
|
|
systemd,
|
|
)
|
|
|
|
|
|
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"""
|
|
files.block(
|
|
name=f'{unit_name!r} service unit',
|
|
path=f'{user_home}/.config/systemd/user/{unit_name}.service',
|
|
content=unit_spec,
|
|
present=True,
|
|
try_prevent_shell_expansion=True,
|
|
_sudo_user=user_name,
|
|
)
|
|
|
|
|
|
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.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,
|
|
)
|
|
|
|
|
|
def run_service(
|
|
user_name: str,
|
|
user_id: int,
|
|
unit_name: str,
|
|
enabled: 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=True,
|
|
enabled=enabled,
|
|
daemon_reload=True,
|
|
user_mode=True,
|
|
_sudo_user=user_name,
|
|
_env={
|
|
'XDG_RUNTIME_DIR': f'/run/user/{user_id}',
|
|
},
|
|
)
|