From 9f37bf230312ce9d185953a9de895b5f0b74299a Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Wed, 27 Mar 2024 18:45:15 -0400 Subject: [PATCH] cephadm: fix host-maintenance command always exiting with a failure The host-maintenance command would always fail because command_maintenance always returns a string. This string is passed to sys.exit and thus always gets printed and causes a non-zero exit code. Fix the command line behavior by renaming the original function and adding a new command_maintenance that prints the string and returns an int like other command_* functions do. Fixes: https://tracker.ceph.com/issues/65122 Signed-off-by: John Mulligan (cherry picked from commit 0a137b140e47d02b03d133adb55e62aa0de7b984) Conflicts: src/cephadm/cephadm.py src/cephadm/tests/test_cephadm.py --- src/cephadm/cephadm | 12 +++++++++++- src/cephadm/tests/test_cephadm.py | 8 ++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index 2addee773e345..8095568ad3368 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -9017,7 +9017,17 @@ def target_exists(ctx: CephadmContext) -> bool: @infer_fsid -def command_maintenance(ctx: CephadmContext) -> str: +def command_maintenance(ctx: CephadmContext) -> int: + msg = change_maintenance_mode(ctx) + # mgr module reads the string emitted here from stderr + sys.stderr.write(msg + '\n') + sys.stderr.flush() + if msg.startswith('fail'): + return 1 + return 0 + + +def change_maintenance_mode(ctx: CephadmContext) -> str: if not ctx.fsid: raise Error('failed - must pass --fsid to specify cluster') diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index 755b7b0a51e7e..497bf32757dfb 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -1139,7 +1139,7 @@ class TestMaintenance: ctx: cd.CephadmContext = cd.cephadm_init_ctx( ['host-maintenance', 'enter', '--fsid', TestMaintenance.fsid]) ctx.container_engine = mock_podman() - retval = cd.command_maintenance(ctx) + retval = cd.change_maintenance_mode(ctx) assert retval.startswith('failed') @mock.patch('os.listdir', return_value=[]) @@ -1151,7 +1151,7 @@ class TestMaintenance: ctx: cd.CephadmContext = cd.cephadm_init_ctx( ['host-maintenance', 'enter', '--fsid', TestMaintenance.fsid]) ctx.container_engine = mock_podman() - retval = cd.command_maintenance(ctx) + retval = cd.change_maintenance_mode(ctx) assert retval.startswith('failed') @mock.patch('os.listdir', return_value=[]) @@ -1165,7 +1165,7 @@ class TestMaintenance: ctx: cd.CephadmContext = cd.cephadm_init_ctx( ['host-maintenance', 'exit', '--fsid', TestMaintenance.fsid]) ctx.container_engine = mock_podman() - retval = cd.command_maintenance(ctx) + retval = cd.change_maintenance_mode(ctx) assert retval.startswith('failed') @mock.patch('os.listdir', return_value=[]) @@ -1179,7 +1179,7 @@ class TestMaintenance: ctx: cd.CephadmContext = cd.cephadm_init_ctx( ['host-maintenance', 'exit', '--fsid', TestMaintenance.fsid]) ctx.container_engine = mock_podman() - retval = cd.command_maintenance(ctx) + retval = cd.change_maintenance_mode(ctx) assert retval.startswith('failed') -- 2.39.5