blob: 7fb3cf96e05a7cf6d0967c84e38fd18d6076274e [file] [log] [blame]
J. Richard Barnette24adbf42012-04-11 15:04:53 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Dale Curtisaa5eedb2011-08-23 16:18:52 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Aviv Keshet74c89a92013-02-04 15:18:30 -08005import functools
Christopher Wiley0ed712b2013-04-09 15:25:12 -07006import httplib
J. Richard Barnette1d78b012012-05-15 13:56:30 -07007import logging
Dan Shi0f466e82013-02-22 15:44:58 -08008import os
Simran Basid5e5e272012-09-24 15:23:59 -07009import re
Christopher Wileyd78249a2013-03-01 13:05:31 -080010import socket
J. Richard Barnette1d78b012012-05-15 13:56:30 -070011import subprocess
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070012import time
J. Richard Barnette1d78b012012-05-15 13:56:30 -070013import xmlrpclib
J. Richard Barnette134ec2c2012-04-25 12:59:37 -070014
J. Richard Barnette45e93de2012-04-11 17:24:15 -070015from autotest_lib.client.bin import utils
Richard Barnette0c73ffc2012-11-19 15:21:18 -080016from autotest_lib.client.common_lib import error
17from autotest_lib.client.common_lib import global_config
J. Richard Barnette45e93de2012-04-11 17:24:15 -070018from autotest_lib.client.common_lib.cros import autoupdater
Richard Barnette03a0c132012-11-05 12:40:35 -080019from autotest_lib.client.common_lib.cros import dev_server
Christopher Wileyd78249a2013-03-01 13:05:31 -080020from autotest_lib.client.common_lib.cros import retry
Richard Barnette82c35912012-11-20 10:09:10 -080021from autotest_lib.client.cros import constants
J. Richard Barnette45e93de2012-04-11 17:24:15 -070022from autotest_lib.server import autoserv_parser
Chris Sosaf4d43ff2012-10-30 11:21:05 -070023from autotest_lib.server import autotest
Scott Zawalski89c44dd2013-02-26 09:28:02 -050024from autotest_lib.server.cros.dynamic_suite import constants as ds_constants
Simran Basi5e6339a2013-03-21 11:34:32 -070025from autotest_lib.server.cros.dynamic_suite import tools, frontend_wrappers
J. Richard Barnette75487572013-03-08 12:47:50 -080026from autotest_lib.server.cros.servo import servo
J. Richard Barnette45e93de2012-04-11 17:24:15 -070027from autotest_lib.server.hosts import remote
Simran Basidcff4252012-11-20 16:13:20 -080028from autotest_lib.site_utils.rpm_control_system import rpm_client
Simran Basid5e5e272012-09-24 15:23:59 -070029
30
J. Richard Barnettebe5ebcc2013-02-11 16:03:15 -080031def _make_servo_hostname(hostname):
32 host_parts = hostname.split('.')
33 host_parts[0] = host_parts[0] + '-servo'
34 return '.'.join(host_parts)
35
36
37def _get_lab_servo(target_hostname):
38 """Instantiate a Servo for |target_hostname| in the lab.
39
40 Assuming that |target_hostname| is a device in the CrOS test
41 lab, create and return a Servo object pointed at the servo
42 attached to that DUT. The servo in the test lab is assumed
43 to already have servod up and running on it.
44
45 @param target_hostname: device whose servo we want to target.
46 @return an appropriately configured Servo instance.
47 """
48 servo_host = _make_servo_hostname(target_hostname)
49 if utils.host_is_in_lab_zone(servo_host):
50 try:
J. Richard Barnetted5f807a2013-02-11 16:51:00 -080051 return servo.Servo(servo_host=servo_host)
J. Richard Barnettebe5ebcc2013-02-11 16:03:15 -080052 except: # pylint: disable=W0702
53 # TODO(jrbarnette): Long-term, if we can't get to
54 # a servo in the lab, we want to fail, so we should
55 # pass any exceptions along. Short-term, we're not
56 # ready to rely on servo, so we ignore failures.
57 pass
58 return None
59
60
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070061def make_ssh_command(user='root', port=22, opts='', hosts_file=None,
62 connect_timeout=None, alive_interval=None):
63 """Override default make_ssh_command to use options tuned for Chrome OS.
64
65 Tuning changes:
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070066 - ConnectTimeout=30; maximum of 30 seconds allowed for an SSH connection
67 failure. Consistency with remote_access.sh.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070068
Dale Curtisaa5eedb2011-08-23 16:18:52 -070069 - ServerAliveInterval=180; which causes SSH to ping connection every
70 180 seconds. In conjunction with ServerAliveCountMax ensures that if the
71 connection dies, Autotest will bail out quickly. Originally tried 60 secs,
72 but saw frequent job ABORTS where the test completed successfully.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070073
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070074 - ServerAliveCountMax=3; consistency with remote_access.sh.
75
76 - ConnectAttempts=4; reduce flakiness in connection errors; consistency
77 with remote_access.sh.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070078
79 - UserKnownHostsFile=/dev/null; we don't care about the keys. Host keys
80 change with every new installation, don't waste memory/space saving them.
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070081
82 - SSH protocol forced to 2; needed for ServerAliveInterval.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -080083
84 @param user User name to use for the ssh connection.
85 @param port Port on the target host to use for ssh connection.
86 @param opts Additional options to the ssh command.
87 @param hosts_file Ignored.
88 @param connect_timeout Ignored.
89 @param alive_interval Ignored.
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070090 """
91 base_command = ('/usr/bin/ssh -a -x %s -o StrictHostKeyChecking=no'
92 ' -o UserKnownHostsFile=/dev/null -o BatchMode=yes'
Chris Sosaf7fcd6e2011-09-27 17:30:47 -070093 ' -o ConnectTimeout=30 -o ServerAliveInterval=180'
94 ' -o ServerAliveCountMax=3 -o ConnectionAttempts=4'
95 ' -o Protocol=2 -l %s -p %d')
Dale Curtiscb7bfaf2011-06-07 16:21:57 -070096 return base_command % (opts, user, port)
J. Richard Barnette45e93de2012-04-11 17:24:15 -070097
98
J. Richard Barnette7214e0b2013-02-06 15:20:49 -080099
Aviv Keshet74c89a92013-02-04 15:18:30 -0800100def add_label_detector(label_function_list, label_list=None, label=None):
101 """Decorator used to group functions together into the provided list.
102 @param label_function_list: List of label detecting functions to add
103 decorated function to.
104 @param label_list: List of detectable labels to add detectable labels to.
105 (Default: None)
106 @param label: Label string that is detectable by this detection function
107 (Default: None)
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800108 """
Simran Basic6f1f7a2012-10-16 10:47:46 -0700109 def add_func(func):
Aviv Keshet74c89a92013-02-04 15:18:30 -0800110 """
111 @param func: The function to be added as a detector.
112 """
113 label_function_list.append(func)
114 if label and label_list is not None:
115 label_list.append(label)
Simran Basic6f1f7a2012-10-16 10:47:46 -0700116 return func
117 return add_func
118
119
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700120class SiteHost(remote.RemoteHost):
121 """Chromium OS specific subclass of Host."""
122
123 _parser = autoserv_parser.autoserv_parser
Scott Zawalski62bacae2013-03-05 10:40:32 -0500124 _AFE = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700125
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800126 # Time to wait for new kernel to be marked successful after
127 # auto update.
Chris Masone163cead2012-05-16 11:49:48 -0700128 _KERNEL_UPDATE_TIMEOUT = 120
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700129
Richard Barnette03a0c132012-11-05 12:40:35 -0800130 # Timeout values (in seconds) associated with various Chrome OS
131 # state changes.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700132 #
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800133 # In general, a good rule of thumb is that the timeout can be up
134 # to twice the typical measured value on the slowest platform.
135 # The times here have not necessarily been empirically tested to
136 # meet this criterion.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700137 #
138 # SLEEP_TIMEOUT: Time to allow for suspend to memory.
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800139 # RESUME_TIMEOUT: Time to allow for resume after suspend, plus
140 # time to restart the netwowrk.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700141 # BOOT_TIMEOUT: Time to allow for boot from power off. Among
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800142 # other things, this must account for the 30 second dev-mode
J. Richard Barnetted4649c62013-03-06 17:42:27 -0800143 # screen delay and time to start the network.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700144 # USB_BOOT_TIMEOUT: Time to allow for boot from a USB device,
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800145 # including the 30 second dev-mode delay and time to start the
J. Richard Barnetted4649c62013-03-06 17:42:27 -0800146 # network.
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800147 # SHUTDOWN_TIMEOUT: Time to allow for shut down.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700148 # REBOOT_TIMEOUT: How long to wait for a reboot.
Richard Barnette03a0c132012-11-05 12:40:35 -0800149 # _INSTALL_TIMEOUT: Time to allow for chromeos-install.
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700150
151 SLEEP_TIMEOUT = 2
J. Richard Barnetted4649c62013-03-06 17:42:27 -0800152 RESUME_TIMEOUT = 10
J. Richard Barnetteeb69d722012-06-18 17:29:44 -0700153 BOOT_TIMEOUT = 45
154 USB_BOOT_TIMEOUT = 150
Chris Sosab76e0ee2013-05-22 16:55:41 -0700155
156 # We have a long timeout to ensure we don't flakily fail due to other
157 # issues. Shorter timeouts are vetted in platform_RebootAfterUpdate.
158 REBOOT_TIMEOUT = 300
159
Richard Barnette03a0c132012-11-05 12:40:35 -0800160 _INSTALL_TIMEOUT = 240
161
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800162 # _USB_POWER_TIMEOUT: Time to allow for USB to power toggle ON and OFF.
163 # _POWER_CYCLE_TIMEOUT: Time to allow for manual power cycle.
164 _USB_POWER_TIMEOUT = 5
165 _POWER_CYCLE_TIMEOUT = 10
166
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800167
Richard Barnette82c35912012-11-20 10:09:10 -0800168 _RPM_RECOVERY_BOARDS = global_config.global_config.get_config_value('CROS',
169 'rpm_recovery_boards', type=str).split(',')
170
171 _MAX_POWER_CYCLE_ATTEMPTS = 6
172 _LAB_MACHINE_FILE = '/mnt/stateful_partition/.labmachine'
173 _RPM_HOSTNAME_REGEX = ('chromeos[0-9]+(-row[0-9]+)?-rack[0-9]+[a-z]*-'
174 'host[0-9]+')
175 _LIGHTSENSOR_FILES = ['in_illuminance0_input',
176 'in_illuminance0_raw',
177 'illuminance0_input']
178 _LIGHTSENSOR_SEARCH_DIR = '/sys/bus/iio/devices'
179 _LABEL_FUNCTIONS = []
Aviv Keshet74c89a92013-02-04 15:18:30 -0800180 _DETECTABLE_LABELS = []
181 label_decorator = functools.partial(add_label_detector, _LABEL_FUNCTIONS,
182 _DETECTABLE_LABELS)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700183
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -0800184 # Constants used in ping_wait_up() and ping_wait_down().
185 #
186 # _PING_WAIT_COUNT is the approximate number of polling
187 # cycles to use when waiting for a host state change.
188 #
189 # _PING_STATUS_DOWN and _PING_STATUS_UP are names used
190 # for arguments to the internal _ping_wait_for_status()
191 # method.
192 _PING_WAIT_COUNT = 40
193 _PING_STATUS_DOWN = False
194 _PING_STATUS_UP = True
195
Ismail Noorbasha07fdb612013-02-14 14:13:31 -0800196 # Allowed values for the power_method argument.
197
198 # POWER_CONTROL_RPM: Passed as default arg for power_off/on/cycle() methods.
199 # POWER_CONTROL_SERVO: Used in set_power() and power_cycle() methods.
200 # POWER_CONTROL_MANUAL: Used in set_power() and power_cycle() methods.
201 POWER_CONTROL_RPM = 'RPM'
202 POWER_CONTROL_SERVO = 'servoj10'
203 POWER_CONTROL_MANUAL = 'manual'
204
205 POWER_CONTROL_VALID_ARGS = (POWER_CONTROL_RPM,
206 POWER_CONTROL_SERVO,
207 POWER_CONTROL_MANUAL)
Richard Barnette0c73ffc2012-11-19 15:21:18 -0800208
Simran Basi5e6339a2013-03-21 11:34:32 -0700209 _RPM_OUTLET_CHANGED = 'outlet_changed'
210
J. Richard Barnette964fba02012-10-24 17:34:29 -0700211 @staticmethod
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800212 def get_servo_arguments(args_dict):
213 """Extract servo options from `args_dict` and return the result.
214
215 Take the provided dictionary of argument options and return
216 a subset that represent standard arguments needed to
217 construct a servo object for a host. The intent is to
218 provide standard argument processing from run_remote_tests
219 for tests that require a servo to operate.
220
221 Recommended usage:
222 ~~~~~~~~
223 args_dict = utils.args_to_dict(args)
224 servo_args = hosts.SiteHost.get_servo_arguments(args_dict)
225 host = hosts.create_host(machine, servo_args=servo_args)
226 ~~~~~~~~
227
228 @param args_dict Dictionary from which to extract the servo
229 arguments.
230 """
J. Richard Barnette964fba02012-10-24 17:34:29 -0700231 servo_args = {}
232 for arg in ('servo_host', 'servo_port'):
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800233 if arg in args_dict:
234 servo_args[arg] = args_dict[arg]
J. Richard Barnette964fba02012-10-24 17:34:29 -0700235 return servo_args
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700236
J. Richard Barnette964fba02012-10-24 17:34:29 -0700237
238 def _initialize(self, hostname, servo_args=None, *args, **dargs):
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700239 """Initialize superclasses, and |self.servo|.
240
241 For creating the host servo object, there are three
242 possibilities: First, if the host is a lab system known to
243 have a servo board, we connect to that servo unconditionally.
244 Second, if we're called from a control file that requires
J. Richard Barnette55fb8062012-05-23 10:29:31 -0700245 servo features for testing, it will pass settings for
246 `servo_host`, `servo_port`, or both. If neither of these
247 cases apply, `self.servo` will be `None`.
J. Richard Barnette67ccb872012-04-19 16:34:56 -0700248
249 """
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700250 super(SiteHost, self)._initialize(hostname=hostname,
251 *args, **dargs)
J. Richard Barnettef0859852012-08-20 14:55:50 -0700252 # self.env is a dictionary of environment variable settings
253 # to be exported for commands run on the host.
254 # LIBC_FATAL_STDERR_ can be useful for diagnosing certain
255 # errors that might happen.
256 self.env['LIBC_FATAL_STDERR_'] = '1'
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700257 self._xmlrpc_proxy_map = {}
J. Richard Barnettebe5ebcc2013-02-11 16:03:15 -0800258 self.servo = _get_lab_servo(hostname)
J. Richard Barnettead7da482012-10-30 16:46:52 -0700259 if not self.servo and servo_args is not None:
J. Richard Barnette964fba02012-10-24 17:34:29 -0700260 self.servo = servo.Servo(**servo_args)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700261
262
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500263 def get_repair_image_name(self):
264 """Generate a image_name from variables in the global config.
265
266 @returns a str of $board-version/$BUILD.
267
268 """
269 stable_version = global_config.global_config.get_config_value(
270 'CROS', 'stable_cros_version')
271 build_pattern = global_config.global_config.get_config_value(
272 'CROS', 'stable_build_pattern')
273 board = self._get_board_from_afe()
274 if board is None:
275 raise error.AutoservError('DUT has no board attribute, '
276 'cannot be repaired.')
277 return build_pattern % (board, stable_version)
278
279
Scott Zawalski62bacae2013-03-05 10:40:32 -0500280 def _host_in_AFE(self):
281 """Check if the host is an object the AFE knows.
282
283 @returns the host object.
284 """
285 return self._AFE.get_hosts(hostname=self.hostname)
286
287
Chris Sosab76e0ee2013-05-22 16:55:41 -0700288 def lookup_job_repo_url(self):
289 """Looks up the job_repo_url for the host.
290
291 @returns job_repo_url from AFE or None if not found.
292
293 @raises KeyError if the host does not have a job_repo_url
294 """
295 if not self._host_in_AFE():
296 return None
297
298 hosts = self._AFE.get_hosts(hostname=self.hostname)
beepsb5efc532013-06-04 11:29:34 -0700299 if hosts and ds_constants.JOB_REPO_URL in hosts[0].attributes:
300 return hosts[0].attributes[ds_constants.JOB_REPO_URL]
Chris Sosab76e0ee2013-05-22 16:55:41 -0700301
302
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500303 def clear_cros_version_labels_and_job_repo_url(self):
304 """Clear cros_version labels and host attribute job_repo_url."""
Scott Zawalski62bacae2013-03-05 10:40:32 -0500305 if not self._host_in_AFE():
Scott Zawalskieadbf702013-03-14 09:23:06 -0400306 return
307
Scott Zawalski62bacae2013-03-05 10:40:32 -0500308 host_list = [self.hostname]
309 labels = self._AFE.get_labels(
310 name__startswith=ds_constants.VERSION_PREFIX,
311 host__hostname=self.hostname)
Dan Shi0f466e82013-02-22 15:44:58 -0800312
Scott Zawalski62bacae2013-03-05 10:40:32 -0500313 for label in labels:
314 label.remove_hosts(hosts=host_list)
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500315
beepscb6f1e22013-06-28 19:14:10 -0700316 self.update_job_repo_url(None, None)
317
318
319 def update_job_repo_url(self, devserver_url, image_name):
320 """
321 Updates the job_repo_url host attribute and asserts it's value.
322
323 @param devserver_url: The devserver to use in the job_repo_url.
324 @param image_name: The name of the image to use in the job_repo_url.
325
326 @raises AutoservError: If we failed to update the job_repo_url.
327 """
328 repo_url = None
329 if devserver_url and image_name:
330 repo_url = tools.get_package_url(devserver_url, image_name)
331 self._AFE.set_host_attribute(ds_constants.JOB_REPO_URL, repo_url,
Scott Zawalski62bacae2013-03-05 10:40:32 -0500332 hostname=self.hostname)
beepscb6f1e22013-06-28 19:14:10 -0700333 if self.lookup_job_repo_url() != repo_url:
334 raise error.AutoservError('Failed to update job_repo_url with %s, '
335 'host %s' % (repo_url, self.hostname))
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500336
337
Dan Shie9309262013-06-19 22:50:21 -0700338 def add_cros_version_labels_and_job_repo_url(self, image_name):
Scott Zawalskieadbf702013-03-14 09:23:06 -0400339 """Add cros_version labels and host attribute job_repo_url.
340
341 @param image_name: The name of the image e.g.
342 lumpy-release/R27-3837.0.0
Dan Shi7458bf62013-06-10 12:50:16 -0700343
Scott Zawalskieadbf702013-03-14 09:23:06 -0400344 """
Scott Zawalski62bacae2013-03-05 10:40:32 -0500345 if not self._host_in_AFE():
Scott Zawalskieadbf702013-03-14 09:23:06 -0400346 return
Scott Zawalski62bacae2013-03-05 10:40:32 -0500347
Scott Zawalskieadbf702013-03-14 09:23:06 -0400348 cros_label = '%s%s' % (ds_constants.VERSION_PREFIX, image_name)
Dan Shie9309262013-06-19 22:50:21 -0700349 devserver_url = dev_server.ImageServer.resolve(image_name).url()
Scott Zawalski62bacae2013-03-05 10:40:32 -0500350
351 labels = self._AFE.get_labels(name=cros_label)
352 if labels:
353 label = labels[0]
354 else:
355 label = self._AFE.create_label(name=cros_label)
356
357 label.add_hosts([self.hostname])
beepscb6f1e22013-06-28 19:14:10 -0700358 self.update_job_repo_url(devserver_url, image_name)
359
360
361 def verify_job_repo_url(self):
362 """
363 Make sure job_repo_url of this host is valid.
364
365 Eg: The job_repo_url "http://lmn.cd.ab.xyx:8080/static/archive/\
366 lumpy-release/R29-4279.0.0/autotest/packages" claims to have the
367 autotest package for lumpy-release/R29-4279.0.0. If this isn't the case,
368 download and extract it. If the devserver embedded in the url is
369 unresponsive, update the job_repo_url of the host after staging it on
370 another devserver.
371
372 @param job_repo_url: A url pointing to the devserver where the autotest
373 package for this build should be staged.
374
375 @raises DevServerException: If we could not resolve a devserver.
376 @raises AutoservError: If we're unable to save the new job_repo_url as
377 a result of choosing a new devserver because the old one failed to
378 respond to a health check.
379 """
380 job_repo_url = self.lookup_job_repo_url()
381 if not job_repo_url:
382 logging.warning('No job repo url set on host %s', self.hostname)
383 return
384
385 logging.info('Verifying job repo url %s', job_repo_url)
386 devserver_url, image_name = tools.get_devserver_build_from_package_url(
387 job_repo_url)
388
389 ds = dev_server.ImageServer.resolve(image_name)
390
391 logging.info('Staging autotest artifacts for %s on devserver %s',
392 image_name, ds.url())
393 ds.stage_artifacts(image_name, ['autotest'])
394
395 if ds.url() != devserver_url:
Dan Shi7458bf62013-06-10 12:50:16 -0700396 logging.info('Devserver url changed, new devserver is %s, '
beepscb6f1e22013-06-28 19:14:10 -0700397 'old devserver was %s',
398 ds.url(), devserver_url)
399 self.update_job_repo_url(ds.url(), image_name)
Scott Zawalskieadbf702013-03-14 09:23:06 -0400400
401
Dan Shi0f466e82013-02-22 15:44:58 -0800402 def _try_stateful_update(self, update_url, force_update, updater):
403 """Try to use stateful update to initialize DUT.
404
405 When DUT is already running the same version that machine_install
406 tries to install, stateful update is a much faster way to clean up
407 the DUT for testing, compared to a full reimage. It is implemeted
408 by calling autoupdater.run_update, but skipping updating root, as
409 updating the kernel is time consuming and not necessary.
410
411 @param update_url: url of the image.
412 @param force_update: Set to True to update the image even if the DUT
413 is running the same version.
414 @param updater: ChromiumOSUpdater instance used to update the DUT.
415 @returns: True if the DUT was updated with stateful update.
416
417 """
418 if not updater.check_version():
419 return False
420 if not force_update:
421 logging.info('Canceling stateful update because the new and '
422 'old versions are the same.')
423 return False
424 # Following folders should be rebuilt after stateful update.
425 # A test file is used to confirm each folder gets rebuilt after
426 # the stateful update.
427 folders_to_check = ['/var', '/home', '/mnt/stateful_partition']
428 test_file = '.test_file_to_be_deleted'
429 for folder in folders_to_check:
430 touch_path = os.path.join(folder, test_file)
431 self.run('touch %s' % touch_path)
432
433 if not updater.run_update(force_update=True, update_root=False):
434 return False
435
436 # Reboot to complete stateful update.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700437 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
Dan Shi0f466e82013-02-22 15:44:58 -0800438 check_file_cmd = 'test -f %s; echo $?'
439 for folder in folders_to_check:
440 test_file_path = os.path.join(folder, test_file)
441 result = self.run(check_file_cmd % test_file_path,
442 ignore_status=True)
443 if result.exit_status == 1:
444 return False
445 return True
446
447
J. Richard Barnette7275b612013-06-04 18:13:11 -0700448 def _post_update_processing(self, updater, expected_kernel=None):
Dan Shi0f466e82013-02-22 15:44:58 -0800449 """After the DUT is updated, confirm machine_install succeeded.
450
451 @param updater: ChromiumOSUpdater instance used to update the DUT.
J. Richard Barnette7275b612013-06-04 18:13:11 -0700452 @param expected_kernel: kernel expected to be active after reboot,
453 or `None` to skip rollback checking.
Dan Shi0f466e82013-02-22 15:44:58 -0800454
455 """
J. Richard Barnette7275b612013-06-04 18:13:11 -0700456 # Touch the lab machine file to leave a marker that
457 # distinguishes this image from other test images.
458 # Afterwards, we must re-run the autoreboot script because
459 # it depends on the _LAB_MACHINE_FILE.
Dan Shi0f466e82013-02-22 15:44:58 -0800460 self.run('touch %s' % self._LAB_MACHINE_FILE)
Dan Shi0f466e82013-02-22 15:44:58 -0800461 self.run('start autoreboot')
462
J. Richard Barnette7275b612013-06-04 18:13:11 -0700463 # Figure out the newly active kernel.
464 active_kernel, _ = updater.get_kernel_state()
465
466 # Check for rollback due to a bad build.
467 if expected_kernel and active_kernel != expected_kernel:
468 # Print out some information to make it easier to debug
469 # the rollback.
Dan Shi0f466e82013-02-22 15:44:58 -0800470 logging.debug('Dumping partition table.')
Dan Shi346725f2013-03-20 15:22:38 -0700471 self.run('cgpt show $(rootdev -s -d)')
Dan Shi0f466e82013-02-22 15:44:58 -0800472 logging.debug('Dumping crossystem for firmware debugging.')
Dan Shi346725f2013-03-20 15:22:38 -0700473 self.run('crossystem --all')
Dan Shi0f466e82013-02-22 15:44:58 -0800474 raise autoupdater.ChromiumOSError(
J. Richard Barnette7275b612013-06-04 18:13:11 -0700475 'Build %s failed to boot on %s; system rolled back '
476 'to previous build' % (updater.update_version,
477 self.hostname))
Dan Shi0f466e82013-02-22 15:44:58 -0800478
J. Richard Barnette7275b612013-06-04 18:13:11 -0700479 # Check that we've got the build we meant to install.
480 if not updater.check_version_to_confirm_install():
481 raise autoupdater.ChromiumOSError(
482 'Failed to update %s to build %s; found build '
483 '%s instead' % (self.hostname,
484 updater.update_version,
485 updater.get_build_id()))
Scott Zawalski62bacae2013-03-05 10:40:32 -0500486
J. Richard Barnette7275b612013-06-04 18:13:11 -0700487 # Make sure chromeos-setgoodkernel runs.
488 try:
Dan Shi0f466e82013-02-22 15:44:58 -0800489 utils.poll_for_condition(
J. Richard Barnette7275b612013-06-04 18:13:11 -0700490 lambda: (updater.get_kernel_tries(active_kernel) == 0
491 and updater.get_kernel_success(active_kernel)),
492 exception=autoupdater.ChromiumOSError(),
Dan Shi0f466e82013-02-22 15:44:58 -0800493 timeout=self._KERNEL_UPDATE_TIMEOUT, sleep_interval=5)
J. Richard Barnette7275b612013-06-04 18:13:11 -0700494 except autoupdater.ChromiumOSError as e:
495 services_status = self.run('status system-services').stdout
496 if services_status != 'system-services start/running\n':
497 event = ('Chrome failed to reach login screen')
498 else:
499 event = ('update-engine failed to call '
500 'chromeos-setgoodkernel')
501 raise autoupdater.ChromiumOSError(
502 'After update and reboot, %s '
503 'within %d seconds' % (event,
504 self._KERNEL_UPDATE_TIMEOUT))
Dan Shi0f466e82013-02-22 15:44:58 -0800505
506
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700507 def _stage_image_for_update(self, image_name=None):
Scott Zawalskieadbf702013-03-14 09:23:06 -0400508 """Stage a build on a devserver and return the update_url.
509
510 @param image_name: a name like lumpy-release/R27-3837.0.0
511 @returns an update URL like:
512 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0
513 """
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700514 if not image_name:
515 image_name = self.get_repair_image_name()
516 logging.info('Staging build for AU: %s', image_name)
Scott Zawalskieadbf702013-03-14 09:23:06 -0400517 devserver = dev_server.ImageServer.resolve(image_name)
518 devserver.trigger_download(image_name, synchronous=False)
519 return tools.image_url_pattern() % (devserver.url(), image_name)
520
521
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700522 def stage_image_for_servo(self, image_name=None):
523 """Stage a build on a devserver and return the update_url.
524
525 @param image_name: a name like lumpy-release/R27-3837.0.0
526 @returns an update URL like:
527 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0
528 """
529 if not image_name:
530 image_name = self.get_repair_image_name()
531 logging.info('Staging build for servo install: %s', image_name)
532 devserver = dev_server.ImageServer.resolve(image_name)
533 devserver.stage_artifacts(image_name, ['test_image'])
534 return devserver.get_test_image_url(image_name)
535
536
Chris Sosaa3ac2152012-05-23 22:23:13 -0700537 def machine_install(self, update_url=None, force_update=False,
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500538 local_devserver=False, repair=False):
539 """Install the DUT.
540
Dan Shi0f466e82013-02-22 15:44:58 -0800541 Use stateful update if the DUT is already running the same build.
542 Stateful update does not update kernel and tends to run much faster
543 than a full reimage. If the DUT is running a different build, or it
544 failed to do a stateful update, full update, including kernel update,
545 will be applied to the DUT.
546
Scott Zawalskieadbf702013-03-14 09:23:06 -0400547 Once a host enters machine_install its cros_version label will be
548 removed as well as its host attribute job_repo_url (used for
549 package install).
550
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500551 @param update_url: The url to use for the update
552 pattern: http://$devserver:###/update/$build
553 If update_url is None and repair is True we will install the
554 stable image listed in global_config under
555 CROS.stable_cros_version.
556 @param force_update: Force an update even if the version installed
557 is the same. Default:False
558 @param local_devserver: Used by run_remote_test to allow people to
559 use their local devserver. Default: False
560 @param repair: Whether or not we are in repair mode. This adds special
561 cases for repairing a machine like starting update_engine.
562 Setting repair to True sets force_update to True as well.
563 default: False
564 @raises autoupdater.ChromiumOSError
565
566 """
Dan Shi7458bf62013-06-10 12:50:16 -0700567 if update_url:
568 logging.debug('update url is set to %s', update_url)
569 else:
570 logging.debug('update url is not set, resolving...')
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700571 if self._parser.options.image:
572 requested_build = self._parser.options.image
573 if requested_build.startswith('http://'):
574 update_url = requested_build
Dan Shi7458bf62013-06-10 12:50:16 -0700575 logging.debug('update url is retrieved from requested_build'
576 ': %s', update_url)
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700577 else:
578 # Try to stage any build that does not start with
579 # http:// on the devservers defined in
580 # global_config.ini.
Dan Shi7458bf62013-06-10 12:50:16 -0700581 update_url = self._stage_image_for_update(requested_build)
582 logging.debug('Build staged, and update_url is set to: %s',
583 update_url)
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700584 elif repair:
585 update_url = self._stage_image_for_update()
Dan Shi7458bf62013-06-10 12:50:16 -0700586 logging.debug('Build staged, and update_url is set to: %s',
587 update_url)
Scott Zawalskieadbf702013-03-14 09:23:06 -0400588 else:
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700589 raise autoupdater.ChromiumOSError(
590 'Update failed. No update URL provided.')
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500591
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500592 if repair:
Dan Shi0f466e82013-02-22 15:44:58 -0800593 # In case the system is in a bad state, we always reboot the machine
594 # before machine_install.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700595 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500596 self.run('stop update-engine; start update-engine')
597 force_update = True
Dan Shi0f466e82013-02-22 15:44:58 -0800598
Chris Sosaa3ac2152012-05-23 22:23:13 -0700599 updater = autoupdater.ChromiumOSUpdater(update_url, host=self,
Chris Sosa72312602013-04-16 15:01:56 -0700600 local_devserver=local_devserver)
Dan Shi0f466e82013-02-22 15:44:58 -0800601 updated = False
Scott Zawalskieadbf702013-03-14 09:23:06 -0400602 # Remove cros-version and job_repo_url host attribute from host.
603 self.clear_cros_version_labels_and_job_repo_url()
Dan Shi0f466e82013-02-22 15:44:58 -0800604 # If the DUT is already running the same build, try stateful update
605 # first. Stateful update does not update kernel and tends to run much
606 # faster than a full reimage.
607 try:
Chris Sosab76e0ee2013-05-22 16:55:41 -0700608 updated = self._try_stateful_update(
609 update_url, force_update, updater)
Dan Shi0f466e82013-02-22 15:44:58 -0800610 if updated:
611 logging.info('DUT is updated with stateful update.')
612 except Exception as e:
613 logging.exception(e)
614 logging.warn('Failed to stateful update DUT, force to update.')
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700615
Dan Shi0f466e82013-02-22 15:44:58 -0800616 inactive_kernel = None
617 # Do a full update if stateful update is not applicable or failed.
618 if not updated:
619 # In case the system is in a bad state, we always reboot the
620 # machine before machine_install.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700621 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
Chris Sosab7612bc2013-03-21 10:32:37 -0700622
623 # TODO(sosa): Remove temporary hack to get rid of bricked machines
624 # that can't update due to a corrupted policy.
625 self.run('rm -rf /var/lib/whitelist')
626 self.run('touch /var/lib/whitelist')
627 self.run('chmod -w /var/lib/whitelist')
Scott Zawalskib550d5a2013-03-22 09:23:59 -0400628 self.run('stop update-engine; start update-engine')
Chris Sosab7612bc2013-03-21 10:32:37 -0700629
Dan Shi0f466e82013-02-22 15:44:58 -0800630 if updater.run_update(force_update):
631 updated = True
632 # Figure out active and inactive kernel.
633 active_kernel, inactive_kernel = updater.get_kernel_state()
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700634
Dan Shi0f466e82013-02-22 15:44:58 -0800635 # Ensure inactive kernel has higher priority than active.
636 if (updater.get_kernel_priority(inactive_kernel)
637 < updater.get_kernel_priority(active_kernel)):
638 raise autoupdater.ChromiumOSError(
639 'Update failed. The priority of the inactive kernel'
640 ' partition is less than that of the active kernel'
641 ' partition.')
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700642
Dan Shi0f466e82013-02-22 15:44:58 -0800643 update_engine_log = '/var/log/update_engine.log'
644 logging.info('Dumping %s', update_engine_log)
645 self.run('cat %s' % update_engine_log)
646 # Updater has returned successfully; reboot the host.
Chris Sosab76e0ee2013-05-22 16:55:41 -0700647 self.reboot(timeout=self.REBOOT_TIMEOUT, wait=True)
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700648
Dan Shi0f466e82013-02-22 15:44:58 -0800649 if updated:
650 self._post_update_processing(updater, inactive_kernel)
Scott Zawalskieadbf702013-03-14 09:23:06 -0400651 image_name = autoupdater.url_to_image_name(update_url)
Dan Shie9309262013-06-19 22:50:21 -0700652 self.add_cros_version_labels_and_job_repo_url(image_name)
Simran Basi13fa1ba2013-03-04 10:56:47 -0800653
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700654 # Clean up any old autotest directories which may be lying around.
655 for path in global_config.global_config.get_config_value(
656 'AUTOSERV', 'client_autodir_paths', type=list):
657 self.run('rm -rf ' + path)
658
659
Simran Basi833814b2013-01-29 13:13:43 -0800660 def _get_label_from_afe(self, label_prefix):
661 """Retrieve a host's specific label from the AFE.
662
663 Looks for a host label that has the form <label_prefix>:<value>
664 and returns the "<value>" part of the label. None is returned
665 if there is not a label matching the pattern
666
667 @returns the label that matches the prefix or 'None'
668 """
Scott Zawalski62bacae2013-03-05 10:40:32 -0500669 labels = self._AFE.get_labels(name__startswith=label_prefix,
670 host__hostname__in=[self.hostname])
671 if labels and len(labels) == 1:
672 return labels[0].name.split(label_prefix, 1)[1]
Simran Basi833814b2013-01-29 13:13:43 -0800673
674
Richard Barnette82c35912012-11-20 10:09:10 -0800675 def _get_board_from_afe(self):
676 """Retrieve this host's board from its labels in the AFE.
677
678 Looks for a host label of the form "board:<board>", and
679 returns the "<board>" part of the label. `None` is returned
680 if there is not a single, unique label matching the pattern.
681
682 @returns board from label, or `None`.
683 """
Simran Basi833814b2013-01-29 13:13:43 -0800684 return self._get_label_from_afe(ds_constants.BOARD_PREFIX)
685
686
687 def get_build(self):
688 """Retrieve the current build for this Host from the AFE.
689
690 Looks through this host's labels in the AFE to determine its build.
691
692 @returns The current build or None if it could not find it or if there
693 were multiple build labels assigned to this host.
694 """
695 return self._get_label_from_afe(ds_constants.VERSION_PREFIX)
Richard Barnette82c35912012-11-20 10:09:10 -0800696
697
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500698 def _install_repair(self):
699 """Attempt to repair this host using upate-engine.
700
701 If the host is up, try installing the DUT with a stable
702 "repair" version of Chrome OS as defined in the global_config
703 under CROS.stable_cros_version.
704
Scott Zawalski62bacae2013-03-05 10:40:32 -0500705 @raises AutoservRepairMethodNA if the DUT is not reachable.
706 @raises ChromiumOSError if the install failed for some reason.
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500707
708 """
709 if not self.is_up():
Scott Zawalski62bacae2013-03-05 10:40:32 -0500710 raise error.AutoservRepairMethodNA('DUT unreachable for install.')
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500711
712 logging.info('Attempting to reimage machine to repair image.')
713 try:
714 self.machine_install(repair=True)
Fang Dengd0672f32013-03-18 17:18:09 -0700715 except autoupdater.ChromiumOSError as e:
716 logging.exception(e)
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500717 logging.info('Repair via install failed.')
Scott Zawalski62bacae2013-03-05 10:40:32 -0500718 raise
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500719
720
Scott Zawalski62bacae2013-03-05 10:40:32 -0500721 def servo_install(self, image_url=None):
722 """
723 Re-install the OS on the DUT by:
724 1) installing a test image on a USB storage device attached to the Servo
725 board,
Richard Barnette03a0c132012-11-05 12:40:35 -0800726 2) booting that image in recovery mode, and then
J. Richard Barnette31b2e312013-04-04 16:05:22 -0700727 3) installing the image with chromeos-install.
728
Scott Zawalski62bacae2013-03-05 10:40:32 -0500729 @param image_url: If specified use as the url to install on the DUT.
730 otherwise boot the currently staged image on the USB stick.
Richard Barnette03a0c132012-11-05 12:40:35 -0800731
Scott Zawalski62bacae2013-03-05 10:40:32 -0500732 @raises AutoservError if the image fails to boot.
Richard Barnette03a0c132012-11-05 12:40:35 -0800733 """
J. Richard Barnette31b2e312013-04-04 16:05:22 -0700734 self.servo.install_recovery_image(image_url)
Richard Barnette03a0c132012-11-05 12:40:35 -0800735 if not self.wait_up(timeout=self.USB_BOOT_TIMEOUT):
Scott Zawalski62bacae2013-03-05 10:40:32 -0500736 raise error.AutoservRepairFailure(
737 'DUT failed to boot from USB after %d seconds' %
738 self.USB_BOOT_TIMEOUT)
739
740 self.run('chromeos-install --yes', timeout=self._INSTALL_TIMEOUT)
Richard Barnette03a0c132012-11-05 12:40:35 -0800741 self.servo.power_long_press()
Fang Dengafb88142013-05-30 17:44:31 -0700742 self.servo.switch_usbkey('off')
Richard Barnette03a0c132012-11-05 12:40:35 -0800743 self.servo.power_short_press()
744 if not self.wait_up(timeout=self.BOOT_TIMEOUT):
745 raise error.AutoservError('DUT failed to reboot installed '
746 'test image after %d seconds' %
Scott Zawalski62bacae2013-03-05 10:40:32 -0500747 self.BOOT_TIMEOUT)
748
749
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700750 def _servo_repair_reinstall(self):
Scott Zawalski62bacae2013-03-05 10:40:32 -0500751 """Reinstall the DUT utilizing servo and a test image.
752
753 Re-install the OS on the DUT by:
754 1) installing a test image on a USB storage device attached to the Servo
755 board,
756 2) booting that image in recovery mode, and then
757 3) installing the image with chromeos-install.
758
Scott Zawalski62bacae2013-03-05 10:40:32 -0500759 @raises AutoservRepairMethodNA if the device does not have servo
760 support.
761
762 """
763 if not self.servo:
764 raise error.AutoservRepairMethodNA('Repair Reinstall NA: '
765 'DUT has no servo support.')
766
767 logging.info('Attempting to recovery servo enabled device with '
768 'servo_repair_reinstall')
769
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700770 image_url = self.stage_image_for_servo()
Scott Zawalski62bacae2013-03-05 10:40:32 -0500771 self.servo_install(image_url)
772
773
774 def _servo_repair_power(self):
775 """Attempt to repair DUT using an attached Servo.
776
777 Attempt to power on the DUT via power_long_press.
778
779 @raises AutoservRepairMethodNA if the device does not have servo
780 support.
781 @raises AutoservRepairFailure if the repair fails for any reason.
782 """
783 if not self.servo:
784 raise error.AutoservRepairMethodNA('Repair Power NA: '
785 'DUT has no servo support.')
786
787 logging.info('Attempting to recover servo enabled device by '
788 'powering it off and on.')
789 self.servo.get_power_state_controller().power_off()
790 self.servo.get_power_state_controller().power_on()
791 if self.wait_up(self.BOOT_TIMEOUT):
792 return
793
794 raise error.AutoservRepairFailure('DUT did not boot after long_press.')
Richard Barnette03a0c132012-11-05 12:40:35 -0800795
796
Richard Barnette82c35912012-11-20 10:09:10 -0800797 def _powercycle_to_repair(self):
798 """Utilize the RPM Infrastructure to bring the host back up.
799
800 If the host is not up/repaired after the first powercycle we utilize
801 auto fallback to the last good install by powercycling and rebooting the
802 host 6 times.
Scott Zawalski62bacae2013-03-05 10:40:32 -0500803
804 @raises AutoservRepairMethodNA if the device does not support remote
805 power.
806 @raises AutoservRepairFailure if the repair fails for any reason.
807
Richard Barnette82c35912012-11-20 10:09:10 -0800808 """
Scott Zawalski62bacae2013-03-05 10:40:32 -0500809 if not self.has_power():
810 raise error.AutoservRepairMethodNA('Device does not support power.')
811
Richard Barnette82c35912012-11-20 10:09:10 -0800812 logging.info('Attempting repair via RPM powercycle.')
813 failed_cycles = 0
814 self.power_cycle()
815 while not self.wait_up(timeout=self.BOOT_TIMEOUT):
816 failed_cycles += 1
817 if failed_cycles >= self._MAX_POWER_CYCLE_ATTEMPTS:
Scott Zawalski62bacae2013-03-05 10:40:32 -0500818 raise error.AutoservRepairFailure(
819 'Powercycled host %s %d times; device did not come back'
820 ' online.' % (self.hostname, failed_cycles))
Richard Barnette82c35912012-11-20 10:09:10 -0800821 self.power_cycle()
822 if failed_cycles == 0:
823 logging.info('Powercycling was successful first time.')
824 else:
825 logging.info('Powercycling was successful after %d failures.',
826 failed_cycles)
827
828
829 def repair_full(self):
830 """Repair a host for repair level NO_PROTECTION.
831
832 This overrides the base class function for repair; it does
833 not call back to the parent class, but instead offers a
834 simplified implementation based on the capabilities in the
835 Chrome OS test lab.
836
J. Richard Barnettefde55fc2013-03-15 17:47:01 -0700837 If `self.verify()` fails, the following procedures are
838 attempted:
839 1. Try to re-install to a known stable image using
840 auto-update.
Scott Zawalski62bacae2013-03-05 10:40:32 -0500841 2. If there's a servo for the DUT, try to power the DUT off and
842 on.
843 3. If there's a servo for the DUT, try to re-install via
J. Richard Barnettefde55fc2013-03-15 17:47:01 -0700844 the servo.
Scott Zawalski62bacae2013-03-05 10:40:32 -0500845 4. If the DUT can be power-cycled via RPM, try to repair
Richard Barnette82c35912012-11-20 10:09:10 -0800846 by power-cycling.
847
848 As with the parent method, the last operation performed on
849 the DUT must be to call `self.verify()`; if that call fails,
850 the exception it raises is passed back to the caller.
J. Richard Barnettefde55fc2013-03-15 17:47:01 -0700851
Scott Zawalski62bacae2013-03-05 10:40:32 -0500852 @raises AutoservRepairTotalFailure if the repair process fails to
853 fix the DUT.
Richard Barnette82c35912012-11-20 10:09:10 -0800854 """
Scott Zawalski62bacae2013-03-05 10:40:32 -0500855 # TODO(scottz): This should use something similar to label_decorator,
856 # but needs to be populated in order so DUTs are repaired with the
857 # least amount of effort.
858 repair_funcs = [self._install_repair, self._servo_repair_power,
J. Richard Barnettee4af8b92013-05-01 13:16:12 -0700859 self._servo_repair_reinstall,
Scott Zawalski62bacae2013-03-05 10:40:32 -0500860 self._powercycle_to_repair]
861 errors = []
862 for repair_func in repair_funcs:
863 try:
864 repair_func()
865 self.verify()
866 return
867 except Exception as e:
868 logging.warn('Failed to repair device: %s', e)
869 errors.append(str(e))
Scott Zawalski89c44dd2013-02-26 09:28:02 -0500870
Scott Zawalski62bacae2013-03-05 10:40:32 -0500871 raise error.AutoservRepairTotalFailure(
872 'All attempts at repairing the device failed:\n%s' %
873 '\n'.join(errors))
Richard Barnette82c35912012-11-20 10:09:10 -0800874
875
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700876 def close(self):
877 super(SiteHost, self).close()
878 self.xmlrpc_disconnect_all()
879
880
Simran Basi5e6339a2013-03-21 11:34:32 -0700881 def _cleanup_poweron(self):
882 """Special cleanup method to make sure hosts always get power back."""
883 afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
884 hosts = afe.get_hosts(hostname=self.hostname)
885 if not hosts or not (self._RPM_OUTLET_CHANGED in
886 hosts[0].attributes):
887 return
888 logging.debug('This host has recently interacted with the RPM'
889 ' Infrastructure. Ensuring power is on.')
890 try:
891 self.power_on()
892 except rpm_client.RemotePowerException:
893 # If cleanup has completed but there was an issue with the RPM
894 # Infrastructure, log an error message rather than fail cleanup
895 logging.error('Failed to turn Power On for this host after '
896 'cleanup through the RPM Infrastructure.')
897 afe.set_host_attribute(self._RPM_OUTLET_CHANGED, None,
898 hostname=self.hostname)
899
900
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700901 def cleanup(self):
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700902 client_at = autotest.Autotest(self)
Richard Barnette82c35912012-11-20 10:09:10 -0800903 self.run('rm -f %s' % constants.CLEANUP_LOGS_PAUSED_FILE)
Scott Zawalskiddbc31e2012-11-15 11:29:01 -0500904 try:
905 client_at.run_static_method('autotest_lib.client.cros.cros_ui',
906 '_clear_login_prompt_state')
907 self.run('restart ui')
908 client_at.run_static_method('autotest_lib.client.cros.cros_ui',
909 '_wait_for_login_prompt')
Alex Millerf4517962013-02-25 15:03:02 -0800910 except (error.AutotestRunError, error.AutoservRunError):
Scott Zawalskiddbc31e2012-11-15 11:29:01 -0500911 logging.warn('Unable to restart ui, rebooting device.')
912 # Since restarting the UI fails fall back to normal Autotest
913 # cleanup routines, i.e. reboot the machine.
914 super(SiteHost, self).cleanup()
Simran Basi5e6339a2013-03-21 11:34:32 -0700915 # Check if the rpm outlet was manipulated.
Simran Basid5e5e272012-09-24 15:23:59 -0700916 if self.has_power():
Simran Basi5e6339a2013-03-21 11:34:32 -0700917 self._cleanup_poweron()
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700918
919
Yu-Ju Honga2be94a2012-07-31 09:48:52 -0700920 def reboot(self, **dargs):
921 """
922 This function reboots the site host. The more generic
923 RemoteHost.reboot() performs sync and sleeps for 5
924 seconds. This is not necessary for Chrome OS devices as the
925 sync should be finished in a short time during the reboot
926 command.
927 """
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +0800928 if 'reboot_cmd' not in dargs:
929 dargs['reboot_cmd'] = ('((reboot & sleep 10; reboot -f &)'
930 ' </dev/null >/dev/null 2>&1 &)')
Yu-Ju Honga2be94a2012-07-31 09:48:52 -0700931 # Enable fastsync to avoid running extra sync commands before reboot.
Tom Wai-Hong Tamf5cd1d42012-08-13 12:04:08 +0800932 if 'fastsync' not in dargs:
933 dargs['fastsync'] = True
Yu-Ju Honga2be94a2012-07-31 09:48:52 -0700934 super(SiteHost, self).reboot(**dargs)
935
936
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700937 def verify_software(self):
Richard Barnetteb2bc13c2013-01-08 17:32:51 -0800938 """Verify working software on a Chrome OS system.
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700939
Richard Barnetteb2bc13c2013-01-08 17:32:51 -0800940 Tests for the following conditions:
941 1. All conditions tested by the parent version of this
942 function.
943 2. Sufficient space in /mnt/stateful_partition.
Fang Deng6b05f5b2013-03-20 13:42:11 -0700944 3. Sufficient space in /mnt/stateful_partition/encrypted.
945 4. update_engine answers a simple status request over DBus.
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700946
J. Richard Barnette45e93de2012-04-11 17:24:15 -0700947 """
948 super(SiteHost, self).verify_software()
949 self.check_diskspace(
950 '/mnt/stateful_partition',
951 global_config.global_config.get_config_value(
Fang Deng6b05f5b2013-03-20 13:42:11 -0700952 'SERVER', 'gb_diskspace_required', type=float,
953 default=20.0))
954 self.check_diskspace(
955 '/mnt/stateful_partition/encrypted',
956 global_config.global_config.get_config_value(
957 'SERVER', 'gb_encrypted_diskspace_required', type=float,
958 default=0.1))
Richard Barnetteb2bc13c2013-01-08 17:32:51 -0800959 self.run('update_engine_client --status')
Scott Zawalskifbca4a92013-03-04 15:56:42 -0500960 # Makes sure python is present, loads and can use built in functions.
961 # We have seen cases where importing cPickle fails with undefined
962 # symbols in cPickle.so.
963 self.run('python -c "import cPickle"')
J. Richard Barnette134ec2c2012-04-25 12:59:37 -0700964
965
Christopher Wileyd78249a2013-03-01 13:05:31 -0800966 def xmlrpc_connect(self, command, port, command_name=None,
967 ready_test_name=None, timeout_seconds=10):
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700968 """Connect to an XMLRPC server on the host.
969
970 The `command` argument should be a simple shell command that
971 starts an XMLRPC server on the given `port`. The command
972 must not daemonize, and must terminate cleanly on SIGTERM.
973 The command is started in the background on the host, and a
974 local XMLRPC client for the server is created and returned
975 to the caller.
976
977 Note that the process of creating an XMLRPC client makes no
978 attempt to connect to the remote server; the caller is
979 responsible for determining whether the server is running
980 correctly, and is ready to serve requests.
981
Christopher Wileyd78249a2013-03-01 13:05:31 -0800982 Optionally, the caller can pass ready_test_name, a string
983 containing the name of a method to call on the proxy. This
984 method should take no parameters and return successfully only
985 when the server is ready to process client requests. When
986 ready_test_name is set, xmlrpc_connect will block until the
987 proxy is ready, and throw a TestError if the server isn't
988 ready by timeout_seconds.
989
J. Richard Barnette1d78b012012-05-15 13:56:30 -0700990 @param command Shell command to start the server.
991 @param port Port number on which the server is expected to
992 be serving.
J. Richard Barnette7214e0b2013-02-06 15:20:49 -0800993 @param command_name String to use as input to `pkill` to
994 terminate the XMLRPC server on the host.
Christopher Wileyd78249a2013-03-01 13:05:31 -0800995 @param ready_test_name String containing the name of a
996 method defined on the XMLRPC server.
997 @param timeout_seconds Number of seconds to wait
998 for the server to become 'ready.' Will throw a
999 TestFail error if server is not ready in time.
1000
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001001 """
1002 self.xmlrpc_disconnect(port)
1003
1004 # Chrome OS on the target closes down most external ports
1005 # for security. We could open the port, but doing that
1006 # would conflict with security tests that check that only
1007 # expected ports are open. So, to get to the port on the
1008 # target we use an ssh tunnel.
1009 local_port = utils.get_unused_port()
1010 tunnel_options = '-n -N -q -L %d:localhost:%d' % (local_port, port)
1011 ssh_cmd = make_ssh_command(opts=tunnel_options)
1012 tunnel_cmd = '%s %s' % (ssh_cmd, self.hostname)
1013 logging.debug('Full tunnel command: %s', tunnel_cmd)
1014 tunnel_proc = subprocess.Popen(tunnel_cmd, shell=True, close_fds=True)
1015 logging.debug('Started XMLRPC tunnel, local = %d'
1016 ' remote = %d, pid = %d',
1017 local_port, port, tunnel_proc.pid)
1018
1019 # Start the server on the host. Redirection in the command
1020 # below is necessary, because 'ssh' won't terminate until
1021 # background child processes close stdin, stdout, and
1022 # stderr.
1023 remote_cmd = '( %s ) </dev/null >/dev/null 2>&1 & echo $!' % command
1024 remote_pid = self.run(remote_cmd).stdout.rstrip('\n')
1025 logging.debug('Started XMLRPC server on host %s, pid = %s',
1026 self.hostname, remote_pid)
1027
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08001028 self._xmlrpc_proxy_map[port] = (command_name, tunnel_proc)
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001029 rpc_url = 'http://localhost:%d' % local_port
Christopher Wileyd78249a2013-03-01 13:05:31 -08001030 proxy = xmlrpclib.ServerProxy(rpc_url, allow_none=True)
1031 if ready_test_name is not None:
J. Richard Barnette13eb7c02013-03-07 12:06:29 -08001032 # retry.retry logs each attempt; calculate delay_sec to
1033 # keep log spam to a dull roar.
Christopher Wiley0ed712b2013-04-09 15:25:12 -07001034 @retry.retry((socket.error,
1035 xmlrpclib.ProtocolError,
1036 httplib.BadStatusLine),
Christopher Wileyd78249a2013-03-01 13:05:31 -08001037 timeout_min=timeout_seconds/60.0,
J. Richard Barnette13eb7c02013-03-07 12:06:29 -08001038 delay_sec=min(max(timeout_seconds/20.0, 0.1), 1))
Christopher Wileyd78249a2013-03-01 13:05:31 -08001039 def ready_test():
1040 """ Call proxy.ready_test_name(). """
1041 getattr(proxy, ready_test_name)()
1042 successful = False
1043 try:
1044 logging.info('Waiting %d seconds for XMLRPC server '
1045 'to start.', timeout_seconds)
1046 ready_test()
1047 successful = True
1048 except retry.TimeoutException:
1049 raise error.TestError('Unable to start XMLRPC server after '
1050 '%d seconds.' % timeout_seconds)
1051 finally:
1052 if not successful:
1053 logging.error('Failed to start XMLRPC server.')
1054 self.xmlrpc_disconnect(port)
1055 logging.info('XMLRPC server started successfully.')
1056 return proxy
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001057
1058 def xmlrpc_disconnect(self, port):
1059 """Disconnect from an XMLRPC server on the host.
1060
1061 Terminates the remote XMLRPC server previously started for
1062 the given `port`. Also closes the local ssh tunnel created
1063 for the connection to the host. This function does not
1064 directly alter the state of a previously returned XMLRPC
1065 client object; however disconnection will cause all
1066 subsequent calls to methods on the object to fail.
1067
1068 This function does nothing if requested to disconnect a port
1069 that was not previously connected via `self.xmlrpc_connect()`
1070
1071 @param port Port number passed to a previous call to
1072 `xmlrpc_connect()`
1073 """
1074 if port not in self._xmlrpc_proxy_map:
1075 return
1076 entry = self._xmlrpc_proxy_map[port]
1077 remote_name = entry[0]
1078 tunnel_proc = entry[1]
1079 if remote_name:
1080 # We use 'pkill' to find our target process rather than
1081 # a PID, because the host may have rebooted since
1082 # connecting, and we don't want to kill an innocent
1083 # process with the same PID.
1084 #
1085 # 'pkill' helpfully exits with status 1 if no target
1086 # process is found, for which run() will throw an
Simran Basid5e5e272012-09-24 15:23:59 -07001087 # exception. We don't want that, so we the ignore
J. Richard Barnette1d78b012012-05-15 13:56:30 -07001088 # status.
1089 self.run("pkill -f '%s'" % remote_name, ignore_status=True)
1090
1091 if tunnel_proc.poll() is None:
1092 tunnel_proc.terminate()
1093 logging.debug('Terminated tunnel, pid %d', tunnel_proc.pid)
1094 else:
1095 logging.debug('Tunnel pid %d terminated early, status %d',
1096 tunnel_proc.pid, tunnel_proc.returncode)
1097 del self._xmlrpc_proxy_map[port]
1098
1099
1100 def xmlrpc_disconnect_all(self):
1101 """Disconnect all known XMLRPC proxy ports."""
1102 for port in self._xmlrpc_proxy_map.keys():
1103 self.xmlrpc_disconnect(port)
1104
1105
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001106 def _ping_check_status(self, status):
1107 """Ping the host once, and return whether it has a given status.
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001108
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001109 @param status Check the ping status against this value.
1110 @return True iff `status` and the result of ping are the same
1111 (i.e. both True or both False).
1112
1113 """
1114 ping_val = utils.ping(self.hostname, tries=1, deadline=1)
1115 return not (status ^ (ping_val == 0))
1116
1117 def _ping_wait_for_status(self, status, timeout):
1118 """Wait for the host to have a given status (UP or DOWN).
1119
1120 Status is checked by polling. Polling will not last longer
1121 than the number of seconds in `timeout`. The polling
1122 interval will be long enough that only approximately
1123 _PING_WAIT_COUNT polling cycles will be executed, subject
1124 to a maximum interval of about one minute.
1125
1126 @param status Waiting will stop immediately if `ping` of the
1127 host returns this status.
1128 @param timeout Poll for at most this many seconds.
1129 @return True iff the host status from `ping` matched the
1130 requested status at the time of return.
1131
1132 """
1133 # _ping_check_status() takes about 1 second, hence the
1134 # "- 1" in the formula below.
1135 poll_interval = min(int(timeout / self._PING_WAIT_COUNT), 60) - 1
1136 end_time = time.time() + timeout
1137 while time.time() <= end_time:
1138 if self._ping_check_status(status):
1139 return True
1140 if poll_interval > 0:
1141 time.sleep(poll_interval)
1142
1143 # The last thing we did was sleep(poll_interval), so it may
1144 # have been too long since the last `ping`. Check one more
1145 # time, just to be sure.
1146 return self._ping_check_status(status)
1147
1148 def ping_wait_up(self, timeout):
1149 """Wait for the host to respond to `ping`.
1150
1151 N.B. This method is not a reliable substitute for
1152 `wait_up()`, because a host that responds to ping will not
1153 necessarily respond to ssh. This method should only be used
1154 if the target DUT can be considered functional even if it
1155 can't be reached via ssh.
1156
1157 @param timeout Minimum time to allow before declaring the
1158 host to be non-responsive.
1159 @return True iff the host answered to ping before the timeout.
1160
1161 """
1162 return self._ping_wait_for_status(self._PING_STATUS_UP, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001163
Andrew Bresticker678c0c72013-01-22 10:44:09 -08001164 def ping_wait_down(self, timeout):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001165 """Wait until the host no longer responds to `ping`.
1166
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001167 This function can be used as a slightly faster version of
1168 `wait_down()`, by avoiding potentially long ssh timeouts.
1169
1170 @param timeout Minimum time to allow for the host to become
1171 non-responsive.
1172 @return True iff the host quit answering ping before the
1173 timeout.
1174
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001175 """
J. Richard Barnetteb6de7e32013-02-14 13:28:04 -08001176 return self._ping_wait_for_status(self._PING_STATUS_DOWN, timeout)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001177
1178 def test_wait_for_sleep(self):
1179 """Wait for the client to enter low-power sleep mode.
1180
1181 The test for "is asleep" can't distinguish a system that is
1182 powered off; to confirm that the unit was asleep, it is
1183 necessary to force resume, and then call
1184 `test_wait_for_resume()`.
1185
1186 This function is expected to be called from a test as part
1187 of a sequence like the following:
1188
1189 ~~~~~~~~
1190 boot_id = host.get_boot_id()
1191 # trigger sleep on the host
1192 host.test_wait_for_sleep()
1193 # trigger resume on the host
1194 host.test_wait_for_resume(boot_id)
1195 ~~~~~~~~
1196
1197 @exception TestFail The host did not go to sleep within
1198 the allowed time.
1199 """
Andrew Bresticker678c0c72013-01-22 10:44:09 -08001200 if not self.ping_wait_down(timeout=self.SLEEP_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001201 raise error.TestFail(
1202 'client failed to sleep after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001203 self.SLEEP_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001204
1205
1206 def test_wait_for_resume(self, old_boot_id):
1207 """Wait for the client to resume from low-power sleep mode.
1208
1209 The `old_boot_id` parameter should be the value from
1210 `get_boot_id()` obtained prior to entering sleep mode. A
1211 `TestFail` exception is raised if the boot id changes.
1212
1213 See @ref test_wait_for_sleep for more on this function's
1214 usage.
1215
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08001216 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001217 target host went to sleep.
1218
1219 @exception TestFail The host did not respond within the
1220 allowed time.
1221 @exception TestFail The host responded, but the boot id test
1222 indicated a reboot rather than a sleep
1223 cycle.
1224 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001225 if not self.wait_up(timeout=self.RESUME_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001226 raise error.TestFail(
1227 'client failed to resume from sleep after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001228 self.RESUME_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001229 else:
1230 new_boot_id = self.get_boot_id()
1231 if new_boot_id != old_boot_id:
1232 raise error.TestFail(
1233 'client rebooted, but sleep was expected'
1234 ' (old boot %s, new boot %s)'
1235 % (old_boot_id, new_boot_id))
1236
1237
1238 def test_wait_for_shutdown(self):
1239 """Wait for the client to shut down.
1240
1241 The test for "has shut down" can't distinguish a system that
1242 is merely asleep; to confirm that the unit was down, it is
1243 necessary to force boot, and then call test_wait_for_boot().
1244
1245 This function is expected to be called from a test as part
1246 of a sequence like the following:
1247
1248 ~~~~~~~~
1249 boot_id = host.get_boot_id()
1250 # trigger shutdown on the host
1251 host.test_wait_for_shutdown()
1252 # trigger boot on the host
1253 host.test_wait_for_boot(boot_id)
1254 ~~~~~~~~
1255
1256 @exception TestFail The host did not shut down within the
1257 allowed time.
1258 """
Andrew Bresticker678c0c72013-01-22 10:44:09 -08001259 if not self.ping_wait_down(timeout=self.SHUTDOWN_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001260 raise error.TestFail(
1261 'client failed to shut down after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001262 self.SHUTDOWN_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001263
1264
1265 def test_wait_for_boot(self, old_boot_id=None):
1266 """Wait for the client to boot from cold power.
1267
1268 The `old_boot_id` parameter should be the value from
1269 `get_boot_id()` obtained prior to shutting down. A
1270 `TestFail` exception is raised if the boot id does not
1271 change. The boot id test is omitted if `old_boot_id` is not
1272 specified.
1273
1274 See @ref test_wait_for_shutdown for more on this function's
1275 usage.
1276
J. Richard Barnette7214e0b2013-02-06 15:20:49 -08001277 @param old_boot_id A boot id value obtained before the
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001278 shut down.
1279
1280 @exception TestFail The host did not respond within the
1281 allowed time.
1282 @exception TestFail The host responded, but the boot id test
1283 indicated that there was no reboot.
1284 """
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001285 if not self.wait_up(timeout=self.REBOOT_TIMEOUT):
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001286 raise error.TestFail(
1287 'client failed to reboot after %d seconds' %
J. Richard Barnetteeb69d722012-06-18 17:29:44 -07001288 self.REBOOT_TIMEOUT)
J. Richard Barnette134ec2c2012-04-25 12:59:37 -07001289 elif old_boot_id:
1290 if self.get_boot_id() == old_boot_id:
1291 raise error.TestFail(
1292 'client is back up, but did not reboot'
1293 ' (boot %s)' % old_boot_id)
Simran Basid5e5e272012-09-24 15:23:59 -07001294
1295
1296 @staticmethod
1297 def check_for_rpm_support(hostname):
1298 """For a given hostname, return whether or not it is powered by an RPM.
1299
1300 @return None if this host does not follows the defined naming format
1301 for RPM powered DUT's in the lab. If it does follow the format,
1302 it returns a regular expression MatchObject instead.
1303 """
Richard Barnette82c35912012-11-20 10:09:10 -08001304 return re.match(SiteHost._RPM_HOSTNAME_REGEX, hostname)
Simran Basid5e5e272012-09-24 15:23:59 -07001305
1306
1307 def has_power(self):
1308 """For this host, return whether or not it is powered by an RPM.
1309
1310 @return True if this host is in the CROS lab and follows the defined
1311 naming format.
1312 """
1313 return SiteHost.check_for_rpm_support(self.hostname)
1314
1315
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001316 def _set_power(self, state, power_method):
1317 """Sets the power to the host via RPM, Servo or manual.
1318
1319 @param state Specifies which power state to set to DUT
1320 @param power_method Specifies which method of power control to
1321 use. By default "RPM" will be used. Valid values
1322 are the strings "RPM", "manual", "servoj10".
1323
1324 """
1325 ACCEPTABLE_STATES = ['ON', 'OFF']
1326
1327 if state.upper() not in ACCEPTABLE_STATES:
1328 raise error.TestError('State must be one of: %s.'
1329 % (ACCEPTABLE_STATES,))
1330
1331 if power_method == self.POWER_CONTROL_SERVO:
1332 logging.info('Setting servo port J10 to %s', state)
1333 self.servo.set('prtctl3_pwren', state.lower())
1334 time.sleep(self._USB_POWER_TIMEOUT)
1335 elif power_method == self.POWER_CONTROL_MANUAL:
1336 logging.info('You have %d seconds to set the AC power to %s.',
1337 self._POWER_CYCLE_TIMEOUT, state)
1338 time.sleep(self._POWER_CYCLE_TIMEOUT)
1339 else:
1340 if not self.has_power():
1341 raise error.TestFail('DUT does not have RPM connected.')
Simran Basi5e6339a2013-03-21 11:34:32 -07001342 afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
1343 afe.set_host_attribute(self._RPM_OUTLET_CHANGED, True,
1344 hostname=self.hostname)
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001345 rpm_client.set_power(self.hostname, state.upper())
Simran Basid5e5e272012-09-24 15:23:59 -07001346
1347
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001348 def power_off(self, power_method=POWER_CONTROL_RPM):
1349 """Turn off power to this host via RPM, Servo or manual.
1350
1351 @param power_method Specifies which method of power control to
1352 use. By default "RPM" will be used. Valid values
1353 are the strings "RPM", "manual", "servoj10".
1354
1355 """
1356 self._set_power('OFF', power_method)
Simran Basid5e5e272012-09-24 15:23:59 -07001357
1358
Ismail Noorbasha07fdb612013-02-14 14:13:31 -08001359 def power_on(self, power_method=POWER_CONTROL_RPM):
1360 """Turn on power to this host via RPM, Servo or manual.
1361
1362 @param power_method Specifies which method of power control to
1363 use. By default "RPM" will be used. Valid values
1364 are the strings "RPM", "manual", "servoj10".
1365
1366 """
1367 self._set_power('ON', power_method)
1368
1369
1370 def power_cycle(self, power_method=POWER_CONTROL_RPM):
1371 """Cycle power to this host by turning it OFF, then ON.
1372
1373 @param power_method Specifies which method of power control to
1374 use. By default "RPM" will be used. Valid values
1375 are the strings "RPM", "manual", "servoj10".
1376
1377 """
1378 if power_method in (self.POWER_CONTROL_SERVO,
1379 self.POWER_CONTROL_MANUAL):
1380 self.power_off(power_method=power_method)
1381 time.sleep(self._POWER_CYCLE_TIMEOUT)
1382 self.power_on(power_method=power_method)
1383 else:
1384 rpm_client.set_power(self.hostname, 'CYCLE')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001385
1386
1387 def get_platform(self):
1388 """Determine the correct platform label for this host.
1389
1390 @returns a string representing this host's platform.
1391 """
1392 crossystem = utils.Crossystem(self)
1393 crossystem.init()
1394 # Extract fwid value and use the leading part as the platform id.
1395 # fwid generally follow the format of {platform}.{firmware version}
1396 # Example: Alex.X.YYY.Z or Google_Alex.X.YYY.Z
1397 platform = crossystem.fwid().split('.')[0].lower()
1398 # Newer platforms start with 'Google_' while the older ones do not.
1399 return platform.replace('google_', '')
1400
1401
Aviv Keshet74c89a92013-02-04 15:18:30 -08001402 @label_decorator()
Simran Basic6f1f7a2012-10-16 10:47:46 -07001403 def get_board(self):
1404 """Determine the correct board label for this host.
1405
1406 @returns a string representing this host's board.
1407 """
1408 release_info = utils.parse_cmd_output('cat /etc/lsb-release',
1409 run_method=self.run)
1410 board = release_info['CHROMEOS_RELEASE_BOARD']
1411 # Devices in the lab generally have the correct board name but our own
1412 # development devices have {board_name}-signed-{key_type}. The board
1413 # name may also begin with 'x86-' which we need to keep.
Simran Basi833814b2013-01-29 13:13:43 -08001414 board_format_string = ds_constants.BOARD_PREFIX + '%s'
Simran Basic6f1f7a2012-10-16 10:47:46 -07001415 if 'x86' not in board:
Simran Basi833814b2013-01-29 13:13:43 -08001416 return board_format_string % board.split('-')[0]
1417 return board_format_string % '-'.join(board.split('-')[0:2])
Simran Basic6f1f7a2012-10-16 10:47:46 -07001418
1419
Aviv Keshet74c89a92013-02-04 15:18:30 -08001420 @label_decorator('lightsensor')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001421 def has_lightsensor(self):
1422 """Determine the correct board label for this host.
1423
1424 @returns the string 'lightsensor' if this host has a lightsensor or
1425 None if it does not.
1426 """
1427 search_cmd = "find -L %s -maxdepth 4 | egrep '%s'" % (
Richard Barnette82c35912012-11-20 10:09:10 -08001428 self._LIGHTSENSOR_SEARCH_DIR, '|'.join(self._LIGHTSENSOR_FILES))
Simran Basic6f1f7a2012-10-16 10:47:46 -07001429 try:
1430 # Run the search cmd following the symlinks. Stderr_tee is set to
1431 # None as there can be a symlink loop, but the command will still
1432 # execute correctly with a few messages printed to stderr.
1433 self.run(search_cmd, stdout_tee=None, stderr_tee=None)
1434 return 'lightsensor'
1435 except error.AutoservRunError:
1436 # egrep exited with a return code of 1 meaning none of the possible
1437 # lightsensor files existed.
1438 return None
1439
1440
Aviv Keshet74c89a92013-02-04 15:18:30 -08001441 @label_decorator('bluetooth')
Simran Basic6f1f7a2012-10-16 10:47:46 -07001442 def has_bluetooth(self):
1443 """Determine the correct board label for this host.
1444
1445 @returns the string 'bluetooth' if this host has bluetooth or
1446 None if it does not.
1447 """
1448 try:
1449 self.run('test -d /sys/class/bluetooth/hci0')
1450 # test exited with a return code of 0.
1451 return 'bluetooth'
1452 except error.AutoservRunError:
1453 # test exited with a return code 1 meaning the directory did not
1454 # exist.
1455 return None
1456
1457
1458 def get_labels(self):
1459 """Return a list of labels for this given host.
1460
1461 This is the main way to retrieve all the automatic labels for a host
1462 as it will run through all the currently implemented label functions.
1463 """
1464 labels = []
Richard Barnette82c35912012-11-20 10:09:10 -08001465 for label_function in self._LABEL_FUNCTIONS:
Simran Basic6f1f7a2012-10-16 10:47:46 -07001466 label = label_function(self)
1467 if label:
1468 labels.append(label)
1469 return labels