Bug 2033 NBI leaves files orphaned in fsmongo 26/12126/1 v10.1.1 v10.1.1-rc1 v10.1.1rc2
authorbeierlm <mark.beierl@canonical.com>
Wed, 25 May 2022 18:11:11 +0000 (14:11 -0400)
committerbeierlm <mark.beierl@canonical.com>
Wed, 25 May 2022 18:11:11 +0000 (14:11 -0400)
If any form of error occurs when uploading a package, including uploading
a package that already exists, the NBI will leave the unpacked descriptor's
files in fsmongo.  This change tracks fsmongo changes and rolls them back
on error.

Fixes Bug 2033

Change-Id: I4513747c10e469a01a1cef8b89986eb4b6ad59ae
Signed-off-by: beierlm <mark.beierl@canonical.com>
osm_nbi/descriptor_topics.py

index 8efde1e..1930254 100644 (file)
@@ -241,6 +241,7 @@ class DescriptorTopic(BaseTopic):
         # TODO change to Content-Disposition filename https://tools.ietf.org/html/rfc6266
         file_pkg = None
         error_text = ""
+        fs_rollback = []
         try:
             if content_range_text:
                 content_range = (
@@ -267,6 +268,7 @@ class DescriptorTopic(BaseTopic):
             else:
                 self.fs.file_delete(temp_folder, ignore_non_exist=True)
                 self.fs.mkdir(temp_folder)
+                fs_rollback.append(temp_folder)
 
             storage = self.fs.get_params()
             storage["folder"] = _id
@@ -385,6 +387,7 @@ class DescriptorTopic(BaseTopic):
             current_desc["_admin"]["modified"] = time()
             self.db.replace(self.topic, _id, current_desc)
             self.fs.dir_rename(temp_folder, _id)
+            fs_rollback = []
 
             indata["_id"] = _id
             self._send_msg("edited", indata)
@@ -416,6 +419,8 @@ class DescriptorTopic(BaseTopic):
         finally:
             if file_pkg:
                 file_pkg.close()
+            for file in fs_rollback:
+                self.fs.file_delete(file, ignore_non_exist=True)
 
     def get_file(self, session, _id, path=None, accept_header=None):
         """