chromite: make gsutil accept a user to create cache dir.

This CL is used for devserver package on DUT calling gsutil in chromite.
In this condition, devserver could pass an argument user to create
cache_dir, to avoid the case that 'root user cannot create cache dir'.

BUG=chromium:698304
TEST=emerge chromite to local moblab DUT, call devserver to provision
it. Ran cros flash ssh://. Ran cbuildbot/run_unittest.

Change-Id: Icb23297bc350620a751f1146bcab969f72ff83b1
Reviewed-on: https://chromium-review.googlesource.com/457859
Commit-Ready: Xixuan Wu <xixuan@chromium.org>
Tested-by: Xixuan Wu <xixuan@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Michael Tang <ntang@chromium.org>
(cherry picked from commit 5f625f556cb38a5958f00f73fbbea20a06bf6ac5)
Reviewed-on: https://chromium-review.googlesource.com/462341
Reviewed-by: Xixuan Wu <xixuan@chromium.org>
Commit-Queue: Michael Tang <ntang@chromium.org>
Tested-by: Michael Tang <ntang@chromium.org>
Trybot-Ready: Michael Tang <ntang@chromium.org>
diff --git a/lib/auto_updater.py b/lib/auto_updater.py
index 87b822d..0a40756 100644
--- a/lib/auto_updater.py
+++ b/lib/auto_updater.py
@@ -289,7 +289,7 @@
     keys = keys or ['CURRENT_OP']
     result = device.RunCommand([cls.REMOTE_UPDATE_ENGINE_BIN_FILENAME,
                                 '--status'],
-                               capture_output=True)
+                               capture_output=True, log_output=True)
 
     if not result.output:
       raise Exception('Cannot get update status')
diff --git a/lib/cache.py b/lib/cache.py
index 50711e3..d9ef9a9 100644
--- a/lib/cache.py
+++ b/lib/cache.py
@@ -188,12 +188,13 @@
 
   _STAGING_DIR = 'staging'
 
-  def __init__(self, cache_dir):
+  def __init__(self, cache_dir, cache_user=None):
     self._cache_dir = cache_dir
+    self._cache_user = cache_user
     self.staging_dir = os.path.join(cache_dir, self._STAGING_DIR)
 
-    osutils.SafeMakedirsNonRoot(self._cache_dir)
-    osutils.SafeMakedirsNonRoot(self.staging_dir)
+    osutils.SafeMakedirsNonRoot(self._cache_dir, user=self._cache_user)
+    osutils.SafeMakedirsNonRoot(self.staging_dir, user=self._cache_user)
 
   def _KeyExists(self, key):
     return os.path.exists(self._GetKeyPath(key))
@@ -205,7 +206,8 @@
   def _LockForKey(self, key, suffix='.lock'):
     """Returns an unacquired lock associated with a key."""
     key_path = self._GetKeyPath(key)
-    osutils.SafeMakedirsNonRoot(os.path.dirname(key_path))
+    osutils.SafeMakedirsNonRoot(os.path.dirname(key_path),
+                                user=self._cache_user)
     lock_path = os.path.join(self._cache_dir, os.path.dirname(key_path),
                              os.path.basename(key_path) + suffix)
     return locking.FileLock(lock_path)
@@ -217,7 +219,8 @@
     """Insert a file or a directory into the cache at a given key."""
     self._Remove(key)
     key_path = self._GetKeyPath(key)
-    osutils.SafeMakedirsNonRoot(os.path.dirname(key_path))
+    osutils.SafeMakedirsNonRoot(os.path.dirname(key_path),
+                                user=self._cache_user)
     shutil.move(path, key_path)
 
   def _InsertText(self, key, text):
diff --git a/lib/gs.py b/lib/gs.py
index df5108f..10aef51 100644
--- a/lib/gs.py
+++ b/lib/gs.py
@@ -320,13 +320,13 @@
                               'without progress')
 
   @classmethod
-  def GetDefaultGSUtilBin(cls, cache_dir=None):
+  def GetDefaultGSUtilBin(cls, cache_dir=None, cache_user=None):
     if cls.DEFAULT_GSUTIL_BIN is None:
       if cache_dir is None:
         cache_dir = path_util.GetCacheDir()
       if cache_dir is not None:
         common_path = os.path.join(cache_dir, constants.COMMON_CACHE)
-        tar_cache = cache.TarballCache(common_path)
+        tar_cache = cache.TarballCache(common_path, cache_user=cache_user)
         key = (cls.GSUTIL_TAR,)
         # The common cache will not be LRU, removing the need to hold a read
         # lock on the cached gsutil.
@@ -418,7 +418,7 @@
 
   def __init__(self, boto_file=None, cache_dir=None, acl=None,
                dry_run=False, gsutil_bin=None, init_boto=False, retries=None,
-               sleep=None):
+               sleep=None, cache_user=None):
     """Constructor.
 
     Args:
@@ -436,9 +436,10 @@
         user to interactively set up the boto config.
       retries: Number of times to retry a command before failing.
       sleep: Amount of time to sleep between failures.
+      cache_user: user for creating cache_dir for gsutil. Default is None.
     """
     if gsutil_bin is None:
-      gsutil_bin = self.GetDefaultGSUtilBin(cache_dir)
+      gsutil_bin = self.GetDefaultGSUtilBin(cache_dir, cache_user=cache_user)
     else:
       self._CheckFile('gsutil not found', gsutil_bin)
     self.gsutil_bin = gsutil_bin
diff --git a/lib/osutils.py b/lib/osutils.py
index 625b904..0e140b1 100644
--- a/lib/osutils.py
+++ b/lib/osutils.py
@@ -257,7 +257,7 @@
     user = GetNonRootUser()
 
   if user is None or user == 'root':
-    raise MakingDirsAsRoot('Refusing to create %s as root!' % path)
+    raise MakingDirsAsRoot('Refusing to create %s as user %s!' % (path, user))
 
   created = False
   should_chown = False