Cloudify support for OOM
[oom.git] / cloudify-onap / plugins / onap-installation-plugin / k8s_installer / common / init_pod.py
1 ########
2 # Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #        http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 #    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #    * See the License for the specific language governing permissions and
14 #    * limitations under the License.
15
16 from cloudify import ctx
17 import yaml
18
19 import constants
20 import resources_services
21
22 SERVICES_FILE_PARTS_SEPARATOR = '---'
23
24
25 def do_create_init_pod():
26     ctx.logger.info('Creating init pod')
27
28     yaml_config = resources_services.render_chart(
29         ctx.node.properties["init_pod"],
30         _retrieve_root_path(),
31         _retrieve_helm_cli_path()
32     )
33     yaml_content_part = yaml_config.split(SERVICES_FILE_PARTS_SEPARATOR)[2]
34     enhanced_yaml = _add_openstack_envs(yaml_content_part)
35
36     resources_services.create_resource(enhanced_yaml)
37
38     ctx.logger.info('Init pod created successfully')
39
40
41 def do_delete_init_pod():
42     ctx.logger.info('Deleting init pod')
43
44     ctx.logger.info('Init pod deleted successfully')
45
46 def _add_openstack_envs(yaml_content):
47     input_dict = yaml.load(yaml_content)
48
49     container_dict = input_dict['spec']['containers'][0]
50     container_dict.pop('envFrom')
51
52     openstack_envs = ctx.node.properties["openstack_envs"]
53     for item in openstack_envs.items():
54         ctx.logger.debug("adding item = {}".format(item))
55         container_dict['env'].append(item)
56
57     return input_dict
58
59 def _retrieve_root_path():
60     return ctx.instance.runtime_properties.get(constants.RT_APPS_ROOT_PATH, None)
61
62 def _retrieve_helm_cli_path():
63     return ctx.instance.runtime_properties.get(constants.RT_HELM_CLI_PATH, None)