2018-03-02 15:23:19 +00:00
|
|
|
from tfw.components.terminado_mini_server import TerminadoMiniServer
|
2018-03-03 21:46:14 +00:00
|
|
|
from tfw.components.history_monitor import HistoryMonitor
|
2018-02-23 13:07:41 +00:00
|
|
|
from tfw.event_handler_base import TriggerlessEventHandler
|
2018-02-09 08:58:47 +00:00
|
|
|
from tfw.config import tfwenv
|
2018-01-31 14:50:52 +00:00
|
|
|
from tfw.config.logs import logging
|
2018-01-29 17:36:29 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2018-01-17 15:33:34 +00:00
|
|
|
|
|
|
|
|
2018-03-02 15:23:19 +00:00
|
|
|
class TerminadoEventHandler(TriggerlessEventHandler):
|
|
|
|
def __init__(self, key):
|
2018-02-21 14:28:16 +00:00
|
|
|
super().__init__(key)
|
2018-02-09 08:58:47 +00:00
|
|
|
self.working_directory = tfwenv.TERMINADO_DIR
|
2018-03-05 15:24:36 +00:00
|
|
|
self._historymonitor = HistoryMonitor(tfwenv.HISTFILE)
|
2018-03-02 15:23:19 +00:00
|
|
|
self.terminado_server = TerminadoMiniServer('/terminal', tfwenv.TERMINADO_PORT, tfwenv.TERMINADO_WD, ['bash'])
|
2018-03-03 21:46:14 +00:00
|
|
|
self.commands = {'write': self.write,
|
2018-03-06 15:15:04 +00:00
|
|
|
'read': self.read}
|
2018-03-05 15:24:36 +00:00
|
|
|
self._historymonitor.watch()
|
2018-03-02 15:23:19 +00:00
|
|
|
self.terminado_server.listen()
|
2018-01-17 15:33:34 +00:00
|
|
|
|
2018-03-05 15:24:36 +00:00
|
|
|
@property
|
|
|
|
def historymonitor(self):
|
|
|
|
return self._historymonitor
|
|
|
|
|
2018-02-21 14:28:16 +00:00
|
|
|
def handle_event(self, key, data_json):
|
2018-03-03 13:14:44 +00:00
|
|
|
log.debug('TerminadoEventHandler received event: {}'.format(data_json))
|
|
|
|
data = data_json['data']
|
2018-03-03 21:46:14 +00:00
|
|
|
data_json['data'] = self.commands[data['command']](data)
|
|
|
|
return data_json
|
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):
|
2018-03-03 22:02:49 +00:00
|
|
|
data['count'] = int(data.get('count', 1))
|
2018-03-05 15:24:36 +00:00
|
|
|
data['history'] = self.historymonitor.history[-data['count']:]
|
2018-03-03 21:46:14 +00:00
|
|
|
return data
|
2018-03-06 12:16:52 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
self.historymonitor.stop()
|