1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-12-05 02:01:33 +00:00

Comply new package structure

This commit is contained in:
Kristóf Tóth 2019-07-24 15:52:38 +02:00
parent 290c301d27
commit 9095a9f85a
6 changed files with 58 additions and 55 deletions

View File

@ -2,8 +2,8 @@ FROM avatao/controller:debian-buster
USER root USER root
ENV PYTHONPATH="/usr/local/lib" \ ENV PYTHONPATH="/usr/local/lib" \
TFW_PUBLISHER_PORT=7654 \ TFW_PUB_PORT=7654 \
TFW_RECEIVER_PORT=8765 \ TFW_PULL_PORT=8765 \
TFW_AUTH_KEY="/tmp/tfw-auth.key" \ TFW_AUTH_KEY="/tmp/tfw-auth.key" \
CONTROLLER_PORT=5555 CONTROLLER_PORT=5555

View File

@ -4,8 +4,8 @@ import json
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, Application from tornado.web import RequestHandler, Application
from tfw.builtins import FSMAwareEventHandler from tfw.event_handlers import FSMAwareEventHandler
from tfw.main import EventHandlerFactory from tfw.main import EventHandlerFactory, setup_signal_handlers
class ControllerPostHandler(RequestHandler): class ControllerPostHandler(RequestHandler):
@ -20,23 +20,18 @@ class ControllerPostHandler(RequestHandler):
})) }))
class ControllerEventHandler(FSMAwareEventHandler):
def handle_event(self, message):
pass
if __name__ == '__main__': if __name__ == '__main__':
controller = ControllerEventHandler('controller') controller_eh = EventHandlerFactory().build(
controller_eh = EventHandlerFactory().build(controller) lambda *_: None,
event_handler_type=FSMAwareEventHandler
)
application = Application([( application = Application([(
f'/{os.environ["SECRET"]}', f'/{os.environ["SECRET"]}',
ControllerPostHandler, ControllerPostHandler,
{'controller': controller} {'controller': controller_eh}
)]) )])
application.listen(os.environ['CONTROLLER_PORT']) application.listen(os.environ['CONTROLLER_PORT'])
try: setup_signal_handlers()
IOLoop.instance().start() IOLoop.instance().start()
finally:
controller.cleanup()

View File

