8548e29b70bcae93cc2b1943cdf4e8b59fa19c0e
[oom.git] / cloudify / scripts / onap / create_resources_services.py
1 import pip
2
3 from cloudify import ctx
4
5
6 SERVICES_FILE_PARTS_SEPARATOR = '---'
7
8
9 def _import_or_install():
10     try:
11         import yaml
12     except ImportError:
13         pip.main(["install", "pyaml"])
14
15     try:
16         import cloudify_kubernetes.tasks as kubernetes_plugin
17     except ImportError:
18         pip.main([
19             "install",
20             "https://github.com/cloudify-incubator/cloudify-kubernetes-plugin/archive/1.2.1rc1.zip"
21         ])
22
23     try:
24         import jinja2
25     except ImportError:
26         pip.main(["install", "jinja2"])
27
28     import yaml
29     import jinja2
30     import cloudify_kubernetes.tasks as kubernetes_plugin
31
32     return yaml, kubernetes_plugin, jinja2
33
34
35 def _init_jinja(jinja2):
36     return jinja2.Environment(
37         loader=jinja2.BaseLoader()
38     )
39
40
41 def _render_template(jinja_env, template_content, values):
42     template_content = template_content.replace('.Values', 'Values')
43
44     template = jinja_env.from_string(template_content)
45     rendered_template = template.render(Values=values)
46     return rendered_template
47
48
49 def _retrieve_resources_paths():
50     return ctx.node.properties.get('resources', [])
51
52
53 def _retrieve_services_paths():
54     return ctx.node.properties.get('services', None)
55
56
57 def _retrieve_values(yaml):
58     values_file_path = ctx.node.properties.get('values', None)
59
60     if values_file_path:
61         return yaml.load(ctx.get_resource(values_file_path))
62
63     ctx.logger.warn('Values file not found')
64
65
66 def _save_deployment_result(key):
67     result = ctx.instance.runtime_properties['kubernetes']
68     ctx.instance.runtime_properties[key] = result
69     ctx.instance.runtime_properties['kubernetes'] = {}
70
71
72 def _do_create_resources(kubernetes_plugin, yaml, jinja_env, values):
73     for path in _retrieve_resources_paths():
74         ctx.logger.info('Creating 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         kubernetes_plugin.resource_create(definition=content)
85         _save_deployment_result(
86             'resource_{0}'.format(content['metadata']['name'])
87         )
88
89     ctx.logger.info('Resources created successfully')
90
91
92 def _do_create_services(kubernetes_plugin, yaml, jinja_env, values):
93     ctx.logger.info('Creating 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         kubernetes_plugin.resource_create(definition=content)
117         _save_deployment_result(
118             'service_{0}'.format(content['metadata']['name'])
119         )
120
121     ctx.logger.info('Services created 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_create_resources(kubernetes_plugin, yaml, jinja_env, values)
130     _do_create_services(kubernetes_plugin, yaml, jinja_env, values)
131