1d6165e4bc0d7c9a0d5036bf72349b11ae2967c8
[osm/devops.git] / descriptor-packages / tools / charm-generator / generator / ansible-charm / templates / ansible_lib.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 import fnmatch
39 import os
40 import yaml
41 import subprocess
42
43 from charmhelpers.core.hookenv import config
44
45
46 def create_hosts(cfg, hosts):
47     inventory_path = '/etc/ansible/hosts'
48
49     with open(inventory_path, 'w') as f:
50         f.write('[{}]\n'.format(hosts))
51         h1 = '{0} ansible_connection=ssh ansible_ssh_user={1} ansible_ssh_pass={2} ' \
52              'ansible_ssh_private_key_file=~/.ssh/id_juju_sshproxy ' \
53              'ansible_python_interpreter=/usr/bin/python\n'.format(cfg['ssh-hostname'], cfg['ssh-username'],
54                                                                     cfg['ssh-password'])
55         f.write(h1)
56
57
58 def create_ansible_cfg():
59     ansible_config_path = '/etc/ansible/ansible.cfg'
60
61     with open(ansible_config_path, 'w') as f:
62         f.write('[defaults]\n')
63         f.write('host_key_checking = False\n')
64         # logs playbook execution attempts to the specified path
65         f.write('log_path = /var/log/ansible.log\n')
66
67         f.write('[ssh_connection]\n')
68         f.write('control_path=%(directory)s/%%h-%%r\n')
69         f.write('control_path_dir=~/.ansible/cp\n')
70
71
72 # Function to find the playbook path
73 def find(pattern, path):
74     result = ''
75     for root, dirs, files in os.walk(path):
76         for name in files:
77             if fnmatch.fnmatch(name, pattern):
78                 result = os.path.join(root, name)
79     return result
80
81
82 def execute_playbook(playbook_file, vars_dict=None):
83     playbook_path = find(playbook_file, '/var/lib/juju/agents/')
84
85     cfg = config()
86
87     with open(playbook_path, 'r') as f:
88         playbook_data = yaml.load(f)
89
90     hosts = 'all'
91     if 'hosts' in playbook_data[0].keys() and playbook_data[0]['hosts']:
92         hosts = playbook_data[0]['hosts']
93
94     create_ansible_cfg()
95     create_hosts(cfg, hosts)
96
97     call = 'ansible-playbook %s ' % playbook_path
98
99     if vars_dict and isinstance(vars_dict, dict) and len(vars_dict) > 0:
100         call += '--extra-vars '
101
102         string_var = ''
103         for v in vars_dict.items():
104             string_var += '%s=%s ' % v
105
106         string_var = string_var.strip()
107         call += '"%s"' % string_var
108
109     call = call.strip()
110     result = subprocess.check_output(call, shell=True)
111
112     return result