From f8786237d7ceac4029421dbac0f2ce7d60528b41 Mon Sep 17 00:00:00 2001 From: Akashdeep Dhar Date: Feb 08 2021 06:39:20 +0000 Subject: Initial container spawning Signed-off-by: Akashdeep Dhar --- diff --git a/base/term.py b/base/term.py deleted file mode 100644 index 262663e..0000000 --- a/base/term.py +++ /dev/null @@ -1,51 +0,0 @@ -""" -########################################################################## -* -* Copyright © 2019-2021 Akashdeep Dhar -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -* -########################################################################## -""" - -import tornado.ioloop -import tornado.web -from terminado import TermSocket, UniqueTermManager - - -def inptloop(termmngr): - loop = tornado.ioloop.IOLoop.instance() - try: - loop.start() - except KeyboardInterrupt: - print("\nShutting down on SIGINT") - finally: - termmngr.shutdown() - loop.close() - - -def mainterm(portqant): - termmngr = UniqueTermManager(shell_command=["sh"]) - handlers = [ - ( - r"/websocket", TermSocket, - { - 'term_manager': termmngr - } - ) - ] - mainobjc = tornado.web.Application(handlers) - print(" * TermSocket started on port " + portqant) - mainobjc.listen(portqant, "0.0.0.0") - inptloop(termmngr) diff --git a/dish/frnt.py b/dish/frnt.py index 935e9e7..f7f164b 100644 --- a/dish/frnt.py +++ b/dish/frnt.py @@ -29,6 +29,7 @@ from dish.back import ( DockerNetworkInformation, DockerVolumeInformation ) +from dish.term import addhandr class PreliminaryInformationEndpoint(object): @@ -84,6 +85,10 @@ 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 new file mode 100644 index 0000000..36b12f6 --- /dev/null +++ b/dish/term.py @@ -0,0 +1,91 @@ +""" +########################################################################## +* +* Copyright © 2019-2021 Akashdeep Dhar +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* +########################################################################## +""" + +import tornado.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() + + +termmngr = UniqueTermManager(shell_command=["sh"]) + + +handlers = [ + ( + r"/websocket", TermSocket, + { + "term_manager": termmngr + } + ) +] + + +def mainterm(portqant): + try: + global mainobjc + mainobjc = tornado.web.Application(handlers) + print(" * TermSocket started on port " + portqant) + mainobjc.listen(portqant, "0.0.0.0") + except KeyboardInterrupt: + print("\nShutting down on SIGINT") + finally: + sys.exit() + + +def addhandr(contiden, comdexec): + try: + print(contiden, comdexec) + urlpatrn = sha256((contiden + comdexec).encode()).hexdigest() + comdexec = comdexec.split() + stndexec = ["docker", "exec", "-ti", contiden] + stndexec.append(comdexec) + mainobjc.add_handlers( + r".*", # match any host + [ + ( + r"/" + urlpatrn, TermSocket, + { + "term_manager": UniqueTermManager(shell_command=stndexec) + } + ) + ] + ) + return { + "retnmesg": "allow", + "urlpatrn": urlpatrn + } + except Exception as expt: + print("Failed to attach terminal" + "\n" + str(expt)) + return { + "retnmesg": "deny" + } diff --git a/falc.py b/falc.py index ce49bee..3dd7eb7 100644 --- a/falc.py +++ b/falc.py @@ -30,7 +30,7 @@ from base.frnt import ( ProcessHandlingEndpoint, StatisticalEndpoint, ) -from base.term import mainterm +from dish.term import mainterm from dish.frnt import ( ContainerInformationEndpoint, ImageInformationEndpoint,