Merge "SNIRO Emulator - Adding SNIRO emulator container"
[oom.git] / cloudify / scripts / onap / delete_resources_services.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     try:
25         import jinja2
26     except ImportError:
27         pip.main(["install", "jinja2"])
28
29     import yaml
30     import jinja2
31     import cloudify_kubernetes.tasks as kubernetes_plugin
32
33     return yaml, kubernetes_plugin, jinja2
34
35
36 def _init_jinja(jinja2):
37     return jinja2.Environment(
38         loader=jinja2.BaseLoader()
39     )
40
41
42 def _render_template(jinja_env, template_content, values):
43     template_content = template_content.replace('.Values', 'Values')
44
45     template = jinja_env.from_string(template_content)
46     rendered_template = template.render(Values=values)
47     return rendered_template
48
49
50 def _retrieve_resources_paths():
51     return ctx.node.properties.get('resources', [])
52
53
54 def _retrieve_services_paths():
55     return ctx.node.properties.get('services', None)
56
57
58 def _retrieve_values(yaml):
59     values_file_path = ctx.node.properties.get('values', None)
60
61     if values_file_path:
62         return yaml.load(ctx.get_resource(values_file_path))
63
64     ctx.logger.warn('Values file not found')
65
66
67 def _set_deployment_result(key):
68     result = ctx.instance.runtime_properties.pop(key)
69     ctx.instance.runtime_properties['kubernetes'] = result
70
71
72 def _do_delete_resources(kubernetes_plugin, yaml, jinja_env, values):
73     for path in _retrieve_resources_paths():
74         ctx.logger.info('Deleting resource defined in: {0}'.format(path))
75
76         template_content = ctx.get_resource(path)
77         yaml_content = _render_template(
78             jinja_env,
79             template_content,
80             values
81         )
82         content = yaml.load(yaml_content)
83
84         _set_deployment_result(
85             'resource_{0}'.format(content['metadata']['name'])
86         )
87         kubernetes_plugin.resource_delete(definition=content)
88
89     ctx.logger.info('Resources deleted successfully')
90
91
92 def _do_delete_services(kubernetes_plugin, yaml, jinja_env, values):
93     ctx.logger.info('Deleting services')
94     services_file_path = _retrieve_services_paths()
95
96     if not services_file_path:
97         ctx.logger.warn(
98             'Service file is not defined. Skipping services provisioning !'
99         )
100
101         return
102
103     template_content = ctx.get_resource(services_file_path)
104     yaml_content = _render_template(
105         jinja_env,
106         template_content,
107         values
108     )
109
110     yaml_content_parts = \
111         yaml_content.split(SERVICES_FILE_PARTS_SEPARATOR)
112
113     for yaml_content_part in yaml_content_parts:
114         content = yaml.load(yaml_content_part)
115
116         _set_deployment_result(
117             'service_{0}'.format(content['metadata']['name'])
118         )
119         kubernetes_plugin.resource_delete(definition=content)
120
121     ctx.logger.info('Services deleted successfully')
122
123
124 if __name__ == '__main__':
125     yaml, kubernetes_plugin, jinja2 = _import_or_install()
126     jinja_env = _init_jinja(jinja2)
127     values = _retrieve_values(yaml)
128
129     _do_delete_services(kubernetes_plugin, yaml, jinja_env, values)
130     _do_delete_resources(kubernetes_plugin, yaml, jinja_env, values)
131
132