mirror of
				https://github.com/avatao-content/test-tutorial-framework
				synced 2025-11-04 03:12:56 +00:00 
			
		
		
		
	Merge branch 'state-rewind'
This commit is contained in:
		@@ -6,7 +6,7 @@ skills: []
 | 
				
			|||||||
owners: ['kristof.toth@avatao.com']
 | 
					owners: ['kristof.toth@avatao.com']
 | 
				
			||||||
crp_config:
 | 
					crp_config:
 | 
				
			||||||
    controller:
 | 
					    controller:
 | 
				
			||||||
        ports: ["4444/controller"]
 | 
					        ports: ["5555/controller"]
 | 
				
			||||||
    solvable:
 | 
					    solvable:
 | 
				
			||||||
        capabilities: ["SETUID", "SETGID", "CHOWN", "AUDIT_WRITE", "DAC_OVERRIDE", "KILL"]
 | 
					        capabilities: ["SETUID", "SETGID", "CHOWN", "AUDIT_WRITE", "DAC_OVERRIDE", "KILL"]
 | 
				
			||||||
        ports: ["8888/http"]
 | 
					        ports: ["8888/http"]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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"]
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										39
									
								
								controller/opt/server.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								controller/opt/server.py
									
									
									
									
									
										Normal file
									
								
							@@ -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()
 | 
				
			||||||
@@ -3,7 +3,7 @@ from functools import partial
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
from tornado.ioloop import IOLoop
 | 
					from tornado.ioloop import IOLoop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from tfw import YamlFSM
 | 
					from tfw import YamlFSM, FSMAwareEventHandler
 | 
				
			||||||
from tfw.components import IdeEventHandler, TerminalEventHandler
 | 
					from tfw.components import IdeEventHandler, TerminalEventHandler
 | 
				
			||||||
from tfw.components import ProcessManagingEventHandler, BashMonitor
 | 
					from tfw.components import ProcessManagingEventHandler, BashMonitor
 | 
				
			||||||
from tfw.components import TerminalCommands, LogMonitoringEventHandler
 | 
					from tfw.components import TerminalCommands, LogMonitoringEventHandler
 | 
				
			||||||
@@ -13,8 +13,6 @@ from tfw.config import TFWENV
 | 
				
			|||||||
from tfw.config.logs import logging
 | 
					from tfw.config.logs import logging
 | 
				
			||||||
from tao.config import TAOENV
 | 
					from tao.config import TAOENV
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from test_fsm import TestFSM
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
LOG = logging.getLogger(__name__)
 | 
					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__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
					    # TFW component EventHandlers (builtins, required for their respective functionalities)
 | 
				
			||||||
    fsm = FSMManagingEventHandler( # TFW FSM
 | 
					    fsm = FSMManagingEventHandler( # TFW FSM
 | 
				
			||||||
        key='fsm',
 | 
					        key='fsm',
 | 
				
			||||||
        fsm_type=partial(YamlFSM, 'test_fsm.yml')
 | 
					        fsm_type=partial(YamlFSM, 'test_fsm.yml')
 | 
				
			||||||
@@ -105,14 +123,22 @@ if __name__ == '__main__':
 | 
				
			|||||||
        process_name='webservice',
 | 
					        process_name='webservice',
 | 
				
			||||||
        log_tail=2000
 | 
					        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')
 | 
					    commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc')
 | 
				
			||||||
    terminal.historymonitor.subscribe_callback(commands.callback)
 | 
					    terminal.historymonitor.subscribe_callback(commands.callback)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Example terminal command callback
 | 
				
			||||||
    terminal.historymonitor.subscribe_callback(cenator)
 | 
					    terminal.historymonitor.subscribe_callback(cenator)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        IOLoop.instance().start()
 | 
					        IOLoop.instance().start()
 | 
				
			||||||
    finally:
 | 
					    finally:
 | 
				
			||||||
 | 
					        eventhandlers = {fsm, ide, terminal, processmanager, logmonitor, message_fsm_steps}
 | 
				
			||||||
        for eh in eventhandlers:
 | 
					        for eh in eventhandlers:
 | 
				
			||||||
            eh.cleanup()
 | 
					            eh.cleanup()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user