From 4d7e0d3a38406d01b9838f63b178108b2dcc909f Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Mon, 12 May 2025 16:09:05 -0400 Subject: [PATCH] cephadm: add --dry-run option to cephadm {enter,logs,unit} commands Add a --dry-run option to the cephadm enter, cephadm logs, and cephadm unit commands. Like cephadm shell --dry-run, this causes cephadm to print the command it would have run rather than running said command. This allows the user to copy and edit or otherwise hack on the output to make variations on these comands without having to teach cephadm all the possible options and switches those commands make take. For instance I can follow recent mgr logging like so: ``` $(/tmp/cephadm logs -i mgr --dry-run | sed 's/ / --since=-1s -f /') ``` Signed-off-by: John Mulligan (cherry picked from commit eb0519b6f88eee2ad07adccbdca6415aded5f4b1) --- src/cephadm/cephadm.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 6a9606f51ebb0..5a6be98bcbd8d 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -3171,6 +3171,9 @@ def command_enter(ctx: CephadmContext) -> int: container_args=container_args, ) command = c.exec_cmd(command) + if ctx.dry_run: + print(' '.join(shlex.quote(arg) for arg in command)) + return 0 return call_timeout(ctx, command, ctx.timeout) ################################## @@ -3238,9 +3241,13 @@ def command_unit(ctx: CephadmContext) -> int: unit_name = lookup_unit_name_by_daemon_name( ctx, ident.fsid, ident.daemon_name ) + command = ['systemctl', ctx.command, unit_name] + if ctx.dry_run: + print(' '.join(shlex.quote(arg) for arg in command)) + return 0 _, _, code = call( ctx, - ['systemctl', ctx.command, unit_name], + command, verbosity=CallVerbosity.VERBOSE, desc='', ) @@ -3260,6 +3267,9 @@ def command_logs(ctx: CephadmContext) -> None: if ctx.command: cmd.extend(ctx.command) + if ctx.dry_run: + print(' '.join(shlex.quote(arg) for arg in cmd)) + return # call this directly, without our wrapper, so that we get an unmolested # stdout with logger prefixing. logger.debug('Running command: %s' % ' '.join(cmd)) @@ -4770,6 +4780,10 @@ def _get_parser(): '--fsid', help='cluster FSID') _name_opts(parser_enter) + parser_enter.add_argument( + '--dry-run', + action='store_true', + help='print, but do not execute, the command to enter the container') parser_enter.add_argument( 'command', nargs=argparse.REMAINDER, help='command') @@ -4818,6 +4832,10 @@ def _get_parser(): parser_unit.add_argument( '--fsid', help='cluster FSID') + parser_unit.add_argument( + '--dry-run', + action='store_true', + help='print, but do not execute, the unit command') _name_opts(parser_unit) parser_unit_install = subparsers.add_parser( @@ -4835,6 +4853,10 @@ def _get_parser(): '--fsid', help='cluster FSID') _name_opts(parser_logs) + parser_logs.add_argument( + '--dry-run', + action='store_true', + help='print, but do not execute, the command to show the logs') parser_logs.add_argument( 'command', nargs='*', help='additional journalctl args') -- 2.39.5