2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
|
|
|
|
2019-05-27 12:09:13 +00:00
|
|
|
from tfw.event_handlers import FrontendEventHandlerBase
|
2018-07-26 11:59:06 +00:00
|
|
|
from tfw.components.terminado_mini_server import TerminadoMiniServer
|
2018-03-25 14:36:31 +00:00
|
|
|
from tfw.config import TFWENV
|
2018-04-04 15:43:18 +00:00
|
|
|
from tao.config import TAOENV
|
2018-03-25 14:06:59 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-01-17 15:33:34 +00:00
|
|
|
|
|
|
|
|
2019-05-26 16:26:33 +00:00
|
|
|
class TerminalEventHandler(FrontendEventHandlerBase):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Event handler responsible for managing terminal sessions for frontend xterm
|
|
|
|
sessions to connect to. You need to instanciate this in order for frontend
|
|
|
|
terminals to work.
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
This EventHandler accepts messages that have a data['command'] key specifying
|
2018-04-18 17:44:26 +00:00
|
|
|
a command to be executed.
|
2018-06-01 14:20:20 +00:00
|
|
|
The API of each command is documented in their respective handler.
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-03-29 09:34:08 +00:00
|
|
|
def __init__(self, key, monitor):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
:param key: key this EventHandler listens to
|
|
|
|
:param monitor: tfw.components.HistoryMonitor instance to read command history from
|
|
|
|
"""
|
2018-02-21 14:28:16 +00:00
|
|
|
super().__init__(key)
|
2018-03-29 09:34:08 +00:00
|
|
|
self._historymonitor = monitor
|
2018-04-05 12:43:07 +00:00
|
|
|
bash_as_user_cmd = ['sudo', '-u', TAOENV.USER, 'bash']
|
2018-06-01 15:19:58 +00:00
|
|
|
|
|
|
|
self.terminado_server = TerminadoMiniServer(
|
2018-06-04 20:16:44 +00:00
|
|
|
'/terminal',
|
|
|
|
TFWENV.TERMINADO_PORT,
|
|
|
|
TFWENV.TERMINADO_WD,
|
|
|
|
bash_as_user_cmd
|
2018-06-01 15:19:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.commands = {
|
|
|
|
'write': self.write,
|
|
|
|
'read': self.read
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:34:08 +00:00
|
|
|
if self._historymonitor:
|
2019-06-11 15:25:35 +00:00
|
|
|
self._historymonitor.start()
|
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-04-13 18:45:34 +00:00
|
|
|
def handle_event(self, message):
|
2018-03-07 13:45:43 +00:00
|
|
|
try:
|
2018-04-14 21:15:44 +00:00
|
|
|
data = message['data']
|
|
|
|
message['data'] = self.commands[data['command']](data)
|
2018-03-08 15:11:43 +00:00
|
|
|
return message
|
2018-03-07 13:45:43 +00:00
|
|
|
except KeyError:
|
2018-03-25 14:25:01 +00:00
|
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
2018-03-03 13:14:44 +00:00
|
|
|
|
|
|
|
def write(self, data):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Writes a string to the terminal session (on the pty level).
|
|
|
|
Useful for pre-typing and executing commands for the user.
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
:param data: TFW message data containing 'value'
|
|
|
|
(command to be written to the pty)
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-05-31 12:08:29 +00:00
|
|
|
self.terminado_server.pty.write(data['value'])
|
2018-07-20 18:34:18 +00:00
|
|
|
return data
|
2018-03-03 21:46:14 +00:00
|
|
|
|
|
|
|
def read(self, data):
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
|
|
|
Reads the history of commands executed.
|
|
|
|
|
2018-06-01 14:20:20 +00:00
|
|
|
:param data: TFW message data containing 'count'
|
|
|
|
(the number of history elements to return)
|
|
|
|
:return dict: message with list of commands in data['history']
|
2018-04-18 17:44:26 +00:00
|
|
|
"""
|
2018-03-03 22:02:49 +00:00
|
|
|
data['count'] = int(data.get('count', 1))
|
2018-03-29 09:34:08 +00:00
|
|
|
if self.historymonitor:
|
|
|
|
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):
|
2019-05-31 11:10:33 +00:00
|
|
|
self.terminado_server.stop()
|
2018-03-29 09:34:08 +00:00
|
|
|
if self.historymonitor:
|
|
|
|
self.historymonitor.stop()
|