Fix hiera deployment JSON generation

We were hitting strange intermittent errors in TripleO where the hiera
JSON file generated by Heat was not even valid JSON, ending with bits of
text which seem out of place, and of varying length, e.g.

"CREATE"}"}

or

"CREATE"}EATE"}

The cause could be that the generation of the file runs multiple times,
and if the first generated JSON is longer than the subsequent one, the
ending of the previous JSON is still present in the file, because the
generation opens the file with O_CREAT (create if not exists) and
O_WRONLY (write only) flags, but not with O_TRUNC (truncate file before
writing). This patch adds the truncation to fix the issue.

Change-Id: Icf184f973decec2eb7de1dee9959b60774d83eb6
Closes-Bug: #1434187
This commit is contained in:
Jiri Stransky 2015-03-27 13:31:10 +01:00
parent 8bfade8908
commit f00cf8fbb8
1 changed files with 4 additions and 2 deletions

View File

@ -66,7 +66,8 @@ def main(argv=sys.argv):
prepare_dir(HIERA_DATADIR)
hiera_data = os.path.join(HIERA_DATADIR,
'heat_config_%s.json' % c['name'])
with os.fdopen(os.open(hiera_data, os.O_CREAT | os.O_WRONLY, 0o600),
with os.fdopen(os.open(hiera_data,
os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0o600),
'w') as hiera_file:
hiera_file.write(json.dumps(hiera).encode('utf8'))
facts['FACTER_deploy_config_name'] = c['name']
@ -80,7 +81,8 @@ def main(argv=sys.argv):
env = os.environ.copy()
env.update(facts)
with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o700), 'w') as f:
with os.fdopen(os.open(fn, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0o700),
'w') as f:
f.write(c.get('config', '').encode('utf-8'))
cmd = [PUPPET_CMD, 'apply', '--detailed-exitcodes', fn]