@ -1,15 +1,15 @@
import logging import logging
from ast import literal_eval from ast import literal_eval
from tfw.components import MessageSender from tfw.components.frontend import MessageSender
from tfw.builtins import TerminalCommandsEventHandler from tfw.components.terminal import TerminalCommandsHandler
from tfw.main import TFWUplinkConnector from tfw.main import TFWUplinkConnector
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
class CenatorEventHandler: class CenatorHandler:
keys = ['history.bash'] keys = ['history.bash']
def handle_event(self, message, server_connector): # pylint: disable=no-self-use def handle_event(self, message, server_connector): # pylint: disable=no-self-use
@ -18,7 +18,7 @@ class CenatorEventHandler:
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 TestCommandsHandler(TerminalCommandsHandler):
# pylint: disable=unused-argument,attribute-defined-outside-init,no-self-use # pylint: disable=unused-argument,attribute-defined-outside-init,no-self-use
def command_sendmessage(self, *args): def command_sendmessage(self, *args):
if not args: if not args:
@ -34,14 +34,13 @@ class TestCommandsEventHandler(TerminalCommandsEventHandler):
TFWUplinkConnector().send_message(literal_eval(args[0])) TFWUplinkConnector().send_message(literal_eval(args[0]))
class MessageFSMStepsEventHandler: def messageFSMStepsHandler(message, server_connector):
def handle_event(self, message, server_connector): # pylint: disable=no-self-use """
""" 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. """
""" MessageSender(server_connector).send(
MessageSender(server_connector).send( 'FSM info',
'FSM info', f'FSM has stepped from state "{message["last_event"]["from_state"]}" '
f'FSM has stepped from state "{message["last_event"]["from_state"]}" ' f'to state "{message["current_state"]}" in response to trigger "{message["last_event"]["trigger"]}"'
f'to state "{message["current_state"]}" in response to trigger "{message["last_event"]["trigger"]}"' )
)

View File

@ -6,16 +6,17 @@ from tornado.ioloop import IOLoop
from tfw.fsm import YamlFSM from tfw.fsm import YamlFSM
from tfw.event_handlers import FSMAwareEventHandler from tfw.event_handlers import FSMAwareEventHandler
from tfw.builtins import IdeEventHandler, TerminalEventHandler, FrontendEventHandler from tfw.components.ide import IdeHandler
from tfw.builtins import LogMonitoringEventHandler, ProcessManagingEventHandler from tfw.components.terminal import TerminalHandler
from tfw.builtins import DirectorySnapshottingEventHandler, FSMManagingEventHandler from tfw.components.frontend import FrontendHandler
from tfw.components.process_management import ProcessHandler, ProcessLogHandler
from tfw.components.snapshots import SnapshotHandler
from tfw.components.fsm import FSMHandler
from tfw.main import EventHandlerFactory, setup_signal_handlers from tfw.main import EventHandlerFactory, setup_signal_handlers
from tfw.logging import Log, Logger, LogFormatter, VerboseLogFormatter from tfw.logging import Log, Logger, LogFormatter, VerboseLogFormatter
from tfw.config import TFWENV from tfw.config import TFWENV, TAOENV
from tao.config import TAOENV
from custom_event_handlers import MessageFSMStepsEventHandler from custom_handlers import CenatorHandler, TestCommandsHandler, messageFSMStepsHandler
from custom_event_handlers import CenatorEventHandler, TestCommandsEventHandler
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -31,7 +32,7 @@ def main():
eh_factory = EventHandlerFactory() eh_factory = EventHandlerFactory()
# TFW builtin EventHandlers (required for their respective functionalities) # TFW builtin EventHandlers (required for their respective functionalities)
# TFW FSM # TFW FSM
fsm_eh = eh_factory.build(FSMManagingEventHandler( fsm_eh = eh_factory.build(FSMHandler(
fsm_type=partial( fsm_type=partial(
YamlFSM, YamlFSM,
'test_fsm.yml', 'test_fsm.yml',
@ -39,42 +40,50 @@ def main():
) )
)) ))
# Web IDE backend # Web IDE backend
ide_eh = eh_factory.build(IdeEventHandler( ide_eh = eh_factory.build(IdeHandler(
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']
)) ))
# Web shell backend # Web shell backend
terminal_eh = eh_factory.build(TerminalEventHandler()) terminal_eh = eh_factory.build(TerminalHandler(
port=TFWENV.TERMINADO_PORT,
user=TAOENV.USER,
workind_directory=TFWENV.TERMINADO_WD,
histfile=TFWENV.HISTFILE
))
# Handles 'deploy' button clicks # Handles 'deploy' button clicks
processmanager_eh = eh_factory.build(ProcessManagingEventHandler( processmanager_eh = eh_factory.build(ProcessHandler(
log_tail=2000 supervisor_uri=TFWENV.SUPERVISOR_HTTP_URI,
log_tail=2000,
)) ))
# Sends live logs of webservice process to frontend # Sends live logs of webservice process to frontend
logmonitor_eh = eh_factory.build(LogMonitoringEventHandler( logmonitor_eh = eh_factory.build(ProcessLogHandler(
process_name='webservice', process_name='webservice',
supervisor_uri=TFWENV.SUPERVISOR_HTTP_URI,
log_tail=2000 log_tail=2000
)) ))
# Manages filesystem snapshots of directories # Manages filesystem snapshots of directories
snapshot_eh = eh_factory.build(DirectorySnapshottingEventHandler( snapshot_eh = eh_factory.build(SnapshotHandler(
directories=[ directories=[
TFWENV.IDE_WD, TFWENV.IDE_WD,
TFWENV.WEBSERVICE_DIR TFWENV.WEBSERVICE_DIR
] ],
snapshots_dir=TFWENV.SNAPSHOTS_DIR
)) ))
# Proxies frontend API calls to frontend # Proxies frontend API calls to frontend
frontend_eh = eh_factory.build(FrontendEventHandler()) frontend_eh = eh_factory.build(FrontendHandler())
# Replace these with your custom event handlers # Replace these with your custom event handlers
# Echoes executed commands to messages # Echoes executed commands to messages
cenator_eh = eh_factory.build(CenatorEventHandler()) cenator_eh = eh_factory.build(CenatorHandler())
# Echoes FSM steps # Echoes FSM steps
message_fsm_steps_eh = eh_factory.build( message_fsm_steps_eh = eh_factory.build(
MessageFSMStepsEventHandler(), messageFSMStepsHandler,
event_handler_type=FSMAwareEventHandler event_handler_type=FSMAwareEventHandler
) )
# Catches special commands # Catches special commands
commands_eh = eh_factory.build(TestCommandsEventHandler( commands_eh = eh_factory.build(TestCommandsHandler(
bashrc=f'/home/{TAOENV.USER}/.bashrc' bashrc=f'/home/{TAOENV.USER}/.bashrc'
)) ))

View File

@ -3,7 +3,7 @@ from sys import stderr
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tfw.builtins import PipeIOEventHandler from tfw.components.pipe_io import PipeIOHandler
from tfw.config import TFWENV from tfw.config import TFWENV
from tfw.logging import Log, Logger, LogFormatter, VerboseLogFormatter from tfw.logging import Log, Logger, LogFormatter, VerboseLogFormatter
from tfw.main import EventHandlerFactory, setup_signal_handlers from tfw.main import EventHandlerFactory, setup_signal_handlers
@ -21,7 +21,7 @@ def main():
eh_factory = EventHandlerFactory() eh_factory = EventHandlerFactory()
json_pipe_eh = eh_factory.build(PipeIOEventHandler( json_pipe_eh = eh_factory.build(PipeIOHandler(
'/tmp/tfw_send', '/tmp/tfw_send',
'/tmp/tfw_recv' '/tmp/tfw_recv'
)) ))

View File

@ -3,8 +3,8 @@
from os.path import exists from os.path import exists
from tfw.fsm import LinearFSM from tfw.fsm import LinearFSM
from tfw.components import MessageSender from tfw.components.frontend import MessageSender
from tfw.builtins import TFWServerUplinkConnector from tfw.main import TFWUplinkConnector
class TestFSM(LinearFSM): class TestFSM(LinearFSM):
@ -12,7 +12,7 @@ class TestFSM(LinearFSM):
def __init__(self): def __init__(self):
super().__init__(6) super().__init__(6)
self.uplink = TFWServerUplinkConnector() self.uplink = TFWUplinkConnector()
self.message_sender = MessageSender(self.uplink) self.message_sender = MessageSender(self.uplink)
self.subscribe_predicate('step_3', self.step_3_allowed) self.subscribe_predicate('step_3', self.step_3_allowed)