Ensure tripleo ansible inventory file update is atomic

Multiple python-tripleoclient commands can run concurrently
and all attempt to update the same inventory file in
~/tripleo-ansible-inventory.yaml. Make the update atomic to
ensure the inventory file is always complete/valid.

(cherry picked from commit 8e082f45dd)

(squashing commits as the 1st patch is failing in the stein gate without
the fix from the 2nd patch)

Ensure atomic inventory file rename runs on the same mountpoint

Linux rename system call is only supported on the same mountpoint [1].
/tmp is often a tmpfs mount, so instead generate the temp inventory file in
the same directory as the target inventory file.

[1] See EXDEV in https://man7.org/linux/man-pages/man2/rename.2.html

Closes-bug: #1892008

Change-Id: Ifa41bfcb921496978f82aee4e67fdb419cf9ffc5
(cherry picked from commit 77a0c827cb)
This commit is contained in:
Oliver Walsh 2020-08-07 16:15:43 +01:00 committed by Daniel Bengtsson
parent face5a95a1
commit 8afc5d23cd
1 changed files with 9 additions and 2 deletions

View File

@ -16,7 +16,8 @@
# under the License.
from collections import OrderedDict
import os.path
import os
import tempfile
import yaml
from heatclient.exc import HTTPNotFound
@ -313,5 +314,11 @@ class TripleoInventory(object):
if var in inventory:
inventory[var]['vars'].update(value)
with open(inventory_file_path, 'w') as inventory_file:
# Atomic update as concurrent tripleoclient commands can call this
inventory_file_dir = os.path.dirname(inventory_file_path)
with tempfile.NamedTemporaryFile(
'w',
dir=inventory_file_dir,
delete=False) as inventory_file:
yaml.dump(inventory, inventory_file, TemplateDumper)
os.rename(inventory_file.name, inventory_file_path)