Merge "Add docker keystone service"
[apex-tripleo-heat-templates.git] / docker / docker-puppet.py
1 #!/usr/bin/env python
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #    Unless required by applicable law or agreed to in writing, software
10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14
15 # Shell script tool to run puppet inside of the given docker container image.
16 # Uses the config file at /var/lib/docker-puppet/docker-puppet.json as a source for a JSON
17 # array of [config_volume, puppet_tags, manifest, config_image, [volumes]] settings
18 # that can be used to generate config files or run ad-hoc puppet modules
19 # inside of a container.
20
21 import json
22 import os
23 import subprocess
24 import sys
25 import tempfile
26
27
28 # this is to match what we do in deployed-server
29 def short_hostname():
30     subproc = subprocess.Popen(['hostname', '-s'],
31                                stdout=subprocess.PIPE,
32                                stderr=subprocess.PIPE)
33     cmd_stdout, cmd_stderr = subproc.communicate()
34     return cmd_stdout.rstrip()
35
36
37 def pull_image(name):
38     print('Pulling image: %s' % name)
39     subproc = subprocess.Popen(['/usr/bin/docker', 'pull', name],
40                                stdout=subprocess.PIPE,
41                                stderr=subprocess.PIPE)
42     cmd_stdout, cmd_stderr = subproc.communicate()
43     print(cmd_stdout)
44     print(cmd_stderr)
45
46
47 def rm_container(name):
48     print('Removing container: %s' % name)
49     subproc = subprocess.Popen(['/usr/bin/docker', 'rm', name],
50                                stdout=subprocess.PIPE,
51                                stderr=subprocess.PIPE)
52     cmd_stdout, cmd_stderr = subproc.communicate()
53     print(cmd_stdout)
54     print(cmd_stderr)
55
56
57 config_file = os.environ.get('CONFIG', '/var/lib/docker-puppet/docker-puppet.json')
58 print('docker-puppet')
59 print('CONFIG: %s' % config_file)
60 with open(config_file) as f:
61     json_data = json.load(f)
62
63 # To save time we support configuring 'shared' services at the same
64 # time. For example configuring all of the heat services
65 # in a single container pass makes sense and will save some time.
66 # To support this we merge shared settings together here.
67 #
68 # We key off of config_volume as this should be the same for a
69 # given group of services.  We are also now specifying the container
70 # in which the services should be configured.  This should match
71 # in all instances where the volume name is also the same.
72
73 configs = {}
74
75 for service in (json_data or []):
76     if service is None:
77         continue
78     config_volume = service[0] or ''
79     puppet_tags = service[1] or ''
80     manifest = service[2] or ''
81     config_image = service[3] or ''
82     volumes = service[4] if len(service) > 4 else []
83
84     print('---------')
85     print('config_volume %s' % config_volume)
86     print('puppet_tags %s' % puppet_tags)
87     print('manifest %s' % manifest)
88     print('config_image %s' % config_image)
89     print('volumes %s' % volumes)
90     # We key off of config volume for all configs.
91     if config_volume in configs:
92         # Append puppet tags and manifest.
93         print("Existing service, appending puppet tags and manifest\n")
94         if puppet_tags:
95             configs[config_volume][1] = '%s,%s' % (configs[config_volume][1],
96                                                    puppet_tags)
97         if manifest:
98             configs[config_volume][2] = '%s\n%s' % (configs[config_volume][2],
99                                                     manifest)
100         if configs[config_volume][3] != config_image:
101             print("WARNING: Config containers do not match even though"
102                   " shared volumes are the same!\n")
103     else:
104         print("Adding new service\n")
105         configs[config_volume] = service
106
107 print('Service compilation completed.\n')
108
109 for config_volume in configs:
110
111     service = configs[config_volume]
112     puppet_tags = service[1] or ''
113     manifest = service[2] or ''
114     config_image = service[3] or ''
115     volumes = service[4] if len(service) > 4 else []
116
117     if puppet_tags:
118         puppet_tags = "file,file_line,concat,%s" % puppet_tags
119     else:
120         puppet_tags = "file,file_line,concat"
121
122     print('---------')
123     print('config_volume %s' % config_volume)
124     print('puppet_tags %s' % puppet_tags)
125     print('manifest %s' % manifest)
126     print('config_image %s' % config_image)
127     hostname = short_hostname()
128
129     with open('/var/lib/docker-puppet/docker-puppet.sh', 'w') as script_file:
130         os.chmod(script_file.name, 0755)
131         script_file.write("""#!/bin/bash
132         set -ex
133         mkdir -p /etc/puppet
134         cp -a /tmp/puppet-etc/* /etc/puppet
135         rm -Rf /etc/puppet/ssl # not in use and causes permission errors
136         echo '{"step": 6}' > /etc/puppet/hieradata/docker.json
137         TAGS=""
138         if [ -n "%(puppet_tags)s" ]; then
139             TAGS='--tags "%(puppet_tags)s"'
140         fi
141         FACTER_hostname=%(hostname)s FACTER_uuid=docker /usr/bin/puppet apply --verbose $TAGS /etc/config.pp
142
143         # Disables archiving
144         if [ -z "%(no_archive)s" ]; then
145             rm -Rf /var/lib/config-data/%(name)s
146
147             # copying etc should be enough for most services
148             mkdir -p /var/lib/config-data/%(name)s/etc
149             cp -a /etc/* /var/lib/config-data/%(name)s/etc/
150
151             if [ -d /root/ ]; then
152               cp -a /root/ /var/lib/config-data/%(name)s/root/
153             fi
154             if [ -d /var/lib/ironic/tftpboot/ ]; then
155               mkdir -p /var/lib/config-data/%(name)s/var/lib/ironic/
156               cp -a /var/lib/ironic/tftpboot/ /var/lib/config-data/%(name)s/var/lib/ironic/tftpboot/
157             fi
158             if [ -d /var/lib/ironic/httpboot/ ]; then
159               mkdir -p /var/lib/config-data/%(name)s/var/lib/ironic/
160               cp -a /var/lib/ironic/httpboot/ /var/lib/config-data/%(name)s/var/lib/ironic/httpboot/
161             fi
162
163             # apache services may files placed in /var/www/
164             if [ -d /var/www/ ]; then
165              mkdir -p /var/lib/config-data/%(name)s/var/www
166              cp -a /var/www/* /var/lib/config-data/%(name)s/var/www/
167             fi
168         fi
169         """ % {'puppet_tags': puppet_tags, 'name': config_volume,
170                'hostname': hostname,
171                'no_archive': os.environ.get('NO_ARCHIVE', '')})
172
173     with tempfile.NamedTemporaryFile() as tmp_man:
174         with open(tmp_man.name, 'w') as man_file:
175             man_file.write('include ::tripleo::packages\n')
176             man_file.write(manifest)
177
178         rm_container('docker-puppet-%s' % config_volume)
179         pull_image(config_image)
180
181         dcmd = ['/usr/bin/docker', 'run',
182                 '--user', 'root',
183                 '--name', 'docker-puppet-%s' % config_volume,
184                 '--volume', '%s:/etc/config.pp:ro' % tmp_man.name,
185                 '--volume', '/etc/puppet/:/tmp/puppet-etc/:ro',
186                 '--volume', '/usr/share/openstack-puppet/modules/:/usr/share/openstack-puppet/modules/:ro',
187                 '--volume', '/var/lib/config-data/:/var/lib/config-data/:rw',
188                 '--volume', 'tripleo_logs:/var/log/tripleo/',
189                 '--volume', '/var/lib/docker-puppet/docker-puppet.sh:/var/lib/docker-puppet/docker-puppet.sh:ro']
190
191         for volume in volumes:
192             dcmd.extend(['--volume', volume])
193
194         dcmd.extend(['--entrypoint', '/var/lib/docker-puppet/docker-puppet.sh'])
195
196         env = {}
197         if os.environ.get('NET_HOST', 'false') == 'true':
198             print('NET_HOST enabled')
199             dcmd.extend(['--net', 'host', '--volume',
200                          '/etc/hosts:/etc/hosts:ro'])
201         dcmd.append(config_image)
202
203         subproc = subprocess.Popen(dcmd, stdout=subprocess.PIPE,
204                                    stderr=subprocess.PIPE, env=env)
205         cmd_stdout, cmd_stderr = subproc.communicate()
206         print(cmd_stdout)
207         print(cmd_stderr)
208         if subproc.returncode != 0:
209             print('Failed running docker-puppet.py for %s' % config_volume)
210             sys.exit(subproc.returncode)
211         else:
212             rm_container('docker-puppet-%s' % config_volume)