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
(cherry picked from commit 662b0acd91)

Change-Id: Ifa41bfcb921496978f82aee4e67fdb419cf9ffc5
This commit is contained in:
Oliver Walsh 2020-08-07 16:15:43 +01:00
parent eab3de0cfc
commit c1af9b7ad9
1 changed files with 9 additions and 2 deletions

View File

@ -16,8 +16,9 @@
# under the License. # under the License.
from collections import OrderedDict from collections import OrderedDict
import os.path import os
import sys import sys
import tempfile
import yaml import yaml
from heatclient.exc import HTTPNotFound from heatclient.exc import HTTPNotFound
@ -356,5 +357,11 @@ class TripleoInventory(object):
if var in inventory: if var in inventory:
inventory[var]['vars'].update(value) 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) yaml.dump(inventory, inventory_file, TemplateDumper)
os.rename(inventory_file.name, inventory_file_path)