summaryrefslogtreecommitdiffstats
path: root/cloudify/scripts/onap/delete_resources_services.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudify/scripts/onap/delete_resources_services.py')
-rw-r--r--cloudify/scripts/onap/delete_resources_services.py132
1 files changed, 132 insertions, 0 deletions
diff --git a/cloudify/scripts/onap/delete_resources_services.py b/cloudify/scripts/onap/delete_resources_services.py
new file mode 100644
index 0000000000..305a7484bd
--- /dev/null
+++ b/cloudify/scripts/onap/delete_resources_services.py
@@ -0,0 +1,132 @@
+import pip
+
+from cloudify import ctx
+from cloudify.exceptions import NonRecoverableError
+
+
+SERVICES_FILE_PARTS_SEPARATOR = '---'
+
+
+def _import_or_install():
+ try:
+ import yaml
+ except ImportError:
+ pip.main(["install", "pyaml"])
+
+ try:
+ import cloudify_kubernetes.tasks as kubernetes_plugin
+ except ImportError:
+ pip.main([
+ "install",
+ "https://github.com/cloudify-incubator/cloudify-kubernetes-plugin/archive/1.2.1rc1.zip"
+ ])
+
+ try:
+ import jinja2
+ except ImportError:
+ pip.main(["install", "jinja2"])
+
+ import yaml
+ import jinja2
+ import cloudify_kubernetes.tasks as kubernetes_plugin
+
+ return yaml, kubernetes_plugin, jinja2
+
+
+def _init_jinja(jinja2):
+ return jinja2.Environment(
+ loader=jinja2.BaseLoader()
+ )
+
+
+def _render_template(jinja_env, template_content, values):
+ template_content = template_content.replace('.Values', 'Values')
+
+ template = jinja_env.from_string(template_content)
+ rendered_template = template.render(Values=values)
+ return rendered_template
+
+
+def _retrieve_resources_paths():
+ return ctx.node.properties.get('resources', [])
+
+
+def _retrieve_services_paths():
+ return ctx.node.properties.get('services', None)
+
+
+def _retrieve_values(yaml):
+ values_file_path = ctx.node.properties.get('values', None)
+
+ if values_file_path:
+ return yaml.load(ctx.get_resource(values_file_path))
+
+ ctx.logger.warn('Values file not found')
+
+
+def _set_deployment_result(key):
+ result = ctx.instance.runtime_properties.pop(key)
+ ctx.instance.runtime_properties['kubernetes'] = result
+
+
+def _do_delete_resources(kubernetes_plugin, yaml, jinja_env, values):
+ for path in _retrieve_resources_paths():
+ ctx.logger.info('Deleting resource defined in: {0}'.format(path))
+
+ template_content = ctx.get_resource(path)
+ yaml_content = _render_template(
+ jinja_env,
+ template_content,
+ values
+ )
+ content = yaml.load(yaml_content)
+
+ _set_deployment_result(
+ 'resource_{0}'.format(content['metadata']['name'])
+ )
+ kubernetes_plugin.resource_delete(definition=content)
+
+ ctx.logger.info('Resources deleted successfully')
+
+
+def _do_delete_services(kubernetes_plugin, yaml, jinja_env, values):
+ ctx.logger.info('Deleting services')
+ services_file_path = _retrieve_services_paths()
+
+ if not services_file_path:
+ ctx.logger.warn(
+ 'Service file is not defined. Skipping services provisioning !'
+ )
+
+ return
+
+ template_content = ctx.get_resource(services_file_path)
+ yaml_content = _render_template(
+ jinja_env,
+ template_content,
+ values
+ )
+
+ yaml_content_parts = \
+ yaml_content.split(SERVICES_FILE_PARTS_SEPARATOR)
+
+ for yaml_content_part in yaml_content_parts:
+ content = yaml.load(yaml_content_part)
+
+ _set_deployment_result(
+ 'service_{0}'.format(content['metadata']['name'])
+ )
+ kubernetes_plugin.resource_delete(definition=content)
+
+ ctx.logger.info('Services deleted successfully')
+
+
+if __name__ == '__main__':
+ yaml, kubernetes_plugin, jinja2 = _import_or_install()
+ jinja_env = _init_jinja(jinja2)
+ values = _retrieve_values(yaml)
+
+ _do_delete_services(kubernetes_plugin, yaml, jinja_env, values)
+ _do_delete_resources(kubernetes_plugin, yaml, jinja_env, values)
+
+