-
Notifications
You must be signed in to change notification settings - Fork 10
/
srreporter.py
129 lines (103 loc) · 4.63 KB
/
srreporter.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
import base64
import dzclient
import json
import logging
import os
import time
import stoneridge
EMAIL_MESSAGE = '''Hello, %s!
Your stone ridge test has completed its run. Your results are attached.
For your reference, here's the details about this particular run:
ID: %s
Operating System: %s
Network Configuration: %s
Enjoy!
-The Stone Ridge System
'''
class StoneRidgeReporter(stoneridge.QueueListener):
def setup(self):
self.host = stoneridge.get_config('report', 'host')
self.project = stoneridge.get_config('report', 'project')
self.key = stoneridge.get_config('report', 'key')
self.secret = stoneridge.get_config('report', 'secret')
self.archives = stoneridge.get_config('stoneridge', 'archives')
self.unittest = stoneridge.get_config_bool('stoneridge', 'unittest')
logging.debug('report host: %s' % (self.host,))
logging.debug('project: %s' % (self.project,))
logging.debug('oauth key: %s' % (self.key,))
logging.debug('oauth secret: %s' % (self.secret,))
logging.debug('archives: %s' % (self.archives,))
logging.debug('unittest: %s' % (self.unittest,))
def save_data(self, srid, netconfig, operating_system, results,
metadata_b64, ldap):
dirname = '%s_%s_%s' % (srid, netconfig, operating_system)
archivedir = os.path.join(self.archives, dirname)
if os.path.exists(archivedir):
# Don't overwrite previous archives, just make yet another
# directory for the new run of this srid
archivedir = '%s_%s' % (archivedir, int(time.time()))
os.makedirs(archivedir)
results_file = os.path.join(archivedir, 'results.json')
with file(results_file, 'w') as f:
json.dump(results, f)
metadata = base64.b64decode(metadata_b64)
metadata_file = os.path.join(archivedir, 'metadata.zip')
with file(metadata_file, 'wb') as f:
f.write(metadata)
if ldap is not None:
msg_text = EMAIL_MESSAGE % (ldap, srid, operating_system,
netconfig)
stoneridge.sendmail(ldap, 'Stone Ridge Complete',
msg_text, (metadata_file, 'results.zip'))
def handle(self, srid, netconfig, operating_system, results, metadata,
ldap):
logging.debug('uploading results for %s' % (srid,))
for name in results:
dataset = results[name]
if not isinstance(dataset, dict):
# This one is crap, ignore it
logging.error('bad json: %s' % (results[name],))
continue
if self.unittest:
logging.debug('would upload data via https to %s, project %s' %
(self.host, self.project))
logging.debug('dataset: %s' % (dataset,))
else:
logging.debug('uploading data')
request = dzclient.DatazillaRequest('https', self.host,
self.project, self.key,
self.secret)
response = request.send(dataset)
logging.debug('got status code %s' % (response.status,))
if response.status != 200:
logging.error('bad http status %s for %s' %
(response.status, srid))
try:
response_text = response.read()
except:
logging.exception('Error reading response')
continue
try:
result = json.loads(response_text)
except:
logging.exception('Error loading resposne %s' %
(response_text,))
continue
logging.debug('got result %s' % (result,))
if result['status'] != 'well-formed JSON stored':
logging.error('bad status for %s: %s' %
(srid, result['status']))
self.save_data(srid, netconfig, operating_system, results, metadata,
ldap)
def daemon():
reporter = StoneRidgeReporter(stoneridge.OUTGOING_QUEUE)
reporter.run()
@stoneridge.main
def main():
parser = stoneridge.DaemonArgumentParser()
parser.parse_args()
parser.start_daemon(daemon)