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 provision
     15 from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
     16 
     17 
     18 AFE = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
     19 _CROS_VERSION_MAP = AFE.get_stable_version_map(AFE.CROS_IMAGE_TYPE)
     20 _FIRMWARE_VERSION_MAP = AFE.get_stable_version_map(AFE.FIRMWARE_IMAGE_TYPE)
     21 _FAFT_VERSION_MAP = AFE.get_stable_version_map(AFE.FAFT_IMAGE_TYPE)
     22 _ANDROID_VERSION_MAP = AFE.get_stable_version_map(AFE.ANDROID_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 get_stable_android_version(board):
     81     """Retrieve the stable Android version a given board.
     82 
     83     @param board: Board to lookup.
     84 
     85     @returns Stable version of Android for the given board.
     86     """
     87     return _ANDROID_VERSION_MAP.get_version(board)
     88 
     89 
     90 def _clear_host_attributes_before_provision(host, info):
     91     """Clear host attributes before provision, e.g., job_repo_url.
     92 
     93     @param host: A Host object to clear attributes before provision.
     94     @param info: A HostInfo to update the attributes in.
     95     """
     96     attributes = host.get_attributes_to_clear_before_provision()
     97     if not attributes:
     98         return
     99 
    100     for key in attributes:
    101         info.attributes.pop(key, None)
    102 
    103 
    104 def machine_install_and_update_labels(host, *args, **dargs):
    105     """Calls machine_install and updates the version labels on a host.
    106 
    107     @param host: Host object to run machine_install on.
    108     @param *args: Args list to pass to machine_install.
    109     @param **dargs: dargs dict to pass to machine_install.
    110 
    111     """
    112     # **dargs also carries an additional bool arg to determine whether
    113     # the provisioning is w/ or w/o cheets. with_cheets arg will be popped in
    114     # beginning so machine_install isn't affected by with_cheets presence.
    115     with_cheets = dargs.pop('with_cheets', False)
    116     info = host.host_info_store.get()
    117     info.clear_version_labels()
    118     _clear_host_attributes_before_provision(host, info)
    119     host.host_info_store.commit(info)
    120     # If ENABLE_DEVSERVER_TRIGGER_AUTO_UPDATE is enabled and the host is a
    121     # CrosHost, devserver will be used to trigger auto-update.
    122     if host.support_devserver_provision:
    123         image_name, host_attributes = host.machine_install_by_devserver(
    124             *args, **dargs)
    125     else:
    126         image_name, host_attributes = host.machine_install(*args, **dargs)
    127 
    128     info = host.host_info_store.get()
    129     info.attributes.update(host_attributes)
    130     if with_cheets == True:
    131         image_name += provision.CHEETS_SUFFIX
    132     info.set_version_label(host.VERSION_PREFIX, image_name)
    133     host.host_info_store.commit(info)
    134