update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwlaunchpad / plugins / rwlaunchpadtasklet / rift / tasklets / rwlaunchpad / message.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import logging
19 import time
20
21
22 class MessageException(Exception):
23 def __init__(self, msg):
24 if not isinstance(msg, Message):
25 raise ValueError("{} is not a message".format(msg.__class__.__name__))
26
27 self.msg = msg
28
29
30 class Message(object):
31 """
32 Messages are events that describe stages of the onboarding process, and
33 any event that may occur during the onboarding process.
34 """
35
36 def __init__(self, level, name, text):
37 self._level = level
38 self._name = name
39 self._text = text
40 self._timestamp = time.time()
41
42 def __repr__(self):
43 return "{} {}:{}:{}".format(
44 self.timestamp,
45 logging._levelToName.get(self.level, self.level),
46 self.name,
47 self.text,
48 )
49
50 @property
51 def level(self):
52 return self._level
53
54 @property
55 def name(self):
56 return self._name
57
58 @property
59 def text(self):
60 return self._text
61
62 @property
63 def timestamp(self):
64 return self._timestamp
65
66 def log(self, logger):
67 logger.log(self.level, self.text)
68
69
70 class WarningMessage(Message):
71 """
72 A warning is a message that does not prevent the onboarding process for
73 continuing, but may not be the intention of the user when they initiated
74 the process.
75 """
76
77 def __init__(self, name, text):
78 super().__init__(logging.WARNING, name, text)
79
80
81 class ErrorMessage(Message):
82 """
83 An error message alerts the user to an event that prevent the continuation
84 of the onboarding process.
85 """
86
87 def __init__(self, name, text):
88 super().__init__(logging.ERROR, name, text)
89
90
91 class StatusMessage(Message):
92 """
93 A status message informs the user of an expected stage in the onboarding
94 process.
95 """
96
97 def __init__(self, name, text):
98 super().__init__(logging.INFO, name, text)
99
100
101 class FilenameMessage(Message):
102 """
103 A status message informs the user of a download file.
104 """
105
106 def __init__(self, filename):
107 super().__init__(logging.INFO, 'filename', filename)
108
109
110 class Logger(object):
111 """
112 This class is used to augment a python logger class so that messages can be
113 passed to it. Messages are recorded so that the uploader application can
114 provide this information to the client, and the messages are also recorded
115 on the server via the standard logging facilities.
116 """
117
118 def __init__(self, logger, messages):
119 self._rift_logger = logger
120 self._messages = messages
121
122 @property
123 def messages(self):
124 return self._messages
125
126 def message(self, msg):
127 msg.log(self._rift_logger)
128 self._messages.append(msg)
129
130 def __getattr__(self, name):
131 """ Return the rift logger attribute
132
133 By returning the rift logger attribute back to the client,
134 the line logged by rwlogger corresponds to the actual file/line
135 logged by the application instead of one in this class. This makes
136 debugging easier and prevents rwlogd from inadvertantly triggering
137 dup detection (which uses event & line information).
138 """
139 return getattr(self._rift_logger, name)
140
141
142 class DownloadError(ErrorMessage):
143 def __init__(self, msg):
144 super().__init__("Download-error", msg)
145
146
147 class DownloadSuccess(StatusMessage):
148 def __init__(self, msg):
149 super().__init__("Download-Successful.", msg)
150
151
152 class OnboardError(ErrorMessage):
153 def __init__(self, msg):
154 super().__init__("onboard-error", msg)
155
156
157 class OnboardWarning(ErrorMessage):
158 def __init__(self, msg):
159 super().__init__("onboard-warning", msg)
160
161
162 class OnboardDescriptorValidation(StatusMessage):
163 def __init__(self):
164 super().__init__("onboard-dsc-validation", "descriptor validation")
165
166
167 class OnboardDescriptorTimeout(OnboardError):
168 def __init__(self):
169 super().__init__("descriptor timeout")
170
171
172 class OnboardDescriptorError(OnboardError):
173 def __init__(self, filename):
174 super().__init__("unable to onboard {}".format(filename))
175
176
177 class OnboardDescriptorFormatError(OnboardError):
178 def __init__(self, filename):
179 super().__init__("{} has unrecognized format".format(filename))
180
181
182 class OnboardMissingContentType(OnboardError):
183 def __init__(self):
184 super().__init__("missing content-type header")
185
186
187 class OnboardUnsupportedMediaType(OnboardError):
188 def __init__(self):
189 super().__init__("multipart/form-data required")
190
191
192 class OnboardMissingContentBoundary(OnboardError):
193 def __init__(self):
194 super().__init__("missing content boundary")
195
196
197 class OnboardMissingTerminalBoundary(OnboardError):
198 def __init__(self):
199 super().__init__("Unable to find terminal content boundary")
200
201
202 class OnboardUnreadableHeaders(OnboardError):
203 def __init__(self):
204 super().__init__("Unable to read message headers")
205
206
207 class OnboardUnreadablePackage(OnboardError):
208 def __init__(self):
209 super().__init__("Unable to read package")
210
211
212 class OnboardExtractionError(OnboardError):
213 def __init__(self):
214 super().__init__("Unable to extract package contents")
215
216
217 class OnboardImageUploadError(OnboardError):
218 def __init__(self, message=""):
219 super().__init__("Unable to upload images: %s" % message)
220
221
222 class OnboardMissingChecksumsFile(OnboardError):
223 def __init__(self):
224 super().__init__("Package does not contain checksums.txt")
225
226
227 class OnboardChecksumMismatch(OnboardError):
228 def __init__(self, filename):
229 super().__init__("checksum mismatch for {}".format(filename))
230
231
232 class OnboardDescriptorExistsError(OnboardError):
233 def __init__(self, descriptor_id):
234 super().__init__("descriptor id {} already onboarded".format(descriptor_id))
235
236
237
238 class OnboardStart(StatusMessage):
239 def __init__(self):
240 super().__init__("onboard-started", "onboarding process started")
241
242
243 class OnboardDescriptorOnboard(StatusMessage):
244 def __init__(self):
245 super().__init__("onboard-dsc-onboard", "onboarding descriptors")
246
247
248 class OnboardSuccess(StatusMessage):
249 def __init__(self):
250 super().__init__("onboard-success", "onboarding process successfully completed")
251
252
253 class OnboardFailure(StatusMessage):
254 def __init__(self):
255 super().__init__("onboard-failure", "onboarding process failed")
256
257
258 class OnboardPackageUpload(StatusMessage):
259 def __init__(self):
260 super().__init__("onboard-pkg-upload", "uploading package")
261
262
263 class OnboardImageUpload(StatusMessage):
264 def __init__(self):
265 super().__init__("onboard-img-upload", "uploading image")
266
267
268 class OnboardPackageValidation(StatusMessage):
269 def __init__(self):
270 super().__init__("onboard-pkg-validation", "package contents validation")
271
272
273
274 class UpdateError(ErrorMessage):
275 def __init__(self, msg):
276 super().__init__("update-error", msg)
277
278
279 class UpdateMissingContentType(UpdateError):
280 def __init__(self):
281 super().__init__("missing content-type header")
282
283
284 class UpdateUnsupportedMediaType(UpdateError):
285 def __init__(self):
286 super().__init__("multipart/form-data required")
287
288
289 class UpdateMissingContentBoundary(UpdateError):
290 def __init__(self):
291 super().__init__("missing content boundary")
292
293
294 class UpdateDescriptorError(UpdateError):
295 def __init__(self, filename):
296 super().__init__("unable to update {}".format(filename))
297
298
299 class UpdatePackageNotFoundError(UpdateError):
300 def __init__(self, descriptor_id):
301 super().__init__("package {} not found".format(descriptor_id))
302
303
304 class UpdateDescriptorFormatError(UpdateError):
305 def __init__(self, filename):
306 super().__init__("{} has unrecognized format".format(filename))
307
308
309 class UpdateExtractionError(UpdateError):
310 def __init__(self):
311 super().__init__("Unable to extract package contents")
312
313
314 class UpdateDescriptorTimeout(UpdateError):
315 def __init__(self):
316 super().__init__("descriptor timeout")
317
318
319 class UpdateUnreadableHeaders(UpdateError):
320 def __init__(self):
321 super().__init__("Unable to read message headers")
322
323
324 class UpdateUnreadablePackage(UpdateError):
325 def __init__(self):
326 super().__init__("Unable to read package")
327
328
329 class UpdateChecksumMismatch(UpdateError):
330 def __init__(self, filename):
331 super().__init__("checksum mismatch for {}".format(filename))
332
333
334 class UpdateImageUploadError(UpdateError):
335 def __init__(self):
336 super().__init__("Unable to upload images")
337
338
339 class UpdateStart(StatusMessage):
340 def __init__(self):
341 super().__init__("update-started", "update process started")
342
343
344 class UpdateSuccess(StatusMessage):
345 def __init__(self):
346 super().__init__("update-success", "updating process successfully completed")
347
348
349 class UpdateFailure(StatusMessage):
350 def __init__(self):
351 super().__init__("update-failure", "updating process failed")
352
353
354 class UpdatePackageUpload(StatusMessage):
355 def __init__(self):
356 super().__init__("update-pkg-upload", "uploading package")
357
358
359 class UpdateDescriptorUpdate(StatusMessage):
360 def __init__(self):
361 super().__init__("update-dsc-onboard", "updating descriptors")
362
363
364 class UpdateDescriptorUpdated(StatusMessage):
365 def __init__(self):
366 super().__init__("update-dsc-updated", "updated descriptors")
367
368
369