blob: af2dae305c3bf3ba8f1a7f3d92371d3a88d0e462 [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 httplib
7import logging
Chris Sosa77556d82012-04-05 15:23:14 -07008import multiprocessing
Dale Curtis5c32c722011-05-04 19:24:23 -07009import os
Sean O'Connor5346e4e2010-08-12 18:49:24 +020010import re
Sean O'Connor5346e4e2010-08-12 18:49:24 +020011import urlparse
12
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
Sean O'Connor5346e4e2010-08-12 18:49:24 +020015
Dale Curtis5c32c722011-05-04 19:24:23 -070016# Local stateful update path is relative to the CrOS source directory.
17LOCAL_STATEFUL_UPDATE_PATH = 'src/platform/dev/stateful_update'
Chris Sosaa3ac2152012-05-23 22:23:13 -070018LOCAL_CHROOT_STATEFUL_UPDATE_PATH = '/usr/bin/stateful_update'
Dale Curtis5c32c722011-05-04 19:24:23 -070019REMOTE_STATEUL_UPDATE_PATH = '/usr/local/bin/stateful_update'
20STATEFUL_UPDATE = '/tmp/stateful_update'
Sean O'Connor5346e4e2010-08-12 18:49:24 +020021UPDATER_BIN = '/usr/bin/update_engine_client'
22UPDATER_IDLE = 'UPDATE_STATUS_IDLE'
Sean Oc053dfe2010-08-23 18:22:26 +020023UPDATER_NEED_REBOOT = 'UPDATE_STATUS_UPDATED_NEED_REBOOT'
Darin Petkov7d572992010-09-23 10:11:05 -070024UPDATED_MARKER = '/var/run/update_engine_autoupdate_completed'
Dale Curtis1e973182011-07-12 18:21:36 -070025UPDATER_LOGS = '/var/log/messages /var/log/update_engine'
Sean O'Connor5346e4e2010-08-12 18:49:24 +020026
27
28class ChromiumOSError(error.InstallError):
29 """Generic error for ChromiumOS-specific exceptions."""
30 pass
31
32
Chris Sosa77556d82012-04-05 15:23:14 -070033class RootFSUpdateError(ChromiumOSError):
34 """Raised when the RootFS fails to update."""
35 pass
36
37
38class StatefulUpdateError(ChromiumOSError):
39 """Raised when the stateful partition fails to update."""
40 pass
41
42
Sean O'Connor5346e4e2010-08-12 18:49:24 +020043def url_to_version(update_url):
Dan Shi0f466e82013-02-22 15:44:58 -080044 """Return the version based on update_url.
45
46 @param update_url: url to the image to update to.
47
48 """
Dale Curtisddfdb942011-07-14 13:59:24 -070049 # The Chrome OS version is generally the last element in the URL. The only
50 # exception is delta update URLs, which are rooted under the version; e.g.,
51 # http://.../update/.../0.14.755.0/au/0.14.754.0. In this case we want to
52 # strip off the au section of the path before reading the version.
Dan Shi5002cfc2013-04-29 10:45:05 -070053 return re.sub('/au/.*', '',
54 urlparse.urlparse(update_url).path).split('/')[-1].strip()
Sean O'Connor5346e4e2010-08-12 18:49:24 +020055
56
Scott Zawalskieadbf702013-03-14 09:23:06 -040057def url_to_image_name(update_url):
58 """Return the image name based on update_url.
59
60 From a URL like:
61 http://172.22.50.205:8082/update/lumpy-release/R27-3837.0.0
62 return lumpy-release/R27-3837.0.0
63
64 @param update_url: url to the image to update to.
65 @returns a string representing the image name in the update_url.
66
67 """
68 return '/'.join(urlparse.urlparse(update_url).path.split('/')[-2:])
69
70
Sean O'Connor5346e4e2010-08-12 18:49:24 +020071class ChromiumOSUpdater():
Dan Shi0f466e82013-02-22 15:44:58 -080072 """Helper class used to update DUT with image of desired version."""
Dale Curtisa94c19c2011-05-02 15:05:17 -070073 KERNEL_A = {'name': 'KERN-A', 'kernel': 2, 'root': 3}
74 KERNEL_B = {'name': 'KERN-B', 'kernel': 4, 'root': 5}
Chris Sosa65425082013-10-16 13:26:22 -070075 # Time to wait for new kernel to be marked successful after
76 # auto update.
77 KERNEL_UPDATE_TIMEOUT = 120
Dale Curtisa94c19c2011-05-02 15:05:17 -070078
79
Chris Sosaa3ac2152012-05-23 22:23:13 -070080 def __init__(self, update_url, host=None, local_devserver=False):
Sean O'Connor5346e4e2010-08-12 18:49:24 +020081 self.host = host
82 self.update_url = update_url
Chris Sosa77556d82012-04-05 15:23:14 -070083 self._update_error_queue = multiprocessing.Queue(2)
Chris Sosaa3ac2152012-05-23 22:23:13 -070084 self.local_devserver = local_devserver
85 if not local_devserver:
86 self.update_version = url_to_version(update_url)
87 else:
88 self.update_version = None
Sean Oc053dfe2010-08-23 18:22:26 +020089
Sean O'Connor5346e4e2010-08-12 18:49:24 +020090 def check_update_status(self):
Dale Curtis5c32c722011-05-04 19:24:23 -070091 """Return current status from update-engine."""
92 update_status = self._run(
93 '%s -status 2>&1 | grep CURRENT_OP' % UPDATER_BIN)
Sean O'Connor5346e4e2010-08-12 18:49:24 +020094 return update_status.stdout.strip().split('=')[-1]
95
Sean Oc053dfe2010-08-23 18:22:26 +020096
97 def reset_update_engine(self):
Dale Curtis5c32c722011-05-04 19:24:23 -070098 """Restarts the update-engine service."""
Darin Petkov7d572992010-09-23 10:11:05 -070099 self._run('rm -f %s' % UPDATED_MARKER)
Sean O267c00b2010-08-31 15:54:55 +0200100 try:
101 self._run('initctl stop update-engine')
Dale Curtis5c32c722011-05-04 19:24:23 -0700102 except error.AutoservRunError:
Sean O267c00b2010-08-31 15:54:55 +0200103 logging.warn('Stopping update-engine service failed. Already dead?')
Sean Oc053dfe2010-08-23 18:22:26 +0200104 self._run('initctl start update-engine')
Dale Curtis5c32c722011-05-04 19:24:23 -0700105
Sean Oc053dfe2010-08-23 18:22:26 +0200106 if self.check_update_status() != UPDATER_IDLE:
107 raise ChromiumOSError('%s is not in an installable state' %
108 self.host.hostname)
109
110
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200111 def _run(self, cmd, *args, **kwargs):
Dale Curtis5c32c722011-05-04 19:24:23 -0700112 """Abbreviated form of self.host.run(...)"""
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200113 return self.host.run(cmd, *args, **kwargs)
114
Sean Oc053dfe2010-08-23 18:22:26 +0200115
Dale Curtisa94c19c2011-05-02 15:05:17 -0700116 def rootdev(self, options=''):
Dan Shi0f466e82013-02-22 15:44:58 -0800117 """Returns the stripped output of rootdev <options>.
118
119 @param options: options to run rootdev.
120
121 """
Dale Curtisa94c19c2011-05-02 15:05:17 -0700122 return self._run('rootdev %s' % options).stdout.strip()
123
124
125 def get_kernel_state(self):
126 """Returns the (<active>, <inactive>) kernel state as a pair."""
127 active_root = int(re.findall('\d+\Z', self.rootdev('-s'))[0])
128 if active_root == self.KERNEL_A['root']:
129 return self.KERNEL_A, self.KERNEL_B
130 elif active_root == self.KERNEL_B['root']:
131 return self.KERNEL_B, self.KERNEL_A
132 else:
Dale Curtis5c32c722011-05-04 19:24:23 -0700133 raise ChromiumOSError('Encountered unknown root partition: %s' %
Dale Curtisa94c19c2011-05-02 15:05:17 -0700134 active_root)
135
136
137 def _cgpt(self, flag, kernel, dev='$(rootdev -s -d)'):
138 """Return numeric cgpt value for the specified flag, kernel, device. """
139 return int(self._run('cgpt show -n -i %d %s %s' % (
140 kernel['kernel'], flag, dev)).stdout.strip())
141
142
143 def get_kernel_priority(self, kernel):
Dan Shi0f466e82013-02-22 15:44:58 -0800144 """Return numeric priority for the specified kernel.
145
146 @param kernel: information of the given kernel, KERNEL_A or KERNEL_B.
147
148 """
Dale Curtisa94c19c2011-05-02 15:05:17 -0700149 return self._cgpt('-P', kernel)
150
151
152 def get_kernel_success(self, kernel):
Dan Shi0f466e82013-02-22 15:44:58 -0800153 """Return boolean success flag for the specified kernel.
154
155 @param kernel: information of the given kernel, KERNEL_A or KERNEL_B.
156
157 """
Dale Curtisa94c19c2011-05-02 15:05:17 -0700158 return self._cgpt('-S', kernel) != 0
159
160
161 def get_kernel_tries(self, kernel):
Dan Shi0f466e82013-02-22 15:44:58 -0800162 """Return tries count for the specified kernel.
163
164 @param kernel: information of the given kernel, KERNEL_A or KERNEL_B.
165
166 """
Dale Curtisa94c19c2011-05-02 15:05:17 -0700167 return self._cgpt('-T', kernel)
Sean O267c00b2010-08-31 15:54:55 +0200168
169
Chris Sosa5e4246b2012-05-22 18:05:22 -0700170 def get_stateful_update_script(self):
171 """Returns the path to the stateful update script on the target."""
Chris Sosaa3ac2152012-05-23 22:23:13 -0700172 # We attempt to load the local stateful update path in 3 different
173 # ways. First we use the location specified in the autotest global
174 # config. If this doesn't exist, we attempt to use the Chromium OS
175 # Chroot path to the installed script. If all else fails, we use the
176 # stateful update script on the host.
Chris Sosa5e4246b2012-05-22 18:05:22 -0700177 stateful_update_path = os.path.join(
178 global_config.global_config.get_config_value(
179 'CROS', 'source_tree', default=''),
180 LOCAL_STATEFUL_UPDATE_PATH)
181
Chris Sosaa3ac2152012-05-23 22:23:13 -0700182 if not os.path.exists(stateful_update_path):
183 logging.warn('Could not find Chrome OS source location for '
184 'stateful_update script at %s, falling back to chroot '
185 'copy.', stateful_update_path)
186 stateful_update_path = LOCAL_CHROOT_STATEFUL_UPDATE_PATH
187
188 if not os.path.exists(stateful_update_path):
189 logging.warn('Could not chroot stateful_update script, falling '
190 'back on client copy.')
191 statefuldev_script = REMOTE_STATEUL_UPDATE_PATH
192 else:
Chris Sosa5e4246b2012-05-22 18:05:22 -0700193 self.host.send_file(
194 stateful_update_path, STATEFUL_UPDATE, delete_dest=True)
195 statefuldev_script = STATEFUL_UPDATE
Chris Sosa5e4246b2012-05-22 18:05:22 -0700196
197 return statefuldev_script
198
199
200 def reset_stateful_partition(self):
Dan Shi0f466e82013-02-22 15:44:58 -0800201 """Clear any pending stateful update request."""
Chris Sosa5e4246b2012-05-22 18:05:22 -0700202 statefuldev_cmd = [self.get_stateful_update_script()]
203 statefuldev_cmd += ['--stateful_change=reset', '2>&1']
Chris Sosa66d74072013-09-19 11:21:29 -0700204 self._run(' '.join(statefuldev_cmd))
Chris Sosa5e4246b2012-05-22 18:05:22 -0700205
206
Sean O267c00b2010-08-31 15:54:55 +0200207 def revert_boot_partition(self):
Dan Shi0f466e82013-02-22 15:44:58 -0800208 """Revert the boot partition."""
Dale Curtisd9b26b92011-10-24 13:34:46 -0700209 part = self.rootdev('-s')
Sean O267c00b2010-08-31 15:54:55 +0200210 logging.warn('Reverting update; Boot partition will be %s', part)
211 return self._run('/postinst %s 2>&1' % part)
212
213
Gilad Arnold0ed760c2012-11-05 23:42:53 -0800214 def trigger_update(self):
215 """Triggers a background update on a test image.
216
217 @raise RootFSUpdateError if anything went wrong.
218
219 """
220 autoupdate_cmd = '%s --check_for_update --omaha_url=%s' % (
221 UPDATER_BIN, self.update_url)
Gilad Arnold0338ff32013-10-02 12:16:26 -0700222 logging.info('Triggering update via: %s', autoupdate_cmd)
Gilad Arnold0ed760c2012-11-05 23:42:53 -0800223 try:
Chris Sosa66d74072013-09-19 11:21:29 -0700224 self._run(autoupdate_cmd)
Richard Barnette83e6b542013-12-13 21:38:03 +0000225 except error.AutoservRunError, e:
226 raise RootFSUpdateError('Update triggering failed on %s: %s' %
227 (self.host.hostname, str(e)))
Gilad Arnold0ed760c2012-11-05 23:42:53 -0800228
Chris Sosac1932172013-10-16 13:28:53 -0700229 def _verify_update_completed(self):
230 """Verifies that an update has completed.
231
232 @raise RootFSUpdateError: if verification fails.
233 """
234 status = self.check_update_status()
235 if status != UPDATER_NEED_REBOOT:
236 raise RootFSUpdateError('Update did not complete with correct '
237 'status. Expecting %s, actual %s' %
238 (UPDATER_NEED_REBOOT, status))
239
240
241 def rollback_rootfs(self, powerwash):
242 """Triggers rollback and waits for it to complete.
243
244 @param powerwash: If true, powerwash as part of rollback.
245
246 @raise RootFSUpdateError if anything went wrong.
247
248 """
249 #TODO(sosa): crbug.com/309051 - Make this one update_engine_client call.
250 rollback_cmd = '%s --rollback' % (UPDATER_BIN)
251 wait_for_update_to_complete_cmd = '%s --update' % (UPDATER_BIN)
252 if not powerwash:
253 rollback_cmd += ' --nopowerwash'
254
255 logging.info('Triggering rollback.')
256 try:
257 self._run(rollback_cmd)
258 self._run(wait_for_update_to_complete_cmd)
259 except error.AutoservRunError as e:
260 raise RootFSUpdateError('Rollback failed on %s: %s' %
261 (self.host.hostname, str(e)))
262
263 self._verify_update_completed()
264
Gilad Arnold0ed760c2012-11-05 23:42:53 -0800265
Chris Sosa2f1ae9f2013-08-13 10:00:15 -0700266 def update_rootfs(self):
267 """Updates the rootfs partition only."""
Chris Sosa77556d82012-04-05 15:23:14 -0700268 logging.info('Updating root partition...')
Dale Curtis5c32c722011-05-04 19:24:23 -0700269
270 # Run update_engine using the specified URL.
271 try:
272 autoupdate_cmd = '%s --update --omaha_url=%s 2>&1' % (
273 UPDATER_BIN, self.update_url)
274 self._run(autoupdate_cmd, timeout=900)
275 except error.AutoservRunError:
Chris Sosa77556d82012-04-05 15:23:14 -0700276 update_error = RootFSUpdateError('update-engine failed on %s' %
277 self.host.hostname)
278 self._update_error_queue.put(update_error)
279 raise update_error
Dale Curtis5c32c722011-05-04 19:24:23 -0700280
Chris Sosac1932172013-10-16 13:28:53 -0700281 try:
282 self._verify_update_completed()
283 except RootFSUpdateError as e:
284 self._update_error_queue.put(e)
285 raise
Dale Curtis5c32c722011-05-04 19:24:23 -0700286
287
Chris Sosa72312602013-04-16 15:01:56 -0700288 def update_stateful(self, clobber=True):
289 """Updates the stateful partition.
290
291 @param clobber: If True, a clean stateful installation.
292 """
Chris Sosa77556d82012-04-05 15:23:14 -0700293 logging.info('Updating stateful partition...')
joychen03eaad92013-06-26 09:55:21 -0700294 statefuldev_url = self.update_url.replace('update',
295 'static')
Chris Sosaa3ac2152012-05-23 22:23:13 -0700296
Dale Curtis5c32c722011-05-04 19:24:23 -0700297 # Attempt stateful partition update; this must succeed so that the newly
298 # installed host is testable after update.
Chris Sosa72312602013-04-16 15:01:56 -0700299 statefuldev_cmd = [self.get_stateful_update_script(), statefuldev_url]
300 if clobber:
301 statefuldev_cmd.append('--stateful_change=clean')
302
303 statefuldev_cmd.append('2>&1')
Dale Curtis5c32c722011-05-04 19:24:23 -0700304 try:
305 self._run(' '.join(statefuldev_cmd), timeout=600)
306 except error.AutoservRunError:
Chris Sosa77556d82012-04-05 15:23:14 -0700307 update_error = StatefulUpdateError('stateful_update failed on %s' %
308 self.host.hostname)
309 self._update_error_queue.put(update_error)
310 raise update_error
Dale Curtis5c32c722011-05-04 19:24:23 -0700311
312
Dan Shi0f466e82013-02-22 15:44:58 -0800313 def run_update(self, force_update, update_root=True):
314 """Update the DUT with image of specific version.
Chris Sosaa3ac2152012-05-23 22:23:13 -0700315
Dan Shi0f466e82013-02-22 15:44:58 -0800316 @param force_update: True to update DUT even if it's running the same
317 version already.
318 @param update_root: True to force a kernel update. If it's False and
319 force_update is True, stateful update will be used to clean up
320 the DUT.
321
322 """
323 booted_version = self.get_build_id()
324 if (self.check_version() and not force_update):
Dale Curtisa94c19c2011-05-02 15:05:17 -0700325 logging.info('System is already up to date. Skipping update.')
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200326 return False
327
Chris Sosaa3ac2152012-05-23 22:23:13 -0700328 if self.update_version:
329 logging.info('Updating from version %s to %s.',
330 booted_version, self.update_version)
Dale Curtis53d55862011-05-16 12:17:59 -0700331
Dale Curtis5c32c722011-05-04 19:24:23 -0700332 # Check that Dev Server is accepting connections (from autoserv's host).
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200333 # If we can't talk to it, the machine host probably can't either.
334 auserver_host = urlparse.urlparse(self.update_url)[1]
335 try:
336 httplib.HTTPConnection(auserver_host).connect()
Dale Curtis5c32c722011-05-04 19:24:23 -0700337 except IOError:
338 raise ChromiumOSError(
339 'Update server at %s not available' % auserver_host)
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200340
Chris Sosaa3ac2152012-05-23 22:23:13 -0700341 logging.info('Installing from %s to %s', self.update_url,
Chris Sosa77556d82012-04-05 15:23:14 -0700342 self.host.hostname)
343
Chris Sosa5e4246b2012-05-22 18:05:22 -0700344 # Reset update state.
Chris Sosa77556d82012-04-05 15:23:14 -0700345 self.reset_update_engine()
Chris Sosa5e4246b2012-05-22 18:05:22 -0700346 self.reset_stateful_partition()
Sean Oc053dfe2010-08-23 18:22:26 +0200347
Dale Curtis1e973182011-07-12 18:21:36 -0700348 try:
Chris Sosa77556d82012-04-05 15:23:14 -0700349 updaters = [
Chris Sosa2f1ae9f2013-08-13 10:00:15 -0700350 multiprocessing.process.Process(target=self.update_rootfs),
Chris Sosa72312602013-04-16 15:01:56 -0700351 multiprocessing.process.Process(target=self.update_stateful)
Chris Sosa77556d82012-04-05 15:23:14 -0700352 ]
Dan Shi0f466e82013-02-22 15:44:58 -0800353 if not update_root:
354 logging.info('Root update is skipped.')
355 updaters = updaters[1:]
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200356
Chris Sosa77556d82012-04-05 15:23:14 -0700357 # Run the updaters in parallel.
358 for updater in updaters: updater.start()
359 for updater in updaters: updater.join()
360
361 # Re-raise the first error that occurred.
362 if not self._update_error_queue.empty():
363 update_error = self._update_error_queue.get()
364 self.revert_boot_partition()
Chris Sosa5e4246b2012-05-22 18:05:22 -0700365 self.reset_stateful_partition()
Chris Sosa77556d82012-04-05 15:23:14 -0700366 raise update_error
Sean Oc053dfe2010-08-23 18:22:26 +0200367
Dale Curtis1e973182011-07-12 18:21:36 -0700368 logging.info('Update complete.')
369 return True
370 except:
371 # Collect update engine logs in the event of failure.
372 if self.host.job:
373 logging.info('Collecting update engine logs...')
374 self.host.get_file(
375 UPDATER_LOGS, self.host.job.sysinfo.sysinfodir,
376 preserve_perm=False)
377 raise
Dan Shi10e992b2013-08-30 11:02:59 -0700378 finally:
379 self.host.show_update_engine_log()
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200380
381
Dale Curtisa94c19c2011-05-02 15:05:17 -0700382 def check_version(self):
Dan Shi0f466e82013-02-22 15:44:58 -0800383 """Check the image running in DUT has the desired version.
384
385 @returns: True if the DUT's image version matches the version that
386 the autoupdater tries to update to.
387
388 """
Dale Curtisf57a25f2011-05-24 14:40:55 -0700389 booted_version = self.get_build_id()
Dan Shib95bb862013-03-22 16:29:28 -0700390 return (self.update_version and
391 self.update_version.endswith(booted_version))
392
393
394 def check_version_to_confirm_install(self):
395 """Check image running in DUT has the desired version to be installed.
396
397 The method should not be used to check if DUT needs to have a full
398 reimage. Only use it to confirm a image is installed.
399
Dan Shi190c7802013-04-04 13:05:30 -0700400 The method is designed to verify version for following 4 scenarios with
401 samples of version to update to and expected booted version:
402 1. trybot paladin build.
403 update version: trybot-lumpy-paladin/R27-3837.0.0-b123
404 booted version: 3837.0.2013_03_21_1340
405
406 2. trybot release build.
407 update version: trybot-lumpy-release/R27-3837.0.0-b456
408 booted version: 3837.0.0
409
410 3. buildbot official release build.
411 update version: lumpy-release/R27-3837.0.0
412 booted version: 3837.0.0
413
414 4. non-official paladin rc build.
415 update version: lumpy-paladin/R27-3878.0.0-rc7
416 booted version: 3837.0.0-rc7
Dan Shib95bb862013-03-22 16:29:28 -0700417
Dan Shi7f795512013-04-12 10:08:17 -0700418 5. chrome-perf build.
419 update version: lumpy-chrome-perf/R28-3837.0.0-b2996
420 booted version: 3837.0.0
421
Dan Shi73aa2902013-05-03 11:22:11 -0700422 6. pgo-generate build.
423 update version: lumpy-release-pgo-generate/R28-3837.0.0-b2996
424 booted version: 3837.0.0-pgo-generate
425
Dan Shib95bb862013-03-22 16:29:28 -0700426 When we are checking if a DUT needs to do a full install, we should NOT
427 use this method to check if the DUT is running the same version, since
Dan Shi190c7802013-04-04 13:05:30 -0700428 it may return false positive for a DUT running trybot paladin build to
429 be updated to another trybot paladin build.
Dan Shib95bb862013-03-22 16:29:28 -0700430
Dan Shi190c7802013-04-04 13:05:30 -0700431 TODO: This logic has a bug if a trybot paladin build failed to be
432 installed in a DUT running an older trybot paladin build with same
433 platform number, but different build number (-b###). So to conclusively
434 determine if a tryjob paladin build is imaged successfully, we may need
435 to find out the date string from update url.
Dan Shib95bb862013-03-22 16:29:28 -0700436
437 @returns: True if the DUT's image version (without the date string if
438 the image is a trybot build), matches the version that the
439 autoupdater is trying to update to.
440
441 """
J. Richard Barnetteec1de422013-06-26 15:44:07 -0700442 # In the local_devserver case, we can't know the expected
443 # build, so just pass.
444 if not self.update_version:
445 return True
446
Dan Shib95bb862013-03-22 16:29:28 -0700447 # Always try the default check_version method first, this prevents
448 # any backward compatibility issue.
449 if self.check_version():
450 return True
451
Dan Shi190c7802013-04-04 13:05:30 -0700452 # Remove R#- and -b# at the end of build version
453 stripped_version = re.sub(r'(R\d+-|-b\d+)', '', self.update_version)
454
Dan Shib95bb862013-03-22 16:29:28 -0700455 booted_version = self.get_build_id()
Dan Shi190c7802013-04-04 13:05:30 -0700456
Dan Shi7f795512013-04-12 10:08:17 -0700457 is_trybot_paladin_build = re.match(r'.+trybot-.+-paladin',
458 self.update_url)
Dan Shi190c7802013-04-04 13:05:30 -0700459
Dan Shi7f795512013-04-12 10:08:17 -0700460 # Replace date string with 0 in booted_version
461 booted_version_no_date = re.sub(r'\d{4}_\d{2}_\d{2}_\d+', '0',
462 booted_version)
463 has_date_string = booted_version != booted_version_no_date
464
Dan Shi73aa2902013-05-03 11:22:11 -0700465 is_pgo_generate_build = re.match(r'.+-pgo-generate',
466 self.update_url)
467
468 # Remove |-pgo-generate| in booted_version
469 booted_version_no_pgo = booted_version.replace('-pgo-generate', '')
470 has_pgo_generate = booted_version != booted_version_no_pgo
471
Dan Shi7f795512013-04-12 10:08:17 -0700472 if is_trybot_paladin_build:
473 if not has_date_string:
474 logging.error('A trybot paladin build is expected. Version ' +
475 '"%s" is not a paladin build.', booted_version)
Dan Shi190c7802013-04-04 13:05:30 -0700476 return False
477 return stripped_version == booted_version_no_date
Dan Shi73aa2902013-05-03 11:22:11 -0700478 elif is_pgo_generate_build:
479 if not has_pgo_generate:
480 logging.error('A pgo-generate build is expected. Version ' +
481 '"%s" is not a pgo-generate build.',
482 booted_version)
483 return False
484 return stripped_version == booted_version_no_pgo
Dan Shi7f795512013-04-12 10:08:17 -0700485 else:
486 if has_date_string:
487 logging.error('Unexpected date found in a non trybot paladin' +
488 ' build.')
489 return False
490 # Versioned build, i.e., rc or release build.
491 return stripped_version == booted_version
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200492
Sean Oc053dfe2010-08-23 18:22:26 +0200493
Sean O'Connor5346e4e2010-08-12 18:49:24 +0200494 def get_build_id(self):
Dale Curtis793f9122011-02-04 15:00:52 -0800495 """Pulls the CHROMEOS_RELEASE_VERSION string from /etc/lsb-release."""
496 return self._run('grep CHROMEOS_RELEASE_VERSION'
497 ' /etc/lsb-release').stdout.split('=')[1].strip()
Chris Sosa65425082013-10-16 13:26:22 -0700498
499
500 def verify_boot_expectations(self, expected_kernel_state, rollback_message):
501 """Verifies that we fully booted given expected kernel state.
502
503 This method both verifies that we booted using the correct kernel
504 state and that the OS has marked the kernel as good.
505
506 @param expected_kernel_state: kernel state that we are verifying with
507 i.e. I expect to be booted onto partition 4 etc. See output of
508 get_kernel_state.
509 @param rollback_message: string to raise as a ChromiumOSError
510 if we booted with the wrong partition.
511
512 @raises ChromiumOSError: If we didn't.
513 """
514 # Figure out the newly active kernel.
515 active_kernel_state = self.get_kernel_state()[0]
516
517 # Check for rollback due to a bad build.
518 if (expected_kernel_state and
519 active_kernel_state != expected_kernel_state):
Don Garrett56b1cc82013-12-06 17:49:20 -0800520
521 # Kernel crash reports should be wiped between test runs, but
522 # may persist from earlier parts of the test, or from problems
523 # with provisioning.
524 #
525 # Kernel crash reports will NOT be present if the crash happened
526 # before encrypted stateful is mounted.
527 #
528 # TODO(dgarrett): Integrate with server/crashcollect.py at some
529 # point.
530 kernel_crashes = glob.glob('/var/spool/crash/kernel.*.kcrash')
531 if kernel_crashes:
532 rollback_message += ': kernel_crash'
533 logging.debug('Found %d kernel crash reports:',
534 len(kernel_crashes))
535 # The crash names contain timestamps that may be useful:
536 # kernel.20131207.005945.0.kcrash
537 for crash in kernel_crashes:
538 logging.debug(' %s', os.path.basename(crash))
539
Chris Sosa65425082013-10-16 13:26:22 -0700540 # Print out some information to make it easier to debug
541 # the rollback.
542 logging.debug('Dumping partition table.')
543 self._run('cgpt show $(rootdev -s -d)')
544 logging.debug('Dumping crossystem for firmware debugging.')
545 self._run('crossystem --all')
546 raise ChromiumOSError(rollback_message)
547
548 # Make sure chromeos-setgoodkernel runs.
549 try:
550 utils.poll_for_condition(
551 lambda: (self.get_kernel_tries(active_kernel_state) == 0
552 and self.get_kernel_success(active_kernel_state)),
553 exception=ChromiumOSError(),
554 timeout=self.KERNEL_UPDATE_TIMEOUT, sleep_interval=5)
555 except ChromiumOSError:
556 services_status = self._run('status system-services').stdout
557 if services_status != 'system-services start/running\n':
558 event = ('Chrome failed to reach login screen')
559 else:
560 event = ('update-engine failed to call '
561 'chromeos-setgoodkernel')
562 raise ChromiumOSError(
563 'After update and reboot, %s '
564 'within %d seconds' % (event,
565 self.KERNEL_UPDATE_TIMEOUT))