1 # ============LICENSE_START=======================================================
3 # ================================================================================
4 # Copyright (c) 2017-2020 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=========================================================
22 from cloudify import ctx
23 from cloudify.exceptions import NonRecoverableError, RecoverableError
25 from k8splugin import discovery as dis
26 from k8splugin import utils
27 from k8splugin.exceptions import (DockerPluginDependencyNotReadyError,
28 DockerPluginDeploymentError)
31 def monkeypatch_loggers(task_func):
32 """Sets up the dependent loggers"""
34 def wrapper(**kwargs):
35 # Ouch! Monkeypatch loggers
36 dis.logger = ctx.logger
38 return task_func(**kwargs)
43 def wrap_error_handling_start(task_start_func):
44 """Wrap error handling for the start operations"""
46 def wrapper(**kwargs):
48 return task_start_func(**kwargs)
49 except DockerPluginDependencyNotReadyError as e:
50 # You are here because things we need like a working docker host is not
51 # available yet so let Cloudify try again later.
52 raise RecoverableError(e)
53 except DockerPluginDeploymentError as e:
54 # Container failed to come up in the allotted time. This is deemed
56 raise NonRecoverableError(e)
57 except Exception as e:
58 ctx.logger.error("Unexpected error while starting container: {0}"
60 raise NonRecoverableError(e)
65 def _wrapper_merge_inputs(task_func, properties, **kwargs):
66 """Merge Cloudify properties with input kwargs before calling task func"""
67 inputs = copy.deepcopy(properties)
69 utils.update_dict(inputs, kwargs)
71 # Apparently kwargs contains "ctx" which is cloudify.context.CloudifyContext
72 # This has to be removed and not copied into runtime_properties else you get
73 # JSON serialization errors.
77 return task_func(**inputs)
79 def merge_inputs_for_create(task_create_func):
80 """Merge all inputs for start operation into one dict"""
82 # Needed to wrap the wrapper because I was seeing issues with
83 # "RuntimeError: No context set in current execution thread"
84 def wrapper(**kwargs):
85 # NOTE: ctx.node.properties is an ImmutableProperties instance which is
86 # why it is passed into a mutable dict so that it can be deep copied
87 return _wrapper_merge_inputs(task_create_func,
88 dict(ctx.node.properties), **kwargs)
92 def merge_inputs_for_start(task_start_func):
93 """Merge all inputs for start operation into one dict"""
95 # Needed to wrap the wrapper because I was seeing issues with
96 # "RuntimeError: No context set in current execution thread"
97 def wrapper(**kwargs):
98 return _wrapper_merge_inputs(task_start_func,
99 ctx.instance.runtime_properties, **kwargs)
103 def wrap_error_handling_update(update_func):
104 """ Wrap error handling for update operations (scale and upgrade) """
106 def wrapper(**kwargs):
108 return update_func(**kwargs)
109 except DockerPluginDeploymentError:
110 raise NonRecoverableError ("Update operation did not complete successfully in the alloted time")
111 except Exception as e:
112 ctx.logger.error ("Unexpected error during update operation: {0}".format(str(e)))
113 raise NonRecoverableError(e)