afdo: Implement orderfile-verify-toolchain builder.

This patch implements the functionality of
orderfile-verify-toolchain builder, which is similar to release
builder and verifies if the orderfile can build Chrome without
any problems.

It adds two stages: (1) before BuildPacakges, it gets most recent
unvetted orderfile and update Chrome ebuild; (2) after the build,
copy the orderfile from unvetted to vetted GS bucket.

If the orderfile to verify is already verified, skip the remaining
part of the builder. (Make the same change to
toolchain-generate-orderfile builder)

Major operations are added to lib/toolchain_util.py. cbuildbot
stage-related changes are added to AFDO stages.

BUG=chromium:950627
TEST=orderfile-verify-toolchain-tryjob passes at https://ci.chromium.org/p/chromeos/builders/general/Try/b8908114095670262240

Change-Id: I8148fc7b976e6c1bcd1b5eafb762be0eb680de7d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1682827
Tested-by: Tiancong Wang <tcwang@google.com>
Auto-Submit: Tiancong Wang <tcwang@google.com>
Commit-Queue: Tiancong Wang <tcwang@google.com>
Reviewed-by: Alex Klein <saklein@chromium.org>
diff --git a/api/controller/toolchain.py b/api/controller/toolchain.py
new file mode 100644
index 0000000..46bb62d
--- /dev/null
+++ b/api/controller/toolchain.py
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+# Copyright 2019 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Toolchain-related operations."""
+
+from __future__ import print_function
+
+import os
+
+from chromite.lib import cros_logging as logging
+from chromite.lib import gs
+from chromite.lib import toolchain_util
+
+
+def UpdateChromeEbuildWithOrderfile(input_proto, _output_proto):
+  """Update Chrome ebuild with most recent unvetted orderfile.
+
+  Args:
+    input_proto (UpdateChromeEbuildRequest): The input proto
+    output_proto (UpdateChromeEbuildResponse): Empty output proto
+  """
+
+  board = input_proto.build_target.name
+  toolchain_util.OrderfileUpdateChromeEbuild(board)
+
+
+def UploadVettedOrderfile(_input_proto, output_proto):
+  """Upload a vetted orderfile to GS bucket.
+
+  Args:
+    input_proto (UploadVettedOrderfileRequest): Empty input proto
+    output_proto (UploadVettedOrderfileResponse): The output proto
+  """
+
+  gs_context = gs.GSContext()
+  orderfile = toolchain_util.FindLatestChromeOrderfile(
+      toolchain_util.ORDERFILE_GS_URL_UNVETTED)
+
+  if gs_context.Exists(
+      os.path.join(toolchain_util.ORDERFILE_GS_URL_VETTED, orderfile)):
+    output_proto.status = False
+    return
+
+  source_url = os.path.join(toolchain_util.ORDERFILE_GS_URL_UNVETTED, orderfile)
+  dest_url = os.path.join(toolchain_util.ORDERFILE_GS_URL_VETTED, orderfile)
+
+  logging.info('Copying tarball from %s to %s', source_url, dest_url)
+  gs_context.Copy(source_url, dest_url, acl='public-read')
+  output_proto.status = True