From 5daf9f5fe82df591ff553e430414cc797664ce27 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Sun, 13 Mar 2016 17:46:08 +0000 Subject: finish PoC of uploading artifacts with paramiko Change-Id: Iddbc8cf3b7c4a1ee3e6a8ed7c7c88399639e7363 --- gear/worker.py | 62 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/gear/worker.py b/gear/worker.py index 8b782be5..40c142b8 100644 --- a/gear/worker.py +++ b/gear/worker.py @@ -6,6 +6,7 @@ worker.registerFunction("build-graph") worker.registerFunction("build-artifact") import time import json +import os from subprocess import Popen, PIPE, STDOUT import distbuild @@ -21,41 +22,36 @@ def ssh_manager(host, port, username, key): ''' returns -> ssh connection ready to be used ''' - connected = False - t = paramiko.Transport((host, port)) -use client?? http://docs.paramiko.org/en/1.16/api/client.html - t.start_client() + client = paramiko.client.SSHClient() + client.load_host_keys(key) + client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + client.connect(host, port=port) try: - ki = paramiko.RSAKey.from_private_key_file(key) - except Exception, e: - print 'Failed loading' % (key, e) - raise e - - agent = paramiko.Agent() - agent_keys = agent.get_keys() + (ki,) - if len(agent_keys) == 0: - print 'No agent keys found in %s!!' % (key) - return - - for key in agent_keys: - print 'Trying ssh-agent key %s' % key.get_fingerprint().encode('hex'), - try: - t.auth_publickey(username, key) - print '... success!' - connected = True - continue - except paramiko.SSHException, e: - print '... failed!', e - - try: - if connected: - yield t - else: - yield False + yield client finally: - t.close() + client.close() + +def upload_files(cache_key, suffixes): + print "DEBUG: start upload_files" + cache_dir = '/src/cache/artifacts' + remote_cache_dir = '/src/cache_server/' + files_to_upload = [os.path.join(cache_dir, cache_key + '.' + suffix) for suffix in suffixes] + with ssh_manager('127.0.0.1', 22, 'root', '/root/gerritbot/gerritbot_rsa') as client: + sftp = client.open_sftp() + for single_file in files_to_upload: + remote_dest = os.path.join(remote_cache_dir, os.path.basename(single_file)) + print "DEBUG: going to upload %s" % single_file + print "DEBUG: upload destination %s" % remote_dest + # TODO: atomic upload!! sftp.rename(remotePath+'/tmp/'+fileName, remotePath+fileName) + try: + sftp.stat(remote_dest) + print "DEBUG: file already exists" + return + except: + print "DEBUG: file not found in cache, uploading" + sftp.put(single_file, remote_dest) while True: print "DEBUG: Waiting for job" @@ -103,9 +99,7 @@ while True: if kind == 'stratum': suffixes.append(filename + '.meta') - - with ssh_manager('localhost', 22, 'root', '/root/gerritbot/gerritbot_rsa') as conn: - print conn + upload_files(artifact.cache_key, suffixes) job.sendWorkComplete(artifact.cache_key) -- cgit v1.2.1