Commit d3138e0c authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Merge branch 'feature-log-astakosclient' into develop

parents a2ed4352 2d1f5058
......@@ -488,8 +488,12 @@ def set_command_params(parameters):
def run_one_cmd(exe_string, parser, cloud):
global _history
_history = History(parser.arguments['config'].get(
'global', 'history_file'))
try:
token = parser.arguments['config'].get_cloud(cloud, 'token').split()[0]
except Exception:
token = None
_history = History(
parser.arguments['config'].get('global', 'history_file'), token=token)
_history.add(' '.join([exe_string] + argv[1:]))
from kamaki.cli import one_command
one_command.run(cloud, parser, _help)
......
......@@ -67,6 +67,7 @@ def with_temp_token(foo):
class _init_synnefo_astakosclient(_command_init):
@errors.generic.all
@errors.user.load
@errors.user.astakosclient
......
......@@ -37,8 +37,9 @@ import codecs
class History(object):
def __init__(self, filepath):
def __init__(self, filepath, token=None):
self.filepath = filepath
self.token = token
@classmethod
def _match(self, line, match_terms):
......@@ -58,6 +59,7 @@ class History(object):
return result[- limit:]
def add(self, line):
line = line.replace(self.token, '...') if self.token else line
with open(self.filepath, 'a+') as f:
f.write(line + '\n')
......
......@@ -145,7 +145,7 @@ class RequestManager(Logged):
self.scheme, self.netloc = self._connection_info(url, path, params)
def dump_log(self):
plog = '\t[%s]' if self.LOG_PID else ''
plog = ('\t[%s]' % self) if self.LOG_PID else ''
sendlog.info('- - - - - - -')
sendlog.info('%s %s://%s%s%s' % (
self.method, self.scheme, self.netloc, self.path, plog))
......@@ -182,7 +182,7 @@ class RequestManager(Logged):
wait = 0.03 * random()
sleep(wait)
keep_trying -= wait
plog = '\t[%s]' if self.LOG_PID else ''
plog = ('\t[%s]' % self) if self.LOG_PID else ''
logmsg = 'Kamaki Timeout %s %s%s' % (self.method, self.path, plog)
recvlog.debug(logmsg)
raise ClientError('HTTPResponse takes too long - kamaki timeout')
......@@ -244,8 +244,8 @@ class ResponseManager(Logged):
data = '%s%s' % (self._content, plog)
if self._token:
data = data.replace(self._token, '...')
sendlog.info(data)
sendlog.info('- - - - - - -')
recvlog.info(data)
recvlog.info('- - - - - - -')
break
except Exception as err:
if isinstance(err, HTTPException):
......@@ -450,7 +450,7 @@ class Client(Logged):
if data:
headers.setdefault('Content-Length', '%s' % len(data))
plog = '\t[%s]' if self.LOG_PID else ''
plog = ('\t[%s]' % self) if self.LOG_PID else ''
sendlog.debug('\n\nCMT %s@%s%s', method, self.base_url, plog)
req = RequestManager(
method, self.base_url, path,
......
......@@ -32,10 +32,10 @@
# or implied, of GRNET S.A.
from logging import getLogger
from astakosclient import AstakosClient as SynnefoAstakosClient
from astakosclient import AstakosClient as SynnefoAstakosClientOrig
from astakosclient import AstakosClientException as SynnefoAstakosClientError
from kamaki.clients import Client, ClientError
from kamaki.clients import Client, ClientError, RequestManager, recvlog
def _astakos_error(foo):
......@@ -47,6 +47,42 @@ def _astakos_error(foo):
return wrap
class SynnefoAstakosClient(SynnefoAstakosClientOrig):
"""A synnefo astakosclient.AstakosClient wrapper, that logs"""
def _dump_response(self, request, status, message, data):
recvlog.info('\n%d %s' % (status, message))
recvlog.info('data size: %s' % len(data))
token = request.headers.get('X-Auth-Token', '')
data = data.replace(token, '...') if token else data
recvlog.info(data)
recvlog.info('- - - - - - -')
def _call_astakos(self, *args, **kwargs):
r = super(SynnefoAstakosClient, self)._call_astakos(*args, **kwargs)
try:
log_request = getattr(self, 'log_request', None)
if log_request:
req = RequestManager(
method=log_request['method'],
url='%s://%s' % (self.scheme, self.astakos_base_url),
path=log_request['path'],
data=log_request.get('body', None),
headers=log_request.get('headers', dict()))
req.dump_log()
log_response = getattr(self, 'log_response', None)
if log_response:
self._dump_response(
req,
status=log_response['status'],
message=log_response['message'],
data=log_response.get('data', ''))
except Exception:
pass
finally:
return r
class AstakosClient(Client):
"""Synnefo Astakos cached client wraper"""
......
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