ghost: Get parameters from test list

It would be convenient if we can get parameters from somewhere. Because
of the following reasons, I choose to get parameters from the current
active test list.
1. It should be customized by each models (ODM).
2. It's a little like factory server, so put the setting in same place.

BUG=b:172784970
TEST=1. Set overlord_urls in dut's active test list
     2. Kill ghost process:
     `kill $(lsof -i | grep 4456 | awk '{print $2}')`
     3. `goofy_ghost start`
     4. ghost can connect back to overlord server successfully
TEST=1. Set "wrong" overlord_urls in my dut's active test list
     2. Kill ghost process
     3. `goofy_ghost start`
     4. ghost "cannot" connect back to overlord server
     5. Modify active test list, set the correct overlord_urls and save
     6. ghost can connect back to overlord server automatically

Change-Id: I5e55e184b20cb3f74f7b0d3897d3605bc2a0dea6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/factory/+/2547614
Reviewed-by: Cheng Yueh <cyueh@chromium.org>
Tested-by: Yilin Yang (kerker) <kerker@chromium.org>
Commit-Queue: Nicolas Boichat <drinkcat@chromium.org>
diff --git a/py/tools/ghost.py b/py/tools/ghost.py
index 6112faa..bebf610 100755
--- a/py/tools/ghost.py
+++ b/py/tools/ghost.py
@@ -35,6 +35,7 @@
 import jsonrpclib
 from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
 
+from cros.factory.test.test_lists import manager
 from cros.factory.utils import process_utils
 
 
@@ -1173,6 +1174,26 @@
     t.daemon = True
     t.start()
 
+  def ApplyTestListParams(self):
+    mgr = manager.Manager()
+    constants = mgr.GetTestListByID(mgr.GetActiveTestListId()).constants
+
+    if 'overlord' not in constants:
+      return
+
+    if 'overlord_urls' in constants['overlord']:
+      for addr in [(x, _OVERLORD_PORT) for x in
+                   constants['overlord']['overlord_urls']]:
+        if addr not in self._overlord_addrs:
+          self._overlord_addrs.append(addr)
+
+    # This is sugar for ODM to turn off the verification quickly if they forgot.
+    # So we don't support to turn on again.
+    # If we want to turn on, we need to restart the ghost daemon.
+    if 'tls_no_verify' in constants['overlord']:
+      if constants['overlord']['tls_no_verify']:
+        self._tls_settings = TLSSettings(None, False)
+
   def ScanServer(self):
     for meth in [self.GetGateWayIP, self.GetFactoryServerIP]:
       for addr in [(x, _OVERLORD_PORT) for x in meth()]:
@@ -1205,6 +1226,9 @@
             logging.info('LAN Discovery: got overlord address %s:%d', *addr)
             self._overlord_addrs.append(addr)
 
+        if self._mode == Ghost.AGENT:
+          self.ApplyTestListParams()
+
         try:
           self.ScanServer()
           self.Register()