From eaa6cbd39fb0e8c1259c0aa7e8a73cc8d65f3a29 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Thu, 28 Sep 2023 17:18:18 -0400 Subject: [PATCH] cephadm: add a higher-level function for managing systemd units 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 --- src/cephadm/cephadm.py | 32 ++++++++++++-------------- src/cephadm/cephadmlib/systemd_unit.py | 14 ++++++++--- src/cephadm/tests/test_unit_file.py | 2 +- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 9d65a97352a..d53bb38cdf6 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -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') diff --git a/src/cephadm/cephadmlib/systemd_unit.py b/src/cephadm/cephadmlib/systemd_unit.py index 849833804c2..0c392c77bfe 100644 --- a/src/cephadm/cephadmlib/systemd_unit.py +++ b/src/cephadm/cephadmlib/systemd_unit.py @@ -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) diff --git a/src/cephadm/tests/test_unit_file.py b/src/cephadm/tests/test_unit_file.py index ab24aab4e57..a1d49c93c20 100644 --- a/src/cephadm/tests/test_unit_file.py +++ b/src/cephadm/tests/test_unit_file.py @@ -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(): -- 2.39.5