Skip to content

Instantly share code, notes, and snippets.

@greggyNapalm
Created February 9, 2013 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggyNapalm/4745462 to your computer and use it in GitHub Desktop.
Save greggyNapalm/4745462 to your computer and use it in GitHub Desktop.
class LogviewClinet():
'''Log storage client, fetch files over SCP rpotocol.
Attributes:
host: str, logs storage FQDN.
usr: str, username for auth.
key_path: str, RSA key to auth with.
prefix: str, remote log file path prefix.
path: str, full log file path of path postfix.
'''
def __init__(self, host, usr, key_path, dst=None):
self.host = host
self.usr = usr
self.key_path = key_path
self.dst = dst
self.logs = []
self.fab_ctx = {
'host_string': self.host,
'user': self.usr,
'key_filename': self.key_path,
#'always_use_pty': False, # Allow to run remote process as daemons
}
self.ping()
def ping(self):
'''Check Fabric config and remote host reachability.
'''
with settings(**self.fab_ctx):
run('uname')
def fetch_log(r_path, l_path=None, l_dir=None):
'''Fetch file from remote side.
Args:
path: str, absolute path to file on remote side.
'''
l_name = lambda r_path: r_path.split('/')[-1]
if kwargs.get('l_path'):
get(r_path, kwargs.get('l_path'))
elif kwargs.get('l_dir'):
get(r_path, '{l_dir}/{f_name}'.formatdir(l_dir=kwargs.get('l_dir'),
f_name=l_name(r_path)))
else:
raise ValueError('At least one of *l_path* or *l_dir* kwargs should present')
def add_group(self, hosts, p_prefix, p_postfix):
'''Add multiple log file to fetch earlier.
Args:
hosts: list of str, hosts names to be used in file pathes.
p_prefix: str, common for all log files path - hosts mount point.
p_postfix: str, particular service log file path.
'''
abs_path = lambda h: '{pref}/{fqdn}/{potsf}'.format(pref=p_prefix,
fqdn=h,
potsf=p_postfix)
self.logs.extend([abs_path(h) for h in hosts])
def fetch_all(self, l_path=None, l_dir=None):
'''
'''
print kwargs
for r_path in self.logs:
#self.fetch_log(r_path, l_path=l_path, l_dir=l_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment