From 83ff83157c3c5bc3c825d09c8724310e02aea988 Mon Sep 17 00:00:00 2001 From: Ricardo Marques Date: Fri, 26 Jun 2020 18:44:45 +0100 Subject: [PATCH] cephadm: Add "--format" option to "ceph orch status" Fixes: https://tracker.ceph.com/issues/46233 Signed-off-by: Ricardo Marques --- qa/tasks/cephadm_cases/test_cli.py | 2 ++ src/pybind/mgr/orchestrator/module.py | 26 ++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/qa/tasks/cephadm_cases/test_cli.py b/qa/tasks/cephadm_cases/test_cli.py index a953efc7b88..b06e276b1a9 100644 --- a/qa/tasks/cephadm_cases/test_cli.py +++ b/qa/tasks/cephadm_cases/test_cli.py @@ -35,3 +35,5 @@ class TestCephadmCLI(MgrTestCase): out = self._orch_cmd('ps', '--format', 'yaml') self.assertNotIn('!!python', out) + out = self._orch_cmd('status', '--format', 'yaml') + self.assertNotIn('!!python', out) diff --git a/src/pybind/mgr/orchestrator/module.py b/src/pybind/mgr/orchestrator/module.py index 0c66f9b68cd..3096d0fe890 100644 --- a/src/pybind/mgr/orchestrator/module.py +++ b/src/pybind/mgr/orchestrator/module.py @@ -1241,21 +1241,31 @@ Usage: @_cli_read_command( 'orch status', + 'name=format,type=CephChoices,strings=plain|json|json-pretty|yaml,req=false', desc='Report configured backend and its status') - def _status(self): + def _status(self, format='plain'): o = self._select_orchestrator() if o is None: raise NoOrchestrator() avail, why = self.available() - if avail is None: - # The module does not report its availability - return HandleCommandResult(stdout="Backend: {0}".format(o)) + result = { + "backend": o + } + if avail is not None: + result['available'] = avail + if not avail: + result['reason'] = why + + if format != 'plain': + output = to_format(result, format, many=False, cls=None) else: - return HandleCommandResult(stdout="Backend: {0}\nAvailable: {1}{2}".format( - o, avail, - " ({0})".format(why) if not avail else "" - )) + output = "Backend: {0}".format(result['backend']) + if 'available' in result: + output += "\nAvailable: {0}".format(result['available']) + if 'reason' in result: + output += ' ({0})'.format(result['reason']) + return HandleCommandResult(stdout=output) def self_test(self): old_orch = self._select_orchestrator()