Commit 34303d51 authored by Stavros Sachtouris's avatar Stavros Sachtouris Committed by Giorgos Korfiatis
Browse files

Implement "launch" and "stop" CLI commands

These commands can be used to launch and stop the agkyra daemon
parent f387bc28
......@@ -16,6 +16,7 @@
import cmd
import sys
import logging
import time
from agkyra import config, protocol, protocol_client
......@@ -212,7 +213,26 @@ class AgkyraCLI(cmd.Cmd):
sys.stdout.write('Not running\n')
sys.stdout.flush()
# AgkyraCLI().run_onecmd(sys.argv)
# or run a shell with
# AgkyraCLI().cmdloop()
def do_launch(self, line):
"""Start the Agkyra daemon if it is not running"""
if self.client:
sys.stderr.write('An Agkyra daemon is already running\n')
else:
sys.stderr.write('Launcing a new Agkyra daemon\n')
protocol.launch_server()
sys.stderr.write('Waiting for the deamon to load\n')
self.helper.wait_session_to_load()
self.do_status('')
sys.stderr.flush()
def do_stop(self, line):
"""Stop the Agkyra daemon, if it is running"""
client = self.client
if client:
client.shutdown()
success = self.helper.wait_session_to_stop()
sys.stderr.write('Stopped' if success else 'Still up (timed out)')
sys.stderr.write('\n')
else:
sys.stderr.write('No daemons running\n')
sys.stderr.flush()
......@@ -115,6 +115,16 @@ class SessionHelper(object):
time.sleep(step)
return None
def wait_session_to_stop(self, timeout=20, step=2):
"""Wait while the session is shutting down
:returns: True if stopped, False if timed out and still running
"""
time_passed = 0
while time_passed < timeout and self.load_active_session():
time.sleep(step)
time_passed += step
return not bool(self.load_active_session())
def start(self):
"""Start the helper server in a thread"""
if getattr(self, 'server', None):
......@@ -521,7 +531,8 @@ class WebSocketProtocol(WebSocket):
'get': self._get
}[method](r)
except KeyError as ke:
self.send_json({'BAD REQUEST': 400})
action = method + ' ' + r.get('path', '')
self.send_json({'BAD REQUEST': 400, 'action': action})
LOG.error('KEY ERROR: %s' % ke)
except setup.ClientError as ce:
action = '%s %s' % (
......
......@@ -39,7 +39,7 @@ class UIClient(WebSocketClient):
def closed(self, code, reason):
"""After the client is closed"""
LOG.debug('Client exits with %s, %s' % code, reason)
LOG.debug('Client exits with %s, %s' % (code, reason))
def wait_until_ready(self, timeout=20):
"""Wait until client is connected"""
......@@ -53,7 +53,7 @@ class UIClient(WebSocketClient):
msg = json.loads('%s' % m)
{
'post ui_id': self.recv_authenticate,
'get status': self.recv_get_status
'get status': self.recv_get_status,
}[msg['action']](msg)
# Request handlers
......@@ -80,3 +80,8 @@ class UIClient(WebSocketClient):
while 'get status' not in self.buf:
time.sleep(1)
return self.buf.pop('get status')
def shutdown(self):
"""Request: POST SHUTDOWN"""
self.wait_until_ready()
self.send(json.dumps(dict(method='post', path='shutdown')))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment