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:
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 []
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")
93 configs[config_volume][1] = '%s,%s' % (configs[config_volume][1],
96 configs[config_volume][2] = '%s\n%s' % (configs[config_volume][2],
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")
102 print("Adding new service\n")
103 configs[config_volume] = service
105 print('Service compilation completed.\n')
107 for config_volume in configs:
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 []
116 puppet_tags = "file,file_line,concat,%s" % puppet_tags
118 puppet_tags = "file,file_line,concat"
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()
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
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
136 if [ -n "%(puppet_tags)s" ]; then
137 TAGS='--tags "%(puppet_tags)s"'
139 FACTER_hostname=%(hostname)s FACTER_uuid=docker /usr/bin/puppet apply --verbose $TAGS /etc/config.pp
142 if [ -z "%(no_archive)s" ]; then
143 rm -Rf /var/lib/config-data/%(name)s
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/
149 if [ -d /root/ ]; then
150 cp -a /root/ /var/lib/config-data/%(name)s/root/
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/
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/
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/
167 """ % {'puppet_tags': puppet_tags, 'name': config_volume,
168 'hostname': hostname,
169 'no_archive': os.environ.get('NO_ARCHIVE', '')})
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)
176 rm_container('docker-puppet-%s' % config_volume)
177 pull_image(config_image)
179 dcmd = ['/usr/bin/docker', 'run',
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']
189 for volume in volumes:
190 dcmd.extend(['--volume', volume])
192 dcmd.extend(['--entrypoint', '/var/lib/docker-puppet/docker-puppet.sh'])
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)
201 subproc = subprocess.Popen(dcmd, stdout=subprocess.PIPE,
202 stderr=subprocess.PIPE, env=env)
203 cmd_stdout, cmd_stderr = subproc.communicate()
206 if subproc.returncode != 0:
207 print('Failed running docker-puppet.py for %s' % config_volume)
208 sys.exit(subproc.returncode)
210 rm_container('docker-puppet-%s' % config_volume)