Merge "docker: new hybrid deployment architecture and configuration"
[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:
76     config_volume = service[0] or ''
77     puppet_tags = service[1] or ''
78     manifest = service[2] or ''
79     config_image = service[3] or ''
80     volumes = service[4] if len(service) > 4 else []
81
82     print('---------')
83     print('config_volume %s' % config_volume)
84     print('puppet_tags %s' % puppet_tags)
85     print('manifest %s' % manifest)
86     print('config_image %s' % config_image)
87     print('volumes %s' % volumes)
88     # We key off of config volume for all configs.
89     if config_volume in configs:
90         # Append puppet tags and manifest.
91         print("Existing service, appending puppet tags and manifest\n")
92         if puppet_tags:
93             configs[config_volume][1] = '%s,%s' % (configs[config_volume][1],
94                                                    puppet_tags)
95         if manifest:
96             configs[config_volume][2] = '%s\n%s' % (configs[config_volume][2],
97                                                     manifest)
98         if configs[config_volume][3] != config_image:
99             print("WARNING: Config containers do not match even though"
100                   " shared volumes are the same!\n")
101     else:
102         print("Adding new service\n")
103         configs[config_volume] = service
104
105 print('Service compilation completed.\n')
106
107 for config_volume in configs:
108
109     service = configs[config_volume]
110     puppet_tags = service[1] or ''
111     manifest = service[2] or ''
112     config_image = service[3] or ''
113     volumes = service[4] if len(service) > 4 else []
114
115     if puppet_tags:
116         puppet_tags = "file,file_line,concat,%s" % puppet_tags
117     else:
118         puppet_tags = "file,file_line,concat"
119
120     print('---------')
121     print('config_volume %s' % config_volume)
122     print('puppet_tags %s' % puppet_tags)
123     print('manifest %s' % manifest)
124     print('config_image %s' % config_image)
125     hostname = short_hostname()
126
127     with open('/var/lib/docker-puppet/docker-puppet.sh', 'w') as script_file:
128         os.chmod(script_file.name, 0755)
129         script_file.write("""#!/bin/bash
130         set -ex
131         mkdir -p /etc/puppet
132         cp -a /tmp/puppet-etc/* /etc/puppet
133         rm -Rf /etc/puppet/ssl # not in use and causes permission errors
134         echo '{"step": 6}' > /etc/puppet/hieradata/docker.json
135         TAGS=""
136         if [ -n "%(puppet_tags)s" ]; then
137             TAGS='--tags "%(puppet_tags)s"'
138         fi
139         FACTER_hostname=%(hostname)s FACTER_uuid=docker /usr/bin/puppet apply --verbose $TAGS /etc/config.pp
140
141         # Disables archiving
142         if [ -z "%(no_archive)s" ]; then
143             rm -Rf /var/lib/config-data/%(name)s
144
145             # copying etc should be enough for most services
146             mkdir -p /var/lib/config-data/%(name)s/etc
147             cp -a /etc/* /var/lib/config-data/%(name)s/etc/
148
149             if [ -d /root/ ]; then
150               cp -a /root/ /var/lib/config-data/%(name)s/root/
151             fi
152             if [ -d /var/lib/ironic/tftpboot/ ]; then
153               mkdir -p /var/lib/config-data/%(name)s/var/lib/ironic/
154               cp -a /var/lib/ironic/tftpboot/ /var/lib/config-data/%(name)s/var/lib/ironic/tftpboot/
155             fi
156             if [ -d /var/lib/ironic/httpboot/ ]; then
157               mkdir -p /var/lib/config-data/%(name)s/var/lib/ironic/
158               cp -a /var/lib/ironic/httpboot/ /var/lib/config-data/%(name)s/var/lib/ironic/httpboot/
159             fi
160
161             # apache services may files placed in /var/www/
162             if [ -d /var/www/ ]; then
163              mkdir -p /var/lib/config-data/%(name)s/var/www
164              cp -a /var/www/* /var/lib/config-data/%(name)s/var/www/
165             fi
166         fi
167         """ % {'puppet_tags': puppet_tags, 'name': config_volume,
168                'hostname': hostname,
169                'no_archive': os.environ.get('NO_ARCHIVE', '')})
170
171     with tempfile.NamedTemporaryFile() as tmp_man:
172         with open(tmp_man.name, 'w') as man_file:
173             man_file.write('include ::tripleo::packages\n')
174             man_file.write(manifest)
175
176         rm_container('docker-puppet-%s' % config_volume)
177         pull_image(config_image)
178
179         dcmd = ['/usr/bin/docker', 'run',
180                 '--user', 'root',
181                 '--name', 'docker-puppet-%s' % config_volume,
182                 '--volume', '%s:/etc/config.pp:ro' % tmp_man.name,
183                 '--volume', '/etc/puppet/:/tmp/puppet-etc/:ro',
184                 '--volume', '/usr/share/openstack-puppet/modules/:/usr/share/openstack-puppet/modules/:ro',
185                 '--volume', '/var/lib/config-data/:/var/lib/config-data/:rw',
186                 '--volume', 'tripleo_logs:/var/log/tripleo/',
187                 '--volume', '/var/lib/docker-puppet/docker-puppet.sh:/var/lib/docker-puppet/docker-puppet.sh:ro']
188
189         for volume in volumes:
190             dcmd.extend(['--volume', volume])
191
192         dcmd.extend(['--entrypoint', '/var/lib/docker-puppet/docker-puppet.sh'])
193
194         env = {}
195         if os.environ.get('NET_HOST', 'false') == 'true':
196             print('NET_HOST enabled')
197             dcmd.extend(['--net', 'host', '--volume',
198                          '/etc/hosts:/etc/hosts:ro'])
199         dcmd.append(config_image)
200
201         subproc = subprocess.Popen(dcmd, stdout=subprocess.PIPE,
202                                    stderr=subprocess.PIPE, env=env)
203         cmd_stdout, cmd_stderr = subproc.communicate()
204         print(cmd_stdout)
205         print(cmd_stderr)
206         if subproc.returncode != 0:
207             print('Failed running docker-puppet.py for %s' % config_volume)
208             sys.exit(subproc.returncode)
209         else:
210             rm_container('docker-puppet-%s' % config_volume)