blob: 1466cef0f621b7389f4187d46fc4603cf5e65ad8 [file] [log] [blame]
Simran Basi5ace6f22016-01-06 17:30:44 -08001# 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
Simran Basiaa467ad2016-02-03 16:56:22 -08005"""Utility functions for AFE-based interactions.
6
7NOTE: 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"""
Simran Basi5ace6f22016-01-06 17:30:44 -080011
12import common
Dan Shi8190eb82016-02-11 17:15:58 -080013from autotest_lib.client.common_lib import error
xixuan5dc64ea2016-05-20 17:27:51 -070014from autotest_lib.client.common_lib import global_config
Simran Basi5ace6f22016-01-06 17:30:44 -080015from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
Dan Shi8190eb82016-02-11 17:15:58 -080016
Simran Basi5ace6f22016-01-06 17:30:44 -080017
18AFE = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
Richard Barnette260cbd02016-10-06 12:23:28 -070019_CROS_VERSION_MAP = AFE.get_stable_version_map(AFE.CROS_IMAGE_TYPE)
Richard Barnettee50453e2016-10-10 16:43:44 -070020_FIRMWARE_VERSION_MAP = AFE.get_stable_version_map(AFE.FIRMWARE_IMAGE_TYPE)
Richard Barnette260cbd02016-10-06 12:23:28 -070021_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)
Simran Basi5ace6f22016-01-06 17:30:44 -080023
xixuan5dc64ea2016-05-20 17:27:51 -070024_CONFIG = global_config.global_config
25ENABLE_DEVSERVER_TRIGGER_AUTO_UPDATE = _CONFIG.get_config_value(
26 'CROS', 'enable_devserver_trigger_auto_update', type=bool,
27 default=False)
28
Simran Basi5ace6f22016-01-06 17:30:44 -080029
Richard Barnette260cbd02016-10-06 12:23:28 -070030def _host_in_lab(host):
Simran Basi5ace6f22016-01-06 17:30:44 -080031 """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 """
Simran Basiaa467ad2016-02-03 16:56:22 -080042 if not host.job or not host.job.in_lab:
Simran Basi5ace6f22016-01-06 17:30:44 -080043 return False
Kevin Cheng05ae2a42016-06-06 10:12:48 -070044 return host._afe_host
Simran Basi5ace6f22016-01-06 17:30:44 -080045
46
Kevin Cheng84a71ba2016-07-14 11:03:57 -070047def get_labels(host, prefix=None):
48 """Get labels of a host with name started with given prefix.
Simran Basi5ace6f22016-01-06 17:30:44 -080049
Kevin Cheng84a71ba2016-07-14 11:03:57 -070050 @param prefix: Prefix of label names, if None, return all labels.
51
52 @returns List of labels that match the prefix or if prefix is None, all
53 labels.
54 """
55 if not prefix:
56 return host._afe_host.labels
57
58 return [label for label in host._afe_host.labels
59 if label.startswith(prefix)]
60
61
Simran Basi5ace6f22016-01-06 17:30:44 -080062def clear_version_labels(host):
63 """Clear version labels for a given host.
64
65 @param host: Host whose version labels to clear.
66 """
Kevin Cheng4ce67612016-07-29 13:34:17 -070067 host._afe_host.labels = [label for label in host._afe_host.labels
68 if not label.startswith(host.VERSION_PREFIX)]
Richard Barnette260cbd02016-10-06 12:23:28 -070069 if not _host_in_lab(host):
Simran Basi5ace6f22016-01-06 17:30:44 -080070 return
71
72 host_list = [host.hostname]
73 labels = AFE.get_labels(
74 name__startswith=host.VERSION_PREFIX,
75 host__hostname=host.hostname)
76
77 for label in labels:
78 label.remove_hosts(hosts=host_list)
79
80
81def add_version_label(host, image_name):
82 """Add version labels to a host.
83
84 @param host: Host to add the version label for.
85 @param image_name: Name of the build version to add to the host.
86 """
Kevin Cheng4ce67612016-07-29 13:34:17 -070087 label = '%s:%s' % (host.VERSION_PREFIX, image_name)
88 host._afe_host.labels.append(label)
Richard Barnette260cbd02016-10-06 12:23:28 -070089 if not _host_in_lab(host):
Simran Basi5ace6f22016-01-06 17:30:44 -080090 return
Simran Basi5ace6f22016-01-06 17:30:44 -080091 AFE.run('label_add_hosts', id=label, hosts=[host.hostname])
92
93
Richard Barnette383ef9c2016-12-13 11:56:49 -080094def get_stable_cros_image_name(board):
95 """Retrieve the Chrome OS stable image name for a given board.
Simran Basibeb2bb22016-02-03 15:25:48 -080096
97 @param board: Board to lookup.
Simran Basibeb2bb22016-02-03 15:25:48 -080098
Richard Barnette383ef9c2016-12-13 11:56:49 -080099 @returns Name of a Chrome OS image to be installed in order to
Richard Barnette260cbd02016-10-06 12:23:28 -0700100 repair the given board.
Simran Basibeb2bb22016-02-03 15:25:48 -0800101 """
Richard Barnette383ef9c2016-12-13 11:56:49 -0800102 return _CROS_VERSION_MAP.get_image_name(board)
Richard Barnette260cbd02016-10-06 12:23:28 -0700103
104
Richard Barnettee50453e2016-10-10 16:43:44 -0700105def get_stable_firmware_version(board):
106 """Retrieve the stable firmware version for a given board.
107
108 @param board: Board to lookup.
109
110 @returns A version of firmware to be installed via
111 `chromeos-firmwareupdate` from a repair build.
112 """
113 return _FIRMWARE_VERSION_MAP.get_version(board)
114
115
Richard Barnette260cbd02016-10-06 12:23:28 -0700116def get_stable_faft_version(board):
117 """Retrieve the stable firmware version for FAFT DUTs.
118
119 @param board: Board to lookup.
120
121 @returns A version of firmware to be installed in order to
122 repair firmware on a DUT used for FAFT testing.
123 """
124 return _FAFT_VERSION_MAP.get_version(board)
125
126
127def get_stable_android_version(board):
128 """Retrieve the stable Android version a given board.
129
130 @param board: Board to lookup.
131
132 @returns Stable version of Android for the given board.
133 """
134 return _ANDROID_VERSION_MAP.get_version(board)
Dan Shi8190eb82016-02-11 17:15:58 -0800135
136
Kevin Cheng05ae2a42016-06-06 10:12:48 -0700137def get_host_attribute(host, attribute, use_local_value=True):
Dan Shie4256c82016-02-18 00:23:49 -0800138 """Looks up the value of host attribute for the host.
139
Dan Shibe3636a2016-02-14 22:48:01 -0800140 @param host: A Host object to lookup for attribute value.
141 @param attribute: Name of the host attribute.
Kevin Cheng05ae2a42016-06-06 10:12:48 -0700142 @param use_local_value: Boolean to indicate if the local value or AFE value
143 should be retrieved.
Dan Shi8190eb82016-02-11 17:15:58 -0800144
Dan Shibe3636a2016-02-14 22:48:01 -0800145 @returns value for the given attribute or None if not found.
Dan Shi8190eb82016-02-11 17:15:58 -0800146 """
Kevin Cheng05ae2a42016-06-06 10:12:48 -0700147 local_value = host._afe_host.attributes.get(attribute)
Richard Barnette260cbd02016-10-06 12:23:28 -0700148 if not _host_in_lab(host) or use_local_value:
Dan Shibe3636a2016-02-14 22:48:01 -0800149 return local_value
150
Dan Shi8190eb82016-02-11 17:15:58 -0800151 hosts = AFE.get_hosts(hostname=host.hostname)
Dan Shibe3636a2016-02-14 22:48:01 -0800152 if hosts and attribute in hosts[0].attributes:
153 return hosts[0].attributes[attribute]
Dan Shi8190eb82016-02-11 17:15:58 -0800154 else:
Dan Shibe3636a2016-02-14 22:48:01 -0800155 return local_value
Dan Shi8190eb82016-02-11 17:15:58 -0800156
157
Dan Shibe3636a2016-02-14 22:48:01 -0800158def clear_host_attributes_before_provision(host):
Dan Shie4256c82016-02-18 00:23:49 -0800159 """Clear host attributes before provision, e.g., job_repo_url.
Dan Shi8190eb82016-02-11 17:15:58 -0800160
Dan Shibe3636a2016-02-14 22:48:01 -0800161 @param host: A Host object to clear attributes before provision.
Dan Shi8190eb82016-02-11 17:15:58 -0800162 """
Dan Shibe3636a2016-02-14 22:48:01 -0800163 attributes = host.get_attributes_to_clear_before_provision()
164 for attribute in attributes:
Kevin Cheng05ae2a42016-06-06 10:12:48 -0700165 host._afe_host.attributes.pop(attribute, None)
Richard Barnette260cbd02016-10-06 12:23:28 -0700166 if not _host_in_lab(host):
Dan Shi8190eb82016-02-11 17:15:58 -0800167 return
Dan Shibe3636a2016-02-14 22:48:01 -0800168
169 for attribute in attributes:
170 update_host_attribute(host, attribute, None)
Dan Shi8190eb82016-02-11 17:15:58 -0800171
172
Dan Shibe3636a2016-02-14 22:48:01 -0800173def update_host_attribute(host, attribute, value):
174 """Updates the host attribute with given value.
Dan Shi8190eb82016-02-11 17:15:58 -0800175
Dan Shibe3636a2016-02-14 22:48:01 -0800176 @param host: A Host object to update attribute value.
177 @param attribute: Name of the host attribute.
178 @param value: Value for the host attribute.
Dan Shi8190eb82016-02-11 17:15:58 -0800179
Dan Shie4256c82016-02-18 00:23:49 -0800180 @raises AutoservError: If we failed to update the attribute.
Dan Shi8190eb82016-02-11 17:15:58 -0800181 """
Kevin Cheng05ae2a42016-06-06 10:12:48 -0700182 host._afe_host.attributes[attribute] = value
Richard Barnette260cbd02016-10-06 12:23:28 -0700183 if not _host_in_lab(host):
Dan Shi8190eb82016-02-11 17:15:58 -0800184 return
185
Dan Shibe3636a2016-02-14 22:48:01 -0800186 AFE.set_host_attribute(attribute, value, hostname=host.hostname)
Kevin Cheng05ae2a42016-06-06 10:12:48 -0700187 if get_host_attribute(host, attribute, use_local_value=False) != value:
Dan Shibe3636a2016-02-14 22:48:01 -0800188 raise error.AutoservError(
189 'Failed to update host attribute `%s` with %s, host %s' %
190 (attribute, value, host.hostname))
191
192
193def machine_install_and_update_labels(host, *args, **dargs):
194 """Calls machine_install and updates the version labels on a host.
195
196 @param host: Host object to run machine_install on.
197 @param *args: Args list to pass to machine_install.
198 @param **dargs: dargs dict to pass to machine_install.
199 """
200 clear_version_labels(host)
201 clear_host_attributes_before_provision(host)
xixuanec801e32016-08-25 10:20:22 -0700202 # If ENABLE_DEVSERVER_TRIGGER_AUTO_UPDATE is enabled and the host is a
203 # CrosHost, devserver will be used to trigger auto-update.
204 if host.support_devserver_provision:
xixuan5dc64ea2016-05-20 17:27:51 -0700205 image_name, host_attributes = host.machine_install_by_devserver(
206 *args, **dargs)
xixuanec801e32016-08-25 10:20:22 -0700207 else:
208 image_name, host_attributes = host.machine_install(*args, **dargs)
Dan Shibe3636a2016-02-14 22:48:01 -0800209 for attribute, value in host_attributes.items():
210 update_host_attribute(host, attribute, value)
Kevin Chenge12e71d2016-08-15 11:37:18 -0700211 add_version_label(host, image_name)