Merge "SNIRO Emulator - Adding SNIRO emulator container"
[oom.git] / cloudify / scripts / onap / delete_init_pod.py
1 import pip
2
3 from cloudify import ctx
4 from cloudify.exceptions import NonRecoverableError
5
6
7 SERVICES_FILE_PARTS_SEPARATOR = '---'
8
9
10 def _import_or_install():
11     try:
12         import yaml
13     except ImportError:
14         pip.main(["install", "pyaml"])
15
16     try:
17         import cloudify_kubernetes.tasks as kubernetes_plugin
18     except ImportError:
19         pip.main([
20             "install",
21             "https://github.com/cloudify-incubator/cloudify-kubernetes-plugin/archive/1.2.1rc1.zip"
22         ])
23
24     import yaml
25     import cloudify_kubernetes.tasks as kubernetes_plugin
26
27     return yaml, kubernetes_plugin
28
29
30 def _retrieve_path():
31     return ctx.node.properties.get('init_pod', None)
32
33
34 def _set_deployment_result(key):
35     result = ctx.instance.runtime_properties.pop(key)
36     ctx.instance.runtime_properties['kubernetes'] = result
37
38
39 def _do_delete_init_pod(kubernetes_plugin, yaml):
40     ctx.logger.info('Deleting init pod')
41     init_pod_file_path = _retrieve_path()
42
43     if not init_pod_file_path:
44         raise NonRecoverableError('Init pod file is not defined.')
45
46     temp_file_path = ctx.download_resource_and_render(
47         init_pod_file_path
48     )
49
50     with open(temp_file_path) as temp_file:
51         init_pod_file_content = temp_file.read()
52         init_pod_yaml_content = yaml.load(init_pod_file_content)
53
54         _set_deployment_result('init_pod')
55         kubernetes_plugin.resource_delete(definition=init_pod_yaml_content)
56
57     ctx.logger.info('Init pod deleted successfully')
58
59
60 if __name__ == '__main__':
61     yaml, kubernetes_plugin = _import_or_install()
62
63     _do_delete_init_pod(kubernetes_plugin, yaml)
64