From 83b66c47b5dae0e2ca07b43a825ddf305dc4df54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 24 Jan 2018 15:10:59 +0100 Subject: [PATCH] Implement and integrate TerminadoEvendHandler to docker container --- Dockerfile | 6 ++- lib/config/envvars.py | 2 + src/event_handlers/event_handler_main.py | 2 + src/event_handlers/terminado_event_handler.py | 40 +++++++------------ src/event_handlers/terminado_server/server.py | 24 +++++++++++ supervisord.conf | 5 +++ 6 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 src/event_handlers/terminado_server/server.py diff --git a/Dockerfile b/Dockerfile index 496941a..99c8127 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,7 +33,7 @@ RUN curl -fSL -o pyenv-installer ${PYENV_INSTALLER_URL} &&\ . $HOME/.pyenvrc &&\ pyenv install ${PYTHON_VERSION} &&\ pyenv global ${PYTHON_VERSION} &&\ - pip install tornado pyzmq transitions + pip install tornado pyzmq transitions terminado USER root WORKDIR /data/ @@ -43,15 +43,17 @@ RUN yarn build --no-progress ENV TFW_WEB_PORT=4242 ENV TFW_LOGIN_APP_PORT=6666 +ENV TFW_TERMINADO_PORT=7878 ENV TFW_PUBLIC_PORT=8888 ENV TFW_SUPERVISOR_HTTP_PORT=9001 -EXPOSE ${TFW_PUBLIC_PORT} +EXPOSE ${TFW_PUBLIC_PORT} ${TFW_TERMINADO_PORT} ENV TFW_EVENT_HANDLERS_DIR="/opt/event_handlers" ENV TFW_APP_DIR="/srv/app" ENV TFW_FRONTEND_DIR="/srv/frontend" ENV TFW_LOGIN_APP_DIR="/tmp/source_code_server" +ENV TFW_TERMINADO_DIR="/tmp/terminado_server" ENV TFW_LIB_DIR="/usr/local/lib/" ENV TFW_SUPERVISORD_CONF="/etc/supervisor/supervisord.conf" diff --git a/lib/config/envvars.py b/lib/config/envvars.py index 7e11869..38a105a 100644 --- a/lib/config/envvars.py +++ b/lib/config/envvars.py @@ -5,7 +5,9 @@ RECEIVER_PORT = os.getenv('RECEIVER_PORT', 8765) WEB_PORT = os.getenv('TFW_WEB_PORT', 4242) SUPERVISOR_HTTP_PORT = os.getenv('TFW_SUPERVISOR_PORT', 9001) LOGIN_APP_PORT= os.getenv('TFW_LOGIN_APP_PORT', 6666) +TERMINADO_PORT= os.getenv('TFW_TERMINADO_PORT', 9999) SUPERVISOR_HTTP_URI = 'http://localhost:{}'.format(SUPERVISOR_HTTP_PORT) LOGIN_APP_DIR = os.getenv('TFW_LOGIN_APP_DIR') +TERMINADO_DIR = os.getenv('TFW_TERMINADO_DIR') diff --git a/src/event_handlers/event_handler_main.py b/src/event_handlers/event_handler_main.py index 60e905d..421cf21 100644 --- a/src/event_handlers/event_handler_main.py +++ b/src/event_handlers/event_handler_main.py @@ -5,6 +5,7 @@ from functools import partial import source_code from event_handler_base import EventHandlerBase from source_code_event_handler import SourceCodeEventHandler +from terminado_event_handler import TerminadoEventHandler from tornado.ioloop import IOLoop from login_component import authorize_login @@ -86,5 +87,6 @@ if __name__ == '__main__': anchor_b = Rot13Handler('anchor_b') anchor_c = ReverseHandler('anchor_c') anchor_webide = SourceCodeEventHandler('anchor_webide', 'login_component.py', 'login') + anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado') IOLoop.instance().start() diff --git a/src/event_handlers/terminado_event_handler.py b/src/event_handlers/terminado_event_handler.py index 6fdc68e..fc12471 100644 --- a/src/event_handlers/terminado_event_handler.py +++ b/src/event_handlers/terminado_event_handler.py @@ -1,33 +1,21 @@ -import logging +from shutil import rmtree, copytree + from event_handler_base import EventHandlerBase -from tornado.ioloop import IOLoop -from tornado.web import Application -from terminado import TermSocket, SingleTermManager +from util import SupervisorMixin +from config import TERMINADO_DIR -class CORSTermSocket(TermSocket): - def check_origin(self, origin): - return True - - -class TerminadoEventHandler(EventHandlerBase): - def __init__(self, anchor, zmq_context=None): +class TerminadoEventHandler(EventHandlerBase, SupervisorMixin): + def __init__(self, anchor, process_name, zmq_context=None): super().__init__(anchor, zmq_context) + self.working_directory = TERMINADO_DIR + self.process_name = process_name + self.setup_terminado_server() + + def setup_terminado_server(self): + rmtree(self.working_directory, ignore_errors=True) + copytree('terminado_server/', self.working_directory) + self.supervisor.startProcess(self.process_name) def handle_event(self, anchor, data_json): raise NotImplementedError # TODO: wat do? - - -if __name__ == '__main__': - application = Application( - [( - r'/terminal', - CORSTermSocket, - {'term_manager': SingleTermManager(shell_command=['bash'])} - )] - ) - HOST, PORT = 'localhost', 7878 - application.listen(PORT, HOST) - logging.getLogger().setLevel(logging.DEBUG) - logging.info('Terminado Mini Server listening on {}:{}'.format(HOST, PORT)) - IOLoop.instance().start() diff --git a/src/event_handlers/terminado_server/server.py b/src/event_handlers/terminado_server/server.py new file mode 100644 index 0000000..28b49b1 --- /dev/null +++ b/src/event_handlers/terminado_server/server.py @@ -0,0 +1,24 @@ +import logging +from tornado.ioloop import IOLoop +from tornado.web import Application +from terminado import TermSocket, SingleTermManager + +from config import TERMINADO_PORT + + +class CORSTermSocket(TermSocket): + def check_origin(self, origin): + return True + +if __name__ == '__main__': + application = Application( + [( + r'/terminal', + CORSTermSocket, + {'term_manager': SingleTermManager(shell_command=['bash'])} + )] + ) + application.listen(TERMINADO_PORT) + logging.getLogger().setLevel(logging.DEBUG) + logging.info('Terminado Mini Server listening on {}'.format(TERMINADO_PORT)) + IOLoop.instance().start() \ No newline at end of file diff --git a/supervisord.conf b/supervisord.conf index e934465..0e301e0 100644 --- a/supervisord.conf +++ b/supervisord.conf @@ -29,6 +29,11 @@ directory=%(ENV_TFW_LOGIN_APP_DIR)s command=env python server.py autostart=false +[program:terminado] +directory=%(ENV_TFW_TERMINADO_DIR)s +command=env python server.py +autostart=false + [program:nginx] command=/usr/sbin/nginx -g 'daemon off;' autostart=true