Improve internal handling of environment variables

This commit is contained in:
Kristóf Tóth 2018-02-09 09:58:47 +01:00 committed by therealkrispet
parent a933248e90
commit 83b24ad538
10 changed files with 31 additions and 35 deletions

View File

@ -44,8 +44,11 @@ ENV TFW_LOGIN_APP_PORT=6666
ENV TFW_TERMINADO_PORT=7878 ENV TFW_TERMINADO_PORT=7878
ENV TFW_SUPERVISOR_HTTP_PORT=9001 ENV TFW_SUPERVISOR_HTTP_PORT=9001
ENV TFW_PUBLIC_PORT=8888 ENV TFW_PUBLIC_PORT=8888
ENV TFW_PUBLISHER_PORT=7654
ENV TFW_RECEIVER_PORT=8765
EXPOSE ${TFW_PUBLIC_PORT} EXPOSE ${TFW_PUBLIC_PORT}
ENV TFW_SUPERVISOR_HTTP_URI="http://localhost:${TFW_SUPERVISOR_HTTP_PORT}"
ENV TFW_EVENT_HANDLERS_DIR="/opt/event_handlers" ENV TFW_EVENT_HANDLERS_DIR="/opt/event_handlers"
ENV TFW_APP_DIR="/srv/app" ENV TFW_APP_DIR="/srv/app"
ENV TFW_FRONTEND_DIR="/srv/frontend" ENV TFW_FRONTEND_DIR="/srv/frontend"

View File

@ -1,15 +1,8 @@
import os from os import environ
from collections import namedtuple
PUBLISHER_PORT = os.getenv('PUBLISHER_PORT', 7654) TFW_PREFIX = 'TFW_'
RECEIVER_PORT = os.getenv('RECEIVER_PORT', 8765) tfwenvvars = {envvar.replace(TFW_PREFIX, '', 1): environ.get(envvar)
WEB_PORT = os.getenv('TFW_WEB_PORT', 4242) for envvar in environ.keys()
SUPERVISOR_HTTP_PORT = os.getenv('TFW_SUPERVISOR_PORT', 9001) if envvar.startswith(TFW_PREFIX)}
LOGIN_APP_PORT = os.getenv('TFW_LOGIN_APP_PORT', 6666) tfwenv = namedtuple('tfwenvtuple', tfwenvvars)(**tfwenvvars)
TERMINADO_PORT = os.getenv('TFW_TERMINADO_PORT', 9999)
TERMINADO_WD = os.getenv('TFW_TERMINADO_WD')
WEBIDE_WD = os.getenv('TFW_WEBIDE_WD')
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')

View File

@ -3,7 +3,7 @@ from functools import partial
from zmq.eventloop.zmqstream import ZMQStream from zmq.eventloop.zmqstream import ZMQStream
from tfw.networking.serialization import serialize_all from tfw.networking.serialization import serialize_all
from tfw.config import PUBLISHER_PORT, RECEIVER_PORT from tfw.config import tfwenv
from tfw.util import ZMQConnectorBase from tfw.util import ZMQConnectorBase
@ -11,7 +11,7 @@ class ServerDownlinkConnector(ZMQConnectorBase):
def __init__(self, zmq_context=None): def __init__(self, zmq_context=None):
super(ServerDownlinkConnector, self).__init__(zmq_context) super(ServerDownlinkConnector, self).__init__(zmq_context)
self._zmq_sub_socket = self._zmq_context.socket(zmq.SUB) self._zmq_sub_socket = self._zmq_context.socket(zmq.SUB)
self._zmq_sub_socket.connect('tcp://localhost:{}'.format(PUBLISHER_PORT)) self._zmq_sub_socket.connect('tcp://localhost:{}'.format(tfwenv.PUBLISHER_PORT))
self._zmq_sub_stream = ZMQStream(self._zmq_sub_socket) self._zmq_sub_stream = ZMQStream(self._zmq_sub_socket)
self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE) self.subscribe = partial(self._zmq_sub_socket.setsockopt_string, zmq.SUBSCRIBE)
@ -23,7 +23,7 @@ class ServerUplinkConnector(ZMQConnectorBase):
def __init__(self, zmq_context=None): def __init__(self, zmq_context=None):
super(ServerUplinkConnector, self).__init__(zmq_context) super(ServerUplinkConnector, self).__init__(zmq_context)
self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH) self._zmq_push_socket = self._zmq_context.socket(zmq.PUSH)
self._zmq_push_socket.connect('tcp://localhost:{}'.format(RECEIVER_PORT)) self._zmq_push_socket.connect('tcp://localhost:{}'.format(tfwenv.RECEIVER_PORT))
def send(self, anchor, response): def send(self, anchor, response):
self._zmq_push_socket.send_multipart(serialize_all(anchor, response)) self._zmq_push_socket.send_multipart(serialize_all(anchor, response))

