Revert "Reland "cbuildbot: Update buildroot location to align with recipes""

This reverts commit 60750b5d8dd8bdbc8fe3854b0267b4f1ec6e39c5.

Reason for revert: Remaining cleanup is far too aggressive and is removing everything on cleanup.  Reverting until I can merge these a single change and verified.

Original change's description:
> Reland "cbuildbot: Update buildroot location to align with recipes"
>
> This reverts commit c4fe5105a3e1caaee92519516f638cb9155011ab.
>
> Reason for revert: Relanded InitialCheckout fix with some additional modifications to address the path for the base repo.
>
> Original change's description:
> > Revert "cbuildbot: Update buildroot location to align with recipes"
> >
> > This reverts commit 8be301ede26bca47d38692cd8012beaf99960654.
> >
> > Reason for revert: This is landing with a nested repo, rather than being a base repo.  I need to figure out why this is working different than recipes, in which we already take this approach.
> >
> > Original change's description:
> > > cbuildbot: Update buildroot location to align with recipes
> > >
> > > Update the buildroot (cache) location to align with recipes, which
> > > allows us to update the legacy recipes to utilize more efficient methods
> > > for syncing and cache management.
> > >
> > > BUG=b:186752777
> > > TEST=`cros tryjob`
> > >
> > > Change-Id: I4263934111115505b75888b155d5c03031a39c56
> > > Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2904506
> > > Tested-by: Mike Nichols <mikenichols@chromium.org>
> > > Commit-Queue: Mike Nichols <mikenichols@chromium.org>
> > > Reviewed-by: LaMont Jones <lamontjones@chromium.org>
> >
> > Bug: b:186752777
> > Change-Id: Ide6b9b910fe4dbdaaedaea1047731628000e1f64
> > Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2906692
> > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> > Commit-Queue: Mike Nichols <mikenichols@chromium.org>
>
> Bug: b:186752777
> Change-Id: Ic23934b09c5d9badad2c26c6b0e17a8d6a036899
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2906694
> Reviewed-by: LaMont Jones <lamontjones@chromium.org>
> Commit-Queue: Mike Nichols <mikenichols@chromium.org>
> Tested-by: Mike Nichols <mikenichols@chromium.org>

Bug: b:186752777
Change-Id: I7b56ac28976576f98011991ad4f1ffef6cfde50b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2906699
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Reviewed-by: Mike Nichols <mikenichols@chromium.org>
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 15ca0f2..59f85a4 100644
--- a/scripts/cbuildbot_launch.py
+++ b/scripts/cbuildbot_launch.py
@@ -128,7 +128,7 @@
 
   if not options.cache_dir:
     options.cache_dir = os.path.join(options.buildroot,
-                                     '.cache')
+                                     'repository', '.cache')
 
   options.Freeze()
 
@@ -281,18 +281,54 @@
   SanitizeCacheDir(cache_dir)
   build_state.distfiles_ts = _MaybeCleanDistfiles(
       cache_dir, previous_state.distfiles_ts)
+
+  if previous_state.buildroot_layout != BUILDROOT_BUILDROOT_LAYOUT:
+    logging.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:
+      logging.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)
+      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 the previous build didn't exit normally, run an expensive step to
     # cleanup abandoned git locks.
     if previous_state.status not in (constants.BUILDER_STATUS_FAILED,
                                      constants.BUILDER_STATUS_PASSED):
       repo.CleanStaleLocks()
+
+    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)
   SetLastBuildState(root, build_state)
 
 
@@ -316,8 +352,8 @@
   logging.PrintBuildbotStepText('Branch: %s' % repo.branch)
   logging.info('Bootstrap script starting initial sync on branch: %s',
                repo.branch)
-  repo.Sync(jobs=20,
-            detach=True,
+  repo.PreLoad('/preload/chromeos')
+  repo.Sync(detach=True,
             downgrade_repo=_ShouldDowngradeRepo(options))
 
 
@@ -473,8 +509,9 @@
     Return code of cbuildbot as an integer.
   """
   branchname = options.branch or 'main'
-  buildroot = options.buildroot
-  workspace = options.workspace
+  root = options.buildroot
+  buildroot = os.path.join(root, 'repository')
+  workspace = os.path.join(root, 'workspace')
   depot_tools_path = os.path.join(buildroot, constants.DEPOT_TOOLS_SUBPATH)
 
   # Does the entire build pass or fail.
@@ -493,12 +530,12 @@
       repo = repository.RepoRepository(manifest_url, buildroot,
                                        branch=branchname,
                                        git_cache_dir=options.git_cache_dir)
-      previous_build_state = GetLastBuildState(buildroot)
+      previous_build_state = GetLastBuildState(root)
 
       # Clean up the buildroot to a safe state.
       with metrics.SecondsTimer(METRIC_CLEAN):
         build_state = GetCurrentBuildState(options, branchname)
-        CleanBuildRoot(buildroot, repo, options.cache_dir, build_state)
+        CleanBuildRoot(root, repo, options.cache_dir, build_state)
 
       # Get a checkout close enough to the branch that cbuildbot can handle it.
       if options.sync:
@@ -523,7 +560,7 @@
       build_state.status = (
           constants.BUILDER_STATUS_PASSED
           if result == 0 else constants.BUILDER_STATUS_FAILED)
-      SetLastBuildState(buildroot, build_state)
+      SetLastBuildState(root, build_state)
 
       with metrics.SecondsTimer(METRIC_CHROOT_CLEANUP):
         CleanupChroot(buildroot)