016659ff8a1a1ea8364fe10b29062fdfa624243d
[osm/devops.git] / descriptor-packages / tools / charm-generator / generator / ansible-charm / templates / ansible_charm.py.j2
1 {#-
2 # Copyright 2019 Whitestack, LLC
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: esousa@whitestack.com or glavado@whitestack.com
18 -#}
19 {%- if license is defined -%}
20 # Copyright {{ license.year }} {{ license.company }}
21 #
22 # Licensed under the Apache License, Version 2.0 (the "License"); you may
23 # not use this file except in compliance with the License. You may obtain
24 # a copy of the License at
25 #
26 #         http://www.apache.org/licenses/LICENSE-2.0
27 #
28 # Unless required by applicable law or agreed to in writing, software
29 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
31 # License for the specific language governing permissions and limitations
32 # under the License.
33 #
34 # For those usages not covered by the Apache License, Version 2.0 please
35 # contact: {{ license.email }}
36 {%- endif %}
37
38
39 import sys
40 import traceback
41
42 from charmhelpers.core.hookenv import (
43     action_get,
44     action_fail,
45     action_set,
46     config,
47     status_set,
48 )
49
50 import charms.libansible
51
52 from charms.reactive import (
53     remove_state as remove_flag,
54     set_state as set_flag,
55     when,
56 )
57
58
59 # Sets the status of the charm to show in OSM: configured
60 @when('config.changed')
61 def config_changed():
62     set_flag('{{ charm_name }}.configured')
63     status_set('active', 'ready!')
64     return
65
66
67 # Edits ansible config files and executes ansible-playbook
68 {% for pb in playbooks -%}
69 @when('{{ charm_name }}.configured')
70 @when('actions.{{ pb.action_name }}')
71 def {{ pb.function_name }}():
72     try:
73         dict_vars = {}
74
75         # edit the following snippet to add your parameters
76         # Note: don't forget to edit actions.yaml to add the parameters
77         # param_variable = action_get('param-name')
78         # dict_vars['param-name'] = param_variable}
79
80         result = charms.libansible.execute_playbook('{{ pb.file }}', dict_vars)
81     except:
82         exc_type, exc_value, exc_traceback = sys.exc_info()
83         err = traceback.format_exception(exc_type, exc_value, exc_traceback)
84         action_fail('{{ pb.action_name }} failed: ' + str(err))
85     else:
86         action_set({'output': result})
87     finally:
88         remove_flag('actions.{{ pb.action_name }}')
89
90
91 {% endfor -%}