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.
26 import multiprocessing
29 # this is to match what we do in deployed-server
31 subproc = subprocess.Popen(['hostname', '-s'],
32 stdout=subprocess.PIPE,
33 stderr=subprocess.PIPE)
34 cmd_stdout, cmd_stderr = subproc.communicate()
35 return cmd_stdout.rstrip()
39 print('Pulling image: %s' % name)
40 subproc = subprocess.Popen(['/usr/bin/docker', 'pull', name],
41 stdout=subprocess.PIPE,
42 stderr=subprocess.PIPE)
43 cmd_stdout, cmd_stderr = subproc.communicate()
48 def rm_container(name):
49 if os.environ.get('SHOW_DIFF', None):
50 print('Diffing container: %s' % name)
51 subproc = subprocess.Popen(['/usr/bin/docker', 'diff', name],
52 stdout=subprocess.PIPE,
53 stderr=subprocess.PIPE)
54 cmd_stdout, cmd_stderr = subproc.communicate()
58 print('Removing container: %s' % name)
59 subproc = subprocess.Popen(['/usr/bin/docker', 'rm', name],
60 stdout=subprocess.PIPE,
61 stderr=subprocess.PIPE)
62 cmd_stdout, cmd_stderr = subproc.communicate()
66 process_count = int(os.environ.get('PROCESS_COUNT',
67 multiprocessing.cpu_count()))
69 config_file = os.environ.get('CONFIG', '/var/lib/docker-puppet/docker-puppet.json')
70 print('docker-puppet')
71 print('CONFIG: %s' % config_file)
72 with open(config_file) as f:
73 json_data = json.load(f)
75 # To save time we support configuring 'shared' services at the same
76 # time. For example configuring all of the heat services
77 # in a single container pass makes sense and will save some time.
78 # To support this we merge shared settings together here.
80 # We key off of config_volume as this should be the same for a
81 # given group of services. We are also now specifying the container
82 # in which the services should be configured. This should match
83 # in all instances where the volume name is also the same.
87 for service in (json_data or []):
90 if isinstance(service, dict):
92 service.get('config_volume'),
93 service.get('puppet_tags'),
94 service.get('step_config'),
95 service.get('config_image'),
96 service.get('volumes', []),
99 config_volume = service[0] or ''
100 puppet_tags = service[1] or ''
101 manifest = service[2] or ''
102 config_image = service[3] or ''
103 volumes = service[4] if len(service) > 4 else []
106 print('config_volume %s' % config_volume)
107 print('puppet_tags %s' % puppet_tags)
108 print('manifest %s' % manifest)
109 print('config_image %s' % config_image)
110 print('volumes %s' % volumes)
111 # We key off of config volume for all configs.
112 if config_volume in configs:
113 # Append puppet tags and manifest.
114 print("Existing service, appending puppet tags and manifest\n")
116 configs[config_volume][1] = '%s,%s' % (configs[config_volume][1],
119 configs[config_volume][2] = '%s\n%s' % (configs[config_volume][2],
121 if configs[config_volume][3] != config_image:
122 print("WARNING: Config containers do not match even though"
123 " shared volumes are the same!\n")
125 print("Adding new service\n")
126 configs[config_volume] = service
128 print('Service compilation completed.\n')
130 def mp_puppet_config((config_volume, puppet_tags, manifest, config_image, volumes)):
133 print('config_volume %s' % config_volume)
134 print('puppet_tags %s' % puppet_tags)
135 print('manifest %s' % manifest)
136 print('config_image %s' % config_image)
137 print('volumes %s' % volumes)
138 hostname = short_hostname()
139 sh_script = '/var/lib/docker-puppet/docker-puppet-%s.sh' % config_volume
141 with open(sh_script, 'w') as script_file:
142 os.chmod(script_file.name, 0755)
143 script_file.write("""#!/bin/bash
146 cp -a /tmp/puppet-etc/* /etc/puppet
147 rm -Rf /etc/puppet/ssl # not in use and causes permission errors
148 echo '{"step": %(step)s}' > /etc/puppet/hieradata/docker.json
150 if [ -n "%(puppet_tags)s" ]; then
151 TAGS='--tags "%(puppet_tags)s"'
153 FACTER_hostname=%(hostname)s FACTER_uuid=docker /usr/bin/puppet apply --verbose $TAGS /etc/config.pp
156 if [ -z "%(no_archive)s" ]; then
157 rm -Rf /var/lib/config-data/%(name)s
159 # copying etc should be enough for most services
160 mkdir -p /var/lib/config-data/%(name)s/etc
161 cp -a /etc/* /var/lib/config-data/%(name)s/etc/
163 if [ -d /root/ ]; then
164 cp -a /root/ /var/lib/config-data/%(name)s/root/
166 if [ -d /var/lib/ironic/tftpboot/ ]; then
167 mkdir -p /var/lib/config-data/%(name)s/var/lib/ironic/
168 cp -a /var/lib/ironic/tftpboot/ /var/lib/config-data/%(name)s/var/lib/ironic/tftpboot/
170 if [ -d /var/lib/ironic/httpboot/ ]; then
171 mkdir -p /var/lib/config-data/%(name)s/var/lib/ironic/
172 cp -a /var/lib/ironic/httpboot/ /var/lib/config-data/%(name)s/var/lib/ironic/httpboot/
175 # apache services may files placed in /var/www/
176 if [ -d /var/www/ ]; then
177 mkdir -p /var/lib/config-data/%(name)s/var/www
178 cp -a /var/www/* /var/lib/config-data/%(name)s/var/www/
181 """ % {'puppet_tags': puppet_tags, 'name': config_volume,
182 'hostname': hostname,
183 'no_archive': os.environ.get('NO_ARCHIVE', ''),
184 'step': os.environ.get('STEP', '6')})
186 with tempfile.NamedTemporaryFile() as tmp_man:
187 with open(tmp_man.name, 'w') as man_file:
188 man_file.write('include ::tripleo::packages\n')
189 man_file.write(manifest)
191 rm_container('docker-puppet-%s' % config_volume)
192 pull_image(config_image)
194 dcmd = ['/usr/bin/docker', 'run',
196 '--name', 'docker-puppet-%s' % config_volume,
197 '--volume', '%s:/etc/config.pp:ro' % tmp_man.name,
198 '--volume', '/etc/puppet/:/tmp/puppet-etc/:ro',
199 '--volume', '/usr/share/openstack-puppet/modules/:/usr/share/openstack-puppet/modules/:ro',
200 '--volume', '/var/lib/config-data/:/var/lib/config-data/:rw',
201 '--volume', 'tripleo_logs:/var/log/tripleo/',
202 '--volume', '%s:%s:rw' % (sh_script, sh_script) ]
204 for volume in volumes:
205 dcmd.extend(['--volume', volume])
207 dcmd.extend(['--entrypoint', sh_script])
210 if os.environ.get('NET_HOST', 'false') == 'true':
211 print('NET_HOST enabled')
212 dcmd.extend(['--net', 'host', '--volume',
213 '/etc/hosts:/etc/hosts:ro'])
214 dcmd.append(config_image)
216 subproc = subprocess.Popen(dcmd, stdout=subprocess.PIPE,
217 stderr=subprocess.PIPE, env=env)
218 cmd_stdout, cmd_stderr = subproc.communicate()
221 if subproc.returncode != 0:
222 print('Failed running docker-puppet.py for %s' % config_volume)
223 rm_container('docker-puppet-%s' % config_volume)
224 return subproc.returncode
226 # Holds all the information for each process to consume.
227 # Instead of starting them all linearly we run them using a process
228 # pool. This creates a list of arguments for the above function
232 for config_volume in configs:
234 service = configs[config_volume]
235 puppet_tags = service[1] or ''
236 manifest = service[2] or ''
237 config_image = service[3] or ''
238 volumes = service[4] if len(service) > 4 else []
241 puppet_tags = "file,file_line,concat,%s" % puppet_tags
243 puppet_tags = "file,file_line,concat"
245 process_map.append([config_volume, puppet_tags, manifest, config_image, volumes])
247 for p in process_map:
250 # Fire off processes to perform each configuration. Defaults
251 # to the number of CPUs on the system.
252 p = multiprocessing.Pool(process_count)
253 p.map(mp_puppet_config, process_map)