[autotest] Add job_repo_url host attribute

Whenever we update a machine via the dynamic_suite code, tag it
with a host attribute indicating where packages corresponding to
the build we just installed live.  That way, when it's time to
install autotest on that machine, we'll be able to consult the
attribute and install the appropriate source.

BUG=chromium-os:25653
TEST=./server/autoserv test_suites/dev_harness and then look for a wget command in the test log that mentions autotest/packages
TEST=./run_remote_tests.sh --board=x86-generic --ssh_port=9222 --remote=127.0.0.1 --use_emerged suite_Smoke
STATUS=Fixed

Change-Id: I56bde1ea247ac934a4bd800ca5dd355f5eeed946
Reviewed-on: https://gerrit.chromium.org/gerrit/15096
Tested-by: Chris Masone <cmasone@chromium.org>
Reviewed-by: Scott Zawalski <scottz@chromium.org>
Commit-Ready: Chris Masone <cmasone@chromium.org>
diff --git a/server/site_autotest.py b/server/site_autotest.py
index 951e5b0..4bb285f 100755
--- a/server/site_autotest.py
+++ b/server/site_autotest.py
@@ -1,10 +1,11 @@
-# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+import logging
 import os
 from autotest_lib.client.common_lib import global_config
-from autotest_lib.server import autoserv_parser, installable_object
+from autotest_lib.server import installable_object, autoserv_parser
 
 
 config = global_config.global_config
@@ -21,9 +22,47 @@
         self.got = True
 
 
+    def _get_fetch_location_from_host_attribute(self):
+        """Get repo to use for packages from host attribute, if possible.
+
+        Hosts are tagged with an attribute containing the URL
+        from which to source packages when running a test on that host.
+        If self.host is set, attempt to look this attribute up by calling out
+        to the AFE.
+
+        @returns value of the 'job_repo_url' host attribute, if present.
+        """
+        try:
+            from autotest_lib.server import frontend
+            if self.host:
+                afe = frontend.AFE(debug=False)
+                hosts = afe.get_hosts(hostname=self.host.hostname)
+                if 'job_repo_url' in hosts[0].attributes:
+                    return hosts[0].attributes['job_repo_url']
+        except ImportError:
+            pass
+        return None
+
+
     def get_fetch_location(self):
-        """Autotest packages are always stored under the image URL."""
+        """Generate list of locations where autotest can look for packages.
+
+        Old n' busted: Autotest packages are always stored at a URL that can
+        be derived from the one passed via the voodoo magic --image argument.
+        New hotness: Hosts are tagged with an attribute containing the URL
+        from which to source packages when running a test on that host.
+
+        @returns the list of candidate locations to check for packages.
+        """
         repos = super(SiteAutotest, self).get_fetch_location()
+
+        # The new way.
+        found_repo = self._get_fetch_location_from_host_attribute()
+        if found_repo is not None:
+            repos.append(found_repo)
+            return repos
+
+        # The old way.
         if parser.options.image:
             # Add our new repo to the end, the package manager will later
             # reverse the list of repositories resulting in ours being first.
@@ -33,5 +72,17 @@
         return repos
 
 
+    def install(self, host=None, autodir=None):
+        """Install autotest.  If |host| is not None, stores it in |self.host|.
+
+        @param host A Host instance on which autotest will be installed
+        @param autodir Location on the remote host to install to
+        """
+        if host:
+            self.host = host
+
+        super(SiteAutotest, self).install(host=host, autodir=autodir)
+
+
 class _SiteRun(object):
     pass