]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
cephadm: create systemd_unit.py for systemd unit related functions
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 28 Sep 2023 21:02:52 +0000 (17:02 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Sun, 10 Dec 2023 15:20:24 +0000 (10:20 -0500)
Continue to modularize cephadm by moving the functions related to
generating systemd unit files to a new module.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/cephadm/cephadm.py
src/cephadm/cephadmlib/systemd_unit.py [new file with mode: 0644]

index 242c859974777eaa5207cfbd4517657630959e2a..9d65a97352a7e56906f8e764260d53b5bdd292ca 100755 (executable)
@@ -83,7 +83,6 @@ from cephadmlib.call_wrappers import (
     concurrent_tasks,
 )
 from cephadmlib.container_engines import (
-    Docker,
     Podman,
     check_container_engine,
     find_container_engine,
@@ -132,9 +131,9 @@ from cephadmlib.logging import (
     cephadm_init_logging,
     Highlight,
     LogDestination,
-    write_cluster_logrotate_config,
 )
 from cephadmlib.systemd import check_unit, check_units
+from cephadmlib.systemd_unit import get_unit_file, install_base_units
 from cephadmlib.container_types import (
     CephContainer,
     InitContainer,
@@ -1381,69 +1380,6 @@ def _write_iscsi_unit_poststop_commands(
     f.write('! ' + 'rm ' + runtime_dir + '/ceph-%s@%s.%s.service-cid' % (ident.fsid, ident.daemon_type, ident.daemon_id + '.tcmu') + '\n')
     f.write(' '.join(CephIscsi.configfs_mount_umount(data_dir, mount=False)) + '\n')
 
-
-def install_base_units(ctx, fsid):
-    # type: (CephadmContext, str) -> None
-    """
-    Set up ceph.target and ceph-$fsid.target units.
-    """
-    # global unit
-    existed = os.path.exists(ctx.unit_dir + '/ceph.target')
-    with write_new(ctx.unit_dir + '/ceph.target', perms=None) as f:
-        f.write('[Unit]\n'
-                'Description=All Ceph clusters and services\n'
-                '\n'
-                '[Install]\n'
-                'WantedBy=multi-user.target\n')
-    if not existed:
-        # we disable before enable in case a different ceph.target
-        # (from the traditional package) is present; while newer
-        # systemd is smart enough to disable the old
-        # (/lib/systemd/...) and enable the new (/etc/systemd/...),
-        # some older versions of systemd error out with EEXIST.
-        call_throws(ctx, ['systemctl', 'disable', 'ceph.target'])
-        call_throws(ctx, ['systemctl', 'enable', 'ceph.target'])
-        call_throws(ctx, ['systemctl', 'start', 'ceph.target'])
-
-    # cluster unit
-    existed = os.path.exists(ctx.unit_dir + '/ceph-%s.target' % fsid)
-    with write_new(ctx.unit_dir + f'/ceph-{fsid}.target', perms=None) as f:
-        f.write(
-            '[Unit]\n'
-            'Description=Ceph cluster {fsid}\n'
-            'PartOf=ceph.target\n'
-            'Before=ceph.target\n'
-            '\n'
-            '[Install]\n'
-            'WantedBy=multi-user.target ceph.target\n'.format(
-                fsid=fsid)
-        )
-    if not existed:
-        call_throws(ctx, ['systemctl', 'enable', 'ceph-%s.target' % fsid])
-        call_throws(ctx, ['systemctl', 'start', 'ceph-%s.target' % fsid])
-
-    # don't overwrite file in order to allow users to manipulate it
-    if os.path.exists(ctx.logrotate_dir + f'/ceph-{fsid}'):
-        return
-
-    write_cluster_logrotate_config(ctx, fsid)
-
-
-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.render(
-        ctx,
-        templating.Templates.ceph_service,
-        fsid=fsid,
-        has_docker_engine=has_docker_engine,
-        has_podman_engine=has_podman_engine,
-        has_podman_split_version=has_podman_split_version,
-    )
-
 ##################################
 
 
diff --git a/src/cephadm/cephadmlib/systemd_unit.py b/src/cephadm/cephadmlib/systemd_unit.py
new file mode 100644 (file)
index 0000000..8498338
--- /dev/null
@@ -0,0 +1,73 @@
+# systemd_unit.py - creating/managing systemd unit files
+
+import os
+
+from . import templating
+from .call_wrappers import call_throws
+from .container_engines import Docker, Podman
+from .context import CephadmContext
+from .file_utils import write_new
+from .logging import write_cluster_logrotate_config
+
+
+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.render(
+        ctx,
+        templating.Templates.ceph_service,
+        fsid=fsid,
+        has_docker_engine=has_docker_engine,
+        has_podman_engine=has_podman_engine,
+        has_podman_split_version=has_podman_split_version,
+    )
+
+
+def install_base_units(ctx, fsid):
+    # type: (CephadmContext, str) -> None
+    """
+    Set up ceph.target and ceph-$fsid.target units.
+    """
+    # global unit
+    existed = os.path.exists(ctx.unit_dir + '/ceph.target')
+    with write_new(ctx.unit_dir + '/ceph.target', perms=None) as f:
+        f.write('[Unit]\n'
+                'Description=All Ceph clusters and services\n'
+                '\n'
+                '[Install]\n'
+                'WantedBy=multi-user.target\n')
+    if not existed:
+        # we disable before enable in case a different ceph.target
+        # (from the traditional package) is present; while newer
+        # systemd is smart enough to disable the old
+        # (/lib/systemd/...) and enable the new (/etc/systemd/...),
+        # some older versions of systemd error out with EEXIST.
+        call_throws(ctx, ['systemctl', 'disable', 'ceph.target'])
+        call_throws(ctx, ['systemctl', 'enable', 'ceph.target'])
+        call_throws(ctx, ['systemctl', 'start', 'ceph.target'])
+
+    # cluster unit
+    existed = os.path.exists(ctx.unit_dir + '/ceph-%s.target' % fsid)
+    with write_new(ctx.unit_dir + f'/ceph-{fsid}.target', perms=None) as f:
+        f.write(
+            '[Unit]\n'
+            'Description=Ceph cluster {fsid}\n'
+            'PartOf=ceph.target\n'
+            'Before=ceph.target\n'
+            '\n'
+            '[Install]\n'
+            'WantedBy=multi-user.target ceph.target\n'.format(
+                fsid=fsid)
+        )
+    if not existed:
+        call_throws(ctx, ['systemctl', 'enable', 'ceph-%s.target' % fsid])
+        call_throws(ctx, ['systemctl', 'start', 'ceph-%s.target' % fsid])
+
+    # don't overwrite file in order to allow users to manipulate it
+    if os.path.exists(ctx.logrotate_dir + f'/ceph-{fsid}'):
+        return
+
+    write_cluster_logrotate_config(ctx, fsid)