2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
|
|
|
|
2018-01-24 14:10:59 +00:00
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
from tornado.web import Application
|
2018-03-02 17:01:00 +00:00
|
|
|
from terminado import TermSocket, SingleTermManager
|
2018-01-24 14:10:59 +00:00
|
|
|
|
2018-03-25 14:36:31 +00:00
|
|
|
from tfw.config import TFWENV
|
2018-03-25 14:06:59 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-01-24 14:10:59 +00:00
|
|
|
|
|
|
|
|
2018-01-25 15:48:48 +00:00
|
|
|
class TerminadoMiniServer:
|
|
|
|
def __init__(self, url, port, workdir, shellcmd):
|
|
|
|
self.port = port
|
2018-06-04 20:16:44 +00:00
|
|
|
self._term_manager = SingleTermManager(
|
|
|
|
shell_command=shellcmd,
|
|
|
|
term_settings={'cwd': workdir}
|
2018-01-25 15:48:48 +00:00
|
|
|
)
|
2018-06-04 20:16:44 +00:00
|
|
|
self.application = Application([(
|
|
|
|
url,
|
|
|
|
TerminadoMiniServer.ResetterTermSocket,
|
|
|
|
{'term_manager': self._term_manager}
|
|
|
|
)])
|
2018-01-25 15:48:48 +00:00
|
|
|
|
2018-03-06 13:28:36 +00:00
|
|
|
@property
|
|
|
|
def term_manager(self):
|
|
|
|
return self._term_manager
|
|
|
|
|
2018-03-02 16:05:01 +00:00
|
|
|
@property
|
|
|
|
def pty(self):
|
2019-05-31 09:13:33 +00:00
|
|
|
if self.term_manager.terminal is None:
|
|
|
|
self.term_manager.get_terminal()
|
2018-03-02 16:05:01 +00:00
|
|
|
return self.term_manager.terminal.ptyproc
|
|
|
|
|
2018-03-30 16:11:38 +00:00
|
|
|
class ResetterTermSocket(TermSocket): # pylint: disable=abstract-method
|
2018-01-25 15:48:48 +00:00
|
|
|
def check_origin(self, origin):
|
|
|
|
return True
|
|
|
|
|
2018-03-06 15:15:04 +00:00
|
|
|
def on_close(self):
|
|
|
|
self.term_manager.terminal = None
|
|
|
|
self.term_manager.get_terminal()
|
|
|
|
|
2018-01-25 15:48:48 +00:00
|
|
|
def listen(self):
|
|
|
|
self.application.listen(self.port)
|
|
|
|
|
2019-05-31 11:10:33 +00:00
|
|
|
def stop(self):
|
|
|
|
self.term_manager.shutdown()
|
|
|
|
|
2018-01-24 14:10:59 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-03-25 14:36:31 +00:00
|
|
|
LOG.info('Terminado Mini Server listening on %s', TFWENV.TERMINADO_PORT)
|
2018-06-04 20:16:44 +00:00
|
|
|
TerminadoMiniServer(
|
|
|
|
'/terminal',
|
|
|
|
TFWENV.TERMINADO_PORT,
|
|
|
|
TFWENV.TERMINADO_WD,
|
|
|
|
['bash']
|
|
|
|
).listen()
|
2018-03-03 13:47:55 +00:00
|
|
|
IOLoop.instance().start()
|