]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
cephadm: update get_unit_file to use jinja template
authorJohn Mulligan <jmulligan@redhat.com>
Sun, 16 Jul 2023 20:15:49 +0000 (16:15 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Fri, 3 Nov 2023 22:51:49 +0000 (18:51 -0400)
The somewhat complex string assembly of the main systemd unit file
for cephadm services can benefit from using a standard templating
approach.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/cephadm/cephadm.py

index ca068c13757c826dda62ff8d9b19514259c4d634..84b4a03175ff5303f892e06c5131197ee4af7b3a 100755 (executable)
@@ -171,6 +171,7 @@ from cephadmlib.deploy import DeploymentType
 from cephadmlib.container_daemon_form import ContainerDaemonForm
 from cephadmlib.sysctl import install_sysctl, migrate_sysctl_dir
 from cephadmlib.firewalld import Firewalld, update_firewalld
+from cephadmlib import templating
 
 
 FuncT = TypeVar('FuncT', bound=Callable)
@@ -3386,40 +3387,30 @@ def install_base_units(ctx, fsid):
 """ % (fsid, ' '.join(targets), '|'.join(targets)))
 
 
-def get_unit_file(ctx, fsid):
-    # type: (CephadmContext, str) -> str
-    extra_args = ''
-    if isinstance(ctx.container_engine, Podman):
-        extra_args = ('ExecStartPre=-/bin/rm -f %t/%n-pid %t/%n-cid\n'
-                      'ExecStopPost=-/bin/rm -f %t/%n-pid %t/%n-cid\n'
-                      'Type=forking\n'
-                      'PIDFile=%t/%n-pid\n')
-        if ctx.container_engine.supports_split_cgroups:
-            extra_args += 'Delegate=yes\n'
-
-    docker = isinstance(ctx.container_engine, Docker)
-    u = """# generated by cephadm
+_TMPL_DAEMON_UNIT = """# generated by cephadm
 [Unit]
-Description=Ceph %i for {fsid}
+Description=Ceph %i for {{fsid}}
 
 # According to:
 #   http://www.freedesktop.org/wiki/Software/systemd/NetworkTarget
 # these can be removed once ceph-mon will dynamically change network
 # configuration.
-After=network-online.target local-fs.target time-sync.target{docker_after}
+After=network-online.target local-fs.target time-sync.target{% if has_docker_engine %} docker.service{% endif %}
 Wants=network-online.target local-fs.target time-sync.target
-{docker_requires}
+{%- if has_docker_engine %}
+Requires=docker.service
+{%- endif %}
 
-PartOf=ceph-{fsid}.target
-Before=ceph-{fsid}.target
+PartOf=ceph-{{fsid}}.target
+Before=ceph-{{fsid}}.target
 
 [Service]
 LimitNOFILE=1048576
 LimitNPROC=1048576
 EnvironmentFile=-/etc/environment
-ExecStart=/bin/bash {data_dir}/{fsid}/%i/unit.run
-ExecStop=-/bin/bash -c 'bash {data_dir}/{fsid}/%i/unit.stop'
-ExecStopPost=-/bin/bash {data_dir}/{fsid}/%i/unit.poststop
+ExecStart=/bin/bash {{ctx.data_dir}}/{{fsid}}/%i/unit.run
+ExecStop=-/bin/bash -c 'bash {{ctx.data_dir}}/{{fsid}}/%i/unit.stop'
+ExecStopPost=-/bin/bash {{ctx.data_dir}}/{{fsid}}/%i/unit.poststop
 KillMode=none
 Restart=on-failure
 RestartSec=10s
@@ -3427,17 +3418,35 @@ TimeoutStartSec=200
 TimeoutStopSec=120
 StartLimitInterval=30min
 StartLimitBurst=5
-{extra_args}
+{%- if has_podman_engine %}
+ExecStartPre=-/bin/rm -f %t/%n-pid %t/%n-cid
+ExecStopPost=-/bin/rm -f %t/%n-pid %t/%n-cid
+Type=forking
+PIDFile=%t/%n-pid
+{%- if has_podman_split_version %}
+Delegate=yes
+{%- endif %}
+{%- endif %}
+
 [Install]
-WantedBy=ceph-{fsid}.target
-""".format(fsid=fsid,
-           data_dir=ctx.data_dir,
-           extra_args=extra_args,
-           # if docker, we depend on docker.service
-           docker_after=' docker.service' if docker else '',
-           docker_requires='Requires=docker.service\n' if docker else '')
-
-    return u
+WantedBy=ceph-{{fsid}}.target
+"""
+
+
+def get_unit_file(ctx: CephadmContext, fsid: str) -> str:
+    has_docker_engine = isinstance(ctx.container_engine, Docker)
+    has_podman_engine = isinstance(ctx.container_engine, Podman)
+    has_podman_split_version = (
+        has_podman_engine and ctx.container_engine.supports_split_cgroups
+    )
+    return templating.template_str(
+        ctx,
+        _TMPL_DAEMON_UNIT,
+        fsid=fsid,
+        has_docker_engine=has_docker_engine,
+        has_podman_engine=has_podman_engine,
+        has_podman_split_version=has_podman_split_version,
+    )
 
 ##################################