From 563a96f87f6d986e0a4a971ae898867776432fe8 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Mon, 10 Feb 2014 09:16:29 -0500 Subject: [PATCH] Add 'start /node//console/session' to confetty --- bin/confetty | 36 ++++++++++++++++++++----------- confluent/sockapi.py | 51 +++++++++++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/bin/confetty b/bin/confetty index 9fc19886..18f09e75 100755 --- a/bin/confetty +++ b/bin/confetty @@ -98,7 +98,7 @@ def do_command(command, server): sys.exit(0) if argv[0] == "cd": otarget = target - change_target(argv[1]) + target = fullpath_target(argv[1]) for res in send_request('retrieve', target, server): if 'error' in res: print target + ': ' + res['error'] @@ -110,19 +110,24 @@ def do_command(command, server): else: for item in res['item']: print item["href"] - - - - - + if argv[0] == 'start': + targpath = fullpath_target(argv[1]) + tlvdata.send_tlvdata(server, {'operation': 'start', 'path': targpath}) + status = tlvdata.recv_tlvdata(server) + if 'error' in status: + print 'Error: ' + status['error'] + return + print '[console session started]' + startconsole() + return prompt() -def change_target(path): +def fullpath_target(path): global target pathcomponents = path.split("/") if pathcomponents[0] == "": # absolute path - target = path + ntarget = path else: targparts = target.split("/")[:-1] for component in pathcomponents: @@ -134,13 +139,16 @@ def change_target(path): else: targparts.append(component) targparts.append('') - target = '/'.join(targparts) - if len(target) ==0 or target[-1] != '/': - target += '/' + ntarget = '/'.join(targparts) + if len(ntarget) ==0 or ntarget[-1] != '/': + ntarget += '/' + return ntarget def startconsole(): + global inconsole tty.setraw(sys.stdin.fileno()) fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, os.O_NONBLOCK) + inconsole = True def exit(code=0): termios.tcsetattr(sys.stdin.fileno(), termios.TCSANOW, oldtcattr) @@ -250,6 +258,9 @@ while not doexit: data = tlvdata.recv_tlvdata(fh) except Exception: data = None + if type(data) == dict: + print repr(data) + continue if data is not None: sys.stdout.write(data) sys.stdout.flush() @@ -265,6 +276,5 @@ while not doexit: tlvdata.send_tlvdata(server, input) else: command = fh.readline() - inconsole = do_command(command, server) - + do_command(command, server) exit() diff --git a/confluent/sockapi.py b/confluent/sockapi.py index ee2dab05..5eb1b429 100644 --- a/confluent/sockapi.py +++ b/confluent/sockapi.py @@ -23,11 +23,23 @@ SO_PEERCRED = 17 class ClientConsole(object): def __init__(self, client): - self.client = clientn + self.client = client + self.xmit = False + self.pendingdata = "" def sendall(self, data): + if not self.xmit: + self.pendingdata += data + return tlvdata.send_tlvdata(self.client, data) + def startsending(self): + self.xmit = True + if self.pendingdata != "": + tlvdata.send_tlvdata(self.client, self.pendingdata) + self.pendingdata = None + + def sessionhdl(connection, authname): # For now, trying to test the console stuff, so let's just do n4. authenticated = False @@ -65,6 +77,7 @@ def send_response(responses, connection): return for rsp in responses: tlvdata.send_tlvdata(connection, rsp.raw()) + tlvdata.send_tlvdata(connection, {'_requestdone': 1}) def process_request(connection, request, cfm, authdata): @@ -75,25 +88,37 @@ def process_request(connection, request, cfm, authdata): params = request.get('parameters', None) hdlr = None try: - hdlr = pluginapi.handle_path(path, operation, cfm, params) + if operation == 'start': + elems = path.split('/') + if elems[3] != "console": + raise exc.InvalidArgumentException() + node = elems[2] + ccons = ClientConsole(connection) + consession = consoleserver.ConsoleSession( + node=node, configmanager=cfm, datacallback=ccons.sendall) + if consession is None: + raise Exception("TODO") + tlvdata.send_tlvdata(connection, {'started': 1}) + ccons.startsending() + while consession is not None: + data = tlvdata.recv_tlvdata(connection) + if not data: + consession.destroy() + return + consession.write(data) + else: + hdlr = pluginapi.handle_path(path, operation, cfm, params) except exc.NotFoundException: tlvdata.send_tlvdata(connection, {"errorcode": 404, "error": "Target not found"}) + tlvdata.send_tlvdata(connection, {"_requestdone": 1}) except exc.InvalidArgumentException: tlvdata.send_tlvdata(connection, {"errorcode": 400, - "error": "Bad Request"}) + "error": "Bad Request", + "_requestdone": 1}) + tlvdata.send_tlvdata(connection, {"_requestdone": 1}) send_response(hdlr, connection) - tlvdata.send_tlvdata(connection, {'_requestdone': 1}) return - ccons = ClientConsole(connection) - consession = consoleserver.ConsoleSession(node='n4', configmanager=cfm, - datacallback=ccons.sendall) - while (1): - data = tlvdata.recv_tlvdata(connection) - if not data: - consession.destroy() - return - consession.write(data) def _tlshandler():