Browse Source

Dynamic inventory backup corruption fix

When multiple users and process are accessing the dynamic inventory
the inventory backup and json can get corrupted. This change checks
for inventory modifictions and only saves if needed. The backup
is also moved to right before the actual save.

Change-Id: Ifd348ddd9c21526f5b523963dd1fd247edd6b109
Closes-Bug: #1750233
tags/18.1.5
Shannon Mitchell 4 months ago
parent
commit
87a4b7b5dd
2 changed files with 12 additions and 3 deletions
  1. 4
    1
      osa_toolkit/filesystem.py
  2. 8
    2
      osa_toolkit/generate.py

+ 4
- 1
osa_toolkit/filesystem.py View File

@@ -245,7 +245,6 @@ def load_inventory(preferred_path=None, default_inv=None, filename=None):
245 245
 
246 246
     if inventory is not False:
247 247
         logger.debug("Loaded existing inventory from {}".format(file_loaded))
248
-        _make_backup(load_path, file_loaded)
249 248
     else:
250 249
         logger.debug("No existing inventory, created fresh skeleton.")
251 250
         inventory = copy.deepcopy(default_inv)
@@ -264,6 +263,10 @@ def save_inventory(inventory_json, save_path):
264 263
         inventory_file = file_find(save_path)
265 264
     else:
266 265
         inventory_file = os.path.join(save_path, INVENTORY_FILENAME)
266
+
267
+    if os.path.isfile(inventory_file):
268
+        _make_backup(save_path, inventory_file)
269
+
267 270
     with open(inventory_file, 'wb') as f:
268 271
         f.write(inventory_json.encode('ascii'))
269 272
         logger.info("Inventory written")

+ 8
- 2
osa_toolkit/generate.py View File

@@ -15,6 +15,7 @@
15 15
 #
16 16
 # (c) 2014, Kevin Carter <kevin.carter@rackspace.com>
17 17
 
18
+import copy
18 19
 import json
19 20
 import logging
20 21
 import netaddr
@@ -1096,6 +1097,9 @@ def main(config=None, check=False, debug=False, environment=None, **kwargs):
1096 1097
     # Load existing inventory file if found
1097 1098
     inventory, inv_path = filesys.load_inventory(config, INVENTORY_SKEL)
1098 1099
 
1100
+    # Make a deep copy for change comparison
1101
+    orig_inventory = copy.deepcopy(inventory)
1102
+
1099 1103
     # Save the users container cidr as a group variable
1100 1104
     cidr_networks = user_defined_config.get('cidr_networks')
1101 1105
     if not cidr_networks:
@@ -1186,7 +1190,9 @@ def main(config=None, check=False, debug=False, environment=None, **kwargs):
1186 1190
         num_hosts = len(inventory['_meta']['hostvars'])
1187 1191
         logger.debug("%d hosts found.", num_hosts)
1188 1192
 
1189
-    # Save new dynamic inventory
1190
-    filesys.save_inventory(inventory_json, inv_path)
1193
+    # Save new dynamic inventory only if modified
1194
+    if orig_inventory != inventory:
1195
+        logger.debug("Saving modified inventory")
1196
+        filesys.save_inventory(inventory_json, inv_path)
1191 1197
 
1192 1198
     return inventory_json

Loading…
Cancel
Save