2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
from .history_monitor import BashMonitor
|
|
|
|
from .terminado_mini_server import TerminadoMiniServer
|
2018-03-25 14:06:59 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-01-17 15:33:34 +00:00
|
|
|
|
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
class TerminalHandler:
|
2019-08-08 13:05:37 +00:00
|
|
|
keys = ['terminal']
|
2018-04-18 17:44:26 +00:00
|
|
|
|
2019-08-07 07:47:28 +00:00
|
|
|
def __init__(self, *, port, user, working_directory, histfile):
|
|
|
|
self.connector, self._historymonitor = None, None
|
2019-07-23 13:32:50 +00:00
|
|
|
self._histfile = histfile
|
|
|
|
bash_as_user_cmd = ['sudo', '-u', user, 'bash']
|
2018-06-01 15:19:58 +00:00
|
|
|
|
|
|
|
self.terminado_server = TerminadoMiniServer(
|
2018-06-04 20:16:44 +00:00
|
|
|
'/terminal',
|
2019-07-23 13:32:50 +00:00
|
|
|
port,
|
2019-08-07 07:47:28 +00:00
|
|
|
working_directory,
|
2018-06-04 20:16:44 +00:00
|
|
|
bash_as_user_cmd
|
2018-06-01 15:19:58 +00:00
|
|
|
)
|
2019-08-07 07:47:28 +00:00
|
|
|
self.terminado_server.listen()
|
2018-06-01 15:19:58 +00:00
|
|
|
|
|
|
|
self.commands = {
|
2019-08-07 07:47:28 +00:00
|
|
|
'terminal.write': self.handle_write
|
2018-06-01 15:19:58 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 21:25:16 +00:00
|
|
|
def start(self):
|
2019-07-30 13:17:29 +00:00
|
|
|
self._historymonitor = BashMonitor(self.connector, self._histfile)
|
2019-07-12 21:25:16 +00:00
|
|
|
self._historymonitor.start()
|
|
|
|
|
2018-03-05 15:24:36 +00:00
|
|
|
@property
|
|
|
|
def historymonitor(self):
|
|
|
|
return self._historymonitor
|
|
|
|
|
2019-07-12 21:25:16 +00:00
|
|
|
def handle_event(self, message, _):
|
2018-03-07 13:45:43 +00:00
|
|
|
try:
|
2019-08-07 07:47:28 +00:00
|
|
|
self.commands[message['key']](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
|
|
|
|
2019-08-07 07:47:28 +00:00
|
|
|
def handle_write(self, message):
|
|
|
|
self.terminado_server.pty.write(message['command'])
|
2018-03-06 12:16:52 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
2019-05-31 11:10:33 +00:00
|
|
|
self.terminado_server.stop()
|
2019-06-27 12:12:01 +00:00
|
|
|
self.historymonitor.stop()
|