[autotest] Stage factory artifacts from the canary branch.

This cl enables us to stage artifacts form either the normal
release branch, or a canary branch, based on the type of artifact.

BUG=chromium:280889
TEST=Staged factory images from the canary branch.

Change-Id: If9a959d0847fecf4aded600974cf45e6e05c9ac5
Reviewed-on: https://chromium-review.googlesource.com/167884
Reviewed-by: Prashanth B <beeps@chromium.org>
Tested-by: Prashanth B <beeps@chromium.org>
Commit-Queue: Prashanth B <beeps@chromium.org>
diff --git a/client/common_lib/cros/dev_server.py b/client/common_lib/cros/dev_server.py
index 5fcf5fe..d8d569b 100644
--- a/client/common_lib/cros/dev_server.py
+++ b/client/common_lib/cros/dev_server.py
@@ -76,6 +76,20 @@
     return CONFIG.get_config_value('CROS', 'canary_channel_server', type=str)
 
 
+def _get_storage_server_for_artifacts(artifacts=None):
+    """Gets the appropriate storage server for the given artifacts.
+
+    @param artifacts: A list of artifacts we need to stage.
+    @return: The address of the storage server that has these artifacts.
+             The default image storage server if no artifacts are specified.
+    """
+    factory_artifact = global_config.global_config.get_config_value(
+            'CROS', 'factory_artifact', type=str, default='')
+    if artifacts and factory_artifact and factory_artifact in artifacts:
+        return _get_canary_channel_server()
+    return _get_image_storage_server()
+
+
 def _get_dev_server_list():
     return CONFIG.get_config_value('CROS', 'dev_server', type=list, default=[])
 
@@ -466,7 +480,8 @@
         """
         assert artifacts or files, 'Must specify something to stage.'
         if not archive_url:
-            archive_url = _get_image_storage_server() + image
+            archive_url = (_get_storage_server_for_artifacts(artifacts) +
+                           image)
 
         artifacts_arg = ','.join(artifacts) if artifacts else ''
         files_arg = ','.join(files) if files else ''
diff --git a/global_config.ini b/global_config.ini
index 25352ea..47ac00c 100644
--- a/global_config.ini
+++ b/global_config.ini
@@ -160,6 +160,7 @@
 
 firmware_url_pattern: %s/static/%s/firmware_from_source.tar.bz2
 factory_image_url_pattern: %s/static/canary-channel/%s/factory_test/chromiumos_factory_image.bin
+factory_artifact: factory_image
 image_url_pattern: %s/update/%s
 log_url_pattern: http://%s/tko/retrieve_logs.cgi?job=/results/%s/
 package_url_pattern: %s/static/%s/autotest/packages
diff --git a/server/hosts/cros_host.py b/server/hosts/cros_host.py
index 88cdcea..ba0f847 100644
--- a/server/hosts/cros_host.py
+++ b/server/hosts/cros_host.py
@@ -541,20 +541,32 @@
         """Stage a build on a devserver and return the update_url.
 
         @param image_name: a name like <baord>/4262.204.0
+
         @return: An update URL, eg:
             http://<devserver>/static/canary-channel/\
             <board>/4262.204.0/factory_test/chromiumos_factory_image.bin
+
+        @raises: ValueError if the factory artifact name is missing from
+                 the config.
+
         """
         if not image_name:
             logging.error('Need an image_name to stage a factory image.')
             return
 
+        factory_artifact = global_config.global_config.get_config_value(
+                'CROS', 'factory_artifact', type=str, default='')
+        if not factory_artifact:
+            raise ValueError('Cannot retrieve the factory artifact name from '
+                             'autotest config, and hence cannot stage factory '
+                             'artifacts.')
+
         logging.info('Staging build for servo install: %s', image_name)
         devserver = dev_server.ImageServer.resolve(image_name)
         devserver.stage_artifacts(
                 image_name,
-                ['factory_image'],
-                archive_url=dev_server._get_canary_channel_server())
+                [factory_artifact],
+                archive_url=None)
 
         return tools.factory_image_url_pattern() % (devserver.url(), image_name)