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/19.0.0.0b1
Shannon Mitchell 3 months ago
parent
commit
71a067abf2
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
@@ -1109,6 +1110,9 @@ def main(config=None, check=False, debug=False, environment=None, **kwargs):
1109 1110
     # Load existing inventory file if found
1110 1111
     inventory, inv_path = filesys.load_inventory(config, INVENTORY_SKEL)
1111 1112
 
1113
+    # Make a deep copy for change comparison
1114
+    orig_inventory = copy.deepcopy(inventory)
1115
+
1112 1116
     # Save the users container cidr as a group variable
1113 1117
     cidr_networks = user_defined_config.get('cidr_networks')
1114 1118
     if not cidr_networks:
@@ -1199,7 +1203,9 @@ def main(config=None, check=False, debug=False, environment=None, **kwargs):
1199 1203
         num_hosts = len(inventory['_meta']['hostvars'])
1200 1204
         logger.debug("%d hosts found.", num_hosts)
1201 1205
 
1202
-    # Save new dynamic inventory
1203
-    filesys.save_inventory(inventory_json, inv_path)
1206
+    # Save new dynamic inventory only if modified
1207
+    if orig_inventory != inventory:
1208
+        logger.debug("Saving modified inventory")
1209
+        filesys.save_inventory(inventory_json, inv_path)
1204 1210
 
1205 1211
     return inventory_json

Loading…
Cancel
Save