Refactor handling of working directory for SourceCodeEventHandler

This commit is contained in:
Kristóf Tóth 2018-02-08 16:49:01 +01:00
parent 89694097cc
commit dceb3b947f
6 changed files with 18 additions and 19 deletions

View File

@ -51,7 +51,8 @@ ENV TFW_APP_DIR="/srv/app"
ENV TFW_FRONTEND_DIR="/srv/frontend" ENV TFW_FRONTEND_DIR="/srv/frontend"
ENV TFW_LOGIN_APP_DIR="/tmp/source_code_server" ENV TFW_LOGIN_APP_DIR="/tmp/source_code_server"
ENV TFW_TERMINADO_DIR="/tmp/terminado_server" ENV TFW_TERMINADO_DIR="/tmp/terminado_server"
ENV TFW_TERMINADO_WD="/home/${AVATAO_USER}" ENV TFW_WEBIDE_WD="/home/${AVATAO_USER}/workdir"
ENV TFW_TERMINADO_WD=${TFW_WEBIDE_WD}
ENV TFW_LIB_DIR="/usr/local/lib/" ENV TFW_LIB_DIR="/usr/local/lib/"
ENV TFW_SUPERVISORD_CONF="/etc/supervisor/supervisord.conf" ENV TFW_SUPERVISORD_CONF="/etc/supervisor/supervisord.conf"
ENV TFW_SUPERVISORD_COMPONENTS="/etc/supervisor/conf" ENV TFW_SUPERVISORD_COMPONENTS="/etc/supervisor/conf"
@ -75,6 +76,12 @@ COPY src/event_handlers ${TFW_EVENT_HANDLERS_DIR}
RUN mv /data/dist ${TFW_FRONTEND_DIR} RUN mv /data/dist ${TFW_FRONTEND_DIR}
COPY src/event_handlers/source_code_server/server.py ${TFW_LOGIN_APP_DIR}/
COPY src/event_handlers/source_code_server/users.db ${TFW_LOGIN_APP_DIR}/
COPY src/event_handlers/source_code_server/login_component.py ${TFW_WEBIDE_WD}/
RUN chown -R ${AVATAO_USER} ${TFW_WEBIDE_WD}
USER ${AVATAO_USER} USER ${AVATAO_USER}
WORKDIR /home/${AVATAO_USER} WORKDIR /home/${AVATAO_USER}

View File

@ -7,6 +7,7 @@ SUPERVISOR_HTTP_PORT = os.getenv('TFW_SUPERVISOR_PORT', 9001)
LOGIN_APP_PORT = os.getenv('TFW_LOGIN_APP_PORT', 6666) LOGIN_APP_PORT = os.getenv('TFW_LOGIN_APP_PORT', 6666)
TERMINADO_PORT = os.getenv('TFW_TERMINADO_PORT', 9999) TERMINADO_PORT = os.getenv('TFW_TERMINADO_PORT', 9999)
TERMINADO_WD = os.getenv('TFW_TERMINADO_WD') TERMINADO_WD = os.getenv('TFW_TERMINADO_WD')
WEBIDE_WD = os.getenv('TFW_WEBIDE_WD')
SUPERVISOR_HTTP_URI = 'http://localhost:{}'.format(SUPERVISOR_HTTP_PORT) SUPERVISOR_HTTP_URI = 'http://localhost:{}'.format(SUPERVISOR_HTTP_PORT)

View File

@ -2,8 +2,10 @@ from source_code_event_handler import SourceCodeEventHandler
from terminado_event_handler import TerminadoEventHandler from terminado_event_handler import TerminadoEventHandler
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tfw.config.envvars import WEBIDE_WD
if __name__ == '__main__': if __name__ == '__main__':
anchor_webide = SourceCodeEventHandler('anchor_webide', 'login_projectdir', 'login') anchor_webide = SourceCodeEventHandler('anchor_webide', WEBIDE_WD, 'login')
anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado') anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado')
IOLoop.instance().start() IOLoop.instance().start()

View File

@ -1,9 +1,7 @@
from shutil import copy, rmtree, copytree
from os.path import splitext, isfile, join, relpath from os.path import splitext, isfile, join, relpath
from glob import glob from glob import glob
from tfw.util import SupervisorMixin from tfw.util import SupervisorMixin
from tfw.config import LOGIN_APP_DIR
from tfw.event_handler_base import EventHandlerBase from tfw.event_handler_base import EventHandlerBase
from tfw.config.logs import logging from tfw.config.logs import logging
@ -11,10 +9,8 @@ log = logging.getLogger(__name__)
class FileManager: class FileManager:
def __init__(self, source_directory, working_directory, selected_file=None): def __init__(self, working_directory, selected_file=None):
self._sourcedir = source_directory
self._workdir = working_directory self._workdir = working_directory
self._reload_files()
self.filename = selected_file or self._relpath(self.files[0]) self.filename = selected_file or self._relpath(self.files[0])
self.language = map_file_extension_to_language(self.filename) self.language = map_file_extension_to_language(self.filename)
@ -44,15 +40,11 @@ class FileManager:
def _relpath(self, filename): def _relpath(self, filename):
return relpath(self._filepath(filename), start=self._workdir) return relpath(self._filepath(filename), start=self._workdir)
def _reload_files(self):
rmtree(self._workdir, ignore_errors=True)
copytree(self._sourcedir, self._workdir)
class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin): class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
def __init__(self, anchor, directory, process_name=None, selected_file=None): def __init__(self, anchor, directory, process_name=None, selected_file=None):
super().__init__(anchor) super().__init__(anchor)
self.filemanager = FileManager(directory, LOGIN_APP_DIR, selected_file=selected_file) self.filemanager = FileManager(directory, selected_file=selected_file)
self.process_name = process_name self.process_name = process_name
self.commands = { self.commands = {
@ -61,10 +53,6 @@ class SourceCodeEventHandler(EventHandlerBase, SupervisorMixin):
'select': self.select 'select': self.select
} }
# Supervisor needs these to run the login program
copy('source_code_server/server.py', LOGIN_APP_DIR)
copy('source_code_server/users.db', LOGIN_APP_DIR)
def read(self, data): def read(self, data):
data['filename'] = self.filemanager.filename data['filename'] = self.filemanager.filename
data['content'] = self.filemanager.file_contents data['content'] = self.filemanager.file_contents

View File

@ -1,9 +1,10 @@
import json import json, sys
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, Application from tornado.web import RequestHandler, Application
from tfw.config import LOGIN_APP_PORT from tfw.config import LOGIN_APP_PORT, WEBIDE_WD
sys.path.append(WEBIDE_WD)
from login_component import authorize_login from login_component import authorize_login