Write md5sum for service config directories

The configuration generated by docker-puppet may change on update,
so checksum the combined files from the config-data directories,
to enable detecting those that have changed and restarting the
appropriate containers - we need to merge this checksum into
the environment passed to the containters, as this will cause
paunch to correctly restart containers when the configuration
generated changes, even if the rest of the json definition
provided by heat does not.

Change-Id: I40d9080cf3ad708ef4ed91e46d2b2ae1138bb9c3
This commit is contained in:
Steven Hardy 2017-05-19 16:38:56 +01:00
parent 61fdeb67a0
commit e77de0d5ff
2 changed files with 59 additions and 3 deletions

View File

@ -23,12 +23,15 @@
##################################################
# Per step starting of the containers using paunch
##################################################
- name: Check if /var/lib/tripleo-config/docker-container-startup-config-step_{{step}}.json exists
- name: Check if /var/lib/hashed-tripleo-config/docker-container-startup-config-step_{{step}}.json exists
stat:
path: /var/lib/tripleo-config/docker-container-startup-config-step_{{step}}.json
path: /var/lib/tripleo-config/hashed-docker-container-startup-config-step_{{step}}.json
register: docker_config_json
# Note docker-puppet.py generates the hashed-*.json file, which is a copy of
# the *step_n.json with a hash of the generated external config added
# This acts as a salt to enable restarting the container if config changes
- name: Start containers for step {{step}}
command: paunch --debug apply --file /var/lib/tripleo-config/docker-container-startup-config-step_{{step}}.json --config-id tripleo_step{{step}} --managed-by tripleo-{{role_name}}
command: paunch --debug apply --file /var/lib/tripleo-config/hashed-docker-container-startup-config-step_{{step}}.json --config-id tripleo_step{{step}} --managed-by tripleo-{{role_name}}
when: docker_config_json.stat.exists
changed_when: false
check_mode: no

View File

@ -18,9 +18,11 @@
# that can be used to generate config files or run ad-hoc puppet modules
# inside of a container.
import glob
import json
import logging
import os
import sys
import subprocess
import sys
import tempfile
@ -55,6 +57,28 @@ def pull_image(name):
log.debug(cmd_stderr)
def match_config_volume(prefix, config):
# Match the mounted config volume - we can't just use the
# key as e.g "novacomute" consumes config-data/nova
volumes = config.get('volumes', [])
config_volume=None
for v in volumes:
if v.startswith(prefix):
config_volume = os.path.relpath(
v.split(":")[0], prefix).split("/")[0]
break
return config_volume
def get_config_hash(prefix, config_volume):
hashfile = os.path.join(prefix, "%s.md5sum" % config_volume)
hash_data = None
if os.path.isfile(hashfile):
with open(hashfile) as f:
hash_data = f.read().rstrip()
return hash_data
def rm_container(name):
if os.environ.get('SHOW_DIFF', None):
log.info('Diffing container: %s' % name)
@ -197,6 +221,10 @@ def mp_puppet_config((config_volume, puppet_tags, manifest, config_image, volume
mkdir -p /var/lib/config-data/${NAME}/var/www
cp -a /var/www/* /var/lib/config-data/${NAME}/var/www/
fi
# Write a checksum of the config-data dir, this is used as a
# salt to trigger container restart when the config changes
tar cf - /var/lib/config-data/${NAME} | md5sum | awk '{print $1}' > /var/lib/config-data/${NAME}.md5sum
fi
""")
@ -297,5 +325,30 @@ for returncode, config_volume in zip(returncodes, config_volumes):
log.error('ERROR configuring %s' % config_volume)
success = False
# Update the startup configs with the config hash we generated above
config_volume_prefix = os.environ.get('CONFIG_VOLUME_PREFIX', '/var/lib/config-data')
log.debug('CONFIG_VOLUME_PREFIX: %s' % config_volume_prefix)
startup_configs = os.environ.get('STARTUP_CONFIG_PATTERN', '/var/lib/tripleo-config/docker-container-startup-config-step_*.json')
log.debug('STARTUP_CONFIG_PATTERN: %s' % startup_configs)
infiles = glob.glob('/var/lib/tripleo-config/docker-container-startup-config-step_*.json')
for infile in infiles:
with open(infile) as f:
infile_data = json.load(f)
for k, v in infile_data.iteritems():
config_volume = match_config_volume(config_volume_prefix, v)
if config_volume:
config_hash = get_config_hash(config_volume_prefix, config_volume)
if config_hash:
env = v.get('environment', [])
env.append("TRIPLEO_CONFIG_HASH=%s" % config_hash)
log.debug("Updating config hash for %s, config_volume=%s hash=%s" % (k, config_volume, config_hash))
infile_data[k]['environment'] = env
outfile = os.path.join(os.path.dirname(infile), "hashed-" + os.path.basename(infile))
with open(outfile, 'w') as out_f:
json.dump(infile_data, out_f)
if not success:
sys.exit(1)