4b6a3fd81e177e2c1eee9568efbf31bbf17e25c1
3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
__))
30 class Message(object):
32 Messages are events that describe stages of the onboarding process, and
33 any event that may occur during the onboarding process.
36 def __init__(self
, level
, name
, text
):
40 self
._timestamp
= time
.time()
43 return "{} {}:{}:{}".format(
45 logging
._levelToName
.get(self
.level
, self
.level
),
64 return self
._timestamp
66 def log(self
, logger
):
67 logger
.log(self
.level
, self
.text
)
70 class WarningMessage(Message
):
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
77 def __init__(self
, name
, text
):
78 super().__init
__(logging
.WARNING
, name
, text
)
81 class ErrorMessage(Message
):
83 An error message alerts the user to an event that prevent the continuation
84 of the onboarding process.
87 def __init__(self
, name
, text
):
88 super().__init
__(logging
.ERROR
, name
, text
)
91 class StatusMessage(Message
):
93 A status message informs the user of an expected stage in the onboarding
97 def __init__(self
, name
, text
):
98 super().__init
__(logging
.INFO
, name
, text
)
101 class FilenameMessage(Message
):
103 A status message informs the user of a download file.
106 def __init__(self
, filename
):
107 super().__init
__(logging
.INFO
, 'filename', filename
)
110 class Logger(object):
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.
118 def __init__(self
, logger
, messages
):
119 self
._rift
_logger
= logger
120 self
._messages
= messages
124 return self
._messages
126 def message(self
, msg
):
127 msg
.log(self
._rift
_logger
)
128 self
._messages
.append(msg
)
130 def __getattr__(self
, name
):
131 """ Return the rift logger attribute
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).
139 return getattr(self
._rift
_logger
, name
)
142 class DownloadError(ErrorMessage
):
143 def __init__(self
, msg
):
144 super().__init
__("Download-error", msg
)
147 class DownloadSuccess(StatusMessage
):
148 def __init__(self
, msg
):
149 super().__init
__("Download-Successful.", msg
)
152 class OnboardError(ErrorMessage
):
153 def __init__(self
, msg
):
154 super().__init
__("onboard-error", msg
)
157 class OnboardWarning(ErrorMessage
):
158 def __init__(self
, msg
):
159 super().__init
__("onboard-warning", msg
)
162 class OnboardDescriptorValidation(StatusMessage
):
164 super().__init
__("onboard-dsc-validation", "descriptor validation")
167 class OnboardDescriptorTimeout(OnboardError
):
169 super().__init
__("descriptor timeout")
172 class OnboardDescriptorError(OnboardError
):
173 def __init__(self
, filename
):
174 super().__init
__("unable to onboard {}".format(filename
))
177 class OnboardDescriptorFormatError(OnboardError
):
178 def __init__(self
, filename
):
179 super().__init
__("{} has unrecognized format".format(filename
))
182 class OnboardMissingContentType(OnboardError
):
184 super().__init
__("missing content-type header")
187 class OnboardUnsupportedMediaType(OnboardError
):
189 super().__init
__("multipart/form-data required")
192 class OnboardMissingContentBoundary(OnboardError
):
194 super().__init
__("missing content boundary")
197 class OnboardMissingTerminalBoundary(OnboardError
):
199 super().__init
__("Unable to find terminal content boundary")
202 class OnboardUnreadableHeaders(OnboardError
):
204 super().__init
__("Unable to read message headers")
207 class OnboardUnreadablePackage(OnboardError
):
209 super().__init
__("Unable to read package")
212 class OnboardExtractionError(OnboardError
):
214 super().__init
__("Unable to extract package contents")
217 class OnboardImageUploadError(OnboardError
):
218 def __init__(self
, message
=""):
219 super().__init
__("Unable to upload images: %s" % message
)
222 class OnboardMissingChecksumsFile(OnboardError
):
224 super().__init
__("Package does not contain checksums.txt")
227 class OnboardChecksumMismatch(OnboardError
):
228 def __init__(self
, filename
):
229 super().__init
__("checksum mismatch for {}".format(filename
))
232 class OnboardDescriptorExistsError(OnboardError
):
233 def __init__(self
, descriptor_id
):
234 super().__init
__("descriptor id {} already onboarded".format(descriptor_id
))
238 class OnboardStart(StatusMessage
):
240 super().__init
__("onboard-started", "onboarding process started")
243 class OnboardDescriptorOnboard(StatusMessage
):
245 super().__init
__("onboard-dsc-onboard", "onboarding descriptors")
248 class OnboardSuccess(StatusMessage
):
250 super().__init
__("onboard-success", "onboarding process successfully completed")
253 class OnboardFailure(StatusMessage
):
255 super().__init
__("onboard-failure", "onboarding process failed")
258 class OnboardPackageUpload(StatusMessage
):
260 super().__init
__("onboard-pkg-upload", "uploading package")
263 class OnboardImageUpload(StatusMessage
):
265 super().__init
__("onboard-img-upload", "uploading image")
268 class OnboardPackageValidation(StatusMessage
):
270 super().__init
__("onboard-pkg-validation", "package contents validation")
274 class UpdateError(ErrorMessage
):
275 def __init__(self
, msg
):
276 super().__init
__("update-error", msg
)
279 class UpdateMissingContentType(UpdateError
):
281 super().__init
__("missing content-type header")
284 class UpdateUnsupportedMediaType(UpdateError
):
286 super().__init
__("multipart/form-data required")
289 class UpdateMissingContentBoundary(UpdateError
):
291 super().__init
__("missing content boundary")
294 class UpdateDescriptorError(UpdateError
):
295 def __init__(self
, filename
):
296 super().__init
__("unable to update {}".format(filename
))
299 class UpdatePackageNotFoundError(UpdateError
):
300 def __init__(self
, descriptor_id
):
301 super().__init
__("package {} not found".format(descriptor_id
))
304 class UpdateDescriptorFormatError(UpdateError
):
305 def __init__(self
, filename
):
306 super().__init
__("{} has unrecognized format".format(filename
))
309 class UpdateExtractionError(UpdateError
):
311 super().__init
__("Unable to extract package contents")
314 class UpdateDescriptorTimeout(UpdateError
):
316 super().__init
__("descriptor timeout")
319 class UpdateUnreadableHeaders(UpdateError
):
321 super().__init
__("Unable to read message headers")
324 class UpdateUnreadablePackage(UpdateError
):
326 super().__init
__("Unable to read package")
329 class UpdateChecksumMismatch(UpdateError
):
330 def __init__(self
, filename
):
331 super().__init
__("checksum mismatch for {}".format(filename
))
334 class UpdateImageUploadError(UpdateError
):
336 super().__init
__("Unable to upload images")
339 class UpdateStart(StatusMessage
):
341 super().__init
__("update-started", "update process started")
344 class UpdateSuccess(StatusMessage
):
346 super().__init
__("update-success", "updating process successfully completed")
349 class UpdateFailure(StatusMessage
):
351 super().__init
__("update-failure", "updating process failed")
354 class UpdatePackageUpload(StatusMessage
):
356 super().__init
__("update-pkg-upload", "uploading package")
359 class UpdateDescriptorUpdate(StatusMessage
):
361 super().__init
__("update-dsc-onboard", "updating descriptors")
364 class UpdateDescriptorUpdated(StatusMessage
):
366 super().__init
__("update-dsc-updated", "updated descriptors")