cbuildbot_launch: Flag control the buildroot cleanup

As we consider moving to overlayfs for legacy builders, we need an
option for testing the buildroot maintenance and cleanup.  This will
flag control the cleanup of the buildroot based on whether we utilize
source-cache.  This should be transparent as the flag is set via the recipe and can be verified in a LED job.

BUG=b:197678613
TEST='local tryjob'

Change-Id: I617e1c0008ecd86832e1e3cd5a1278a534c99c53
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/3140363
Reviewed-by: Sean McAllister <smcallis@google.com>
Commit-Queue: Mike Nichols <mikenichols@chromium.org>
Tested-by: Mike Nichols <mikenichols@chromium.org>
diff --git a/scripts/cbuildbot_launch.py b/scripts/cbuildbot_launch.py
index 4f73d0f..76bf076 100644
--- a/scripts/cbuildbot_launch.py
+++ b/scripts/cbuildbot_launch.py
@@ -255,7 +255,7 @@
 
 
 @StageDecorator
-def CleanBuildRoot(root, repo, cache_dir, build_state):
+def CleanBuildRoot(root, repo, cache_dir, build_state, source_cache=False):
   """Some kinds of branch transitions break builds.
 
   This method ensures that cbuildbot's buildroot is a clean checkout on the
@@ -269,6 +269,7 @@
     build_state: BuildSummary object containing the current build state that
         will be saved into the cleaned root.  The distfiles_ts property will
         be updated if the distfiles cache is cleaned.
+    source_cache: Bool whether to use source cache mounts.
   """
   previous_state = GetLastBuildState(root)
   SetLastBuildState(root, build_state)
@@ -276,38 +277,41 @@
   build_state.distfiles_ts = _MaybeCleanDistfiles(
       cache_dir, previous_state.distfiles_ts)
 
-  if previous_state.buildroot_layout != BUILDROOT_BUILDROOT_LAYOUT:
-    cbuildbot_alerts.PrintBuildbotStepText('Unknown layout: Wiping buildroot.')
-    metrics.Counter(METRIC_CLOBBER).increment(
-        fields=field({}, reason='layout_change'))
-    chroot_dir = os.path.join(root, constants.DEFAULT_CHROOT_DIR)
-    if os.path.exists(chroot_dir) or os.path.exists(chroot_dir + '.img'):
-      cros_sdk_lib.CleanupChrootMount(chroot_dir, delete=True)
-    osutils.RmDir(root, ignore_missing=True, sudo=True)
-    osutils.RmDir(cache_dir, ignore_missing=True, sudo=True)
-  else:
-    if previous_state.branch != repo.branch:
+  if not source_cache:
+    if previous_state.buildroot_layout != BUILDROOT_BUILDROOT_LAYOUT:
       cbuildbot_alerts.PrintBuildbotStepText(
-          'Branch change: Cleaning buildroot.')
-      logging.info('Unmatched branch: %s -> %s', previous_state.branch,
-                   repo.branch)
-      metrics.Counter(METRIC_BRANCH_CLEANUP).increment(
-          fields=field({}, old_branch=previous_state.branch))
-
-      logging.info('Remove Chroot.')
-      chroot_dir = os.path.join(repo.directory, constants.DEFAULT_CHROOT_DIR)
+        'Unknown layout: Wiping buildroot.')
+      metrics.Counter(METRIC_CLOBBER).increment(
+          fields=field({}, reason='layout_change'))
+      chroot_dir = os.path.join(root, constants.DEFAULT_CHROOT_DIR)
       if os.path.exists(chroot_dir) or os.path.exists(chroot_dir + '.img'):
         cros_sdk_lib.CleanupChrootMount(chroot_dir, delete=True)
+      osutils.RmDir(root, ignore_missing=True, sudo=True)
+      osutils.RmDir(cache_dir, ignore_missing=True, sudo=True)
+    else:
+      if previous_state.branch != repo.branch:
+        cbuildbot_alerts.PrintBuildbotStepText(
+            'Branch change: Cleaning buildroot.')
+        logging.info('Unmatched branch: %s -> %s', previous_state.branch,
+                    repo.branch)
+        metrics.Counter(METRIC_BRANCH_CLEANUP).increment(
+            fields=field({}, old_branch=previous_state.branch))
 
-      logging.info('Remove Chrome checkout.')
-      osutils.RmDir(os.path.join(repo.directory, '.cache', 'distfiles'),
-                    ignore_missing=True, sudo=True)
+        logging.info('Remove Chroot.')
+        chroot_dir = os.path.join(repo.directory, constants.DEFAULT_CHROOT_DIR)
+        if os.path.exists(chroot_dir) or os.path.exists(chroot_dir + '.img'):
+          cros_sdk_lib.CleanupChrootMount(chroot_dir, delete=True)
+
+        logging.info('Remove Chrome checkout.')
+        osutils.RmDir(os.path.join(repo.directory, '.cache', 'distfiles'),
+                      ignore_missing=True, sudo=True)
 
   try:
     # If there is any failure doing the cleanup, wipe everything.
     # The previous run might have been killed in the middle leaving stale git
     # locks. Clean those up, first.
-    repo.PreLoad()
+    if not source_cache:
+      repo.PreLoad()
 
     # If the previous build didn't exit normally, run an expensive step to
     # cleanup abandoned git locks.
@@ -315,15 +319,17 @@
                                      constants.BUILDER_STATUS_PASSED):
       repo.CleanStaleLocks()
 
-    repo.BuildRootGitCleanup(prune_all=True)
+    if not source_cache:
+      repo.BuildRootGitCleanup(prune_all=True)
   except Exception:
     logging.info('Checkout cleanup failed, wiping buildroot:', exc_info=True)
     metrics.Counter(METRIC_CLOBBER).increment(
         fields=field({}, reason='repo_cleanup_failure'))
     repository.ClearBuildRoot(repo.directory)
 
-  # Ensure buildroot exists. Save the state we are prepped for.
-  osutils.SafeMakedirs(repo.directory)
+  if not source_cache:
+    # Ensure buildroot exists. Save the state we are prepped for.
+    osutils.SafeMakedirs(repo.directory)
   SetLastBuildState(root, build_state)
 
 
@@ -349,7 +355,8 @@
     logging.info('Bootstrap script starting initial sync on branch: %s',
                  repo.branch)
     repo.PreLoad('/preload/chromeos')
-  repo.Sync(detach=True,
+  repo.Sync(jobs=32,
+            detach=True,
             downgrade_repo=_ShouldDowngradeRepo(options))
 
 
@@ -508,6 +515,10 @@
   root = options.buildroot
   buildroot = os.path.join(root, 'repository')
   workspace = os.path.join(root, 'workspace')
+  if options.source_cache:
+    buildroot = options.buildroot
+    if options.workspace:
+      workspace = options.workspace
   depot_tools_path = os.path.join(buildroot, constants.DEPOT_TOOLS_SUBPATH)
 
   # Does the entire build pass or fail.
@@ -531,7 +542,8 @@
       # Clean up the buildroot to a safe state.
       with metrics.SecondsTimer(METRIC_CLEAN):
         build_state = GetCurrentBuildState(options, branchname)
-        CleanBuildRoot(root, repo, options.cache_dir, build_state)
+        CleanBuildRoot(root, repo, options.cache_dir, build_state,
+                       options.source_cache)
 
       # Get a checkout close enough to the branch that cbuildbot can handle it.
       if options.sync: