• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright 2016 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Utility functions for AFE-based interactions.
7
8NOTE: This module should only be used in the context of a running test. Any
9      utilities that require accessing the AFE, should do so by creating
10      their own instance of the AFE client and interact with it directly.
11"""
12
13from __future__ import absolute_import
14from __future__ import division
15from __future__ import print_function
16
17import logging
18import traceback
19
20from autotest_lib.client.common_lib import error
21from autotest_lib.server.cros import provision
22from autotest_lib.server.cros import provisioner
23from autotest_lib.server import site_utils as server_utils
24
25
26def _log_image_name(image_name):
27    try:
28        logging.debug("_log_image_name: image (%s)", image_name)
29        server_utils.ParseBuildName(name=image_name)
30    except Exception:
31        logging.error(traceback.format_exc())
32
33
34def _format_image_name(board, version):
35    return "%s-release/%s" % (board, version)
36
37
38def get_stable_cros_image_name_v2(host_info):
39    """Retrieve the Chrome OS stable image name for a given board.
40
41    @param host_info: a host_info_store object.
42
43    @returns Name of a Chrome OS image to be installed in order to
44            repair the given board.
45    """
46    if not host_info.cros_stable_version:
47        raise error.AutoservError("No cros stable_version found"
48                                  " in host_info_store.")
49
50    logging.debug("Get cros stable_version for board: %s",
51                  getattr(host_info, "board", None))
52    out = _format_image_name(board=host_info.board,
53                             version=host_info.cros_stable_version)
54    _log_image_name(out)
55    return out
56
57
58def get_stable_firmware_version_v2(host_info):
59    """Retrieve the stable firmware version for a given model.
60
61    @param host_info: a host_info_store object.
62
63    @returns A version of firmware to be installed via
64             `chromeos-firmwareupdate` from a repair build.
65    """
66    logging.debug("Get firmware stable_version for model: %s",
67                  getattr(host_info, "model", None))
68    return host_info.firmware_stable_version
69
70
71def get_stable_faft_version_v2(host_info):
72    """Retrieve the stable firmware version for FAFT DUTs.
73
74    @param host_info: a host_info_store object.
75
76    @returns A version of firmware to be installed in order to
77            repair firmware on a DUT used for FAFT testing.
78    """
79    logging.debug("Get faft stable_version for model: %s",
80                  getattr(host_info, "model", None))
81    return host_info.faft_stable_version
82
83
84def clean_provision_labels(host):
85    """Clean provision-related labels.
86
87    @param host: Host object.
88    """
89    info = host.host_info_store.get()
90    info.clear_version_labels()
91    attributes = host.get_attributes_to_clear_before_provision()
92    for key in attributes:
93        info.attributes.pop(key, None)
94
95    host.host_info_store.commit(info)
96
97
98def add_provision_labels(host, version_prefix, image_name,
99                         provision_attributes={}):
100    """Add provision labels for host.
101
102    @param host: Host object.
103    @param version_prefix: a string version prefix, e.g. "cros-version:"
104    @param image_name: a string image name, e.g. peppy-release/R70-11011.0.0.
105    @param provision_attributes: a map, including attributes for provisioning,
106        e.g. {"job_repo_url": "http://..."}
107    """
108    info = host.host_info_store.get()
109    info.attributes.update(provision_attributes)
110    info.set_version_label(version_prefix, image_name)
111    host.host_info_store.commit(info)
112
113
114def machine_install_and_update_labels(host, update_url, with_cheets=False,
115                                      staging_server=None):
116    """Install a build and update the version labels on a host.
117
118    @param host: Host object where the build is to be installed.
119    @param update_url: URL of the build to install.
120    @param with_cheets: If true, installation is for a specific, custom
121        version of Android for a target running ARC.
122    @param staging_server: Server where images have been staged. Typically,
123        an instance of dev_server.ImageServer.
124    """
125    clean_provision_labels(host)
126
127    logging.debug('Attempting to provision with quick-provision.')
128    cros_provisioner = provisioner.ChromiumOSProvisioner(update_url, host=host)
129    image_name, host_attributes = cros_provisioner.run_provision()
130
131    if with_cheets:
132        image_name += provision.CHEETS_SUFFIX
133    add_provision_labels(host, host.VERSION_PREFIX, image_name, host_attributes)
134