1# Microsoft Azure Linux Agent
2#
3# Copyright 2018 Microsoft Corporation
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# Requires Python 2.6+ and Openssl 1.0+
18#
19
20"""
21Defines all exceptions
22"""
23
24
25class AgentError(Exception):
26    """
27    Base class of agent error.
28    """
29
30    def __init__(self, msg, inner=None):
31        msg = u"[{0}] {1}".format(type(self).__name__, msg)
32        if inner is not None:
33            msg = u"{0}\nInner error: {1}".format(msg, inner)
34        super(AgentError, self).__init__(msg)
35
36
37class AgentConfigError(AgentError):
38    """
39    When configure file is not found or malformed.
40    """
41
42    def __init__(self, msg=None, inner=None):
43        super(AgentConfigError, self).__init__(msg, inner)
44
45
46class AgentNetworkError(AgentError):
47    """
48    When network is not available.
49    """
50
51    def __init__(self, msg=None, inner=None):
52        super(AgentNetworkError, self).__init__(msg, inner)
53
54
55class CGroupsException(AgentError):
56    """
57    Exception to classify any cgroups related issue.
58    """
59
60    def __init__(self, msg=None, inner=None):
61        super(CGroupsException, self).__init__(msg, inner)
62
63
64class ExtensionError(AgentError):
65    """
66    When failed to execute an extension
67    """
68
69    def __init__(self, msg=None, inner=None, code=-1):
70        super(ExtensionError, self).__init__(msg, inner)
71        self.code = code
72
73
74class ExtensionOperationError(ExtensionError):
75    """
76    When the command times out or returns with a non-zero exit_code
77    """
78
79    def __init__(self, msg=None, inner=None, code=-1, exit_code=-1):
80        super(ExtensionOperationError, self).__init__(msg, inner)
81        self.code = code
82        self.exit_code = exit_code
83
84
85class ExtensionUpdateError(ExtensionError):
86    """
87    Error raised when failed to update an extension
88    """
89
90
91class ExtensionDownloadError(ExtensionError):
92    """
93    Error raised when failed to download and setup an extension
94    """
95
96
97class ExtensionConfigError(ExtensionError):
98    """
99    Error raised when extension config file is malformed
100    """
101
102
103class ProvisionError(AgentError):
104    """
105    When provision failed
106    """
107
108    def __init__(self, msg=None, inner=None):
109        super(ProvisionError, self).__init__(msg, inner)
110
111
112class ResourceDiskError(AgentError):
113    """
114    Mount resource disk failed
115    """
116
117    def __init__(self, msg=None, inner=None):
118        super(ResourceDiskError, self).__init__(msg, inner)
119
120
121class DhcpError(AgentError):
122    """
123    Failed to handle dhcp response
124    """
125
126    def __init__(self, msg=None, inner=None):
127        super(DhcpError, self).__init__(msg, inner)
128
129
130class OSUtilError(AgentError):
131    """
132    Failed to perform operation to OS configuration
133    """
134
135    def __init__(self, msg=None, inner=None):
136        super(OSUtilError, self).__init__(msg, inner)
137
138
139class ProtocolError(AgentError):
140    """
141    Azure protocol error
142    """
143
144    def __init__(self, msg=None, inner=None):
145        super(ProtocolError, self).__init__(msg, inner)
146
147
148class ProtocolNotFoundError(ProtocolError):
149    """
150    Error raised when Azure protocol endpoint not found
151    """
152
153
154class IncompleteGoalStateError(ProtocolError):
155    """
156    Goal state is returned incomplete.
157    """
158
159
160class HttpError(AgentError):
161    """
162    Http request failure
163    """
164
165    def __init__(self, msg=None, inner=None):
166        super(HttpError, self).__init__(msg, inner)
167
168
169class InvalidContainerError(HttpError):
170    """
171    Error raised when Container id sent in the header is invalid
172    """
173
174
175class EventError(AgentError):
176    """
177    Event reporting error
178    """
179
180    def __init__(self, msg=None, inner=None):
181        super(EventError, self).__init__(msg, inner)
182
183
184class CryptError(AgentError):
185    """
186    Encrypt/Decrypt error
187    """
188
189    def __init__(self, msg=None, inner=None):
190        super(CryptError, self).__init__(msg, inner)
191
192
193class UpdateError(AgentError):
194    """
195    Update Guest Agent error
196    """
197
198    def __init__(self, msg=None, inner=None):
199        super(UpdateError, self).__init__(msg, inner)
200
201
202class ResourceGoneError(HttpError):
203    """
204   The requested resource no longer exists (i.e., status code 410)
205    """
206
207    def __init__(self, msg=None, inner=None):
208        if msg is None:
209            msg = "Resource is gone"
210        super(ResourceGoneError, self).__init__(msg, inner)
211
212
213class InvalidExtensionEventError(AgentError):
214    """
215    Error thrown when the extension telemetry event is invalid as defined per the contract with extensions.
216    """
217    # Types of InvalidExtensionEventError
218    MissingKeyError = "MissingKeyError"
219    EmptyMessageError = "EmptyMessageError"
220    OversizeEventError = "OversizeEventError"
221
222    def __init__(self, msg=None, inner=None):
223        super(InvalidExtensionEventError, self).__init__(msg, inner)
224
225
226class ServiceStoppedError(AgentError):
227    """
228    Error thrown when trying to access a Service which is stopped
229    """
230    def __init__(self, msg=None, inner=None):
231        super(ServiceStoppedError, self).__init__(msg, inner)
232
233
234class ExtensionErrorCodes(object):
235    """
236    Common Error codes used across by Compute RP for better understanding
237    the cause and clarify common occurring errors
238    """
239
240    # Unknown Failures
241    PluginUnknownFailure = -1
242
243    # Success
244    PluginSuccess = 0
245
246    # Catch all error code.
247    PluginProcessingError = 1000
248
249    # Plugin failed to download
250    PluginManifestDownloadError = 1001
251
252    # Cannot find or load successfully the HandlerManifest.json
253    PluginHandlerManifestNotFound = 1002
254
255    # Cannot successfully serialize the HandlerManifest.json
256    PluginHandlerManifestDeserializationError = 1003
257
258    # Cannot download the plugin package
259    PluginPackageDownloadFailed = 1004
260
261    # Cannot extract the plugin form package
262    PluginPackageExtractionFailed = 1005
263
264    # Install failed
265    PluginInstallProcessingFailed = 1007
266
267    # Update failed
268    PluginUpdateProcessingFailed = 1008
269
270    # Enable failed
271    PluginEnableProcessingFailed = 1009
272
273    # Disable failed
274    PluginDisableProcessingFailed = 1010
275
276    # Extension script timed out
277    PluginHandlerScriptTimedout = 1011
278
279    # Invalid status file of the extension.
280    PluginSettingsStatusInvalid = 1012
281
282    def __init__(self):
283        pass
284