mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-26 21:45:20 +02:00
sys-service: add systemd_directive filter and refactor service template
Introduced custom filter plugin to render optional systemd directives, refactored template to loop over directives, and adjusted default vars (TimeoutStartSec, RuntimeMaxSec handling). Details: see ChatGPT conversation https://chatgpt.com/share/68a5a730-6344-800f-b9a3-dc62d5902e9b
This commit is contained in:
parent
8608d89653
commit
42d6c1799b
17
roles/sys-service/filter_plugins/systemd_directive.py
Normal file
17
roles/sys-service/filter_plugins/systemd_directive.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
def systemd_directive(value, key: str) -> str:
|
||||||
|
"""
|
||||||
|
Render a single systemd directive line if value is non-empty.
|
||||||
|
Example: {{ myval | systemd_directive('ExecStart') }}
|
||||||
|
"""
|
||||||
|
if value is None:
|
||||||
|
return ""
|
||||||
|
sval = str(value).strip()
|
||||||
|
if not sval:
|
||||||
|
return ""
|
||||||
|
return f"{key}={sval}"
|
||||||
|
|
||||||
|
class FilterModule(object):
|
||||||
|
def filters(self):
|
||||||
|
return {
|
||||||
|
"systemd_directive": systemd_directive,
|
||||||
|
}
|
@ -1,15 +1,14 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description={{ SOFTWARE_NAME }} - Service for role '{{ system_service_id }}'
|
Description={{ SOFTWARE_NAME }} - Service for role '{{ system_service_id }}'
|
||||||
{% if system_service_tpl_on_failure |length > 0 %}
|
{{- system_service_tpl_on_failure | systemd_directive('OnFailure') }}
|
||||||
OnFailure={{ system_service_tpl_on_failure }}
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type={{ system_service_tpl_type }}
|
Type={{ system_service_tpl_type }}
|
||||||
{% if system_service_tpl_exec_start_pre |length > 0 %}
|
{%- for key, val in [
|
||||||
ExecStartPre={{ system_service_tpl_exec_start_pre }}
|
('TimeoutStartSec', system_service_tpl_timeout_start_sec),
|
||||||
{% endif %}
|
('ExecStartPre', system_service_tpl_exec_start_pre),
|
||||||
ExecStart={{ system_service_tpl_exec_start }}
|
('ExecStart', system_service_tpl_exec_start),
|
||||||
{% if system_service_tpl_runtime |length > 0 %}
|
('RuntimeMaxSec', system_service_tpl_runtime)
|
||||||
RuntimeMaxSec={{ system_service_tpl_runtime }}
|
] -%}
|
||||||
{% endif %}
|
{{- val | systemd_directive(key) }}
|
||||||
|
{%- endfor %}
|
||||||
|
@ -17,8 +17,9 @@ system_service_script_inter: "/bin/{{ 'bash' if system_service_script_type ==
|
|||||||
system_service_script_exec: "{{ system_service_script_inter }} {{ system_service_id | get_service_script_path( system_service_script_type ) }}"
|
system_service_script_exec: "{{ system_service_script_inter }} {{ system_service_id | get_service_script_path( system_service_script_type ) }}"
|
||||||
|
|
||||||
# Service template
|
# Service template
|
||||||
system_service_tpl_on_failure: "{{ SYS_SERVICE_ON_FAILURE_COMPOSE }}"
|
system_service_tpl_on_failure: "{{ SYS_SERVICE_ON_FAILURE_COMPOSE }}"
|
||||||
system_service_tpl_type: "oneshot"
|
system_service_tpl_type: "oneshot"
|
||||||
system_service_tpl_exec_start: "{{ system_service_script_exec }}"
|
system_service_tpl_exec_start: "{{ system_service_script_exec }}"
|
||||||
system_service_tpl_runtime: "{{ SYS_SERVICE_DEFAULT_RUNTIME }}"
|
system_service_tpl_runtime: "{{ '' if system_service_tpl_type == 'oneshot' else SYS_SERVICE_DEFAULT_RUNTIME }}"
|
||||||
system_service_tpl_exec_start_pre: ""
|
system_service_tpl_exec_start_pre: ""
|
||||||
|
system_service_tpl_timeout_start_sec: "60s"
|
Loading…
x
Reference in New Issue
Block a user