[autotest] Always use tko tables on master from shards.

tko/db.py was changed to always use settings for the global database.
This added configuration overhead for non-shard machines. This adds
the normal, old settings as a failover.

Also, this adds the global database to django.
A django database router is added to determine to which database
should be used for which models. All tko models are always taken or
written from or into the global database while all other objects
remain unchanged.

BUG=chromium:419435
TEST=Ran suites and manually tried rpcs.
DEPLOY=apache,tko

Change-Id: I675b064ffdcb0440cd33835b9a83e3858f826ca7
Reviewed-on: https://chromium-review.googlesource.com/221018
Tested-by: Jakob Jülich <jakobjuelich@chromium.org>
Reviewed-by: Fang Deng <fdeng@chromium.org>
Commit-Queue: Jakob Jülich <jakobjuelich@chromium.org>
diff --git a/tko/db.py b/tko/db.py
index bdce8c7..3129110 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -1,4 +1,5 @@
 import re, os, sys, types, time, random
+from django.conf import settings
 
 import common
 from autotest_lib.client.common_lib import global_config
@@ -37,37 +38,56 @@
 
 
     def _load_config(self, host, database, user, password):
-        # grab the global config
-        get_value = global_config.global_config.get_config_value
+        """Loads configuration settings required to connect to the database.
+
+        This will try to connect to use the settings prefixed with global_db_.
+        If they do not exist, they un-prefixed settings will be used.
+
+        If parameters are supplied, these will be taken instead of the values
+        in global_config.
+
+        @param host: If set, this host will be used, if not, the host will be
+                     retrieved from global_config.
+        @param database: If set, this database will be used, if not, the
+                         database will be retrieved from global_config.
+        @param user: If set, this user will be used, if not, the
+                         user will be retrieved from global_config.
+        @param password: If set, this password will be used, if not, the
+                         password will be retrieved from global_config.
+        """
+        DATABASE_SETTINGS = settings.DATABASES['global']
 
         # grab the host, database
         if host:
             self.host = host
         else:
-            self.host = get_value("AUTOTEST_WEB", "global_db_host")
+            self.host = DATABASE_SETTINGS['HOST']
         if database:
             self.database = database
         else:
-            self.database = get_value("AUTOTEST_WEB", "global_db_database")
+            self.database = DATABASE_SETTINGS['NAME']
 
         # grab the user and password
         if user:
             self.user = user
         else:
-            self.user = get_value("AUTOTEST_WEB", "global_db_user")
+            self.user = DATABASE_SETTINGS['USER']
         if password is not None:
             self.password = password
         else:
-            self.password = get_value("AUTOTEST_WEB", "global_db_password")
+            self.password = DATABASE_SETTINGS['PASSWORD']
 
         # grab the timeout configuration
-        self.query_timeout = get_value("AUTOTEST_WEB",
-                                       "global_db_query_timeout",
-                                       type=int, default=3600)
+        self.query_timeout =(
+                DATABASE_SETTINGS.get('OPTIONS', {}).get('timeout', 3600))
+
+        # Using fallback to non-global in order to work without configuration
+        # overhead on non-shard instances.
+        get_value = global_config.global_config.get_config_value_with_fallback
         self.min_delay = get_value("AUTOTEST_WEB", "global_db_min_retry_delay",
-                                   type=int, default=20)
+                                   "min_retry_delay", type=int, default=20)
         self.max_delay = get_value("AUTOTEST_WEB", "global_db_max_retry_delay",
-                                   type=int, default=60)
+                                   "max_retry_delay", type=int, default=60)
 
 
     def _init_db(self):
@@ -552,8 +572,9 @@
 
 def _get_db_type():
     """Get the database type name to use from the global config."""
-    get_value = global_config.global_config.get_config_value
-    return "db_" + get_value("AUTOTEST_WEB", "global_db_type", default="mysql")
+    get_value = global_config.global_config.get_config_value_with_fallback
+    return "db_" + get_value("AUTOTEST_WEB", "global_db_type", "db_type",
+                             default="mysql")
 
 
 def _get_error_class(class_name):