Simplify *main setup

This commit is contained in:
Kristóf Tóth 2019-07-15 13:43:34 +02:00
parent d9e69a6327
commit 290c301d27
3 changed files with 37 additions and 69 deletions

View File

@ -11,33 +11,16 @@ LOG = logging.getLogger(__name__)
class CenatorEventHandler: class CenatorEventHandler:
keys = ['history.bash'] keys = ['history.bash']
# pylint: disable=no-self-use
""" def handle_event(self, message, server_connector): # pylint: disable=no-self-use
Logs commands executed in terminal to messages and invokes an
additional callback function to handle special commands.
!! Please remove from production code. !!
"""
def handle_event(self, message, server_connector):
command = message['value'] command = message['value']
LOG.debug('User executed command: "%s"', command) LOG.debug('User executed command: "%s"', command)
MessageSender(server_connector).send('JOHN CENA', f'You\'ve executed "{command}"') MessageSender(server_connector).send('JOHN CENA', f'You\'ve executed "{command}"')
class TestCommandsEventHandler(TerminalCommandsEventHandler): class TestCommandsEventHandler(TerminalCommandsEventHandler):
""" # pylint: disable=unused-argument,attribute-defined-outside-init,no-self-use
Some example commands useful for debugging.
!! Please remove from production code !! and inherit your own
class from TerminalCommands if you need to define custom
commands in your challenge.
"""
# pylint: disable=unused-argument, attribute-defined-outside-init, no-self-use
def command_sendmessage(self, *args): def command_sendmessage(self, *args):
"""
Insert TFW message template as first argument if executed without args.
Evaluate first argumen as a dict and send it to the frontend.
This is useful for playing around with frontend APIs.
"""
if not args: if not args:
message_template = """'{"key": "", "data": {"command": ""}}'""" message_template = """'{"key": "", "data": {"command": ""}}'"""
TFWUplinkConnector().send_message({ TFWUplinkConnector().send_message({
@ -52,12 +35,7 @@ class TestCommandsEventHandler(TerminalCommandsEventHandler):
class MessageFSMStepsEventHandler: class MessageFSMStepsEventHandler:
# pylint: disable=no-self-use def handle_event(self, message, server_connector): # pylint: disable=no-self-use
"""
This example EventHandler is capable of detecting FSM state.
!! Please remove from production code !!
"""
def handle_event(self, message, server_connector):
""" """
When the FSM steps this method is invoked. When the FSM steps this method is invoked.
Receives a 'data' field from an fsm_update message as kwargs. Receives a 'data' field from an fsm_update message as kwargs.

View File

@ -22,69 +22,61 @@ LOG = logging.getLogger(__name__)
def main(): def main():
# pylint: disable=unused-variable,too-many-locals # pylint: disable=unused-variable
Logger([ Logger([
Log(stderr, LogFormatter(20)), Log(stderr, LogFormatter(20)),
Log(TFWENV.LOGFILE, VerboseLogFormatter()) Log(TFWENV.LOGFILE, VerboseLogFormatter())
]).start() ]).start()
eh_factory = EventHandlerFactory()
# TFW component EventHandlers (builtins, required for their respective functionalities) eh_factory = EventHandlerFactory()
fsm = FSMManagingEventHandler( # TFW FSM # TFW builtin EventHandlers (required for their respective functionalities)
# TFW FSM
fsm_eh = eh_factory.build(FSMManagingEventHandler(
fsm_type=partial( fsm_type=partial(
YamlFSM, YamlFSM,
'test_fsm.yml', 'test_fsm.yml',
{} # jinja2 variables, use empty dict to enable jinja2 parsing without any variables {} # jinja2 variables, empty dict enables jinja2 without any variables
) )
) ))
fsm_eh = eh_factory.build(fsm) # Web IDE backend
ide_eh = eh_factory.build(IdeEventHandler(
ide = IdeEventHandler( # Web IDE backend
allowed_directories=[TFWENV.IDE_WD, TFWENV.WEBSERVICE_DIR], allowed_directories=[TFWENV.IDE_WD, TFWENV.WEBSERVICE_DIR],
directory=TFWENV.IDE_WD, directory=TFWENV.IDE_WD,
exclude=['*.pyc'] exclude=['*.pyc']
) ))
ide_eh = eh_factory.build(ide) # Web shell backend
terminal_eh = eh_factory.build(TerminalEventHandler())
terminal = TerminalEventHandler() # Web shell backend # Handles 'deploy' button clicks
terminal_eh = eh_factory.build(terminal) processmanager_eh = eh_factory.build(ProcessManagingEventHandler(
processmanager = ProcessManagingEventHandler( # Handles 'deploy' button clicks
log_tail=2000 log_tail=2000
) ))
processmanager_eh = eh_factory.build(processmanager) # Sends live logs of webservice process to frontend
logmonitor_eh = eh_factory.build(LogMonitoringEventHandler(
logmonitor = LogMonitoringEventHandler( # Sends live logs of webservice process to frontend
process_name='webservice', process_name='webservice',
log_tail=2000 log_tail=2000
) ))
logmonitor_eh = eh_factory.build(logmonitor) # Manages filesystem snapshots of directories
snapshot_eh = eh_factory.build(DirectorySnapshottingEventHandler(
snapshot = DirectorySnapshottingEventHandler( # Manages filesystem snapshots of directories
directories=[ directories=[
TFWENV.IDE_WD, TFWENV.IDE_WD,
TFWENV.WEBSERVICE_DIR TFWENV.WEBSERVICE_DIR
] ]
) ))
snapshot_eh = eh_factory.build(snapshot) # Proxies frontend API calls to frontend
frontend_eh = eh_factory.build(FrontendEventHandler())
frontend = FrontendEventHandler() # Proxies frontend API calls to frontend # Replace these with your custom event handlers
frontend_eh = eh_factory.build(frontend) # Echoes executed commands to messages
cenator_eh = eh_factory.build(CenatorEventHandler())
# Your custom event handlers # Echoes FSM steps
cenator = CenatorEventHandler()
cenator_eh = eh_factory.build(cenator)
message_fsm_steps = MessageFSMStepsEventHandler()
message_fsm_steps_eh = eh_factory.build( message_fsm_steps_eh = eh_factory.build(
message_fsm_steps, MessageFSMStepsEventHandler(),
event_handler_type=FSMAwareEventHandler event_handler_type=FSMAwareEventHandler
) )
# Catches special commands
commands = TestCommandsEventHandler( # Catches special commands commands_eh = eh_factory.build(TestCommandsEventHandler(
bashrc=f'/home/{TAOENV.USER}/.bashrc' bashrc=f'/home/{TAOENV.USER}/.bashrc'
) ))
commands_eh = eh_factory.build(commands)
setup_signal_handlers() setup_signal_handlers()
IOLoop.instance().start() IOLoop.instance().start()

View File

@ -21,12 +21,10 @@ def main():
eh_factory = EventHandlerFactory() eh_factory = EventHandlerFactory()
json_pipe = PipeIOEventHandler( json_pipe_eh = eh_factory.build(PipeIOEventHandler(
'/tmp/tfw_send', '/tmp/tfw_send',
'/tmp/tfw_recv' '/tmp/tfw_recv'
) ))
json_pipe_eh = eh_factory.build(json_pipe)
setup_signal_handlers() setup_signal_handlers()
IOLoop.instance().start() IOLoop.instance().start()