View File

@ -2,7 +2,7 @@ import zmq
from zmq.eventloop.zmqstream import ZMQStream from zmq.eventloop.zmqstream import ZMQStream
from tfw.networking.serialization import serialize_all from tfw.networking.serialization import serialize_all
from tfw.config import PUBLISHER_PORT, RECEIVER_PORT from tfw.config import tfwenv
from tfw.config.logs import logging from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
from tfw.util import ZMQConnectorBase from tfw.util import ZMQConnectorBase
@ -13,7 +13,7 @@ class EventHandlerDownlinkConnector(ZMQConnectorBase):
super(EventHandlerDownlinkConnector, self).__init__(zmq_context) super(EventHandlerDownlinkConnector, self).__init__(zmq_context)
self._zmq_pull_socket = self._zmq_context.socket(zmq.PULL) self._zmq_pull_socket = self._zmq_context.socket(zmq.PULL)
self._zmq_pull_stream = ZMQStream(self._zmq_pull_socket) self._zmq_pull_stream = ZMQStream(self._zmq_pull_socket)
address = 'tcp://*:{}'.format(RECEIVER_PORT) address = 'tcp://*:{}'.format(tfwenv.RECEIVER_PORT)
self._zmq_pull_socket.bind(address) self._zmq_pull_socket.bind(address)
log.debug('Pull socket bound to {}'.format(address)) log.debug('Pull socket bound to {}'.format(address))
@ -22,7 +22,7 @@ class EventHandlerUplinkConnector(ZMQConnectorBase):
def __init__(self, zmq_context=None): def __init__(self, zmq_context=None):
super(EventHandlerUplinkConnector, self).__init__(zmq_context) super(EventHandlerUplinkConnector, self).__init__(zmq_context)
self._zmq_pub_socket = self._zmq_context.socket(zmq.PUB) self._zmq_pub_socket = self._zmq_context.socket(zmq.PUB)
address = 'tcp://*:{}'.format(PUBLISHER_PORT) address = 'tcp://*:{}'.format(tfwenv.PUBLISHER_PORT)
self._zmq_pub_socket.bind(address) self._zmq_pub_socket.bind(address)
log.debug('Pub socket bound to {}'.format(address)) log.debug('Pub socket bound to {}'.format(address))

View File

@ -2,7 +2,7 @@ import xmlrpc.client, zmq
from contextlib import suppress from contextlib import suppress
from xmlrpc.client import Fault as SupervisorFault from xmlrpc.client import Fault as SupervisorFault
from .config.envvars import SUPERVISOR_HTTP_URI from tfw.config import tfwenv
def create_source_code_response_data(filename, content, language): def create_source_code_response_data(filename, content, language):
@ -14,7 +14,7 @@ def create_source_code_response_data(filename, content, language):
class SupervisorMixin: class SupervisorMixin:
supervisor = xmlrpc.client.ServerProxy(SUPERVISOR_HTTP_URI).supervisor supervisor = xmlrpc.client.ServerProxy(tfwenv.SUPERVISOR_HTTP_URI).supervisor
def stop_process(self): def stop_process(self):
with suppress(SupervisorFault): with suppress(SupervisorFault):

View File

