From 74e011e9640aa5aad55182981bfd1e6355d42b1e Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Tue, 25 May 2021 15:46:22 -0400 Subject: [PATCH] Implement node name subtitution in nodeshell If wanting to ssh to 'altenative interfaces', provide -s to facilitate that behavior, with added flexibility compared to previous incarnations. --- confluent_client/bin/nodeshell | 20 ++++++++++++++++++-- confluent_client/doc/man/nodeshell.ronn | 9 ++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/confluent_client/bin/nodeshell b/confluent_client/bin/nodeshell index fd589e3a..ff201486 100755 --- a/confluent_client/bin/nodeshell +++ b/confluent_client/bin/nodeshell @@ -48,6 +48,8 @@ def run(): help='Do not prefix output with node names') argparser.add_option('-p', '--port', type='int', default=0, help='Specify a custom port for ssh') + argparser.add_option('-s', '--substitutename', + help='Use a different name other than the nodename for ssh') argparser.add_option('-m', '--maxnodes', type='int', help='Specify a maximum number of ' 'nodes to run remote ssh command to, ' @@ -71,6 +73,19 @@ def run(): exitcode = 0 c.stop_if_noderange_over(args[0], options.maxnodes) + nodemap = {} + if options.substitutename: + subname = options.substitutename + if '{' not in subname: + subname = '{node}' + subname + for exp in c.create('/noderange/{0}/attributes/expression'.format(args[0]), + {'expression': subname}): + if 'error' in exp: + sys.stderr.write(exp['error'] + '\n') + exitcode |= exp.get('errorcode', 1) + ex = exp.get('databynode', ()) + for node in ex: + nodemap[node] = ex[node]['value'] for exp in c.create('/noderange/{0}/attributes/expression'.format(args[0]), {'expression': cmdstr}): if 'error' in exp: @@ -79,12 +94,13 @@ def run(): ex = exp.get('databynode', ()) for node in ex: cmd = ex[node]['value'] + sshnode = nodemap.get(node, node) if not isinstance(cmd, str) and not isinstance(cmd, bytes): cmd = cmd.encode('utf-8') if options.port: - cmdv = ['ssh', '-p', '{0}'.format(options.port), node, cmd] + cmdv = ['ssh', '-p', '{0}'.format(options.port), sshnode, cmd] else: - cmdv = ['ssh', node, cmd] + cmdv = ['ssh', sshnode, cmd] if currprocs < concurrentprocs: currprocs += 1 run_cmdv(node, cmdv, all, pipedesc) diff --git a/confluent_client/doc/man/nodeshell.ronn b/confluent_client/doc/man/nodeshell.ronn index c77165df..3f9a6be1 100644 --- a/confluent_client/doc/man/nodeshell.ronn +++ b/confluent_client/doc/man/nodeshell.ronn @@ -26,7 +26,14 @@ as stderr, unlike psh which combines all stdout and stderr into stdout. * `-p PORT`, `--port=PORT` Specify a custom port for ssh - + +* `-s SUBSTITUTION`, `--substitutename=SUBSTITITUTION` + Specify a substitution name instead of the nodename. If no {} are in the substitution, + it is considered to be an append. For example, '-s -ib' would produce 'node1-ib' from 'node1'. + Full expression syntax is supported, in which case the substitution is considered to be the entire + new name. {node}-ib would be equivalent to -ib. For example, nodeshell -s {bmc} node1 + would ssh to the BMC instead of the node. + ## EXAMPLES * Running `echo hi` on for nodes: