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
._levelNames
.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
)
143 class OnboardError(ErrorMessage
):
144 def __init__(self
, msg
):
145 super().__init
__("onboard-error", msg
)
148 class OnboardWarning(ErrorMessage
):
149 def __init__(self
, msg
):
150 super().__init
__("onboard-warning", msg
)
153 class OnboardDescriptorValidation(StatusMessage
):
155 super().__init
__("onboard-dsc-validation", "descriptor validation")
158 class OnboardDescriptorTimeout(OnboardError
):
160 super().__init
__("descriptor timeout")
163 class OnboardDescriptorError(OnboardError
):
164 def __init__(self
, filename
):
165 super().__init
__("unable to onboard {}".format(filename
))
168 class OnboardDescriptorFormatError(OnboardError
):
169 def __init__(self
, filename
):
170 super().__init
__("{} has unrecognized format".format(filename
))
173 class OnboardMissingContentType(OnboardError
):
175 super().__init
__("missing content-type header")
178 class OnboardUnsupportedMediaType(OnboardError
):
180 super().__init
__("multipart/form-data required")
183 class OnboardMissingContentBoundary(OnboardError
):
185 super().__init
__("missing content boundary")
188 class OnboardMissingTerminalBoundary(OnboardError
):
190 super().__init
__("Unable to find terminal content boundary")
193 class OnboardUnreadableHeaders(OnboardError
):
195 super().__init
__("Unable to read message headers")
198 class OnboardUnreadablePackage(OnboardError
):
200 super().__init
__("Unable to read package")
203 class OnboardExtractionError(OnboardError
):
205 super().__init
__("Unable to extract package contents")
208 class OnboardImageUploadError(OnboardError
):
209 def __init__(self
, message
=""):
210 super().__init
__("Unable to upload images: %s" % message
)
213 class OnboardMissingChecksumsFile(OnboardError
):
215 super().__init
__("Package does not contain checksums.txt")
218 class OnboardChecksumMismatch(OnboardError
):
219 def __init__(self
, filename
):
220 super().__init
__("checksum mismatch for {}".format(filename
))
223 class OnboardDescriptorExistsError(OnboardError
):
224 def __init__(self
, descriptor_id
):
225 super().__init
__("descriptor id {} already onboarded".format(descriptor_id
))
229 class OnboardStart(StatusMessage
):
231 super().__init
__("onboard-started", "onboarding process started")
234 class OnboardDescriptorOnboard(StatusMessage
):
236 super().__init
__("onboard-dsc-onboard", "onboarding descriptors")
239 class OnboardSuccess(StatusMessage
):
241 super().__init
__("onboard-success", "onboarding process successfully completed")
244 class OnboardFailure(StatusMessage
):
246 super().__init
__("onboard-failure", "onboarding process failed")
249 class OnboardPackageUpload(StatusMessage
):
251 super().__init
__("onboard-pkg-upload", "uploading package")
254 class OnboardImageUpload(StatusMessage
):
256 super().__init
__("onboard-img-upload", "uploading image")
259 class OnboardPackageValidation(StatusMessage
):
261 super().__init
__("onboard-pkg-validation", "package contents validation")
265 class UpdateError(ErrorMessage
):
266 def __init__(self
, msg
):
267 super().__init
__("update-error", msg
)
270 class UpdateMissingContentType(UpdateError
):
272 super().__init
__("missing content-type header")
275 class UpdateUnsupportedMediaType(UpdateError
):
277 super().__init
__("multipart/form-data required")
280 class UpdateMissingContentBoundary(UpdateError
):
282 super().__init
__("missing content boundary")
285 class UpdateDescriptorError(UpdateError
):
286 def __init__(self
, filename
):
287 super().__init
__("unable to update {}".format(filename
))
290 class UpdatePackageNotFoundError(UpdateError
):
291 def __init__(self
, descriptor_id
):
292 super().__init
__("package {} not found".format(descriptor_id
))
295 class UpdateDescriptorFormatError(UpdateError
):
296 def __init__(self
, filename
):
297 super().__init
__("{} has unrecognized format".format(filename
))
300 class UpdateExtractionError(UpdateError
):
302 super().__init
__("Unable to extract package contents")
305 class UpdateDescriptorTimeout(UpdateError
):
307 super().__init
__("descriptor timeout")
310 class UpdateUnreadableHeaders(UpdateError
):
312 super().__init
__("Unable to read message headers")
315 class UpdateUnreadablePackage(UpdateError
):
317 super().__init
__("Unable to read package")
320 class UpdateChecksumMismatch(UpdateError
):
321 def __init__(self
, filename
):
322 super().__init
__("checksum mismatch for {}".format(filename
))
325 class UpdateImageUploadError(UpdateError
):
327 super().__init
__("Unable to upload images")
330 class UpdateStart(StatusMessage
):
332 super().__init
__("update-started", "update process started")
335 class UpdateSuccess(StatusMessage
):
337 super().__init
__("update-success", "updating process successfully completed")
340 class UpdateFailure(StatusMessage
):
342 super().__init
__("update-failure", "updating process failed")
345 class UpdatePackageUpload(StatusMessage
):
347 super().__init
__("update-pkg-upload", "uploading package")
350 class UpdateDescriptorUpdate(StatusMessage
):
352 super().__init
__("update-dsc-onboard", "updating descriptors")
355 class UpdateDescriptorUpdated(StatusMessage
):
357 super().__init
__("update-dsc-updated", "updated descriptors")