From a07ef7e9761960681cfa8e878760b1ded96633a5 Mon Sep 17 00:00:00 2001 From: Akashdeep Dhar Date: Feb 10 2021 08:05:53 +0000 Subject: Added options for attaching handlers on-the-fly Signed-off-by: Akashdeep Dhar --- diff --git a/dish/frnt.py b/dish/frnt.py index f7f164b..935e9e7 100644 --- a/dish/frnt.py +++ b/dish/frnt.py @@ -29,7 +29,6 @@ from dish.back import ( DockerNetworkInformation, DockerVolumeInformation ) -from dish.term import addhandr class PreliminaryInformationEndpoint(object): @@ -85,10 +84,6 @@ class ContainerInformationEndpoint(object): elif opername == "STAT": contiden = rqst.get_param("contiden") retnjson = DockerContainerInformation(self.unixsock).get_per_container_statistics(contiden) - elif opername == "CONS": - contiden = rqst.get_param("contiden") - comdexec = rqst.get_param("comdexec") - retnjson = addhandr(contiden, comdexec) else: retnjson = {"retnmesg": "deny"} else: diff --git a/dish/term.py b/dish/term.py index 36b12f6..c7d8d41 100644 --- a/dish/term.py +++ b/dish/term.py @@ -19,22 +19,26 @@ ########################################################################## """ -import tornado.ioloop +from tornado.ioloop import IOLoop import tornado.web from terminado import TermSocket, UniqueTermManager import sys from hashlib import sha256 -def inptloop(termmngr): - loop = tornado.ioloop.IOLoop.instance() - try: - loop.start() - except KeyboardInterrupt: - print("\nShutting down on SIGINT") - finally: - termmngr.shutdown() - loop.close() +class AttachmentEndpoint(tornado.web.RequestHandler): + def set_default_headers(self): + self.set_header("Access-Control-Allow-Origin", "*") + + def get(self): + try: + contiden = self.get_argument("contiden") + comdexec = self.get_argument("comdexec") + self.write(addhandr(contiden, comdexec)) + except Exception as expt: + print("Console attachment failed! - " + str(expt)) + self.set_header("Access-Control-Allow-Origin", "*") + self.write({"retnmesg": "deny"}) termmngr = UniqueTermManager(shell_command=["sh"]) @@ -46,6 +50,9 @@ handlers = [ { "term_manager": termmngr } + ), + ( + r"/atchcons/", AttachmentEndpoint ) ] @@ -56,6 +63,7 @@ def mainterm(portqant): mainobjc = tornado.web.Application(handlers) print(" * TermSocket started on port " + portqant) mainobjc.listen(portqant, "0.0.0.0") + IOLoop.instance().start() except KeyboardInterrupt: print("\nShutting down on SIGINT") finally: @@ -64,11 +72,12 @@ def mainterm(portqant): def addhandr(contiden, comdexec): try: - print(contiden, comdexec) + print(" * " + comdexec + " attached to " + contiden) urlpatrn = sha256((contiden + comdexec).encode()).hexdigest() comdexec = comdexec.split() stndexec = ["docker", "exec", "-ti", contiden] - stndexec.append(comdexec) + for indx in comdexec: + stndexec.append(indx) mainobjc.add_handlers( r".*", # match any host [ @@ -85,7 +94,7 @@ def addhandr(contiden, comdexec): "urlpatrn": urlpatrn } except Exception as expt: - print("Failed to attach terminal" + "\n" + str(expt)) + print(" * Failed to attach terminal" + "\n" + str(expt)) return { "retnmesg": "deny" }