blob: c44e83547acbd5baad8b57180725aca292c94864 [file] [log] [blame]
Chris Sosa5e4246b2012-05-22 18:05:22 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Sean O'Connor5346e4e2010-08-12 18:49:24 +02002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Don Garrett56b1cc82013-12-06 17:49:20 -08005import glob
Sean O'Connor5346e4e2010-08-12 18:49:24 +02006import logging
Dale Curtis5c32c722011-05-04 19:24:23 -07007import os
Sean O'Connor5346e4e2010-08-12 18:49:24 +02008import re
Richard Barnette621a8e42018-06-25 17:34:11 -07009import time
Prashanth B32baa9b2014-03-13 13:23:01 -070010import urllib2
Richard Barnette0beb14b2018-05-15 18:07:52 +000011import urlparse
Sean O'Connor5346e4e2010-08-12 18:49:24 +020012
Chris Sosa65425082013-10-16 13:26:22 -070013from autotest_lib.client.bin import utils
Dale Curtis5c32c722011-05-04 19:24:23 -070014from autotest_lib.client.common_lib import error, global_config
Prashanth B32baa9b2014-03-13 13:23:01 -070015from autotest_lib.client.common_lib.cros import dev_server
Richard Barnette0beb14b2018-05-15 18:07:52 +000016from autotest_lib.server import autotest
Shelley Chen61d28982016-10-28 09:40:20 -070017from autotest_lib.server import utils as server_utils
Richard Barnette0beb14b2018-05-15 18:07:52 +000018from autotest_lib.server.cros.dynamic_suite import constants as ds_constants
19from autotest_lib.server.cros.dynamic_suite import tools
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -080020from chromite.lib import retry_util
Dan Shif3a35f72016-01-25 11:18:14 -080021
Shelley Chen16b8df32016-10-27 16:24:21 -070022try:
23 from chromite.lib import metrics
Dan Shi5e2efb72017-02-07 11:40:23 -080024except ImportError:
25 metrics = utils.metrics_mock
Sean O'Connor5346e4e2010-08-12 18:49:24 +020026
Gwendal Grignou3e96cc22017-06-07 16:22:51 -070027
Richard Barnette621a8e42018-06-25 17:34:11 -070028def _metric_name(base_name):
29 return 'chromeos/autotest/provision/' + base_name
30
31
Dale Curtis5c32c722011-05-04 19:24:23 -070032# Local stateful update path is relative to the CrOS source directory.
Sean O'Connor5346e4e2010-08-12 18:49:24 +020033UPDATER_IDLE = 'UPDATE_STATUS_IDLE'
Sean Oc053dfe2010-08-23 18:22:26 +020034UPDATER_NEED_REBOOT = 'UPDATE_STATUS_UPDATED_NEED_REBOOT'
beeps5e8c45a2013-12-17 22:05:11 -080035# A list of update engine client states that occur after an update is triggered.
36UPDATER_PROCESSING_UPDATE = ['UPDATE_STATUS_CHECKING_FORUPDATE',
37 'UPDATE_STATUS_UPDATE_AVAILABLE',
38 'UPDATE_STATUS_DOWNLOADING',
39 'UPDATE_STATUS_FINALIZING']
Sean O'Connor5346e4e2010-08-12 18:49:24 +020040
Richard Barnette0beb14b2018-05-15 18:07:52 +000041
Richard Barnette3e8b2282018-05-15 20:42:20 +000042_STATEFUL_UPDATE_SCRIPT = 'stateful_update'
Richard Barnettee86b1ce2018-06-07 10:37:23 -070043_QUICK_PROVISION_SCRIPT = 'quick-provision'
Richard Barnette3e8b2282018-05-15 20:42:20 +000044
45_UPDATER_BIN = '/usr/bin/update_engine_client'
46_UPDATER_LOGS = ['/var/log/messages', '/var/log/update_engine']
47
48_KERNEL_A = {'name': 'KERN-A', 'kernel': 2, 'root': 3}
49_KERNEL_B = {'name': 'KERN-B', 'kernel': 4, 'root': 5}
50
51# Time to wait for new kernel to be marked successful after
52# auto update.
53_KERNEL_UPDATE_TIMEOUT = 120
54
55
Richard Barnette0beb14b2018-05-15 18:07:52 +000056# PROVISION_FAILED - A flag file to indicate provision failures. The
57# file is created at the start of any AU procedure (see
Richard Barnette9d43e562018-06-05 17:20:10 +000058# `ChromiumOSUpdater._prepare_host()`). The file's location in
Richard Barnette0beb14b2018-05-15 18:07:52 +000059# stateful means that on successul update it will be removed. Thus, if
60# this file exists, it indicates that we've tried and failed in a
61# previous attempt to update.
62PROVISION_FAILED = '/var/tmp/provision_failed'
63
64
Richard Barnette3e8b2282018-05-15 20:42:20 +000065# A flag file used to enable special handling in lab DUTs. Some
66# parts of the system in Chromium OS test images will behave in ways
67# convenient to the test lab when this file is present. Generally,
68# we create this immediately after any update completes.
69_LAB_MACHINE_FILE = '/mnt/stateful_partition/.labmachine'
70
71
Richard Barnette3ef29a82018-06-28 13:52:54 -070072# _TARGET_VERSION - A file containing the new version to which we plan
73# to update. This file is used by the CrOS shutdown code to detect and
74# handle certain version downgrade cases. Specifically: Downgrading
75# may trigger an unwanted powerwash in the target build when the
76# following conditions are met:
77# * Source build is a v4.4 kernel with R69-10756.0.0 or later.
78# * Target build predates the R69-10756.0.0 cutoff.
79# When this file is present and indicates a downgrade, the OS shutdown
80# code on the DUT knows how to prevent the powerwash.
81_TARGET_VERSION = '/run/update_target_version'
82
83
Richard Barnette9d43e562018-06-05 17:20:10 +000084class RootFSUpdateError(error.TestFail):
Chris Sosa77556d82012-04-05 15:23:14 -070085 """Raised when the RootFS fails to update."""
Chris Sosa77556d82012-04-05 15:23:14 -070086
87
Richard Barnette9d43e562018-06-05 17:20:10 +000088class StatefulUpdateError(error.TestFail):
Chris Sosa77556d82012-04-05 15:23:14 -070089 """Raised when the stateful partition fails to update."""
Chris Sosa77556d82012-04-05 15:23:14 -070090
91
Richard Barnette9d43e562018-06-05 17:20:10 +000092class _AttributedUpdateError(error.TestFail):
93 """Update failure with an attributed cause."""
94
95 def __init__(self, attribution, msg):
96 super(_AttributedUpdateError, self).__init__(
97 '%s: %s' % (attribution, msg))
98
99
100class HostUpdateError(_AttributedUpdateError):
101 """Failure updating a DUT attributable to the DUT.
102
103 This class of exception should be raised when the most likely cause
104 of failure was a condition existing on the DUT prior to the update,
105 such as a hardware problem, or a bug in the software on the DUT.
106 """
107
108 def __init__(self, hostname, msg):
109 super(HostUpdateError, self).__init__(
110 'Error on %s prior to update' % hostname, msg)
111
Richard Barnette621a8e42018-06-25 17:34:11 -0700112 @property
113 def failure_summary(self):
114 #pylint: disable=missing-docstring
115 return 'DUT failed prior to update'
116
Richard Barnette9d43e562018-06-05 17:20:10 +0000117
118class DevServerError(_AttributedUpdateError):
119 """Failure updating a DUT attributable to the devserver.
120
121 This class of exception should be raised when the most likely cause
122 of failure was the devserver serving the target image for update.
123 """
124
125 def __init__(self, devserver, msg):
126 super(DevServerError, self).__init__(
127 'Devserver error on %s' % devserver, msg)
128
Richard Barnette621a8e42018-06-25 17:34:11 -0700129 @property
130 def failure_summary(self):
131 #pylint: disable=missing-docstring
132 return 'Devserver failed prior to update'
133
Richard Barnette9d43e562018-06-05 17:20:10 +0000134
135class ImageInstallError(_AttributedUpdateError):
136 """Failure updating a DUT when installing from the devserver.
137
138 This class of exception should be raised when the target DUT fails
139 to download and install the target image from the devserver, and
140 either the devserver or the DUT might be at fault.
141 """
142
143 def __init__(self, hostname, devserver, msg):
144 super(ImageInstallError, self).__init__(
145 'Download and install failed from %s onto %s'
146 % (devserver, hostname), msg)
147
Richard Barnette621a8e42018-06-25 17:34:11 -0700148 @property
149 def failure_summary(self):
150 #pylint: disable=missing-docstring
151 return 'Image failed to download and install'
152
Richard Barnette9d43e562018-06-05 17:20:10 +0000153
154class NewBuildUpdateError(_AttributedUpdateError):
155 """Failure updating a DUT attributable to the target build.
156
157 This class of exception should be raised when updating to a new
158 build fails, and the most likely cause of the failure is a bug in
159 the newly installed target build.
160 """
161
162 def __init__(self, update_version, msg):
163 super(NewBuildUpdateError, self).__init__(
164 'Failure in build %s' % update_version, msg)
165
Richard Barnette621a8e42018-06-25 17:34:11 -0700166 @property
167 def failure_summary(self):
168 #pylint: disable=missing-docstring
169 return 'Build failed to work after installing'
170
Richard Barnette9d43e562018-06-05 17:20:10 +0000171
Richard Barnette3e8b2282018-05-15 20:42:20 +0000172def _url_to_version(update_url):
Dan Shi0f466e82013-02-22 15:44:58 -0800173 """Return the version based on update_url.
174
175 @param update_url: url to the image to update to.
176
177 """
Dale Curtisddfdb942011-07-14 13:59:24 -0700178 # The Chrome OS version is generally the last element in the URL. The only
179 # exception is delta update URLs, which are rooted under the version; e.g.,
180 # http://.../update/.../0.14.755.0/au/0.14.754.0. In this case we want to
181 # strip off the au section of the path before reading the version.
Dan Shi5002cfc2013-04-29 10:45:05 -0700182 return re.sub('/au/.*', '',
183 urlparse.urlparse(update_url).path).split('/')[-1].strip()
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200184
185
Scott Zawalskieadbf702013-03-14 09:23:06 -0400186def url_to_image_name(update_url):
187 """Return the image name based on update_url.
188
189 From a URL like:
190 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0
191 return lumpy-release/R27-3837.0.0
192
193 @param update_url: url to the image to update to.
194 @returns a string representing the image name in the update_url.
195
196 """
197 return '/'.join(urlparse.urlparse(update_url).path.split('/')[-2:])
198
199
Prashanth B32baa9b2014-03-13 13:23:01 -0700200def _get_devserver_build_from_update_url(update_url):
201 """Get the devserver and build from the update url.
202
203 @param update_url: The url for update.
204 Eg: http://devserver:port/update/build.
205
206 @return: A tuple of (devserver url, build) or None if the update_url
207 doesn't match the expected pattern.
208
209 @raises ValueError: If the update_url doesn't match the expected pattern.
210 @raises ValueError: If no global_config was found, or it doesn't contain an
211 image_url_pattern.
212 """
213 pattern = global_config.global_config.get_config_value(
214 'CROS', 'image_url_pattern', type=str, default='')
215 if not pattern:
216 raise ValueError('Cannot parse update_url, the global config needs '
217 'an image_url_pattern.')
218 re_pattern = pattern.replace('%s', '(\S+)')
219 parts = re.search(re_pattern, update_url)
220 if not parts or len(parts.groups()) < 2:
221 raise ValueError('%s is not an update url' % update_url)
222 return parts.groups()
223
224
Richard Barnette3e8b2282018-05-15 20:42:20 +0000225def _list_image_dir_contents(update_url):
Prashanth B32baa9b2014-03-13 13:23:01 -0700226 """Lists the contents of the devserver for a given build/update_url.
227
228 @param update_url: An update url. Eg: http://devserver:port/update/build.
229 """
230 if not update_url:
231 logging.warning('Need update_url to list contents of the devserver.')
232 return
233 error_msg = 'Cannot check contents of devserver, update url %s' % update_url
234 try:
235 devserver_url, build = _get_devserver_build_from_update_url(update_url)
236 except ValueError as e:
237 logging.warning('%s: %s', error_msg, e)
238 return
239 devserver = dev_server.ImageServer(devserver_url)
240 try:
241 devserver.list_image_dir(build)
242 # The devserver will retry on URLError to avoid flaky connections, but will
243 # eventually raise the URLError if it persists. All HTTPErrors get
244 # converted to DevServerExceptions.
245 except (dev_server.DevServerException, urllib2.URLError) as e:
246 logging.warning('%s: %s', error_msg, e)
247
248
Richard Barnette621a8e42018-06-25 17:34:11 -0700249def _get_metric_fields(update_url):
250 """Return a dict of metric fields.
251
252 This is used for sending autoupdate metrics for the given update URL.
253
254 @param update_url Metrics fields will be calculated from this URL.
255 """
256 build_name = url_to_image_name(update_url)
257 try:
258 board, build_type, milestone, _ = server_utils.ParseBuildName(
259 build_name)
260 except server_utils.ParseBuildNameException:
261 logging.warning('Unable to parse build name %s for metrics. '
262 'Continuing anyway.', build_name)
263 board, build_type, milestone = ('', '', '')
264 return {
265 'dev_server': dev_server.get_resolved_hostname(update_url),
266 'board': board,
267 'build_type': build_type,
268 'milestone': milestone,
269 }
270
271
272def _emit_provision_metrics(update_url, dut_host_name,
273 failure_reason, duration):
274 """Send metrics for provision request."""
275 # The following is high cardinality, but sparse.
276 # Each DUT is of a single board type, and likely build type.
277 # The affinity also results in each DUT being attached to the same
278 # dev_server as well.
279 image_fields = _get_metric_fields(update_url)
280 fields = {
281 'board': image_fields['board'],
282 'build_type': image_fields['build_type'],
283 'dut_host_name': dut_host_name,
284 'dev_server': image_fields['dev_server'],
285 'success': not failure_reason,
286 }
287 build_name = url_to_image_name(update_url)
288
289 # reset_after=True is required for String gauges events to ensure that
290 # the metrics are not repeatedly emitted until the server restarts.
291
292 metrics.String(_metric_name('provision_build_by_devserver_dut'),
293 reset_after=True).set(build_name, fields=fields)
294 if failure_reason:
295 metrics.String(
296 _metric_name('provision_failure_reason_by_devserver_dut'),
297 reset_after=True).set(failure_reason, fields=fields)
298 metrics.SecondsDistribution(
299 _metric_name('provision_duration_by_devserver_dut')).add(
300 duration, fields=fields)
301
302
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700303# TODO(garnold) This implements shared updater functionality needed for
304# supporting the autoupdate_EndToEnd server-side test. We should probably
305# migrate more of the existing ChromiumOSUpdater functionality to it as we
306# expand non-CrOS support in other tests.
Richard Barnette3e8b2282018-05-15 20:42:20 +0000307class ChromiumOSUpdater(object):
308 """Chromium OS specific DUT update functionality."""
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700309
Richard Barnette3e8b2282018-05-15 20:42:20 +0000310 def __init__(self, update_url, host=None, interactive=True):
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700311 """Initializes the object.
312
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700313 @param update_url: The URL we want the update to use.
314 @param host: A client.common_lib.hosts.Host implementation.
David Haddock76a4c882017-12-13 18:50:09 -0800315 @param interactive: Bool whether we are doing an interactive update.
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700316 """
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700317 self.update_url = update_url
318 self.host = host
David Haddock76a4c882017-12-13 18:50:09 -0800319 self.interactive = interactive
Richard Barnette3e8b2282018-05-15 20:42:20 +0000320 self.update_version = _url_to_version(update_url)
321
322
323 def _run(self, cmd, *args, **kwargs):
324 """Abbreviated form of self.host.run(...)"""
325 return self.host.run(cmd, *args, **kwargs)
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700326
327
328 def check_update_status(self):
329 """Returns the current update engine state.
330
331 We use the `update_engine_client -status' command and parse the line
332 indicating the update state, e.g. "CURRENT_OP=UPDATE_STATUS_IDLE".
333 """
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800334 update_status = self.host.run(command='%s -status | grep CURRENT_OP' %
Richard Barnette3e8b2282018-05-15 20:42:20 +0000335 _UPDATER_BIN)
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700336 return update_status.stdout.strip().split('=')[-1]
337
338
Richard Barnette55d1af82018-05-22 23:40:14 +0000339 def _rootdev(self, options=''):
340 """Returns the stripped output of rootdev <options>.
341
342 @param options: options to run rootdev.
343
344 """
345 return self._run('rootdev %s' % options).stdout.strip()
346
347
348 def get_kernel_state(self):
Richard Barnette9d43e562018-06-05 17:20:10 +0000349 """Returns the (<active>, <inactive>) kernel state as a pair.
350
351 @raise RootFSUpdateError if the DUT reports a root partition
352 number that isn't one of the known valid values.
353 """
Richard Barnette55d1af82018-05-22 23:40:14 +0000354 active_root = int(re.findall('\d+\Z', self._rootdev('-s'))[0])
355 if active_root == _KERNEL_A['root']:
356 return _KERNEL_A, _KERNEL_B
357 elif active_root == _KERNEL_B['root']:
358 return _KERNEL_B, _KERNEL_A
359 else:
Richard Barnette9d43e562018-06-05 17:20:10 +0000360 raise RootFSUpdateError(
361 'Encountered unknown root partition: %s' % active_root)
Richard Barnette55d1af82018-05-22 23:40:14 +0000362
363
Richard Barnette18fd5842018-05-25 18:21:14 +0000364 def _cgpt(self, flag, kernel):
365 """Return numeric cgpt value for the specified flag, kernel, device."""
366 return int(self._run('cgpt show -n -i %d %s $(rootdev -s -d)' % (
367 kernel['kernel'], flag)).stdout.strip())
Richard Barnette55d1af82018-05-22 23:40:14 +0000368
369
370 def _get_next_kernel(self):
371 """Return the kernel that has priority for the next boot."""
372 priority_a = self._cgpt('-P', _KERNEL_A)
373 priority_b = self._cgpt('-P', _KERNEL_B)
374 if priority_a > priority_b:
375 return _KERNEL_A
376 else:
377 return _KERNEL_B
378
379
380 def _get_kernel_success(self, kernel):
381 """Return boolean success flag for the specified kernel.
382
383 @param kernel: information of the given kernel, either _KERNEL_A
384 or _KERNEL_B.
385 """
386 return self._cgpt('-S', kernel) != 0
387
388
389 def _get_kernel_tries(self, kernel):
390 """Return tries count for the specified kernel.
391
392 @param kernel: information of the given kernel, either _KERNEL_A
393 or _KERNEL_B.
394 """
395 return self._cgpt('-T', kernel)
396
397
Richard Barnette3e8b2282018-05-15 20:42:20 +0000398 def _get_last_update_error(self):
Shuqian Zhaod9992722016-02-29 12:26:38 -0800399 """Get the last autoupdate error code."""
Richard Barnette3e8b2282018-05-15 20:42:20 +0000400 command_result = self._run(
401 '%s --last_attempt_error' % _UPDATER_BIN)
402 return command_result.stdout.strip().replace('\n', ', ')
Shuqian Zhaod9992722016-02-29 12:26:38 -0800403
404
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800405 def _base_update_handler_no_retry(self, run_args):
Shuqian Zhaod9992722016-02-29 12:26:38 -0800406 """Base function to handle a remote update ssh call.
407
408 @param run_args: Dictionary of args passed to ssh_host.run function.
Shuqian Zhaod9992722016-02-29 12:26:38 -0800409
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800410 @throws: intercepts and re-throws all exceptions
Shuqian Zhaod9992722016-02-29 12:26:38 -0800411 """
Shuqian Zhaod9992722016-02-29 12:26:38 -0800412 try:
413 self.host.run(**run_args)
Shuqian Zhaod9992722016-02-29 12:26:38 -0800414 except Exception as e:
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800415 logging.debug('exception in update handler: %s', e)
416 raise e
Shuqian Zhaod9992722016-02-29 12:26:38 -0800417
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800418
419 def _base_update_handler(self, run_args, err_msg_prefix=None):
420 """Handle a remote update ssh call, possibly with retries.
421
422 @param run_args: Dictionary of args passed to ssh_host.run function.
423 @param err_msg_prefix: Prefix of the exception error message.
424 """
425 def exception_handler(e):
426 """Examines exceptions and returns True if the update handler
427 should be retried.
428
429 @param e: the exception intercepted by the retry util.
430 """
431 return (isinstance(e, error.AutoservSSHTimeout) or
432 (isinstance(e, error.GenericHostRunError) and
433 hasattr(e, 'description') and
434 (re.search('ERROR_CODE=37', e.description) or
435 re.search('generic error .255.', e.description))))
436
437 try:
438 # Try the update twice (arg 2 is max_retry, not including the first
439 # call). Some exceptions may be caught by the retry handler.
440 retry_util.GenericRetry(exception_handler, 1,
441 self._base_update_handler_no_retry,
442 run_args)
443 except Exception as e:
444 message = err_msg_prefix + ': ' + str(e)
445 raise RootFSUpdateError(message)
Shuqian Zhaod9992722016-02-29 12:26:38 -0800446
447
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800448 def _wait_for_update_service(self):
449 """Ensure that the update engine daemon is running, possibly
450 by waiting for it a bit in case the DUT just rebooted and the
451 service hasn't started yet.
452 """
453 def handler(e):
454 """Retry exception handler.
455
456 Assumes that the error is due to the update service not having
457 started yet.
458
459 @param e: the exception intercepted by the retry util.
460 """
461 if isinstance(e, error.AutoservRunError):
462 logging.debug('update service check exception: %s\n'
463 'retrying...', e)
464 return True
465 else:
466 return False
467
468 # Retry at most three times, every 5s.
469 status = retry_util.GenericRetry(handler, 3,
470 self.check_update_status,
471 sleep=5)
472
473 # Expect the update engine to be idle.
474 if status != UPDATER_IDLE:
Richard Barnette9d43e562018-06-05 17:20:10 +0000475 raise RootFSUpdateError(
476 'Update engine status is %s (%s was expected).'
477 % (status, UPDATER_IDLE))
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800478
479
Richard Barnette55d1af82018-05-22 23:40:14 +0000480 def _reset_update_engine(self):
481 """Resets the host to prepare for a clean update regardless of state."""
482 self._run('stop ui || true')
483 self._run('stop update-engine || true')
484 self._run('start update-engine')
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800485 self._wait_for_update_service()
486
Richard Barnette55d1af82018-05-22 23:40:14 +0000487
488 def _reset_stateful_partition(self):
489 """Clear any pending stateful update request."""
Richard Barnette18fd5842018-05-25 18:21:14 +0000490 self._run('%s --stateful_change=reset 2>&1'
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700491 % self._get_stateful_update_script())
Richard Barnette3ef29a82018-06-28 13:52:54 -0700492 self._run('rm -f %s' % _TARGET_VERSION)
493
494
495 def _set_target_version(self):
496 """Set the "target version" for the update."""
497 version_number = self.update_version.split('-')[1]
498 self._run('echo %s > %s' % (version_number, _TARGET_VERSION))
Richard Barnette55d1af82018-05-22 23:40:14 +0000499
500
501 def _revert_boot_partition(self):
502 """Revert the boot partition."""
503 part = self._rootdev('-s')
504 logging.warning('Reverting update; Boot partition will be %s', part)
505 return self._run('/postinst %s 2>&1' % part)
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700506
507
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700508 def _verify_kernel_state(self):
509 """Verify that the next kernel to boot is correct for update.
510
511 This tests that the kernel state is correct for a successfully
512 downloaded and installed update. That is, the next kernel to
513 boot must be the currently inactive kernel.
514
515 @raise RootFSUpdateError if the DUT next kernel isn't the
516 expected next kernel.
517 """
518 inactive_kernel = self.get_kernel_state()[1]
519 next_kernel = self._get_next_kernel()
520 if next_kernel != inactive_kernel:
521 raise RootFSUpdateError(
522 'Update failed. The kernel for next boot is %s, '
523 'but %s was expected.'
524 % (next_kernel['name'], inactive_kernel['name']))
525 return inactive_kernel
526
527
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700528 def _verify_update_completed(self):
529 """Verifies that an update has completed.
530
Richard Barnette9d43e562018-06-05 17:20:10 +0000531 @raise RootFSUpdateError if the DUT doesn't indicate that
532 download is complete and the DUT is ready for reboot.
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700533 """
534 status = self.check_update_status()
535 if status != UPDATER_NEED_REBOOT:
Shuqian Zhaod9992722016-02-29 12:26:38 -0800536 error_msg = ''
537 if status == UPDATER_IDLE:
Richard Barnette3e8b2282018-05-15 20:42:20 +0000538 error_msg = 'Update error: %s' % self._get_last_update_error()
Richard Barnette9d43e562018-06-05 17:20:10 +0000539 raise RootFSUpdateError(
540 'Update engine status is %s (%s was expected). %s'
541 % (status, UPDATER_NEED_REBOOT, error_msg))
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700542 return self._verify_kernel_state()
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700543
544
Richard Barnette55d1af82018-05-22 23:40:14 +0000545 def trigger_update(self):
Richard Barnette9d43e562018-06-05 17:20:10 +0000546 """Triggers a background update."""
547 # If this function is called immediately after reboot (which it
548 # can be), there is no guarantee that the update engine is up
549 # and running yet, so wait for it.
Richard Barnette55d1af82018-05-22 23:40:14 +0000550 self._wait_for_update_service()
551
552 autoupdate_cmd = ('%s --check_for_update --omaha_url=%s' %
553 (_UPDATER_BIN, self.update_url))
554 run_args = {'command': autoupdate_cmd}
555 err_prefix = 'Failed to trigger an update on %s. ' % self.host.hostname
556 logging.info('Triggering update via: %s', autoupdate_cmd)
557 metric_fields = {'success': False}
558 try:
559 self._base_update_handler(run_args, err_prefix)
560 metric_fields['success'] = True
561 finally:
562 c = metrics.Counter('chromeos/autotest/autoupdater/trigger')
Richard Barnette621a8e42018-06-25 17:34:11 -0700563 metric_fields.update(_get_metric_fields(self.update_url))
Richard Barnette55d1af82018-05-22 23:40:14 +0000564 c.increment(fields=metric_fields)
565
566
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700567 def update_image(self):
Richard Barnette18fd5842018-05-25 18:21:14 +0000568 """Updates the device root FS and kernel and verifies success."""
Shuqian Zhaofe4d62e2016-06-23 14:46:45 -0700569 autoupdate_cmd = ('%s --update --omaha_url=%s' %
Richard Barnette3e8b2282018-05-15 20:42:20 +0000570 (_UPDATER_BIN, self.update_url))
David Haddock76a4c882017-12-13 18:50:09 -0800571 if not self.interactive:
572 autoupdate_cmd = '%s --interactive=false' % autoupdate_cmd
Shuqian Zhaod9992722016-02-29 12:26:38 -0800573 run_args = {'command': autoupdate_cmd, 'timeout': 3600}
574 err_prefix = ('Failed to install device image using payload at %s '
575 'on %s. ' % (self.update_url, self.host.hostname))
576 logging.info('Updating image via: %s', autoupdate_cmd)
Allen Li1a5cc0a2017-06-20 14:08:59 -0700577 metric_fields = {'success': False}
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800578 try:
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800579 self._base_update_handler(run_args, err_prefix)
Allen Li1a5cc0a2017-06-20 14:08:59 -0700580 metric_fields['success'] = True
581 finally:
Allen Li1a5cc0a2017-06-20 14:08:59 -0700582 c = metrics.Counter('chromeos/autotest/autoupdater/update')
Richard Barnette621a8e42018-06-25 17:34:11 -0700583 metric_fields.update(_get_metric_fields(self.update_url))
Allen Li1a5cc0a2017-06-20 14:08:59 -0700584 c.increment(fields=metric_fields)
Richard Barnette4d211c92018-05-24 18:56:08 +0000585 return self._verify_update_completed()
Gilad Arnoldd6adeb82015-09-21 07:10:03 -0700586
587
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700588 def _get_remote_script(self, script_name):
589 """Ensure that `script_name` is present on the DUT.
Chris Sosa5e4246b2012-05-22 18:05:22 -0700590
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700591 The given script (e.g. `stateful_update`) may be present in the
592 stateful partition under /usr/local/bin, or we may have to
593 download it from the devserver.
Chris Sosaa3ac2152012-05-23 22:23:13 -0700594
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700595 Determine whether the script is present or must be downloaded
596 and download if necessary. Then, return a command fragment
597 sufficient to run the script from whereever it now lives on the
598 DUT.
Richard Barnette9d43e562018-06-05 17:20:10 +0000599
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700600 @param script_name The name of the script as expected in
601 /usr/local/bin and on the devserver.
602 @return A string with the command (minus arguments) that will
603 run the target script.
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700604 """
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700605 remote_script = '/usr/local/bin/%s' % script_name
606 if self.host.path_exists(remote_script):
607 return remote_script
608 remote_tmp_script = '/tmp/%s' % script_name
609 server_name = urlparse.urlparse(self.update_url)[1]
610 script_url = 'http://%s/static/%s' % (server_name, script_name)
611 fetch_script = (
612 'curl -o %s %s && head -1 %s | grep "^#!" | sed "s/#!//"') % (
613 remote_tmp_script, script_url, remote_tmp_script)
614 script_interpreter = self._run(fetch_script,
615 ignore_status=True).stdout.strip()
616 if not script_interpreter:
617 return None
618 return '%s %s' % (script_interpreter, remote_tmp_script)
Chris Sosa5e4246b2012-05-22 18:05:22 -0700619
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700620
621 def _get_stateful_update_script(self):
622 """Returns a command to run the stateful update script.
623
624 Find `stateful_update` on the target or install it, as
625 necessary. If installation fails, raise an exception.
626
627 @raise StatefulUpdateError if the script can't be found or
628 installed.
629 @return A string that can be joined with arguments to run the
630 `stateful_update` command on the DUT.
631 """
632 script_command = self._get_remote_script(_STATEFUL_UPDATE_SCRIPT)
633 if not script_command:
634 raise StatefulUpdateError('Could not install %s on DUT'
Richard Barnette9d43e562018-06-05 17:20:10 +0000635 % _STATEFUL_UPDATE_SCRIPT)
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700636 return script_command
Chris Sosa5e4246b2012-05-22 18:05:22 -0700637
638
Chris Sosac1932172013-10-16 13:28:53 -0700639 def rollback_rootfs(self, powerwash):
640 """Triggers rollback and waits for it to complete.
641
642 @param powerwash: If true, powerwash as part of rollback.
643
644 @raise RootFSUpdateError if anything went wrong.
Chris Sosac1932172013-10-16 13:28:53 -0700645 """
Dan Shi549fb822015-03-24 18:01:11 -0700646 version = self.host.get_release_version()
Chris Sosac8617522014-06-09 23:22:26 +0000647 # Introduced can_rollback in M36 (build 5772). # etc/lsb-release matches
648 # X.Y.Z. This version split just pulls the first part out.
649 try:
650 build_number = int(version.split('.')[0])
651 except ValueError:
652 logging.error('Could not parse build number.')
653 build_number = 0
654
655 if build_number >= 5772:
Richard Barnette3e8b2282018-05-15 20:42:20 +0000656 can_rollback_cmd = '%s --can_rollback' % _UPDATER_BIN
Chris Sosac8617522014-06-09 23:22:26 +0000657 logging.info('Checking for rollback.')
658 try:
659 self._run(can_rollback_cmd)
660 except error.AutoservRunError as e:
661 raise RootFSUpdateError("Rollback isn't possible on %s: %s" %
662 (self.host.hostname, str(e)))
663
Richard Barnette3e8b2282018-05-15 20:42:20 +0000664 rollback_cmd = '%s --rollback --follow' % _UPDATER_BIN
Chris Sosac1932172013-10-16 13:28:53 -0700665 if not powerwash:
Dan Shif3a35f72016-01-25 11:18:14 -0800666 rollback_cmd += ' --nopowerwash'
Chris Sosac1932172013-10-16 13:28:53 -0700667
Chris Sosac8617522014-06-09 23:22:26 +0000668 logging.info('Performing rollback.')
Chris Sosac1932172013-10-16 13:28:53 -0700669 try:
670 self._run(rollback_cmd)
Chris Sosac1932172013-10-16 13:28:53 -0700671 except error.AutoservRunError as e:
672 raise RootFSUpdateError('Rollback failed on %s: %s' %
673 (self.host.hostname, str(e)))
674
675 self._verify_update_completed()
676
Gilad Arnold0ed760c2012-11-05 23:42:53 -0800677
Chris Sosa72312602013-04-16 15:01:56 -0700678 def update_stateful(self, clobber=True):
679 """Updates the stateful partition.
680
681 @param clobber: If True, a clean stateful installation.
Richard Barnette9d43e562018-06-05 17:20:10 +0000682
683 @raise StatefulUpdateError if the update script fails to
684 complete successfully.
Chris Sosa72312602013-04-16 15:01:56 -0700685 """
Chris Sosa77556d82012-04-05 15:23:14 -0700686 logging.info('Updating stateful partition...')
Richard Barnette18fd5842018-05-25 18:21:14 +0000687 statefuldev_url = self.update_url.replace('update', 'static')
Chris Sosaa3ac2152012-05-23 22:23:13 -0700688
Dale Curtis5c32c722011-05-04 19:24:23 -0700689 # Attempt stateful partition update; this must succeed so that the newly
690 # installed host is testable after update.
Richard Barnettef00a2ee2018-06-08 11:51:38 -0700691 statefuldev_cmd = [self._get_stateful_update_script(), statefuldev_url]
Chris Sosa72312602013-04-16 15:01:56 -0700692 if clobber:
693 statefuldev_cmd.append('--stateful_change=clean')
694
695 statefuldev_cmd.append('2>&1')
Dale Curtis5c32c722011-05-04 19:24:23 -0700696 try:
Dan Shi205b8732016-01-25 10:56:22 -0800697 self._run(' '.join(statefuldev_cmd), timeout=1200)
Dale Curtis5c32c722011-05-04 19:24:23 -0700698 except error.AutoservRunError:
Richard Barnette18fd5842018-05-25 18:21:14 +0000699 raise StatefulUpdateError(
Gilad Arnold62cf3a42015-10-01 09:15:25 -0700700 'Failed to perform stateful update on %s' %
701 self.host.hostname)
Dale Curtis5c32c722011-05-04 19:24:23 -0700702
Chris Sosaa3ac2152012-05-23 22:23:13 -0700703
Richard Barnette54d14f52018-05-18 16:39:49 +0000704 def verify_boot_expectations(self, expected_kernel, rollback_message):
Richard Barnette55d1af82018-05-22 23:40:14 +0000705 """Verifies that we fully booted given expected kernel state.
706
707 This method both verifies that we booted using the correct kernel
708 state and that the OS has marked the kernel as good.
709
Richard Barnette54d14f52018-05-18 16:39:49 +0000710 @param expected_kernel: kernel that we are verifying with,
Richard Barnette55d1af82018-05-22 23:40:14 +0000711 i.e. I expect to be booted onto partition 4 etc. See output of
712 get_kernel_state.
Richard Barnette9d43e562018-06-05 17:20:10 +0000713 @param rollback_message: string include in except message text
Richard Barnette55d1af82018-05-22 23:40:14 +0000714 if we booted with the wrong partition.
715
Richard Barnette9d43e562018-06-05 17:20:10 +0000716 @raise NewBuildUpdateError if any of the various checks fail.
Richard Barnette55d1af82018-05-22 23:40:14 +0000717 """
718 # Figure out the newly active kernel.
Richard Barnette54d14f52018-05-18 16:39:49 +0000719 active_kernel = self.get_kernel_state()[0]
Richard Barnette55d1af82018-05-22 23:40:14 +0000720
721 # Check for rollback due to a bad build.
Richard Barnette54d14f52018-05-18 16:39:49 +0000722 if active_kernel != expected_kernel:
Richard Barnette55d1af82018-05-22 23:40:14 +0000723
724 # Kernel crash reports should be wiped between test runs, but
725 # may persist from earlier parts of the test, or from problems
726 # with provisioning.
727 #
728 # Kernel crash reports will NOT be present if the crash happened
729 # before encrypted stateful is mounted.
730 #
731 # TODO(dgarrett): Integrate with server/crashcollect.py at some
732 # point.
733 kernel_crashes = glob.glob('/var/spool/crash/kernel.*.kcrash')
734 if kernel_crashes:
735 rollback_message += ': kernel_crash'
736 logging.debug('Found %d kernel crash reports:',
737 len(kernel_crashes))
738 # The crash names contain timestamps that may be useful:
739 # kernel.20131207.005945.0.kcrash
740 for crash in kernel_crashes:
741 logging.debug(' %s', os.path.basename(crash))
742
743 # Print out some information to make it easier to debug
744 # the rollback.
745 logging.debug('Dumping partition table.')
746 self._run('cgpt show $(rootdev -s -d)')
747 logging.debug('Dumping crossystem for firmware debugging.')
748 self._run('crossystem --all')
Richard Barnette9d43e562018-06-05 17:20:10 +0000749 raise NewBuildUpdateError(self.update_version, rollback_message)
Richard Barnette55d1af82018-05-22 23:40:14 +0000750
751 # Make sure chromeos-setgoodkernel runs.
752 try:
753 utils.poll_for_condition(
Richard Barnette54d14f52018-05-18 16:39:49 +0000754 lambda: (self._get_kernel_tries(active_kernel) == 0
755 and self._get_kernel_success(active_kernel)),
Richard Barnette9d43e562018-06-05 17:20:10 +0000756 exception=RootFSUpdateError(),
Richard Barnette55d1af82018-05-22 23:40:14 +0000757 timeout=_KERNEL_UPDATE_TIMEOUT, sleep_interval=5)
Richard Barnette9d43e562018-06-05 17:20:10 +0000758 except RootFSUpdateError:
Richard Barnette55d1af82018-05-22 23:40:14 +0000759 services_status = self._run('status system-services').stdout
760 if services_status != 'system-services start/running\n':
761 event = ('Chrome failed to reach login screen')
762 else:
763 event = ('update-engine failed to call '
764 'chromeos-setgoodkernel')
Richard Barnette9d43e562018-06-05 17:20:10 +0000765 raise NewBuildUpdateError(self.update_version, event)
Richard Barnette55d1af82018-05-22 23:40:14 +0000766
767
Richard Barnette14ee84c2018-05-18 20:23:42 +0000768 def _prepare_host(self):
769 """Make sure the target DUT is working and ready for update.
770
771 Initially, the target DUT's state is unknown. The DUT is
772 expected to be online, but we strive to be forgiving if Chrome
773 and/or the update engine aren't fully functional.
774 """
775 # Summary of work, and the rationale:
776 # 1. Reboot, because it's a good way to clear out problems.
777 # 2. Touch the PROVISION_FAILED file, to allow repair to detect
778 # failure later.
779 # 3. Run the hook for host class specific preparation.
780 # 4. Stop Chrome, because the system is designed to eventually
781 # reboot if Chrome is stuck in a crash loop.
782 # 5. Force `update-engine` to start, because if Chrome failed
783 # to start properly, the status of the `update-engine` job
784 # will be uncertain.
785 self._reset_stateful_partition()
786 self.host.reboot(timeout=self.host.REBOOT_TIMEOUT)
787 self._run('touch %s' % PROVISION_FAILED)
788 self.host.prepare_for_update()
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700789 self._reset_update_engine()
Richard Barnette14ee84c2018-05-18 20:23:42 +0000790 logging.info('Updating from version %s to %s.',
791 self.host.get_release_version(),
792 self.update_version)
793
794
795 def _verify_devserver(self):
Richard Barnette9d43e562018-06-05 17:20:10 +0000796 """Check that our chosen devserver is still working.
797
798 @raise DevServerError if the devserver fails any sanity check.
799 """
Richard Barnette14ee84c2018-05-18 20:23:42 +0000800 server = 'http://%s' % urlparse.urlparse(self.update_url)[1]
801 try:
802 if not dev_server.ImageServer.devserver_healthy(server):
Richard Barnette9d43e562018-06-05 17:20:10 +0000803 raise DevServerError(
804 server, 'Devserver is not healthy')
Richard Barnette14ee84c2018-05-18 20:23:42 +0000805 except Exception as e:
Richard Barnette9d43e562018-06-05 17:20:10 +0000806 raise DevServerError(
807 server, 'Devserver is not up and available')
Richard Barnette14ee84c2018-05-18 20:23:42 +0000808
809
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700810 def _install_via_update_engine(self):
811 """Install an updating using the production AU flow.
812
813 This uses the standard AU flow and the `stateful_update` script
814 to download and install a root FS, kernel and stateful
815 filesystem content.
816
817 @return The kernel expected to be booted next.
818 """
819 logging.info('Installing image using update_engine.')
820 expected_kernel = self.update_image()
821 self.update_stateful()
Richard Barnette3ef29a82018-06-28 13:52:54 -0700822 self._set_target_version()
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700823 return expected_kernel
824
825
826 def _install_via_quick_provision(self):
827 """Install an updating using the `quick-provision` script.
828
829 This uses the `quick-provision` script to download and install
830 a root FS, kernel and stateful filesystem content.
831
832 @return The kernel expected to be booted next.
833 """
834 build_re = global_config.global_config.get_config_value(
835 'CROS', 'quick_provision_build_regex', type=str, default='')
836 image_name = url_to_image_name(self.update_url)
837 if not build_re or re.match(build_re, image_name) is None:
838 logging.info('Not eligible for quick-provision.')
839 return None
840 logging.info('Installing image using quick-provision.')
841 provision_command = self._get_remote_script(_QUICK_PROVISION_SCRIPT)
842 server_name = urlparse.urlparse(self.update_url)[1]
843 static_url = 'http://%s/static' % server_name
844 command = '%s --noreboot %s %s' % (
845 provision_command, image_name, static_url)
846 try:
847 self._run(command)
Richard Barnette3ef29a82018-06-28 13:52:54 -0700848 self._set_target_version()
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700849 return self._verify_kernel_state()
850 except Exception:
851 # N.B. We handle only `Exception` here. Non-Exception
852 # classes (such as KeyboardInterrupt) are handled by our
853 # caller.
854 logging.exception('quick-provision script failed; '
855 'will fall back to update_engine.')
856 self._revert_boot_partition()
857 self._reset_stateful_partition()
858 self._reset_update_engine()
859 return None
860
861
Richard Barnette54d14f52018-05-18 16:39:49 +0000862 def _install_update(self):
Richard Barnette0beb14b2018-05-15 18:07:52 +0000863 """Install the requested image on the DUT, but don't start it.
864
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700865 This downloads and installs a root FS, kernel and stateful
866 filesystem content. This does not reboot the DUT, so the update
867 is merely pending when the method returns.
868
869 @return The kernel expected to be booted next.
Dan Shi0f466e82013-02-22 15:44:58 -0800870 """
Richard Barnette14ee84c2018-05-18 20:23:42 +0000871 logging.info('Installing image at %s onto %s',
872 self.update_url, self.host.hostname)
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200873 try:
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700874 return (self._install_via_quick_provision()
875 or self._install_via_update_engine())
Dale Curtis1e973182011-07-12 18:21:36 -0700876 except:
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700877 # N.B. This handling code includes non-Exception classes such
878 # as KeyboardInterrupt. We need to clean up, but we also must
879 # re-raise.
Richard Barnette14ee84c2018-05-18 20:23:42 +0000880 self._revert_boot_partition()
881 self._reset_stateful_partition()
Richard Barnettee86b1ce2018-06-07 10:37:23 -0700882 self._reset_update_engine()
Dale Curtis1e973182011-07-12 18:21:36 -0700883 # Collect update engine logs in the event of failure.
884 if self.host.job:
Aviv Keshet2610d3e2016-06-01 16:37:01 -0700885 logging.info('Collecting update engine logs due to failure...')
Dale Curtis1e973182011-07-12 18:21:36 -0700886 self.host.get_file(
Richard Barnette3e8b2282018-05-15 20:42:20 +0000887 _UPDATER_LOGS, self.host.job.sysinfo.sysinfodir,
Gilad Arnold0c0df732015-09-21 06:37:59 -0700888 preserve_perm=False)
Richard Barnette3e8b2282018-05-15 20:42:20 +0000889 _list_image_dir_contents(self.update_url)
Dale Curtis1e973182011-07-12 18:21:36 -0700890 raise
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200891
892
Richard Barnette14ee84c2018-05-18 20:23:42 +0000893 def _complete_update(self, expected_kernel):
894 """Finish the update, and confirm that it succeeded.
Richard Barnette0beb14b2018-05-15 18:07:52 +0000895
Richard Barnette14ee84c2018-05-18 20:23:42 +0000896 Initial condition is that the target build has been downloaded
897 and installed on the DUT, but has not yet been booted. This
898 function is responsible for rebooting the DUT, and checking that
899 the new build is running successfully.
Richard Barnette0beb14b2018-05-15 18:07:52 +0000900
Richard Barnette14ee84c2018-05-18 20:23:42 +0000901 @param expected_kernel: kernel expected to be active after reboot.
Richard Barnette0beb14b2018-05-15 18:07:52 +0000902 """
Richard Barnette14ee84c2018-05-18 20:23:42 +0000903 # Regarding the 'crossystem' command below: In some cases,
904 # the update flow puts the TPM into a state such that it
905 # fails verification. We don't know why. However, this
906 # call papers over the problem by clearing the TPM during
907 # the reboot.
908 #
909 # We ignore failures from 'crossystem'. Although failure
910 # here is unexpected, and could signal a bug, the point of
911 # the exercise is to paper over problems; allowing this to
912 # fail would defeat the purpose.
913 self._run('crossystem clear_tpm_owner_request=1',
914 ignore_status=True)
915 self.host.reboot(timeout=self.host.REBOOT_TIMEOUT)
916
Richard Barnette0beb14b2018-05-15 18:07:52 +0000917 # Touch the lab machine file to leave a marker that
918 # distinguishes this image from other test images.
919 # Afterwards, we must re-run the autoreboot script because
920 # it depends on the _LAB_MACHINE_FILE.
921 autoreboot_cmd = ('FILE="%s" ; [ -f "$FILE" ] || '
922 '( touch "$FILE" ; start autoreboot )')
Richard Barnette3e8b2282018-05-15 20:42:20 +0000923 self._run(autoreboot_cmd % _LAB_MACHINE_FILE)
Richard Barnette0beb14b2018-05-15 18:07:52 +0000924 self.verify_boot_expectations(
925 expected_kernel, rollback_message=
926 'Build %s failed to boot on %s; system rolled back to previous '
927 'build' % (self.update_version, self.host.hostname))
928
929 logging.debug('Cleaning up old autotest directories.')
930 try:
931 installed_autodir = autotest.Autotest.get_installed_autodir(
932 self.host)
933 self._run('rm -rf ' + installed_autodir)
934 except autotest.AutodirNotFoundError:
935 logging.debug('No autotest installed directory found.')
936
937
Richard Barnette621a8e42018-06-25 17:34:11 -0700938 def _run_update_steps(self):
939 """Perform a full update of a DUT, with diagnosis for failures.
Richard Barnette0beb14b2018-05-15 18:07:52 +0000940
Richard Barnette621a8e42018-06-25 17:34:11 -0700941 Run the individual steps of the update. If a step fails, make
942 sure that the exception raised describes the failure with a
943 diagnosis based on the step that failed.
Richard Barnette0beb14b2018-05-15 18:07:52 +0000944
Richard Barnette621a8e42018-06-25 17:34:11 -0700945 @raise HostUpdateError if a failure is caused by a problem on
Richard Barnette9d43e562018-06-05 17:20:10 +0000946 the DUT prior to the update.
Richard Barnette621a8e42018-06-25 17:34:11 -0700947 @raise ImageInstallError if a failure occurs during download
Richard Barnette9d43e562018-06-05 17:20:10 +0000948 and install of the update and cannot be definitively
949 blamed on either the DUT or the devserver.
Richard Barnette621a8e42018-06-25 17:34:11 -0700950 @raise NewBuildUpdateError if a failure occurs because the
Richard Barnette9d43e562018-06-05 17:20:10 +0000951 new build fails to function correctly.
Richard Barnette0beb14b2018-05-15 18:07:52 +0000952 """
Richard Barnette14ee84c2018-05-18 20:23:42 +0000953 self._verify_devserver()
Richard Barnette9d43e562018-06-05 17:20:10 +0000954
955 try:
956 self._prepare_host()
957 except _AttributedUpdateError:
958 raise
959 except Exception as e:
960 logging.exception('Failure preparing host prior to update.')
961 raise HostUpdateError(self.host.hostname, str(e))
962
963 try:
964 expected_kernel = self._install_update()
965 except _AttributedUpdateError:
966 raise
967 except Exception as e:
968 logging.exception('Failure during download and install.')
Richard Barnette621a8e42018-06-25 17:34:11 -0700969 server_name = dev_server.get_resolved_hostname(self.update_url)
Richard Barnette9d43e562018-06-05 17:20:10 +0000970 raise ImageInstallError(self.host.hostname, server_name, str(e))
971
972 try:
973 self._complete_update(expected_kernel)
974 except _AttributedUpdateError:
975 raise
976 except Exception as e:
977 logging.exception('Failure from build after update.')
978 raise NewBuildUpdateError(self.update_version, str(e))
Richard Barnette0beb14b2018-05-15 18:07:52 +0000979
Richard Barnette621a8e42018-06-25 17:34:11 -0700980
981 def run_update(self):
982 """Perform a full update of a DUT in the test lab.
983
984 This downloads and installs the root FS and stateful partition
985 content needed for the update specified in `self.host` and
986 `self.update_url`. The update is performed according to the
987 requirements for provisioning a DUT for testing the requested
988 build.
989
990 At the end of the procedure, metrics are reported describing the
991 outcome of the operation.
992
993 @returns A tuple of the form `(image_name, attributes)`, where
994 `image_name` is the name of the image installed, and
995 `attributes` is new attributes to be applied to the DUT.
996 """
997 start_time = time.time()
998 failure_reason = None
999 server_name = dev_server.get_resolved_hostname(self.update_url)
1000 metrics.Counter(_metric_name('install')).increment(
1001 fields={'devserver': server_name})
1002 try:
1003 self._run_update_steps()
1004 except _AttributedUpdateError as e:
1005 failure_reason = e.failure_summary
1006 raise
1007 except Exception as e:
1008 failure_reason = 'Unknown failure'
1009 raise
1010 finally:
1011 end_time = time.time()
1012 _emit_provision_metrics(
1013 self.update_url, self.host.hostname,
1014 failure_reason, end_time - start_time)
1015
Richard Barnette0beb14b2018-05-15 18:07:52 +00001016 image_name = url_to_image_name(self.update_url)
1017 # update_url is different from devserver url needed to stage autotest
1018 # packages, therefore, resolve a new devserver url here.
1019 devserver_url = dev_server.ImageServer.resolve(
1020 image_name, self.host.hostname).url()
1021 repo_url = tools.get_package_url(devserver_url, image_name)
1022 return image_name, {ds_constants.JOB_REPO_URL: repo_url}