Bug 2032 NBI leaves files orphaned in fsmongo 27/12127/1 v11.0.3
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:26:03 +0000 (20:26 +0200)
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 20332

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

index deae786..60317aa 100644 (file)
@@ -248,6 +248,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 = (
@@ -274,6 +275,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
@@ -433,6 +435,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)
@@ -464,6 +467,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):
         """