2018-05-26 20:53:02 +00:00
|
|
|
from ast import literal_eval
|
|
|
|
|
2018-03-23 14:27:42 +00:00
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
|
2018-04-26 08:12:47 +00:00
|
|
|
from tfw.components import IdeEventHandler, TerminalEventHandler, ProcessManagingEventHandler, BashMonitor
|
2018-05-29 16:00:57 +00:00
|
|
|
from tfw.components import TerminalCommands, LogMonitor
|
2018-04-06 14:23:20 +00:00
|
|
|
from tfw.networking import MessageSender, TFWServerConnector
|
2018-03-25 14:34:31 +00:00
|
|
|
from tfw.config import TFWENV
|
2018-03-23 14:27:42 +00:00
|
|
|
from tfw.config.logs import logging
|
2018-04-12 09:08:58 +00:00
|
|
|
from tao.config import TAOENV
|
2018-03-31 22:45:15 +00:00
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2018-03-23 14:27:42 +00:00
|
|
|
|
|
|
|
|
2018-03-27 14:04:33 +00:00
|
|
|
def cenator(history):
|
2018-03-31 22:45:15 +00:00
|
|
|
LOG.debug('User executed command: "%s"', history[-1])
|
2018-04-19 07:19:32 +00:00
|
|
|
MessageSender().send('JOHN CENA', f'You\'ve executed "{history[-1]}"')
|
2018-03-27 14:04:33 +00:00
|
|
|
|
|
|
|
|
2018-04-12 08:55:33 +00:00
|
|
|
class TestCommands(TerminalCommands):
|
2018-05-26 20:53:02 +00:00
|
|
|
"""
|
|
|
|
Some example commands useful for debugging.
|
|
|
|
Please remove from production code and inherit your own
|
|
|
|
class from TerminalCommands if you need to define custom
|
|
|
|
commands in your challenge.
|
|
|
|
"""
|
2018-04-18 18:45:17 +00:00
|
|
|
# pylint: disable=unused-argument, attribute-defined-outside-init, no-self-use
|
2018-05-26 20:53:02 +00:00
|
|
|
def command_sendmessage(self, *args):
|
|
|
|
"""
|
|
|
|
Insert TFW message template as first argument if executed without args.
|
2018-04-11 15:34:44 +00:00
|
|
|
|
2018-05-26 20:53:02 +00:00
|
|
|
Evaluate first argumen as a dict and send it to the frontend.
|
|
|
|
This is useful for playing around with frontend APIs.
|
|
|
|
"""
|
|
|
|
if not args:
|
|
|
|
message_template = """'{"key": "", "data": {"command": ""}}'"""
|
|
|
|
TFWServerConnector().send_to_eventhandler({
|
|
|
|
'key': 'shell',
|
|
|
|
'data': {
|
|
|
|
'command': 'write',
|
|
|
|
'shellcmd': f'sendmessage {message_template}'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
TFWServerConnector().send(literal_eval(args[0]))
|
2018-03-27 14:04:33 +00:00
|
|
|
|
2018-04-19 14:23:46 +00:00
|
|
|
def command_seppuku_tfw(self, *args):
|
2018-05-26 20:53:02 +00:00
|
|
|
"""
|
|
|
|
Restart tfw_server.py and event_handler_main.py.
|
|
|
|
This can speed up development when combined with mounting
|
|
|
|
volumes from host to container.
|
|
|
|
"""
|
2018-04-19 14:23:46 +00:00
|
|
|
seppuku = ('nohup sh -c "supervisorctl restart tfwserver event_handler_main" &> /dev/null & '
|
|
|
|
'clear && echo "Committed seppuku! :)" && sleep infinity')
|
2018-04-20 12:51:08 +00:00
|
|
|
uplink = TFWServerConnector()
|
2018-05-26 20:53:02 +00:00
|
|
|
uplink.send_to_eventhandler({
|
|
|
|
'key': 'shell',
|
|
|
|
'data': {
|
|
|
|
'command': 'write',
|
|
|
|
'shellcmd': f'{seppuku}\n'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
uplink.send({
|
|
|
|
'key': 'dashboard',
|
|
|
|
'data': {
|
|
|
|
'command': 'reload_frontend'
|
|
|
|
}
|
|
|
|
})
|
2018-04-20 12:42:32 +00:00
|
|
|
|
2018-03-27 14:04:33 +00:00
|
|
|
|
2018-03-23 14:27:42 +00:00
|
|
|
if __name__ == '__main__':
|
2018-05-26 20:53:02 +00:00
|
|
|
ide = IdeEventHandler( # Web IDE backend
|
|
|
|
key='ide', allowed_directories=[TFWENV.IDE_WD, TFWENV.WEBSERVICE_DIR],
|
|
|
|
directory=TFWENV.IDE_WD, exclude=['*.pyc'],
|
|
|
|
additional_watched_directories=[TFWENV.WEBSERVICE_DIR]
|
|
|
|
)
|
2018-05-29 16:00:57 +00:00
|
|
|
terminal = TerminalEventHandler( # Web shell backend
|
2018-05-26 20:53:02 +00:00
|
|
|
key='shell',
|
|
|
|
monitor=BashMonitor(TFWENV.HISTFILE)
|
|
|
|
)
|
2018-05-29 16:00:57 +00:00
|
|
|
processmanager = ProcessManagingEventHandler( # Handles 'deploy' button clicks
|
2018-05-26 20:53:02 +00:00
|
|
|
key='processmanager',
|
2018-05-29 16:00:57 +00:00
|
|
|
dirmonitor=ide.monitor,
|
|
|
|
log_tail=2000
|
2018-05-26 20:53:02 +00:00
|
|
|
)
|
2018-05-29 16:00:57 +00:00
|
|
|
LogMonitor('webservice', log_tail=2000).watch() # Sends live logs of webservice process to frontend
|
2018-05-26 20:53:02 +00:00
|
|
|
|
|
|
|
terminal.historymonitor.subscribe_callback(cenator)
|
|
|
|
|
2018-04-12 09:08:58 +00:00
|
|
|
commands = TestCommands(bashrc=f'/home/{TAOENV.USER}/.bashrc')
|
2018-05-26 20:53:02 +00:00
|
|
|
terminal.historymonitor.subscribe_callback(commands.callback)
|
2018-03-23 14:27:42 +00:00
|
|
|
|
2018-05-26 20:53:02 +00:00
|
|
|
eventhandlers = {ide, terminal, processmanager}
|
2018-03-23 14:27:42 +00:00
|
|
|
try:
|
|
|
|
IOLoop.instance().start()
|
|
|
|
finally:
|
2018-03-31 22:45:15 +00:00
|
|
|
for eh in eventhandlers:
|
|
|
|
eh.cleanup()
|