1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
import pip
from cloudify import ctx
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 _save_deployment_result(key):
result = ctx.instance.runtime_properties['kubernetes']
ctx.instance.runtime_properties[key] = result
ctx.instance.runtime_properties['kubernetes'] = {}
def _do_create_resources(kubernetes_plugin, yaml, jinja_env, values):
for path in _retrieve_resources_paths():
ctx.logger.info('Creating 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)
kubernetes_plugin.resource_create(definition=content)
_save_deployment_result(
'resource_{0}'.format(content['metadata']['name'])
)
ctx.logger.info('Resources created successfully')
def _do_create_services(kubernetes_plugin, yaml, jinja_env, values):
ctx.logger.info('Creating 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)
kubernetes_plugin.resource_create(definition=content)
_save_deployment_result(
'service_{0}'.format(content['metadata']['name'])
)
ctx.logger.info('Services created successfully')
if __name__ == '__main__':
yaml, kubernetes_plugin, jinja2 = _import_or_install()
jinja_env = _init_jinja(jinja2)
values = _retrieve_values(yaml)
_do_create_resources(kubernetes_plugin, yaml, jinja_env, values)
_do_create_services(kubernetes_plugin, yaml, jinja_env, values)
|