From 884a6be68c74435334ec1ab76e6fd3bce6452612 Mon Sep 17 00:00:00 2001 From: Tkucherera <43506274+Tkucherera@users.noreply.github.com> Date: Thu, 27 Apr 2023 15:46:40 -0400 Subject: [PATCH 1/2] Wkill like command (#1) * wkill like command fix * tmux session resuse --------- Co-authored-by: Tinashe --- confluent_client/bin/nodeconsole | 33 +++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/confluent_client/bin/nodeconsole b/confluent_client/bin/nodeconsole index 2aa02973..93818ed4 100755 --- a/confluent_client/bin/nodeconsole +++ b/confluent_client/bin/nodeconsole @@ -1,4 +1,4 @@ -#!/usr/bin/python2 +#!/usr/libexec/platform-python # vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2015 Lenovo @@ -23,6 +23,7 @@ path = os.path.dirname(os.path.realpath(__file__)) path = os.path.realpath(os.path.join(path, '..', 'lib', 'python')) if path.startswith('/opt'): sys.path.append(path) + import confluent.client as client import confluent.sortutil as sortutil import confluent.logreader as logreader @@ -106,12 +107,20 @@ def kill(noderange): 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) + s=socket.socket(socket.AF_UNIX) + winid=None + try: + win=subprocess.Popen(['xwininfo', '-tree', '-root'], stdout=subprocess.PIPE) + wintr=win.communicate()[0] + for line in wintr.decode('utf-8').split('\n'): + if 'console: {0}'.format(node) in line or 'confetty' in line: + win_obj = [ele for ele in line.split(' ') if ele.strip()] + winid = win_obj[0] + except: + print("Error: cannot retrieve window id of node {}".format(node)) + + if winid: + ps_data=subprocess.Popen(['xkill', '-id', winid ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) sys.exit(0) @@ -259,7 +268,7 @@ if options.windowed: else: pass with open(os.devnull, 'wb') as devnull: - xopen=subprocess.Popen(envlist + [confettypath, '-m', '5', 'start', '/nodes/{0}/console/session'.format(node)] , stdin=devnull) + xopen=subprocess.Popen(envlist + [confettypath, '-c', '/tmp/controlpath-{0}'.format(node), '-m', '5', 'start', '/nodes/{0}/console/session'.format(node)] , stdin=devnull) sys.exit(0) #end of wcons if options.tile: @@ -273,8 +282,13 @@ if options.tile: sys.exit(1) nodes.append(node) initial = True + in_tmux = False pane = 0 sessname = 'nodeconsole_{0}'.format(os.getpid()) + if os.environ.get("TMUX"): + initial = False + in_tmux = True + subprocess.call(['tmux', 'rename-session', sessname]) for node in sortutil.natural_sort(nodes): panename = '{0}:{1}'.format(sessname, pane) if initial: @@ -295,7 +309,8 @@ if options.tile: pane += 1 subprocess.call(['tmux', 'select-pane', '-t', sessname]) subprocess.call(['tmux', 'set-option', '-t', panename, 'pane-border-status', 'top'], stderr=null) - os.execlp('tmux', 'tmux', 'attach', '-t', sessname) + if not in_tmux: + os.execlp('tmux', 'tmux', 'attach', '-t', sessname) else: os.execl(confettypath, confettypath, 'start', '/nodes/{0}/console/session'.format(args[0])) \ No newline at end of file From 190ec2473b777c22563b54e6e4ce6874b467df8f Mon Sep 17 00:00:00 2001 From: Tinashe Date: Wed, 31 May 2023 17:39:53 -0400 Subject: [PATCH 2/2] dump log to stfout with timestamps --- confluent_client/bin/nodeconsole | 14 +++++++++ confluent_client/confluent/logreader.py | 42 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/confluent_client/bin/nodeconsole b/confluent_client/bin/nodeconsole index 4dc0d5f4..a7f5a501 100755 --- a/confluent_client/bin/nodeconsole +++ b/confluent_client/bin/nodeconsole @@ -42,6 +42,10 @@ argparser.add_option('-t', '--tile', action='store_true', default=False, help='Tile console windows in the terminal') argparser.add_option('-l', '--log', action='store_true', default=False, help='Enter log replay mode instead of showing a live console') + +argparser.add_option('-d', '--dump', action='store_true', default=False, + help= 'Dump log in stdout with timestamps') + argparser.add_option('-w','--windowed', action='store_true', default=False, help='Open terminal windows for each node. The ' 'environment variable NODECONSOLE_WINDOWED_COMMAND ' @@ -92,6 +96,16 @@ if options.log: logreader.replay_to_console(logname) sys.exit(0) +if options.dump: + logname = args[0] + if not os.path.exists(logname) and logname[0] != '/': + logname = os.path.join('/var/log/confluent/consoles', logname) + if not os.path.exists(logname): + sys.stderr.write('Unable to locate {0} on local system\n'.format(logname)) + sys.exit(1) + logreader.dump_to_console(logname) + sys.exit(0) + def kill(noderange): sess = client.Command() envstring=os.environ.get('NODECONSOLE_WINDOWED_COMMAND') diff --git a/confluent_client/confluent/logreader.py b/confluent_client/confluent/logreader.py index 5bb14892..d1a86fa3 100644 --- a/confluent_client/confluent/logreader.py +++ b/confluent_client/confluent/logreader.py @@ -296,5 +296,47 @@ def replay_to_console(txtfile): sys.exit(1) _replay_to_console(txtfile, binfile) +def dump_to_console(txtfile): + if os.path.exists(txtfile + '.cbl'): + binfile = txtfile + '.cbl' + elif '.' not in txtfile: + if '/' not in txtfile: + txtfile = os.getcwd() + '/' + txtfile + sys.stderr.write('Unable to locate cbl file: "{0}"\n'.format(txtfile + '.cbl')) + sys.exit(1) + else: + fileparts = txtfile.split('.') + prefix = '.'.join(fileparts[:-1]) + binfile = prefix + '.cbl.' + fileparts[-1] + if not os.path.exists(binfile): + sys.stderr.write('Unable to locate cbl file: "{0}"\n'.format(binfile)) + sys.exit(1) + replay = LogReplay(txtfile, binfile) + quitit = False + writeout('\x1b[2J\x1b[;H') + prepend = '' + try: + while not quitit: + newdata, delay = replay.get_output(False) + if newdata: + if prepend: + newdata = prepend + newdata + prepend = b'' + writeout(newdata.replace(b'\r\n', '\r\n[ {0} ] '.format(time.strftime('%m/%d %H:%M:%S', time.localtime(replay.laststamp))).encode('utf8'))) + newdata = '' + try: + if hasattr(sys.stdout, 'buffer'): + sys.stdout.buffer.flush() + else: + sys.stdout.flush() + except IOError: + pass + else: + break + except Exception: + writeout('\x1b[m\x1b[?25h\n') + raise + writeout('\x1b[m\x1b[?25h\n') + if __name__ == '__main__': replay_to_console(sys.argv[1])