From b5e6483e6069d48d5c76209c16af24de7d08e580 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Moritz=20R=C3=B6hrich?= Date: Mon, 21 Mar 2022 17:32:25 +0100 Subject: [PATCH] cephadm: avoid crashing on expected non-zero exit MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit - Avoid crashing when a call out to an external program expectedly does not return exit status zero. There are programs that communicate other information than error/no error through exit status. E.g. `systemctl status` will return different exit codes depending on the actual status of the units in question. In cases where this is expected crashing with a RuntimeError exception is inappropriate and should be avoided. Fixes: https://tracker.ceph.com/issues/55117 Signed-off-by: Moritz Röhrich (cherry picked from commit a02be6f22fa18094cd8758700ab74581b6ce1701) --- qa/workunits/cephadm/test_cephadm.sh | 20 ++++++++++++++++++++ src/cephadm/cephadm | 10 +++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/qa/workunits/cephadm/test_cephadm.sh b/qa/workunits/cephadm/test_cephadm.sh index 643065b73b1c7..31ebf0a5dd98d 100755 --- a/qa/workunits/cephadm/test_cephadm.sh +++ b/qa/workunits/cephadm/test_cephadm.sh @@ -77,6 +77,22 @@ function expect_false() if eval "$@"; then return 1; else return 0; fi } +# expect_return_code $expected_code $command ... +function expect_return_code() +{ + set -x + local expected_code="$1" + shift + local command="$@" + + set +e + eval "$command" + local return_code="$?" + set -e + + if [ ! "$return_code" -eq "$expected_code" ]; then return 1; else return 0; fi +} + function is_available() { local name="$1" @@ -370,6 +386,10 @@ $CEPHADM unit --fsid $FSID --name mon.a -- disable expect_false $CEPHADM unit --fsid $FSID --name mon.a -- is-enabled $CEPHADM unit --fsid $FSID --name mon.a -- enable $CEPHADM unit --fsid $FSID --name mon.a -- is-enabled +$CEPHADM unit --fsid $FSID --name mon.a -- status +$CEPHADM unit --fsid $FSID --name mon.a -- stop +expect_return_code 3 $CEPHADM unit --fsid $FSID --name mon.a -- status +$CEPHADM unit --fsid $FSID --name mon.a -- start ## shell $CEPHADM shell --fsid $FSID -- true diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index 866c461c610f4..224c371455c69 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -5785,19 +5785,19 @@ def command_ceph_volume(ctx): @infer_fsid def command_unit(ctx): - # type: (CephadmContext) -> None + # type: (CephadmContext) -> int if not ctx.fsid: raise Error('must pass --fsid to specify cluster') unit_name = get_unit_name_by_daemon_name(ctx, ctx.fsid, ctx.name) - call_throws(ctx, [ - 'systemctl', - ctx.command, - unit_name], + _, _, code = call( + ctx, + ['systemctl', ctx.command, unit_name], verbosity=CallVerbosity.VERBOSE, desc='' ) + return code ################################## -- 2.39.5