From 676a6da7107d2ae3fb8b8939476e35cd7098376b Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Thu, 28 Sep 2023 17:02:52 -0400 Subject: [PATCH] cephadm: create systemd_unit.py for systemd unit related functions Continue to modularize cephadm by moving the functions related to generating systemd unit files to a new module. Signed-off-by: John Mulligan --- src/cephadm/cephadm.py | 66 +---------------------- src/cephadm/cephadmlib/systemd_unit.py | 73 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 65 deletions(-) create mode 100644 src/cephadm/cephadmlib/systemd_unit.py diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 242c8599747..9d65a97352a 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -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 index 00000000000..849833804c2 --- /dev/null +++ b/src/cephadm/cephadmlib/systemd_unit.py @@ -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) -- 2.39.5