@ -5,7 +5,7 @@ from tornado.web import Application
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from sql_injection_fsm import SQLInjectionFSM from sql_injection_fsm import SQLInjectionFSM
from tfw.config import WEB_PORT from tfw.config import tfwenv
from tfw.config.logs import logging from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler from tfw.networking.server.zmq_websocket_handler import FSMManagingSocketHandler
@ -21,10 +21,10 @@ if __name__ == '__main__':
autoreload=True autoreload=True
) )
application.listen(WEB_PORT) application.listen(tfwenv.WEB_PORT)
log.debug('Python version: {}'.format(sys.version[:5])) log.debug('Python version: {}'.format(sys.version[:5]))
log.debug('Tornado version: {}'.format(tornado.version)) log.debug('Tornado version: {}'.format(tornado.version))
log.debug('ZeroMQ version: {}'.format(zmq.zmq_version())) log.debug('ZeroMQ version: {}'.format(zmq.zmq_version()))
log.debug('PyZMQ version: {}'.format(zmq.pyzmq_version())) log.debug('PyZMQ version: {}'.format(zmq.pyzmq_version()))
log.info('Tornado application listening on port {}'.format(WEB_PORT)) log.info('Tornado application listening on port {}'.format(tfwenv.WEB_PORT))
IOLoop.instance().start() IOLoop.instance().start()

View File

@ -2,10 +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 from tfw.config import tfwenv
if __name__ == '__main__': if __name__ == '__main__':
anchor_webide = SourceCodeEventHandler('anchor_webide', WEBIDE_WD, 'login') anchor_webide = SourceCodeEventHandler('anchor_webide', tfwenv.WEBIDE_WD, 'login')
anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado') anchor_terminado = TerminadoEventHandler('anchor_terminado', 'terminado')
IOLoop.instance().start() IOLoop.instance().start()

View File

@ -2,9 +2,9 @@ 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, WEBIDE_WD from tfw.config import tfwenv
sys.path.append(WEBIDE_WD) sys.path.append(tfwenv.WEBIDE_WD)
from login_component import authorize_login from login_component import authorize_login
@ -23,5 +23,5 @@ class LoginHandler(RequestHandler):
if __name__ == '__main__': if __name__ == '__main__':
application = Application([(r'/login', LoginHandler)]) application = Application([(r'/login', LoginHandler)])
application.listen(LOGIN_APP_PORT) application.listen(tfwenv.LOGIN_APP_PORT)
IOLoop.instance().start() IOLoop.instance().start()

View File

@ -2,7 +2,7 @@ from shutil import rmtree, copytree
from tfw.event_handler_base import EventHandlerBase from tfw.event_handler_base import EventHandlerBase
from tfw.util import SupervisorMixin from tfw.util import SupervisorMixin
from tfw.config import TERMINADO_DIR from tfw.config import tfwenv
from tfw.config.logs import logging from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -10,7 +10,7 @@ log = logging.getLogger(__name__)
class TerminadoEventHandler(EventHandlerBase, SupervisorMixin): class TerminadoEventHandler(EventHandlerBase, SupervisorMixin):
def __init__(self, anchor, process_name): def __init__(self, anchor, process_name):
super().__init__(anchor) super().__init__(anchor)
self.working_directory = TERMINADO_DIR self.working_directory = tfwenv.TERMINADO_DIR
self.process_name = process_name self.process_name = process_name
self.setup_terminado_server() self.setup_terminado_server()

View File

@ -2,7 +2,7 @@ from tornado.ioloop import IOLoop
from tornado.web import Application from tornado.web import Application
from terminado import TermSocket, UniqueTermManager from terminado import TermSocket, UniqueTermManager
from tfw.config import TERMINADO_PORT, TERMINADO_WD from tfw.config import tfwenv
from tfw.config.logs import logging from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -29,5 +29,5 @@ class TerminadoMiniServer:
if __name__ == '__main__': if __name__ == '__main__':
log.info('Terminado Mini Server listening on {}'.format(TERMINADO_PORT)) log.info('Terminado Mini Server listening on {}'.format(tfwenv.TERMINADO_PORT))
TerminadoMiniServer('/terminal', TERMINADO_PORT, TERMINADO_WD, ['bash']).listen() TerminadoMiniServer('/terminal', tfwenv.TERMINADO_PORT, tfwenv.TERMINADO_WD, ['bash']).listen()