From b2c773bb847985e296d2dbd10c41b0b057ab2dc3 Mon Sep 17 00:00:00 2001 From: Tinashe Date: Thu, 16 Feb 2023 15:54:21 -0500 Subject: [PATCH] nodeconsole kill:added functionality for closing open windowed consoles --- confluent_client/bin/nodeconsole | 44 ++++++++++++++++++++--- confluent_client/doc/man/nodeconsole.ronn | 5 ++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/confluent_client/bin/nodeconsole b/confluent_client/bin/nodeconsole index b606497a..2aa02973 100755 --- a/confluent_client/bin/nodeconsole +++ b/confluent_client/bin/nodeconsole @@ -32,7 +32,7 @@ import re confettypath = os.path.join(os.path.dirname(sys.argv[0]), 'confetty') argparser = optparse.OptionParser( - usage="Usage: %prog [options] node", + usage="Usage: %prog [options] [kill][-- [passthroughoptions]]", epilog="Command sequences are available while connected to a console, hit " "ctrl-'e', then release ctrl, then 'c', then '?' for a full list. " "For example, ctrl-'e', then 'c', then '.' will exit the current " @@ -65,13 +65,22 @@ argparser.add_option('-w','--windowed', action='store_true', default=False, (options, args) = argparser.parse_args() pass_through_args = [] -if len(args) > 1: - pass_through_args = args[1:] - args = args[:1] +killcon = False +try: + noderange = args[0] + if len(args) > 1: + if args[1] == 'kill': + killcon = True + pass_through_args = args[1:] + args = args[:1] +except IndexError: + argparser.print_help() + sys.exit(1) if len(args) != 1: argparser.print_help() sys.exit(1) + if options.log: logname = args[0] if not os.path.exists(logname) and logname[0] != '/': @@ -82,6 +91,29 @@ if options.log: logreader.replay_to_console(logname) sys.exit(0) +def kill(noderange): + sess = client.Command() + envstring=os.environ.get('NODECONSOLE_WINDOWED_COMMAND') + if not envstring: + envstring = 'xterm' + + nodes = [] + for res in sess.read('/noderange/{0}/nodes/'.format(args[0])): + node = res.get('item', {}).get('href', '/').replace('/', '') + if not node: + sys.stderr.write(res.get('error', repr(res)) + '\n') + sys.exit(1) + nodes.append(node) + + for node in nodes: + ps_data=subprocess.Popen(['ps', '-auxww' ], stdout=subprocess.PIPE) + wintr=ps_data.communicate()[0] + for line in wintr.decode('utf-8').split('\n'): + if confettypath in line and envstring in line and node in line: + pid_line = [x for x in line.split(' ') if x != ''] + ps_data=subprocess.Popen(['kill', '-9', pid_line[1] ], stdout=subprocess.PIPE) + + sys.exit(0) def handle_geometry(envlist, sizegeometry, side_pad=0, top_pad=0, first=False): if '-geometry' in envlist: @@ -101,6 +133,10 @@ def handle_geometry(envlist, sizegeometry, side_pad=0, top_pad=0, first=False): g_index = 1 return envlist +# add funcltionality to close/kill all open consoles +if killcon: + kill(noderange) + #added functionality for wcons if options.windowed: result=subprocess.Popen(['xwininfo', '-root'], stdout=subprocess.PIPE) diff --git a/confluent_client/doc/man/nodeconsole.ronn b/confluent_client/doc/man/nodeconsole.ronn index 73676b5c..4271a887 100644 --- a/confluent_client/doc/man/nodeconsole.ronn +++ b/confluent_client/doc/man/nodeconsole.ronn @@ -2,7 +2,7 @@ nodeconsole(8) -- Open a console to a confluent node ===================================================== ## SYNOPSIS -`nodeconsole [options] [-- [passthroughoptions]]` +`nodeconsole [options] [kill][-- [passthroughoptions]]` ## DESCRIPTION @@ -16,6 +16,9 @@ will initiate an automatic retry interval that is randomized between 2 and 4 min The reopen escape sequence below requests an immediate retry, as does connecting a new session. +When a windowed console is open the `nodeconsole kill` command will kill the +console process which will result in the console window closing. + ## OPTIONS * `-t`, `--tile`: