1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
import gear
worker = gear.Worker('reverser')
worker.addServer('localhost')
worker.registerFunction("reverse")
worker.registerFunction("build-graph")
worker.registerFunction("build-artifact")
import time
import json
from subprocess import Popen, PIPE, STDOUT
import distbuild
from contextlib import contextmanager
import paramiko
import logging
logging.basicConfig()
@contextmanager
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()
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
finally:
t.close()
while True:
print "DEBUG: Waiting for job"
job = worker.getJob()
print "DEBUG: Received job '%s'" % job.name
if job.name == "reverse":
print "DEBUG: Starting job reverse with '%s'" % job.arguments
for x in range(0, 100):
job.sendWorkData("This is: %s" % x)
job.sendWorkComplete("answer")
elif job.name == "build-graph":
bg_request=json.loads(job.arguments)
print ("DEBUG: Starting build-graph calculation for Repo: '%s' "
"Ref: '%s' System: '%s'") % (bg_request['repo'],
bg_request['ref'],
bg_request['system'])
# TODO: There should be another way of doing this.
cmd = ['morph', 'calculate-build-graph', '--quiet', bg_request['repo'], bg_request['ref'], bg_request['system']]
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read()
# TODO: catch errors calculating build-graph here instead of sending the error as build-graph :)
print "====="
print output
print "====="
print "DEBUG: finished computing build graph"
job.sendWorkComplete(output)
elif job.name == "build-artifact":
artifact = distbuild.decode_artifact_reference(job.arguments)
print "building %s" % artifact.name
cmd = ['morph', 'worker-build', '--build-log-on-stdout', artifact.name]
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.communicate(input=job.arguments)[0]
job.sendWorkData(output)
kind = artifact.kind
if kind == 'chunk':
artifact_names = artifact.source_artifact_names
suffixes = ['%s.%s' % (kind, name) for name in artifact_names]
suffixes.append('build-log')
else:
filename = '%s.%s' % (kind, job.artifact.name)
suffixes = [filename]
if kind == 'stratum':
suffixes.append(filename + '.meta')
with ssh_manager('localhost', 22, 'root', '/root/gerritbot/gerritbot_rsa') as conn:
print conn
job.sendWorkComplete(artifact.cache_key)
|