From 8d97f74e933261f08ed6b0c6e102cfb98580c345 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Fri, 17 Nov 2023 11:23:47 -0500 Subject: [PATCH] cephadm: use service_name method of daemon identity Use the new service_name method of the DaemonIdentity type in some basic cases where string concatenation was being used. I also converted the name of the agent's helper method to match and made it private to avoid future confusion. Signed-off-by: John Mulligan --- src/cephadm/cephadm.py | 15 +++++++-------- src/cephadm/cephadmlib/container_daemon_form.py | 3 +-- src/cephadm/tests/test_agent.py | 9 +++++---- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 79f5bab5f246f..dcb62c9517e48 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -930,9 +930,8 @@ def _update_container_args_for_podman( ) -> None: if not isinstance(ctx.container_engine, Podman): return - service_name = f'{ident.unit_name}.service' container_args.extend( - ctx.container_engine.service_args(ctx, service_name) + ctx.container_engine.service_args(ctx, ident.service_name) ) @@ -1394,19 +1393,19 @@ class CephadmAgent(DaemonForm): with write_new(meta_file_path) as f: f.write(json.dumps(meta, indent=4) + '\n') - unit_file_path = os.path.join(self.ctx.unit_dir, self.unit_name()) + unit_file_path = os.path.join(self.ctx.unit_dir, self._service_name()) with write_new(unit_file_path) as f: f.write(self.unit_file()) call_throws(self.ctx, ['systemctl', 'daemon-reload']) - call(self.ctx, ['systemctl', 'stop', self.unit_name()], + call(self.ctx, ['systemctl', 'stop', self._service_name()], verbosity=CallVerbosity.DEBUG) - call(self.ctx, ['systemctl', 'reset-failed', self.unit_name()], + call(self.ctx, ['systemctl', 'reset-failed', self._service_name()], verbosity=CallVerbosity.DEBUG) - call_throws(self.ctx, ['systemctl', 'enable', '--now', self.unit_name()]) + call_throws(self.ctx, ['systemctl', 'enable', '--now', self._service_name()]) - def unit_name(self) -> str: - return '{}.service'.format(get_unit_name(self.fsid, self.daemon_type, self.daemon_id)) + def _service_name(self) -> str: + return self.identity.service_name def unit_run(self) -> str: py3 = shutil.which('python3') diff --git a/src/cephadm/cephadmlib/container_daemon_form.py b/src/cephadm/cephadmlib/container_daemon_form.py index 693b0cf8df0bc..1fc6960d65209 100644 --- a/src/cephadm/cephadmlib/container_daemon_form.py +++ b/src/cephadm/cephadmlib/container_daemon_form.py @@ -157,9 +157,8 @@ def daemon_to_container( if auto_podman_mounts and _is_podman: ctx.container_engine.update_mounts(ctx, container_mounts) if auto_podman_args and _is_podman: - service_name = f'{daemon.identity.unit_name}.service' container_args.extend( - ctx.container_engine.service_args(ctx, service_name) + ctx.container_engine.service_args(ctx, daemon.identity.service_name) ) return CephContainer.for_daemon( diff --git a/src/cephadm/tests/test_agent.py b/src/cephadm/tests/test_agent.py index 38c35e3558307..4904cb4f61fc1 100644 --- a/src/cephadm/tests/test_agent.py +++ b/src/cephadm/tests/test_agent.py @@ -69,17 +69,18 @@ def test_agent_deploy_daemon_unit(_call_throws, cephadm_fs): _check_file(f'{AGENT_DIR}/unit.meta', json.dumps({'meta': 'data'}, indent=4) + '\n') # check unit file was created correctly - _check_file(f'{ctx.unit_dir}/{agent.unit_name()}', agent.unit_file()) + svcname = agent._service_name() + _check_file(f'{ctx.unit_dir}/{svcname}', agent.unit_file()) expected_call_throws_calls = [ mock.call(ctx, ['systemctl', 'daemon-reload']), - mock.call(ctx, ['systemctl', 'enable', '--now', agent.unit_name()]), + mock.call(ctx, ['systemctl', 'enable', '--now', svcname]), ] _call_throws.assert_has_calls(expected_call_throws_calls) expected_call_calls = [ - mock.call(ctx, ['systemctl', 'stop', agent.unit_name()], verbosity=_cephadm.CallVerbosity.DEBUG), - mock.call(ctx, ['systemctl', 'reset-failed', agent.unit_name()], verbosity=_cephadm.CallVerbosity.DEBUG), + mock.call(ctx, ['systemctl', 'stop', svcname], verbosity=_cephadm.CallVerbosity.DEBUG), + mock.call(ctx, ['systemctl', 'reset-failed', svcname], verbosity=_cephadm.CallVerbosity.DEBUG), ] _cephadm.call.assert_has_calls(expected_call_calls) -- 2.39.5