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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
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.
28 # this is to match what we do in deployed-server
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()
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()
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()
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)
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.
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.
75 for service in (json_data or []):
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 []
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")
95 configs[config_volume][1] = '%s,%s' % (configs[config_volume][1],
98 configs[config_volume][2] = '%s\n%s' % (configs[config_volume][2],
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")
104 print("Adding new service\n")
105 configs[config_volume] = service
107 print('Service compilation completed.\n')
109 for config_volume in configs:
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 []
118 puppet_tags = "file,file_line,concat,%s" % puppet_tags
120 puppet_tags = "file,file_line,concat"
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()
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
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
138 if [ -n "%(puppet_tags)s" ]; then
139 TAGS='--tags "%(puppet_tags)s"'
141 FACTER_hostname=%(hostname)s FACTER_uuid=docker /usr/bin/puppet apply --verbose $TAGS /etc/config.pp
144 if [ -z "%(no_archive)s" ]; then
145 rm -Rf /var/lib/config-data/%(name)s
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/
151 if [ -d /root/ ]; then
152 cp -a /root/ /var/lib/config-data/%(name)s/root/
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/
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/
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/
169 """ % {'puppet_tags': puppet_tags, 'name': config_volume,
170 'hostname': hostname,
171 'no_archive': os.environ.get('NO_ARCHIVE', '')})
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)
178 rm_container('docker-puppet-%s' % config_volume)
179 pull_image(config_image)
181 dcmd = ['/usr/bin/docker', 'run',
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']
191 for volume in volumes:
192 dcmd.extend(['--volume', volume])
194 dcmd.extend(['--entrypoint', '/var/lib/docker-puppet/docker-puppet.sh'])
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)
203 subproc = subprocess.Popen(dcmd, stdout=subprocess.PIPE,
204 stderr=subprocess.PIPE, env=env)
205 cmd_stdout, cmd_stderr = subproc.communicate()
208 if subproc.returncode != 0:
209 print('Failed running docker-puppet.py for %s' % config_volume)
210 sys.exit(subproc.returncode)
212 rm_container('docker-puppet-%s' % config_volume)