devserver: pass a cache_user to initialize gsutil.

This CL passes an argument cache_user in gsutil initialization. This
'cache_user' is used to create cache directory for gsutil.

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

Change-Id: Ia5789ce18ac4d87e74ccf4f0bb9db7ec535cdccf
Reviewed-on: https://chromium-review.googlesource.com/457861
Commit-Ready: Xixuan Wu <xixuan@chromium.org>
Tested-by: Xixuan Wu <xixuan@chromium.org>
Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
(cherry picked from commit 178263c26da66538203e47a28a102097a7cf2929)
Reviewed-on: https://chromium-review.googlesource.com/478051
Reviewed-by: Jinsong Mu <jinsong@google.com>
Tested-by: Keith Haddow <haddowk@chromium.org>
diff --git a/common_util.py b/common_util.py
index b0092d8..a606d9d 100644
--- a/common_util.py
+++ b/common_util.py
@@ -414,3 +414,26 @@
 def IsInsideChroot():
   """Returns True if we are inside chroot."""
   return os.path.exists('/etc/debian_chroot')
+
+
+def IsRunningOnMoblab():
+  """Returns True if this code is running on a chromiumOS DUT."""
+  cmd = ['cat', '/etc/lsb-release']
+  try:
+    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                            stderr=subprocess.PIPE)
+    cmd_output, cmd_error = proc.communicate()
+
+    if cmd_error:
+      _Log('Error happened while reading lsb-release file: %s',
+           cmd_error.rstrip())
+      return False
+
+    if '_moblab' in cmd_output:
+      return True
+    else:
+      return False
+  except subprocess.CalledProcessError as e:
+    _Log('Error happened while checking whether devserver package is running '
+         'on a DUT: %s\n%s', e, e.output)
+    return False
diff --git a/downloader.py b/downloader.py
index 37dd54b..f3ff144 100755
--- a/downloader.py
+++ b/downloader.py
@@ -308,7 +308,10 @@
 
     self._archive_url = archive_url
 
-    self._ctx = gs.GSContext() if gs else None
+    if common_util.IsRunningOnMoblab():
+      self._ctx = gs.GSContext(cache_user='chronos') if gs else None
+    else:
+      self._ctx = gs.GSContext() if gs else None
 
   def Wait(self, name, is_regex_name, timeout):
     """Waits for artifact to exist and returns the appropriate names.
diff --git a/xbuddy.py b/xbuddy.py
index b0fa5f5..af5581e 100644
--- a/xbuddy.py
+++ b/xbuddy.py
@@ -213,7 +213,10 @@
     else:
       self.images_dir = os.path.join(self.GetSourceRoot(), 'src/build/images')
 
-    self._ctx = gs.GSContext() if gs else None
+    if common_util.IsRunningOnMoblab():
+      self._ctx = gs.GSContext(cache_user='chronos') if gs else None
+    else:
+      self._ctx = gs.GSContext() if gs else None
 
     common_util.MkDirP(self._timestamp_folder)