1 # ============LICENSE_START=======================================================
3 # ================================================================================
4 # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 from cloudify import ctx
23 from cloudify.exceptions import NonRecoverableError, RecoverableError
24 from k8splugin import discovery as dis
25 from k8splugin.exceptions import DockerPluginDeploymentError, \
26 DockerPluginDependencyNotReadyError
27 from k8splugin import utils
30 def monkeypatch_loggers(task_func):
31 """Sets up the dependent loggers"""
33 def wrapper(**kwargs):
34 # Ouch! Monkeypatch loggers
35 dis.logger = ctx.logger
37 return task_func(**kwargs)
42 def wrap_error_handling_start(task_start_func):
43 """Wrap error handling for the start operations"""
45 def wrapper(**kwargs):
47 return task_start_func(**kwargs)
48 except DockerPluginDependencyNotReadyError as e:
49 # You are here because things we need like a working docker host is not
50 # available yet so let Cloudify try again later.
51 raise RecoverableError(e)
52 except DockerPluginDeploymentError as e:
53 # Container failed to come up in the allotted time. This is deemed
55 raise NonRecoverableError(e)
56 except Exception as e:
57 ctx.logger.error("Unexpected error while starting container: {0}"
59 raise NonRecoverableError(e)
64 def _wrapper_merge_inputs(task_func, properties, **kwargs):
65 """Merge Cloudify properties with input kwargs before calling task func"""
66 inputs = copy.deepcopy(properties)
68 utils.update_dict(inputs, kwargs)
70 # Apparently kwargs contains "ctx" which is cloudify.context.CloudifyContext
71 # This has to be removed and not copied into runtime_properties else you get
72 # JSON serialization errors.
76 return task_func(**inputs)
78 def merge_inputs_for_create(task_create_func):
79 """Merge all inputs for start operation into one dict"""
81 # Needed to wrap the wrapper because I was seeing issues with
82 # "RuntimeError: No context set in current execution thread"
83 def wrapper(**kwargs):
84 # NOTE: ctx.node.properties is an ImmutableProperties instance which is
85 # why it is passed into a mutable dict so that it can be deep copied
86 return _wrapper_merge_inputs(task_create_func,
87 dict(ctx.node.properties), **kwargs)
91 def merge_inputs_for_start(task_start_func):
92 """Merge all inputs for start operation into one dict"""
94 # Needed to wrap the wrapper because I was seeing issues with
95 # "RuntimeError: No context set in current execution thread"
96 def wrapper(**kwargs):
97 return _wrapper_merge_inputs(task_start_func,
98 ctx.instance.runtime_properties, **kwargs)
102 def wrap_error_handling_update(update_func):
103 """ Wrap error handling for update operations (scale and upgrade) """
105 def wrapper(**kwargs):
107 return update_func(**kwargs)
108 except DockerPluginDeploymentError:
109 raise NonRecoverableError ("Update operation did not complete successfully in the alloted time")
110 except Exception as e:
111 ctx.logger.error ("Unexpected error during update operation: {0}".format(str(e)))
112 raise NonRecoverableError(e)