Add model label to autotest DUTs for unibuilds

CBuildBot needs to schedule specific tests in autotest based on the
model.  With unified builds, the board (aka build) is no longer granular
enough for scheduling test coverage.

This adds the model:[modelname] label to test targets in the autotest
farm, which will be used for test scheduling in the future.

BUG=chromium:576363
TEST=no staging environment available

Change-Id: I07633950732a389b3c3d92f801f63000d4ee7326
Reviewed-on: https://chromium-review.googlesource.com/576363
Commit-Ready: C Shapiro <shapiroc@google.com>
Tested-by: C Shapiro <shapiroc@google.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Jason Clinton <jclinton@chromium.org>
diff --git a/server/cros/dynamic_suite/constants.py b/server/cros/dynamic_suite/constants.py
index 8bf35af..750af50 100644
--- a/server/cros/dynamic_suite/constants.py
+++ b/server/cros/dynamic_suite/constants.py
@@ -28,6 +28,7 @@
 JOB_REPO_URL = 'job_repo_url'
 VERSION_PREFIX = 'cros-version:'
 BOARD_PREFIX = 'board:'
+MODEL_LABEL = 'model'
 OS_PREFIX = 'os'
 
 # Bug filing
diff --git a/server/hosts/cros_label.py b/server/hosts/cros_label.py
index c7a85df..ef52f39 100644
--- a/server/hosts/cros_label.py
+++ b/server/hosts/cros_label.py
@@ -47,6 +47,27 @@
         return [release_info['CHROMEOS_RELEASE_BOARD']]
 
 
+class ModelLabel(base_label.StringPrefixLabel):
+    """Determine the correct model label for the device."""
+
+    _NAME = ds_constants.MODEL_LABEL
+
+    def generate_labels(self, host):
+        # Return the existing label if set to defend against any bad image
+        # pushes to the host.  See comment in BoardLabel for more details.
+        for label in host._afe_host.labels:
+            if label.startswith(self._NAME + ':'):
+                return [label.split(':')[-1]]
+
+        cmd = "mosys platform model"
+        result = host.run(command=cmd, ignore_status=True)
+        if result.exit_status == 0:
+            return result.stddout
+        else:
+            logging.info("%s exited with status %d", cmd, result.exit_status)
+            return ""
+
+
 class LightSensorLabel(base_label.BaseLabel):
     """Label indicating if a light sensor is detected."""