baseimage-tutorial-framework/lib/tfw/components/terminado_event_handler.py

50 lines
1.7 KiB
Python
Raw Normal View History

# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
from tfw import TriggerlessEventHandler
from tfw.config import TFWENV
2018-01-31 14:50:52 +00:00
from tfw.config.logs import logging
from tao.config import TAOENV
from .terminado_mini_server import TerminadoMiniServer
LOG = logging.getLogger(__name__)
class TerminadoEventHandler(TriggerlessEventHandler):
def __init__(self, key, monitor):
super().__init__(key)
self.working_directory = TFWENV.TERMINADO_DIR
self._historymonitor = monitor
bash_as_user_cmd = ['sudo', '-u', TAOENV.USER, 'bash']
self.terminado_server = TerminadoMiniServer('/terminal', TFWENV.TERMINADO_PORT, TFWENV.TERMINADO_WD, bash_as_user_cmd)
2018-03-03 21:46:14 +00:00
self.commands = {'write': self.write,
'read': self.read}
if self._historymonitor:
self._historymonitor.watch()
self.terminado_server.listen()
@property
def historymonitor(self):
return self._historymonitor
def handle_event(self, message):
LOG.debug('TerminadoEventHandler received event: %s', message)
try:
message['data'] = self.commands[message['data']['command']](message['data'])
return message
except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
2018-03-03 13:14:44 +00:00
def write(self, data):
self.terminado_server.pty.write(data['shellcmd'])
2018-03-03 21:46:14 +00:00
def read(self, data):
data['count'] = int(data.get('count', 1))
if self.historymonitor:
data['history'] = self.historymonitor.history[-data['count']:]
2018-03-03 21:46:14 +00:00
return data
def cleanup(self):
if self.historymonitor:
self.historymonitor.stop()