cidb: use the cidb on our builders

This CL enables use of the cidb on builders run with --buildbot. In
particular:
 - The cidb connection factory uses a cached cidb connection to avoid
   creating duplicate connections per cbuildbot execution.
 - A new stage, BuildStart is added, which is the first stage to run and
   which inserts a build row into the database if appropriate.
 - ReportBuildStart is renamed to BuildReexecutionFinished for clarity.
 - UploadMetadata is extended to also update the build row in the
   database.
 - Calls to RecordCLAction are accompanied by a CL action database
   insert.
 - ReportStage calls FinishBuild and writes the final build status to
   the database.

BUG=chromium:387755
TEST=cidb_integration_test;
remote trybots with --buildbot --debug, verified by inspecting database
that expected data is written there.

Change-Id: Ia2d9d355a744cafda0ade851be141519c56093d8
Reviewed-on: https://chromium-review.googlesource.com/211648
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
Commit-Queue: Aviv Keshet <akeshet@chromium.org>
Tested-by: Aviv Keshet <akeshet@chromium.org>
diff --git a/scripts/cbuildbot.py b/scripts/cbuildbot.py
index 5237255..7dc3fd0 100644
--- a/scripts/cbuildbot.py
+++ b/scripts/cbuildbot.py
@@ -48,6 +48,7 @@
 from chromite.cbuildbot.stages import test_stages
 
 
+from chromite.lib import cidb
 from chromite.lib import cgroups
 from chromite.lib import cleanup
 from chromite.lib import commandline
@@ -169,6 +170,8 @@
     if self._run.options.resume:
       results_lib.LoadCheckpoint(self._run.buildroot)
 
+    self._RunStage(report_stages.BuildStartStage)
+
     self._RunStage(build_stages.CleanUpStage)
 
   def _GetStageInstance(self, stage, *args, **kwargs):
@@ -357,8 +360,6 @@
     Returns:
       Whether the build succeeded.
     """
-    report_stages.WriteBasicMetadata(self._run)
-
     self._InitializeTrybotPatchPool()
 
     if self._run.options.bootstrap:
@@ -388,7 +389,7 @@
         print_report = False
         success = self._ReExecuteInBuildroot(sync_instance)
       else:
-        self._RunStage(report_stages.ReportBuildStartStage)
+        self._RunStage(report_stages.BuildReexecutionFinishedStage)
         self.RunStages()
 
     except Exception as ex:
@@ -1663,6 +1664,23 @@
   return options, args
 
 
+def _SetupCidb(options):
+  """Set up CIDB the appropriate Setup call.
+
+  Args:
+    options: Command line options structure.
+  """
+  # Do not use cidb if not run with --buildbot
+  if not options.buildbot:
+    cidb.CIDBConnectionFactory.SetupNoCidb()
+    return
+
+  if options.debug:
+    cidb.CIDBConnectionFactory.SetupDebugCidb()
+  else:
+    cidb.CIDBConnectionFactory.SetupProdCidb()
+
+
 # TODO(build): This function is too damn long.
 def main(argv):
   # Turn on strict sudo checks.
@@ -1833,4 +1851,6 @@
                 '_FetchSlaveStatuses',
                 return_value=mock_statuses)
 
+    _SetupCidb(options)
+
     _RunBuildStagesWrapper(options, build_config)