Commit 3cc6c0f2 authored by Stavros Sachtouris's avatar Stavros Sachtouris Committed by Giorgos Korfiatis
Browse files

Implement all CLI commands

Removed: launch_daemon, stop_daemon
New: start, shutdown, pause

start: Start syncing. If the daemon is not running, launch it
shutdown: Cleanly shutdown everything in Agkyra (backend, GUI,
    daemons)
pause: Set the running Agkyra session to "paused" mode
parent f0f7db82
......@@ -199,40 +199,85 @@ class AgkyraCLI(cmd.Cmd):
def do_status(self, line):
"""Get Agkyra client status. Status may be one of the following:
Up and syncing There is a process syncing right now
Up and paused Notifiers are active but syncing is paused
Not running No active processes
Syncing There is a process syncing right now
Paused Notifiers are active but syncing is paused
Not running No active processes
"""
client = self.client
if client:
# Ask the server for the status
status = client.get_status()
msg = 'paused' if status['paused'] else 'running'
sys.stdout.write('Up and %s\n' % msg)
msg = 'Paused' if status['paused'] else 'Syncing'
sys.stdout.write('%s\n' % msg)
else:
sys.stdout.write('Not running\n')
sys.stdout.flush()
def do_start_daemon(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')
# def do_start_daemon(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_start(self, line):
"""Start the session. If no daemons are running, start one first"""
client = self.client
if not client:
sys.stderr.write('No Agkyra daemons running, starting one ...')
protocol.launch_server()
sys.stderr.write('Waiting for the deamon to load\n')
sys.stderr.write(' ... ')
self.helper.wait_session_to_load()
self.do_status('')
sys.stderr.write('OK\n')
else:
status = client.get_status()
if status['paused']:
client.start()
sys.stderr.write('Starting syncer ... ')
try:
client.wait_until_syncing()
except AssertionError:
sys.stderr.write('\n')
raise
sys.stderr.write('OK\n')
else:
sys.stderr.write('Already syncing\n')
sys.stderr.flush()
def do_pause(self, line):
"""Pause a session (stop it from syncing, but keep it running)"""
client = self.client
if client:
status = client.get_status()
if status['paused']:
sys.stderr.write('Already paused\n')
else:
client.pause()
sys.stderr.write('Pausing syncer ... ')
try:
client.wait_until_paused()
except AssertionError:
sys.stderr.write('\n')
raise
sys.stderr.write('OK\n')
else:
sys.stderr.write('Not running (use "start" to launch)\n')
sys.stderr.flush()
def do_stop_daemon(self, line):
"""Stop the Agkyra daemon, if it is running"""
def do_shutdown(self, line):
"""Shutdown Agkyra, if it is running"""
client = self.client
if client:
client.shutdown()
sys.stderr.write('Shutting down Agkyra ... ')
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.write('Not running\n')
sys.stderr.flush()
......@@ -43,16 +43,37 @@ class UIClient(WebSocketClient):
def wait_until_ready(self, timeout=20):
"""Wait until client is connected"""
while not self.ready:
while timeout and not self.ready:
time.sleep(1)
timeout -= 1
assert self.ready, 'UI client timed out while waiting to be ready'
return self.ready
def wait_until_syncing(self, timeout=20):
"""Wait until session reaches syncing status"""
status = self.get_status()
while timeout and status['paused']:
time.sleep(1)
status = self.get_status()
timeout -= 1
assert not status['paused'], 'Timed out, still in paused status'
def wait_until_paused(self, timeout=20):
"""Wait until session reaches paused status"""
status = self.get_status()
while timeout and not status['paused']:
time.sleep(1)
status = self.get_status()
timeout -= 1
assert status['paused'], 'Timed out, still in syncing status'
def received_message(self, m):
"""handle server responces according to the protocol"""
msg = json.loads('%s' % m)
{
'post ui_id': self.recv_authenticate,
'post start': self.recv_start,
'post pause': self.recv_pause,
'get status': self.recv_get_status,
}[msg['action']](msg)
......@@ -67,6 +88,14 @@ class UIClient(WebSocketClient):
assert 'ACCEPTED' in msg, json.dumps(msg)
self.ready = True
def recv_start(self, msg):
"""Receive: start response"""
assert 'OK' in msg, json.dumps(msg)
def recv_pause(self, msg):
"""Receive: start response"""
assert 'OK' in msg, json.dumps(msg)
def recv_get_status(self, msg):
"""Receive: GET STATUS"""
assert 'can_sync' in msg, json.dumps(msg)
......@@ -81,7 +110,21 @@ class UIClient(WebSocketClient):
time.sleep(1)
return self.buf.pop('get status')
def _post(self, path):
"""send json with action=POST and path=path"""
self.send(json.dumps(dict(method='post', path=path)))
def shutdown(self):
"""Request: POST SHUTDOWN"""
self.wait_until_ready()
self.send(json.dumps(dict(method='post', path='shutdown')))
self._post('shutdown')
def start(self):
"""Request: POST START"""
self.wait_until_ready()
self._post('start')
def pause(self):
"""Request: POST PAUSE"""
self.wait_until_ready()
self._post('pause')
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