Home | History | Annotate | Download | only in server
      1 # Copyright 2016 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 """Utility functions for AFE-based interactions.
      6 
      7 NOTE: This module should only be used in the context of a running test. Any
      8       utilities that require accessing the AFE, should do so by creating
      9       their own instance of the AFE client and interact with it directly.
     10 """
     11 
     12 import common
     13 from autotest_lib.client.common_lib import global_config
     14 from autotest_lib.server.cros import autoupdater
     15 from autotest_lib.server.cros import provision
     16 from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
     17 
     18 
     19 AFE = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
     20 _CROS_VERSION_MAP = AFE.get_stable_version_map(AFE.CROS_IMAGE_TYPE)
     21 _FIRMWARE_VERSION_MAP = AFE.get_stable_version_map(AFE.FIRMWARE_IMAGE_TYPE)
     22 _FAFT_VERSION_MAP = AFE.get_stable_version_map(AFE.FAFT_IMAGE_TYPE)
     23 
     24 _CONFIG = global_config.global_config
     25 ENABLE_DEVSERVER_TRIGGER_AUTO_UPDATE = _CONFIG.get_config_value(
     26         'CROS', 'enable_devserver_trigger_auto_update', type=bool,
     27         default=False)
     28 
     29 
     30 def _host_in_lab(host):
     31     """Check if the host is in the lab and an object the AFE knows.
     32 
     33     This check ensures that autoserv and the host's current job is running
     34     inside a fully Autotest instance, aka a lab environment. If this is the
     35     case it then verifies the host is registed with the configured AFE
     36     instance.
     37 
     38     @param host: Host object to verify.
     39 
     40     @returns The host model object.
     41     """
     42     if not host.job or not host.job.in_lab:
     43         return False
     44     return host._afe_host
     45 
     46 
     47 def get_stable_cros_image_name(board):
     48     """Retrieve the Chrome OS stable image name for a given board.
     49 
     50     @param board: Board to lookup.
     51 
     52     @returns Name of a Chrome OS image to be installed in order to
     53             repair the given board.
     54     """
     55     return _CROS_VERSION_MAP.get_image_name(board)
     56 
     57 
     58 def get_stable_firmware_version(model):
     59     """Retrieve the stable firmware version for a given model.
     60 
     61     @param model: Model to lookup.
     62 
     63     @returns A version of firmware to be installed via
     64              `chromeos-firmwareupdate` from a repair build.
     65     """
     66     return _FIRMWARE_VERSION_MAP.get_version(model)
     67 
     68 
     69 def get_stable_faft_version(board):
     70     """Retrieve the stable firmware version for FAFT DUTs.
     71 
     72     @param board: Board to lookup.
     73 
     74     @returns A version of firmware to be installed in order to
     75             repair firmware on a DUT used for FAFT testing.
     76     """
     77     return _FAFT_VERSION_MAP.get_version(board)
     78 
     79 
     80 def _clear_host_attributes_before_provision(host, info):
     81     """Clear host attributes before provision, e.g., job_repo_url.
     82 
     83     @param host: A Host object to clear attributes before provision.
     84     @param info: A HostInfo to update the attributes in.
     85     """
     86     attributes = host.get_attributes_to_clear_before_provision()
     87     if not attributes:
     88         return
     89 
     90     for key in attributes:
     91         info.attributes.pop(key, None)
     92 
     93 
     94 def machine_install_and_update_labels(host, update_url,
     95                                       use_quick_provision=False,
     96                                       with_cheets=False):
     97     """Install a build and update the version labels on a host.
     98 
     99     @param host: Host object where the build is to be installed.
    100     @param update_url: URL of the build to install.
    101     @param use_quick_provision:  If true, then attempt to use
    102         quick-provision for the update.
    103     @param with_cheets: If true, installation is for a specific, custom
    104         version of Android for a target running ARC.
    105     """
    106     info = host.host_info_store.get()
    107     info.clear_version_labels()
    108     _clear_host_attributes_before_provision(host, info)
    109     host.host_info_store.commit(info)
    110     updater = autoupdater.ChromiumOSUpdater(
    111             update_url, host=host, use_quick_provision=use_quick_provision)
    112     image_name, host_attributes = updater.run_update()
    113     info = host.host_info_store.get()
    114     info.attributes.update(host_attributes)
    115     if with_cheets:
    116         image_name += provision.CHEETS_SUFFIX
    117     info.set_version_label(host.VERSION_PREFIX, image_name)
    118     host.host_info_store.commit(info)
    119