c82172d15f87288620e772003fcb183af9b59f4c
[oom.git] / cloudify / scripts / onap / create_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 _save_deployment_result(key):
35     result = ctx.instance.runtime_properties['kubernetes']
36     ctx.instance.runtime_properties[key] = result
37     ctx.instance.runtime_properties['kubernetes'] = {}
38
39
40 def _do_create_init_pod(kubernetes_plugin, yaml):
41     ctx.logger.info('Creating init pod')
42     init_pod_file_path = _retrieve_path()
43
44     if not init_pod_file_path:
45         raise NonRecoverableError('Init pod file is not defined.')
46
47     temp_file_path = ctx.download_resource_and_render(
48         init_pod_file_path
49     )
50
51     with open(temp_file_path) as temp_file:
52         init_pod_file_content = temp_file.read()
53         init_pod_yaml_content = yaml.load(init_pod_file_content)
54
55         kubernetes_plugin.resource_create(definition=init_pod_yaml_content)
56         _save_deployment_result('init_pod')
57
58     ctx.logger.info('Init pod created successfully')
59
60
61 if __name__ == '__main__':
62     yaml, kubernetes_plugin = _import_or_install()
63
64     _do_create_init_pod(kubernetes_plugin, yaml)
65