autotest: Ensure newly rebooted labstation is ready before run servod

BUG=b:170782988
TEST=Run repair locally

Change-Id: I210c805897d6402b30fdbe93c8a86994dd4e0b5b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2469810
Tested-by: Garry Wang <xianuowang@chromium.org>
Reviewed-by: Otabek Kasimov <otabek@google.com>
Commit-Queue: Garry Wang <xianuowang@chromium.org>
diff --git a/server/hosts/base_servohost.py b/server/hosts/base_servohost.py
index ec8f14f..a0a60aa 100644
--- a/server/hosts/base_servohost.py
+++ b/server/hosts/base_servohost.py
@@ -14,6 +14,7 @@
 import logging
 import socket
 import six.moves.xmlrpc_client
+import time
 
 from autotest_lib.client.bin import utils
 from autotest_lib.client.common_lib import autotest_enum
@@ -557,3 +558,25 @@
         """
         result = self.run('umount %s' % mount_path, ignore_status=True)
         return result.exit_status == 0
+
+    def wait_ready(self, required_uptime=300):
+        """Wait ready for a servohost if it has been rebooted recently.
+
+        It may take a few minutes until all servos and their componments
+        re-enumerated and become ready after a servohost(especially labstation
+        as it supports multiple servos) reboot, so we need to make sure the
+        servohost has been up for a given a mount of time before trying to
+        start any actions.
+
+        @param required_uptime: Minimum uptime in seconds that we can
+                                consdier a servohost be ready.
+        """
+        uptime = float(self.check_uptime())
+        # To prevent unexpected output from check_uptime() that causes long
+        # sleep, make sure the maximum wait time <= required_uptime.
+        diff = min(required_uptime - uptime, required_uptime)
+        if diff > 0:
+            logging.info(
+                    'The servohost was just rebooted, wait %s'
+                    ' seconds for it to become ready', diff)
+            time.sleep(diff)
diff --git a/server/hosts/servo_host.py b/server/hosts/servo_host.py
index a355dc4..58c112e 100644
--- a/server/hosts/servo_host.py
+++ b/server/hosts/servo_host.py
@@ -218,6 +218,12 @@
         if (self.wait_up(self.REBOOT_TIMEOUT) and self.is_in_lab()
             and self.is_labstation()):
             self._lock()
+            try:
+                self.wait_ready()
+            except Exception as e:
+                logging.info(
+                        'Unexpected error while ensure labstation'
+                        ' readiness; %s', str(e))
 
         self._repair_strategy = (
                 servo_repair.create_servo_repair_strategy())