]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: add a higher-level function for managing systemd units
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 28 Sep 2023 21:18:18 +0000 (17:18 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Sun, 10 Dec 2023 15:25:57 +0000 (10:25 -0500)
Add the function update_files to systemd_unit.py to encapsulate and
abstract the details regarding the generation of system unit files.
This will make it simpler in the future to add more advanced systemd
configurations include managing customized unit files and systemd
unit drop-in files.

Some additional work was needed to update the recently added
command_unit_install function. Because the new systemd_unit.update_files
function requires a full daemon identity. The command_unit_install
function now requires a daemon name. In addition, while testing this
change it was found that the function could not have worked as it was
because it required the fsid but neither used the infer_fsid decorator
nor provided a `--fsid` argument. Both were added.

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

index 9d65a97352a7e56906f8e764260d53b5bdd292ca..d53bb38cdf6b7321877d560ee3c77c3de4204c02 100755 (executable)
@@ -133,7 +133,7 @@ from cephadmlib.logging import (
     LogDestination,
 )
 from cephadmlib.systemd import check_unit, check_units
-from cephadmlib.systemd_unit import get_unit_file, install_base_units
+from cephadmlib import systemd_unit
 from cephadmlib.container_types import (
     CephContainer,
     InitContainer,
@@ -1254,11 +1254,7 @@ def deploy_daemon_units(
     install_sysctl(ctx, fsid, daemon_form_create(ctx, ident))
 
     # systemd
-    install_base_units(ctx, fsid)
-    unit = get_unit_file(ctx, fsid)
-    unit_file = 'ceph-%s@.service' % (fsid)
-    with write_new(ctx.unit_dir + '/' + unit_file, perms=None) as f:
-        f.write(unit)
+    systemd_unit.update_files(ctx, ident)
     call_throws(ctx, ['systemctl', 'daemon-reload'])
 
     unit_name = get_unit_name(fsid, daemon_type, daemon_id)
@@ -3431,21 +3427,16 @@ def command_ceph_volume(ctx):
 ##################################
 
 
+@infer_fsid
 def command_unit_install(ctx):
     # type: (CephadmContext) -> int
-    if not ctx.fsid:
+    if not getattr(ctx, 'fsid', None):
         raise Error('must pass --fsid to specify cluster')
-
-    fsid = ctx.fsid
-    install_base_units(ctx, fsid)
-    unit = get_unit_file(ctx, fsid)
-    unit_file = 'ceph-%s@.service' % (fsid)
-    with open(ctx.unit_dir + '/' + unit_file + '.new', 'w') as f:
-        f.write(unit)
-        os.rename(ctx.unit_dir + '/' + unit_file + '.new',
-                  ctx.unit_dir + '/' + unit_file)
+    if not getattr(ctx, 'name', None):
+        raise Error('daemon name required')
+    ident = DaemonIdentity.from_context(ctx)
+    systemd_unit.update_files(ctx, ident)
     call_throws(ctx, ['systemctl', 'daemon-reload'])
-
     return 0
 
 
@@ -5191,6 +5182,13 @@ def _get_parser():
     parser_unit_install = subparsers.add_parser(
         'unit-install', help="Install the daemon's systemd unit")
     parser_unit_install.set_defaults(func=command_unit_install)
+    parser_unit_install.add_argument(
+        '--fsid',
+        help='cluster FSID')
+    parser_unit_install.add_argument(
+        '--name', '-n',
+        required=True,
+        help='daemon name (type.id)')
 
     parser_logs = subparsers.add_parser(
         'logs', help='print journald logs for a daemon container')
index 849833804c25a62cd11a102615cc2d037fbf3d0b..0c392c77bfe36e02ab66e0234a04ea7e9f5b5693 100644 (file)
@@ -6,11 +6,12 @@ from . import templating
 from .call_wrappers import call_throws
 from .container_engines import Docker, Podman
 from .context import CephadmContext
+from .daemon_identity import DaemonIdentity
 from .file_utils import write_new
 from .logging import write_cluster_logrotate_config
 
 
-def get_unit_file(ctx: CephadmContext, fsid: str) -> str:
+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 = (
@@ -26,8 +27,7 @@ def get_unit_file(ctx: CephadmContext, fsid: str) -> str:
     )
 
 
-def install_base_units(ctx, fsid):
-    # type: (CephadmContext, str) -> None
+def _install_base_units(ctx: CephadmContext, fsid: str) -> None:
     """
     Set up ceph.target and ceph-$fsid.target units.
     """
@@ -71,3 +71,11 @@ def install_base_units(ctx, fsid):
         return
 
     write_cluster_logrotate_config(ctx, fsid)
+
+
+def update_files(ctx: CephadmContext, ident: DaemonIdentity) -> None:
+    _install_base_units(ctx, ident.fsid)
+    unit = _get_unit_file(ctx, ident.fsid)
+    unit_file = 'ceph-%s@.service' % (ident.fsid)
+    with write_new(ctx.unit_dir + '/' + unit_file, perms=None) as f:
+        f.write(unit)
index ab24aab4e57b9dc74ded262f70013b3bf4c91579..a1d49c93c20184d561b2af2e049a9f1067159296 100644 (file)
@@ -24,7 +24,7 @@ _cephadm = import_cephadm()
 
 
 def _get_unit_file(ctx, fsid):
-    return str(systemd_unit.get_unit_file(ctx, fsid))
+    return str(systemd_unit._get_unit_file(ctx, fsid))
 
 
 def test_docker_engine_requires_docker():