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/17.1.9
Shannon Mitchell 4 months ago
parent
commit
c7fbe594d0
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

@@ -244,7 +244,6 @@ def load_inventory(preferred_path=None, default_inv=None, filename=None):
244 244
 
245 245
     if inventory is not False:
246 246
         logger.debug("Loaded existing inventory from {}".format(file_loaded))
247
-        _make_backup(load_path, file_loaded)
248 247
     else:
249 248
         logger.debug("No existing inventory, created fresh skeleton.")
250 249
         inventory = copy.deepcopy(default_inv)
@@ -263,6 +262,10 @@ def save_inventory(inventory_json, save_path):
263 262
         inventory_file = file_find(save_path)
264 263
     else:
265 264
         inventory_file = os.path.join(save_path, INVENTORY_FILENAME)
265
+
266
+    if os.path.isfile(inventory_file):
267
+        _make_backup(save_path, inventory_file)
268
+
266 269
     with open(inventory_file, 'wb') as f:
267 270
         f.write(inventory_json.encode('ascii'))
268 271
         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
@@ -1079,6 +1080,9 @@ def main(config=None, check=False, debug=False, environment=None, **kwargs):
1079 1080
     # Load existing inventory file if found
1080 1081
     inventory, inv_path = filesys.load_inventory(config, INVENTORY_SKEL)
1081 1082
 
1083
+    # Make a deep copy for change comparison
1084
+    orig_inventory = copy.deepcopy(inventory)
1085
+
1082 1086
     # Save the users container cidr as a group variable
1083 1087
     cidr_networks = user_defined_config.get('cidr_networks')
1084 1088
     if not cidr_networks:
@@ -1169,7 +1173,9 @@ def main(config=None, check=False, debug=False, environment=None, **kwargs):
1169 1173
         num_hosts = len(inventory['_meta']['hostvars'])
1170 1174
         logger.debug("%d hosts found.", num_hosts)
1171 1175
 
1172
-    # Save new dynamic inventory
1173
-    filesys.save_inventory(inventory_json, inv_path)
1176
+    # Save new dynamic inventory only if modified
1177
+    if orig_inventory != inventory:
1178
+        logger.debug("Saving modified inventory")
1179
+        filesys.save_inventory(inventory_json, inv_path)
1174 1180
 
1175 1181
     return inventory_json

Loading…
Cancel
Save