1
from curtin import url_helper
3
from . import (BaseReporter, LoadReporterException)
12
class MAASReporter(BaseReporter):
14
def __init__(self, config):
15
"""Load config dictionary and initialize object."""
16
self.url = config['url']
17
self.urlhelper = url_helper.OauthUrlHelper(
18
consumer_key=config.get('consumer_key'),
19
token_key=config.get('token_key'),
20
token_secret=config.get('token_secret'),
22
skew_data_file="/run/oauth_skew.json")
24
self.retries = config.get('retries', [1, 1, 2, 4, 8, 16, 32])
26
def report_success(self):
27
"""Report installation success."""
29
message = "Installation succeeded."
30
self.report(status, message, files=self.files)
32
def report_failure(self, message):
33
"""Report installation failure."""
35
self.report(status, message, files=self.files)
37
def encode_multipart_data(self, data, files):
38
"""Create a MIME multipart payload from L{data} and L{files}.
40
@param data: A mapping of names (ASCII strings) to data (byte string).
41
@param files: A mapping of names (ASCII strings) to file objects ready
43
@return: A 2-tuple of C{(body, headers)}, where C{body} is a a byte
44
string and C{headers} is a dict of headers to add to the enclosing
45
request in which this payload will travel.
47
boundary = self._random_string(30)
51
lines.extend(self._encode_field(name, data[name], boundary))
53
lines.extend(self._encode_file(name, files[name], boundary))
54
lines.extend(('--%s--' % boundary, ''))
55
body = '\r\n'.join(lines)
58
'content-type': 'multipart/form-data; boundary=' + boundary,
59
'content-length': "%d" % len(body),
63
def report(self, status, message=None, files=None):
64
"""Send the report."""
67
params['status'] = status
68
if message is not None:
69
params['error'] = message
75
install_files[os.path.basename(fpath)] = open(fpath, "r")
77
data, headers = self.encode_multipart_data(params, install_files)
81
if not isinstance(data, bytes):
85
payload = self.urlhelper.geturl(
86
self.url, data=data, headers=headers,
89
raise TypeError("Unexpected result from call: %s" % payload)
92
except url_helper.UrlError as exc:
94
except Exception as exc:
97
sys.stderr.write("%s\n" % msg)
99
def _encode_field(self, field_name, data, boundary):
102
'Content-Disposition: form-data; name="%s"' % field_name,
106
def _encode_file(self, name, fileObj, boundary):
109
'Content-Disposition: form-data; name="%s"; filename="%s"'
111
'Content-Type: %s' % self._get_content_type(name),
116
def _random_string(self, length):
117
return ''.join(random.choice(string.ascii_letters)
118
for ii in range(length + 1))
120
def _get_content_type(self, filename):
121
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
124
def load_factory(options):
126
return MAASReporter(options)
128
raise LoadReporterException