From 2f60ddd46a6a3a679bd1253c4dd522fb91d7559a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 20 Jul 2018 13:37:22 +0200 Subject: [PATCH 1/3] Add FSMAwareEH example to event_handler_main --- solvable/src/event_handler_main.py | 34 ++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/solvable/src/event_handler_main.py b/solvable/src/event_handler_main.py index fb28b21..9ff99e5 100644 --- a/solvable/src/event_handler_main.py +++ b/solvable/src/event_handler_main.py @@ -3,7 +3,7 @@ from functools import partial from tornado.ioloop import IOLoop -from tfw import YamlFSM +from tfw import YamlFSM, FSMAwareEventHandler from tfw.components import IdeEventHandler, TerminalEventHandler from tfw.components import ProcessManagingEventHandler, BashMonitor from tfw.components import TerminalCommands, LogMonitoringEventHandler @@ -13,8 +13,6 @@ from tfw.config import TFWENV from tfw.config.logs import logging from tao.config import TAOENV -from test_fsm import TestFSM - LOG = logging.getLogger(__name__) @@ -80,7 +78,27 @@ class TestCommands(TerminalCommands): }) +class MessageFSMStepsEventHandler(FSMAwareEventHandler): + """ + This example EventHandler is capable of detecting FSM state. + !! Please remove from production code !! + """ + def handle_event(self, message): + pass + + def handle_fsm_step(self, from_state, to_state, trigger): + """ + When the FSM steps this method is invoked. + """ + MessageSender().send( + 'FSM info', + f'FSM has stepped from state "{from_state}" ' + f'to state "{to_state}" in response to trigger "{trigger}"' + ) + + if __name__ == '__main__': + # TFW component EventHandlers (builtins, required for their respective functionalities) fsm = FSMManagingEventHandler( # TFW FSM key='fsm', fsm_type=partial(YamlFSM, 'test_fsm.yml') @@ -105,14 +123,22 @@ if __name__ == '__main__': process_name='webservice', log_tail=2000 ) - eventhandlers = {fsm, ide, terminal, processmanager, logmonitor} + # Your custom event handlers + message_fsm_steps = MessageFSMStepsEventHandler( + key='test' + ) + + # Terminal command handlers commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc') terminal.historymonitor.subscribe_callback(commands.callback) + + # Example terminal command callback terminal.historymonitor.subscribe_callback(cenator) try: IOLoop.instance().start() finally: + eventhandlers = {fsm, ide, terminal, processmanager, logmonitor, message_fsm_steps} for eh in eventhandlers: eh.cleanup() From 69721039eb89090ad6b48fb404a487d454e817f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 20 Jul 2018 14:41:27 +0200 Subject: [PATCH 2/3] Prepare controller/Dockerfile for an actual controller --- config.yml | 2 +- controller/Dockerfile | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/config.yml b/config.yml index 49b46a9..4339252 100644 --- a/config.yml +++ b/config.yml @@ -6,7 +6,7 @@ skills: [] owners: ['kristof.toth@avatao.com'] crp_config: controller: - ports: ["4444/controller"] + ports: ["5555/controller"] solvable: capabilities: ["SETUID", "SETGID", "CHOWN", "AUDIT_WRITE", "DAC_OVERRIDE", "KILL"] ports: ["8888/http"] diff --git a/controller/Dockerfile b/controller/Dockerfile index 2aa9563..ef96d41 100644 --- a/controller/Dockerfile +++ b/controller/Dockerfile @@ -1 +1,13 @@ -FROM avatao/controller:ubuntu-16.04 +FROM avatao/controller:debian-buster + +USER root +ENV PYTHONPATH="/usr/local/lib" \ + TFW_PUBLISHER_PORT=7654 \ + TFW_RECEIVER_PORT=8765 \ + TFW_AUTH_KEY="/tmp/tfw-auth.key" \ + CONTROLLER_PORT=5555 + +RUN pip3 install watchdog transitions +COPY ./controller/ / + +CMD ["python3", "/opt/server.py"] From 67b630401b3b3d98281c917308282c0071f1013e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 20 Jul 2018 14:41:53 +0200 Subject: [PATCH 3/3] =?UTF-8?q?Implement=20TFW=20controller=20after=20mont?= =?UTF-8?q?hs=20of=20pain=20and=20suffering=20=C2=AF\=5F(=E3=83=84)=5F/?= =?UTF-8?q?=C2=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/opt/server.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 controller/opt/server.py diff --git a/controller/opt/server.py b/controller/opt/server.py new file mode 100644 index 0000000..82a3811 --- /dev/null +++ b/controller/opt/server.py @@ -0,0 +1,39 @@ +import os +import json + +from tornado.ioloop import IOLoop +from tornado.web import RequestHandler, Application + +from tfw import FSMAwareEventHandler + + +class ControllerPostHandler(RequestHandler): + # pylint: disable=abstract-method + def initialize(self, **kwargs): # pylint: disable=arguments-differ + self.controller = kwargs['controller'] + + def post(self, *args, **kwargs): + self.set_header('Content-Type', 'application/json') + self.write(json.dumps({ + 'solved': self.controller.in_accepted_state + })) + + +class ControllerEventHandler(FSMAwareEventHandler): + def handle_event(self, message): + pass + + +if __name__ == '__main__': + controller = ControllerEventHandler('controller') + application = Application([( + f'/{os.environ["SECRET"]}', + ControllerPostHandler, + {'controller': controller} + )]) + application.listen(os.environ['CONTROLLER_PORT']) + + try: + IOLoop.instance().start() + finally: + controller.cleanup()