auto_updater.py: Delete update payload files after use

Currently, we keep around the update files for rootfs and stateful once
the update is finished at each stage (although we clean them up after
the entire process is finished or it is failed). Keeping these files
around can cause the stateful partition to go out of space. To alleviate
that, once each stage (rootfs and stateful) of the update is finished,
we should remove the files from the DUT explicitly.

BUG=chromium:1086788
TEST=cros flash

Change-Id: Ie376cd31b0f29b81ed41a0ccb84f598294327559
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2219470
Tested-by: Amin Hassani <ahassani@chromium.org>
Auto-Submit: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Achuith Bhandarkar <achuith@chromium.org>
Reviewed-by: Andrew Lassalle <andrewlassalle@chromium.org>
Commit-Queue: Amin Hassani <ahassani@chromium.org>
diff --git a/lib/auto_updater.py b/lib/auto_updater.py
index b261b2d..8840ef6 100644
--- a/lib/auto_updater.py
+++ b/lib/auto_updater.py
@@ -564,11 +564,18 @@
       payload_dir = self.device.work_dir
 
     try:
+      stateful_update_payload = os.path.join(
+          payload_dir, auto_updater_transfer.STATEFUL_FILENAME)
+
       updater = stateful_updater.StatefulUpdater(self.device)
       updater.Update(
-          os.path.join(payload_dir, auto_updater_transfer.STATEFUL_FILENAME),
+          stateful_update_payload,
           update_type=(stateful_updater.StatefulUpdater.UPDATE_TYPE_CLOBBER if
                        self._clobber_stateful else None))
+
+      # Delete the stateful update file on success so it doesn't occupy extra
+      # disk space. On failure it will get cleaned up.
+      self.device.DeletePath(stateful_update_payload)
     except stateful_updater.Error as e:
       error_msg = 'Stateful update failed with error: %s' % str(e)
       logging.exception(error_msg)
@@ -632,6 +639,11 @@
 
     self.UpdateRootfs()
 
+    # Delete the update file so it doesn't take much space on disk for the
+    # remainder of the update process.
+    self.device.DeletePath(self.PAYLOAD_DIR_NAME, relative_to_work_dir=True,
+                           recursive=True)
+
   def RunUpdateStateful(self):
     """Run all processes needed by updating stateful.
 
diff --git a/lib/remote_access.py b/lib/remote_access.py
index f3d77b0..798c5ff 100644
--- a/lib/remote_access.py
+++ b/lib/remote_access.py
@@ -1064,6 +1064,28 @@
       raise CatFileError('Failed to read file "%s" on the device' % path)
     return result.output
 
+  def DeletePath(self, path, relative_to_work_dir=False, recursive=False):
+    """Deletes a path on the remote device.
+
+    Args:
+      path: The path on the remote device that should be deleted.
+      relative_to_work_dir: If true, the path is relative to |self.work_dir|.
+      recursive: If true, the |path| is deleted recursively.
+
+    Raises:
+      cros_build_lib.RunCommandError if |path| does not exist or the remote
+      command to delete the |path| has failed.
+    """
+    if relative_to_work_dir:
+      path = os.path.join(self.work_dir, path)
+
+    cmd = ['rm', '-f']
+    if recursive:
+      cmd += ['-r']
+    cmd += [path]
+
+    self.run(cmd)
+
   def PipeOverSSH(self, filepath, cmd, **kwargs):
     """Cat a file and pipe over SSH."""
     producer_cmd = ['cat', filepath]