blob: 8d579ad70d47688bea8a4612ed9ad7559f27814d [file] [log] [blame]
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08001# -*- coding: utf-8 -*-
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08002# Copyright 2017 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""ChromeOS utility.
6
7Terminology used in this module.
8 short_version: ChromeOS version number without milestone, like "9876.0.0".
9 full_version: ChromeOS version number with milestone, like "R62-9876.0.0".
Zheng-Jie Chang127c3302019-09-10 17:17:04 +080010 snapshot_version: ChromeOS version number with milestone and snapshot id,
11 like "R62-9876.0.0-12345".
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080012 version: if not specified, it could be in short or full format.
13"""
14
15from __future__ import print_function
Kuang-che Wub9705bd2018-06-28 17:59:18 +080016import ast
Kuang-che Wu72b5a572019-10-29 20:37:57 +080017import calendar
Zheng-Jie Chang127c3302019-09-10 17:17:04 +080018import datetime
Kuang-che Wu721e8902021-03-19 12:18:53 +080019import enum
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080020import errno
Kuang-che Wu9d14c162020-11-03 19:35:18 +080021import glob
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080022import json
23import logging
24import os
25import re
Kuang-che Wuad5ac7b2020-02-20 19:02:52 +080026import shutil
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080027import subprocess
Kuang-che Wud558a042020-06-06 02:11:00 +080028import sys
Kuang-che Wu721e8902021-03-19 12:18:53 +080029import tempfile
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080030import time
Kuang-che Wu721e8902021-03-19 12:18:53 +080031import urllib.parse
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080032
Zheng-Jie Chang4fabff62019-12-08 21:54:35 +080033from google.protobuf import json_format
34
Zheng-Jie Chang2b6d1472019-11-13 12:40:17 +080035from bisect_kit import buildbucket_util
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080036from bisect_kit import cli
Kuang-che Wue4bae0b2018-07-19 12:10:14 +080037from bisect_kit import codechange
Kuang-che Wu721e8902021-03-19 12:18:53 +080038from bisect_kit import common
Kuang-che Wu3eb6b502018-06-06 16:15:18 +080039from bisect_kit import cr_util
Kuang-che Wue121fae2018-11-09 16:18:39 +080040from bisect_kit import errors
Kuang-che Wubfc4a642018-04-19 11:54:08 +080041from bisect_kit import git_util
Kuang-che Wufb553102018-10-02 18:14:29 +080042from bisect_kit import locking
Kuang-che Wubfc4a642018-04-19 11:54:08 +080043from bisect_kit import repo_util
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080044from bisect_kit import util
45
46logger = logging.getLogger(__name__)
47
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080048re_chromeos_full_version = r'^R\d+-\d+\.\d+\.\d+$'
Kuang-che Wuacb6efd2018-04-25 18:52:58 +080049re_chromeos_short_version = r'^\d+\.\d+\.\d+$'
Zheng-Jie Chang127c3302019-09-10 17:17:04 +080050re_chromeos_snapshot_version = r'^R\d+-\d+\.\d+\.\d+-\d+$'
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080051
Kuang-che Wu721e8902021-03-19 12:18:53 +080052gs_archive_base = 'gs://chromeos-image-archive/'
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080053gs_archive_path = 'gs://chromeos-image-archive/{board}-release'
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080054
55# Assume gsutil is in PATH.
56gsutil_bin = 'gsutil'
Zheng-Jie Changb8697042019-10-29 16:03:26 +080057
58# Since snapshots with version >= 12618.0.0 have android and chrome version
59# info.
60snapshot_cutover_version = '12618.0.0'
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080061
Zheng-Jie Changdbe1f8e2021-01-26 12:33:32 +080062# http://crbug.com/1170601, small snapshot ids should be ignored
63# 21000 is R80-12617.0.0
64snapshot_cutover_id = 21000
65
Zheng-Jie Chang61a645a2020-04-29 10:12:13 +080066# current earliest buildbucket buildable versions
67# picked from https://crrev.com/c/2072618
68buildbucket_cutover_versions = [
69 '12931.0.0',
70 '12871.26.0', # R81
71 '12871.24.2', # stabilize-12871.24.B
72 '12812.10.0', # factory-excelsior-12812.B
73 '12768.14.0', # firmware-servo-12768.B
74 '12739.85.0', # R80
75 '12739.67.1', # stabilize-excelsior-12739.67.B
76 '12692.36.0', # factory-hatch-12692.B
77 '12672.104.0', # firmware-hatch-12672.B
78 '12607.110.0', # R79
79 '12607.83.2', # stabilize-quickfix-12607.83.B
80 '12587.59.0', # factory-kukui-12587.B
81 '12573.78.0', # firmware-kukui-12573.B
82 '12499.96.0', # R78
83 '12422.33.0', # firmware-mistral-12422.B
84 '12371.190.0', # R77
85 '12361.38.0', # factory-mistral-12361.B
86 '12200.65.0', # firmware-sarien-12200.B
87 '12105.128.0', # R75
88 '12033.82.0', # factory-sarien-12033.B
89]
90
Kuang-che Wub9705bd2018-06-28 17:59:18 +080091chromeos_root_inside_chroot = '/mnt/host/source'
92# relative to chromeos_root
Kuang-che Wu721e8902021-03-19 12:18:53 +080093in_tree_autotest_dir = 'src/third_party/autotest/files'
Kuang-che Wu7f82c6f2019-08-12 14:29:28 +080094prebuilt_autotest_dir = 'tmp/autotest-prebuilt'
Kuang-che Wu5963ebf2020-10-21 09:01:04 +080095prebuilt_tast_dir = 'tmp/tast-prebuilt'
Kuang-che Wu721e8902021-03-19 12:18:53 +080096# Relative to chromeos root. Images are build_images_dir/$board/$image_name.
97build_images_dir = 'src/build/images'
98cached_images_dir = 'tmp/images'
Kuang-che Wu28980b22019-07-31 19:51:45 +080099test_image_filename = 'chromiumos_test_image.bin'
Kuang-che Wu721e8902021-03-19 12:18:53 +0800100sample_partition_filename = 'full_dev_part_KERN.bin.gz'
Kuang-che Wub9705bd2018-06-28 17:59:18 +0800101
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800102VERSION_KEY_CROS_SHORT_VERSION = 'cros_short_version'
103VERSION_KEY_CROS_FULL_VERSION = 'cros_full_version'
104VERSION_KEY_MILESTONE = 'milestone'
105VERSION_KEY_CR_VERSION = 'cr_version'
Kuang-che Wu708310b2018-03-28 17:24:34 +0800106VERSION_KEY_ANDROID_BUILD_ID = 'android_build_id'
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800107VERSION_KEY_ANDROID_BRANCH = 'android_branch'
Zheng-Jie Chang5f9ae4e2020-02-07 14:26:06 +0800108CROSLAND_URL_TEMPLATE = 'https://crosland.corp.google.com/log/%s..%s'
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800109
Kuang-che Wu721e8902021-03-19 12:18:53 +0800110autotest_shadow_config = """
111[CROS]
112enable_ssh_tunnel_for_servo: True
113enable_ssh_tunnel_for_chameleon: True
114enable_ssh_connection_for_devserver: True
115enable_ssh_tunnel_for_moblab: True
116"""
117
118
119class ImageType(enum.Enum):
120 """Chrome OS image type
121
122 It describes the image format, not image location.
123 """
124 # Full disk image like chromiumos_test_image.bin and
125 # chromiumos_test_image.tar.xz.
126 # Supported by 'cros flash'.
127 DISK_IMAGE = enum.auto()
128 # Contains files like full_dev_part_KERN.bin.gz.
129 # Supported by quick-provision and newer 'cros flash'.
130 PARTITION_IMAGE = enum.auto()
131 # Contains files like image.zip. We need to unzip first.
132 ZIP_FILE = enum.auto()
133
134
135class ImageInfo(dict):
136 """Image info (dict: image type -> path).
137
138 For a given Chrome OS version, there are several image formats available.
139 This class describes a collection of images for a certain Chrome OS version.
140 cros_flash() or quick_provision() can resolve a compatible image from this
141 object. `image type` is an ImageType enum. `path` could be a path on the
142 local disk or a remote URI.
143 """
144
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800145
Kuang-che Wu9890ce82018-07-07 15:14:10 +0800146class NeedRecreateChrootException(Exception):
147 """Failed to build ChromeOS because of chroot mismatch or corruption"""
148
149
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800150def is_cros_short_version(s):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800151 """Determines if `s` is chromeos short version.
152
153 This function doesn't accept version number of local build.
154 """
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800155 return bool(re.match(re_chromeos_short_version, s))
156
157
158def is_cros_full_version(s):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800159 """Determines if `s` is chromeos full version.
160
161 This function doesn't accept version number of local build.
162 """
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800163 return bool(re.match(re_chromeos_full_version, s))
164
165
166def is_cros_version(s):
167 """Determines if `s` is chromeos version (either short or full)"""
168 return is_cros_short_version(s) or is_cros_full_version(s)
169
170
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800171def is_cros_snapshot_version(s):
172 """Determines if `s` is chromeos snapshot version"""
173 return bool(re.match(re_chromeos_snapshot_version, s))
174
175
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800176def is_cros_version_lesseq(ver1, ver2):
177 """Determines if ver1 is less or equal to ver2.
178
179 Args:
180 ver1: a Chrome OS version in short, full, or snapshot format.
181 ver2: a Chrome OS version in short, full, or snapshot format.
182
183 Returns:
Zheng-Jie Chang61a645a2020-04-29 10:12:13 +0800184 True if ver1 is less or equal to ver2.
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800185 """
186 assert is_cros_version(ver1) or is_cros_snapshot_version(ver1)
187 assert is_cros_version(ver2) or is_cros_snapshot_version(ver2)
188
Kuang-che Wu430c5282021-01-27 21:10:25 +0800189 # Compare milestone if available.
190 m1 = re.match(r'R(\d+)', ver1)
191 m2 = re.match(r'R(\d+)', ver2)
192 if m1 and m2 and int(m1.group(1)) > int(m2.group(1)):
193 return False
194
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800195 ver1 = [int(x) for x in re.split(r'[.-]', ver1) if not x.startswith('R')]
196 ver2 = [int(x) for x in re.split(r'[.-]', ver2) if not x.startswith('R')]
197 return ver1 <= ver2
198
199
Kuang-che Wu430c5282021-01-27 21:10:25 +0800200def is_ancestor_version(ver1, ver2):
201 """Determines `ver1` version is ancestor of `ver2` version.
202
203 Returns:
204 True only if `ver1` is the ancestor of `ver2`. One version is not considered
205 as ancestor of itself.
206 """
207 assert is_cros_version(ver1) or is_cros_snapshot_version(ver1)
208 assert is_cros_version(ver2) or is_cros_snapshot_version(ver2)
209
210 if is_cros_version_lesseq(ver2, ver1): # pylint: disable=arguments-out-of-order
211 return False
212 if not is_cros_version_lesseq(ver1, ver2):
213 return False
214
215 if not util.is_direct_relative_version(
216 version_to_short(ver1), version_to_short(ver2)):
217 return False
218
219 # Compare snapshot id if available.
220 if is_cros_snapshot_version(ver1) and is_cros_snapshot_version(ver2):
221 _, short_1, snapshot_1 = snapshot_version_split(ver1)
222 _, short_2, snapshot_2 = snapshot_version_split(ver2)
223 if short_1 == short_2 and snapshot_1 >= snapshot_2:
224 return False
225
226 return True
227
228
Zheng-Jie Chang61a645a2020-04-29 10:12:13 +0800229def is_buildbucket_buildable(version):
230 """Determines if a version is buildable on buildbucket."""
231 short_version = version_to_short(version)
232 # If given version is child of any cutover, then it's buildable
233 return any([
234 util.is_direct_relative_version(x, short_version) and
235 is_cros_version_lesseq(x, version) for x in buildbucket_cutover_versions
236 ])
237
238
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800239def make_cros_full_version(milestone, short_version):
240 """Makes full_version from milestone and short_version"""
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800241 assert milestone
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800242 return 'R%s-%s' % (milestone, short_version)
243
244
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800245def make_cros_snapshot_version(milestone, short_version, snapshot_id):
246 """Makes snapshot version from milestone, short_version and snapshot id"""
247 return 'R%s-%s-%s' % (milestone, short_version, snapshot_id)
248
249
250def version_split(version):
251 """Splits full_version or snapshot_version into milestone and short_version"""
252 assert is_cros_full_version(version) or is_cros_snapshot_version(version)
253 if is_cros_snapshot_version(version):
254 return snapshot_version_split(version)[0:2]
255 milestone, short_version = version.split('-')
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800256 return milestone[1:], short_version
257
258
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800259def snapshot_version_split(snapshot_version):
260 """Splits snapshot_version into milestone, short_version and snapshot_id"""
261 assert is_cros_snapshot_version(snapshot_version)
Kuang-che Wu430c5282021-01-27 21:10:25 +0800262 milestone, short_version, snapshot_id = snapshot_version.split('-')
263 return milestone[1:], short_version, snapshot_id
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800264
265
Zheng-Jie Changb8697042019-10-29 16:03:26 +0800266def query_snapshot_buildbucket_id(board, snapshot_version):
267 """Query buildbucket id of a snapshot"""
268 assert is_cros_snapshot_version(snapshot_version)
Zheng-Jie Chang54020832020-04-21 15:52:48 +0800269 path = ('gs://chromeos-image-archive/{board}-snapshot'
Zheng-Jie Changb8697042019-10-29 16:03:26 +0800270 '/{snapshot_version}-*/image.zip')
271 output = gsutil_ls(
272 '-d',
273 path.format(board=board, snapshot_version=snapshot_version),
274 ignore_errors=True)
275 for line in output:
Zheng-Jie Chang54020832020-04-21 15:52:48 +0800276 m = re.match(r'.*-snapshot/R\d+-\d+\.\d+\.\d+-\d+-(.+)/image\.zip', line)
Zheng-Jie Changb8697042019-10-29 16:03:26 +0800277 if m:
278 return m.group(1)
279 return None
280
281
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800282def argtype_cros_version(s):
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800283 if (not is_cros_version(s)) and (not is_cros_snapshot_version(s)):
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800284 msg = 'invalid cros version'
Kuang-che Wuce2f3be2019-10-28 19:44:54 +0800285 raise cli.ArgTypeError(msg, '9876.0.0, R62-9876.0.0 or R77-12369.0.0-11681')
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800286 return s
287
288
289def query_dut_lsb_release(host):
290 """Query /etc/lsb-release of given DUT
291
292 Args:
293 host: the DUT address
294
295 Returns:
Kuang-che Wu3eb6b502018-06-06 16:15:18 +0800296 dict for keys and values of /etc/lsb-release.
297
298 Raises:
Kuang-che Wu44278142019-03-04 11:33:57 +0800299 errors.SshConnectionError: cannot connect to host
300 errors.ExternalError: lsb-release file doesn't exist
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800301 """
302 try:
Kuang-che Wu342d3e02021-02-19 15:40:57 +0800303 output = util.ssh_cmd(host, 'cat', '/etc/lsb-release', allow_retry=True)
Kuang-che Wu6d91b8c2020-11-24 20:14:35 +0800304 except subprocess.CalledProcessError as e:
305 raise errors.ExternalError(
306 'unable to read /etc/lsb-release; not a DUT') from e
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800307 return dict(re.findall(r'^(\w+)=(.*)$', output, re.M))
308
309
Kuang-che Wuf134ebf2020-08-18 21:06:47 +0800310def query_dut_os_release(host):
311 """Query /etc/os-release of given DUT
312
313 Args:
314 host: the DUT address
315
316 Returns:
317 dict for keys and values of /etc/os-release.
318
319 Raises:
320 errors.SshConnectionError: cannot connect to host
321 errors.ExternalError: lsb-release file doesn't exist
322 """
323 try:
Kuang-che Wu342d3e02021-02-19 15:40:57 +0800324 output = util.ssh_cmd(host, 'cat', '/etc/os-release', allow_retry=True)
Kuang-che Wu6d91b8c2020-11-24 20:14:35 +0800325 except subprocess.CalledProcessError as e:
326 raise errors.ExternalError(
327 'unable to read /etc/os-release; not a DUT') from e
Kuang-che Wuf134ebf2020-08-18 21:06:47 +0800328 return dict(re.findall(r'^(\w+)=(.*)$', output, re.M))
329
330
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800331def is_dut(host):
Kuang-che Wubfc4a642018-04-19 11:54:08 +0800332 """Determines whether a host is a chromeos device.
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800333
334 Args:
335 host: the DUT address
336
337 Returns:
338 True if the host is a chromeos device.
339 """
Kuang-che Wu44278142019-03-04 11:33:57 +0800340 try:
Kuang-che Wuf134ebf2020-08-18 21:06:47 +0800341 return query_dut_os_release(host).get('ID') in [
342 'chromiumos',
343 'chromeos',
Kuang-che Wu44278142019-03-04 11:33:57 +0800344 ]
345 except (errors.ExternalError, errors.SshConnectionError):
346 return False
347
348
349def is_good_dut(host):
350 if not is_dut(host):
351 return False
352
353 # Sometimes python is broken after 'cros flash'.
354 try:
Kuang-che Wu342d3e02021-02-19 15:40:57 +0800355 util.ssh_cmd(host, 'python', '-c', '1', allow_retry=True)
Kuang-che Wu44278142019-03-04 11:33:57 +0800356 return True
357 except (subprocess.CalledProcessError, errors.SshConnectionError):
358 return False
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800359
360
361def query_dut_board(host):
362 """Query board name of a given DUT"""
363 return query_dut_lsb_release(host).get('CHROMEOS_RELEASE_BOARD')
364
365
366def query_dut_short_version(host):
Kuang-che Wuacb6efd2018-04-25 18:52:58 +0800367 """Query short version of a given DUT.
368
369 This function may return version of local build, which
370 is_cros_short_version() is false.
371 """
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800372 return query_dut_lsb_release(host).get('CHROMEOS_RELEASE_VERSION')
373
374
Zheng-Jie Chang26084542020-04-29 06:34:33 +0800375def query_dut_prebuilt_version(host):
376 """Return a snapshot version or short version of a given DUT.
377
378 Args:
379 host: dut host
380
381 Returns:
382 Snapshot version or short version.
383 """
384 lsb_release = query_dut_lsb_release(host)
385 release_version = lsb_release.get('CHROMEOS_RELEASE_VERSION')
Kuang-che Wu25fec6f2021-01-28 12:40:43 +0800386 builder_path = lsb_release.get('CHROMEOS_RELEASE_BUILDER_PATH', '')
Zheng-Jie Chang26084542020-04-29 06:34:33 +0800387 match = re.match(r'\S+-(?:snapshot|postsubmit)/(R\d+-\d+\.\d+\.\d+-\d+)-\d+',
388 builder_path)
389 if match:
390 return match.group(1)
391 return release_version
392
393
Zheng-Jie Chang181be6f2020-03-17 16:16:08 +0800394def query_dut_is_by_official_builder(host):
Kuang-che Wuc092bd52021-01-05 14:25:56 +0800395 """Query if given DUT is build by official builder"""
396 build_type = query_dut_lsb_release(host).get('CHROMEOS_RELEASE_BUILD_TYPE',
397 '')
398 build_type = build_type.split(' - ')[0]
399 assert build_type in ('Official Build', 'Continuous Builder',
400 'Developer Build',
401 'Test Build'), 'unknown build type (%s)' % build_type
402 return build_type in ['Official Build', 'Continuous Builder']
Zheng-Jie Chang181be6f2020-03-17 16:16:08 +0800403
404
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800405def query_dut_boot_id(host, connect_timeout=None):
Kuang-che Wubfc4a642018-04-19 11:54:08 +0800406 """Query boot id.
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800407
408 Args:
409 host: DUT address
410 connect_timeout: connection timeout
411
412 Returns:
413 boot uuid
414 """
Kuang-che Wu44278142019-03-04 11:33:57 +0800415 return util.ssh_cmd(
416 host,
417 'cat',
418 '/proc/sys/kernel/random/boot_id',
419 connect_timeout=connect_timeout).strip()
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800420
421
Kuang-che Wu2ac9a922020-09-03 16:50:12 +0800422def reboot(host, force_reboot_callback=None):
423 """Reboot a DUT and verify.
424
425 Args:
426 host: DUT address
427 force_reboot_callback: powerful reboot hook (via servo). This will be
428 invoked if normal reboot failed.
429 """
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800430 logger.debug('reboot %s', host)
Kuang-che Wu2534bab2020-10-23 17:37:16 +0800431 boot_id = None
Kuang-che Wu44278142019-03-04 11:33:57 +0800432 try:
Kuang-che Wu2534bab2020-10-23 17:37:16 +0800433 boot_id = query_dut_boot_id(host)
Kuang-che Wu2ac9a922020-09-03 16:50:12 +0800434
Kuang-che Wu2534bab2020-10-23 17:37:16 +0800435 try:
436 util.ssh_cmd(host, 'reboot')
437 except errors.SshConnectionError:
438 # Depends on timing, ssh may return failure due to broken pipe, which is
439 # working as intended. Ignore such kind of errors.
440 pass
441
Kuang-che Wu2ac9a922020-09-03 16:50:12 +0800442 wait_reboot_done(host, boot_id)
443 except (errors.SshConnectionError, errors.ExternalError):
444 if force_reboot_callback and force_reboot_callback(host):
445 wait_reboot_done(host, boot_id)
446 return
447 raise
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800448
Kuang-che Wu708310b2018-03-28 17:24:34 +0800449
450def wait_reboot_done(host, boot_id):
Kuang-che Wu4fe945b2018-03-31 16:46:38 +0800451 # For dev-mode test image, the reboot time is roughly at least 16 seconds
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800452 # (dev screen short delay) or more (long delay).
453 time.sleep(15)
454 for _ in range(100):
455 try:
456 # During boot, DUT does not response and thus ssh may hang a while. So
457 # set a connect timeout. 3 seconds are enough and 2 are not. It's okay to
458 # set tight limit because it's inside retry loop.
459 assert boot_id != query_dut_boot_id(host, connect_timeout=3)
460 return
Kuang-che Wu5f662e82019-03-05 11:49:56 +0800461 except errors.SshConnectionError:
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800462 logger.debug('reboot not ready? sleep wait 1 sec')
463 time.sleep(1)
464
Kuang-che Wue121fae2018-11-09 16:18:39 +0800465 raise errors.ExternalError('reboot failed?')
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800466
467
468def gsutil(*args, **kwargs):
Kuang-che Wubfc4a642018-04-19 11:54:08 +0800469 """gsutil command line wrapper.
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800470
471 Args:
472 args: command line arguments passed to gsutil
473 kwargs:
474 ignore_errors: if true, return '' for failures, for example 'gsutil ls'
475 but the path not found.
476
477 Returns:
478 stdout of gsutil
479
480 Raises:
Zheng-Jie Changb8697042019-10-29 16:03:26 +0800481 errors.ExternalError: gsutil failed to run
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800482 subprocess.CalledProcessError: command failed
483 """
484 stderr_lines = []
485 try:
486 return util.check_output(
487 gsutil_bin, *args, stderr_callback=stderr_lines.append)
488 except subprocess.CalledProcessError as e:
489 stderr = ''.join(stderr_lines)
490 if re.search(r'ServiceException:.* does not have .*access', stderr):
Kuang-che Wue121fae2018-11-09 16:18:39 +0800491 raise errors.ExternalError(
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800492 'gsutil failed due to permission. ' +
493 'Run "%s config" and follow its instruction. ' % gsutil_bin +
494 'Fill any string if it asks for project-id')
495 if kwargs.get('ignore_errors'):
496 return ''
497 raise
498 except OSError as e:
499 if e.errno == errno.ENOENT:
Kuang-che Wue121fae2018-11-09 16:18:39 +0800500 raise errors.ExternalError(
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800501 'Unable to run %s. gsutil is not installed or not in PATH?' %
502 gsutil_bin)
503 raise
504
505
506def gsutil_ls(*args, **kwargs):
Kuang-che Wubfc4a642018-04-19 11:54:08 +0800507 """gsutil ls.
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800508
509 Args:
510 args: arguments passed to 'gsutil ls'
511 kwargs: extra parameters, where
Kuang-che Wu4fe945b2018-03-31 16:46:38 +0800512 ignore_errors: if true, return empty list instead of raising
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800513 exception, ex. path not found.
514
515 Returns:
516 list of 'gsutil ls' result. One element for one line of gsutil output.
517
518 Raises:
519 subprocess.CalledProcessError: gsutil failed, usually means path not found
520 """
521 return gsutil('ls', *args, **kwargs).splitlines()
522
523
Kuang-che Wu876a5382020-10-27 14:24:58 +0800524def gsutil_stat_creation_time(*args, **kwargs):
525 """Returns the creation time of a file or multiple files.
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800526
527 Args:
528 args: arguments passed to 'gsutil stat'.
529 kwargs: extra parameters for gsutil.
530
531 Returns:
Kuang-che Wu876a5382020-10-27 14:24:58 +0800532 A integer indicates the creation timestamp.
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800533
534 Raises:
535 subprocess.CalledProcessError: gsutil failed, usually means path not found
Kuang-che Wu876a5382020-10-27 14:24:58 +0800536 errors.ExternalError: creation time is not found
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800537 """
538 result = -1
539 # Currently we believe stat always returns a UTC time, and strptime also
540 # parses a UTC time by default.
541 time_format = '%a, %d %b %Y %H:%M:%S GMT'
542
543 for line in gsutil('stat', *args, **kwargs).splitlines():
544 if ':' not in line:
545 continue
Kuang-che Wuc89f2a22019-11-26 15:30:50 +0800546 key, value = line.split(':', 1)
547 key, value = key.strip(), value.strip()
Kuang-che Wu876a5382020-10-27 14:24:58 +0800548 if key != 'Creation time':
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800549 continue
550 dt = datetime.datetime.strptime(value, time_format)
Kuang-che Wu72b5a572019-10-29 20:37:57 +0800551 unixtime = int(calendar.timegm(dt.utctimetuple()))
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800552 result = max(result, unixtime)
553
554 if result == -1:
Kuang-che Wu876a5382020-10-27 14:24:58 +0800555 raise errors.ExternalError("didn't find creation time")
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800556 return result
557
558
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800559def query_milestone_by_version(board, short_version):
Kuang-che Wubfc4a642018-04-19 11:54:08 +0800560 """Query milestone by ChromeOS version number.
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800561
562 Args:
563 board: ChromeOS board name
564 short_version: ChromeOS version number in short format, ex. 9300.0.0
565
566 Returns:
567 ChromeOS milestone number (string). For example, '58' for '9300.0.0'.
568 None if failed.
569 """
570 path = gs_archive_path.format(board=board) + '/R*-' + short_version
571 for line in gsutil_ls('-d', path, ignore_errors=True):
572 m = re.search(r'/R(\d+)-', line)
573 if not m:
574 continue
575 return m.group(1)
576
Zheng-Jie Chang4e25a8d2020-01-08 16:00:13 +0800577 logger.debug('unable to query milestone of %s for %s', short_version, board)
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800578 return None
579
580
Kuang-che Wu80bf6a52019-05-31 12:48:06 +0800581def list_board_names(chromeos_root):
582 """List board names.
583
584 Args:
585 chromeos_root: chromeos tree root
586
587 Returns:
588 list of board names
589 """
590 # Following logic is simplified from chromite/lib/portage_util.py
591 cros_list_overlays = os.path.join(chromeos_root,
592 'chromite/bin/cros_list_overlays')
593 overlays = util.check_output(cros_list_overlays).splitlines()
594 result = set()
595 for overlay in overlays:
596 conf_file = os.path.join(overlay, 'metadata', 'layout.conf')
597 name = None
598 if os.path.exists(conf_file):
599 for line in open(conf_file):
600 m = re.match(r'^repo-name\s*=\s*(\S+)\s*$', line)
601 if m:
602 name = m.group(1)
603 break
604
605 if not name:
606 name_file = os.path.join(overlay, 'profiles', 'repo_name')
607 if os.path.exists(name_file):
Kuang-che Wua5723492019-11-25 20:59:34 +0800608 with open(name_file) as f:
609 name = f.read().strip()
Kuang-che Wu80bf6a52019-05-31 12:48:06 +0800610
611 if name:
612 name = re.sub(r'-private$', '', name)
613 result.add(name)
614
615 return list(result)
616
617
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800618def recognize_version(board, version):
Kuang-che Wubfc4a642018-04-19 11:54:08 +0800619 """Recognize ChromeOS version.
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800620
621 Args:
622 board: ChromeOS board name
623 version: ChromeOS version number in short or full format
624
625 Returns:
626 (milestone, version in short format)
627 """
628 if is_cros_short_version(version):
629 milestone = query_milestone_by_version(board, version)
630 short_version = version
631 else:
632 milestone, short_version = version_split(version)
633 return milestone, short_version
634
635
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800636def extract_major_version(version):
637 """Converts a version to its major version.
638
639 Args:
Kuang-che Wu9501f342019-11-15 17:15:21 +0800640 version: ChromeOS version number or snapshot version
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800641
642 Returns:
643 major version number in string format
644 """
645 version = version_to_short(version)
646 m = re.match(r'^(\d+)\.\d+\.\d+$', version)
647 return m.group(1)
648
649
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800650def version_to_short(version):
Kuang-che Wubfc4a642018-04-19 11:54:08 +0800651 """Convert ChromeOS version number to short format.
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800652
653 Args:
654 version: ChromeOS version number in short or full format
655
656 Returns:
657 version number in short format
658 """
659 if is_cros_short_version(version):
660 return version
661 _, short_version = version_split(version)
662 return short_version
663
664
665def version_to_full(board, version):
Kuang-che Wubfc4a642018-04-19 11:54:08 +0800666 """Convert ChromeOS version number to full format.
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800667
668 Args:
669 board: ChromeOS board name
670 version: ChromeOS version number in short or full format
671
672 Returns:
673 version number in full format
674 """
Zheng-Jie Changc47af3a2019-11-11 17:28:58 +0800675 if is_cros_snapshot_version(version):
676 milestone, short_version, _ = snapshot_version_split(version)
677 return make_cros_full_version(milestone, short_version)
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800678 if is_cros_full_version(version):
679 return version
680 milestone = query_milestone_by_version(board, version)
Kuang-che Wub529d2d2020-09-10 12:26:56 +0800681 if not milestone:
682 raise errors.ExternalError('incorrect board=%s or version=%s ?' %
683 (board, version))
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800684 return make_cros_full_version(milestone, version)
685
686
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800687def list_snapshots_from_image_archive(board, major_version):
Kuang-che Wu9501f342019-11-15 17:15:21 +0800688 """List ChromeOS snapshot image available from gs://chromeos-image-archive.
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800689
690 Args:
691 board: ChromeOS board
692 major_version: ChromeOS major version
693
694 Returns:
695 list of (version, gs_path):
696 version: Chrome OS snapshot version
697 gs_path: gs path of test image
698 """
699
Zheng-Jie Chang43a08412019-12-05 17:05:45 +0800700 def extract_snapshot_id(result):
701 m = re.match(r'^R\d+-\d+\.\d+\.\d+-(\d+)', result[0])
702 assert m
703 return int(m.group(1))
704
Zheng-Jie Changa331b872020-01-06 17:09:32 +0800705 short_version = '%s.0.0' % major_version
706 milestone = query_milestone_by_version(board, short_version)
707 if not milestone:
708 milestone = '*'
709
Zheng-Jie Chang54020832020-04-21 15:52:48 +0800710 path = ('gs://chromeos-image-archive/{board}-snapshot/R{milestone}-'
Zheng-Jie Changa331b872020-01-06 17:09:32 +0800711 '{short_version}-*/image.zip')
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800712 result = []
713 output = gsutil_ls(
Zheng-Jie Changa331b872020-01-06 17:09:32 +0800714 path.format(
715 board=board, milestone=milestone, short_version=short_version),
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800716 ignore_errors=True)
717
Zheng-Jie Changa331b872020-01-06 17:09:32 +0800718 for gs_path in sorted(output):
719 m = re.match(r'^gs:\S+(R\d+-\d+\.\d+\.\d+-\d+)', gs_path)
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800720 if m:
721 snapshot_version = m.group(1)
Zheng-Jie Chang43a08412019-12-05 17:05:45 +0800722 # we should skip if there is duplicate snapshot
723 if result and result[-1][0] == snapshot_version:
724 continue
Zheng-Jie Chang1ae64c32020-03-10 14:08:28 +0800725
Zheng-Jie Chang1ae64c32020-03-10 14:08:28 +0800726 _, _, snapshot_id = snapshot_version_split(snapshot_version)
Zheng-Jie Changdbe1f8e2021-01-26 12:33:32 +0800727
728 # crbug/1170601: ignore small snapshot ids
729 if int(snapshot_id) <= snapshot_cutover_id:
730 continue
731
732 # b/151054108: snapshot version in [29288, 29439] is broken
Zheng-Jie Chang1ae64c32020-03-10 14:08:28 +0800733 if 29288 <= int(snapshot_id) <= 29439:
734 continue
735
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800736 result.append((snapshot_version, gs_path))
Zheng-Jie Chang43a08412019-12-05 17:05:45 +0800737
738 # sort by its snapshot_id
739 result.sort(key=extract_snapshot_id)
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800740 return result
741
742
Kuang-che Wu575dc442019-03-05 10:30:55 +0800743def list_prebuilt_from_image_archive(board):
744 """Lists ChromeOS prebuilt image available from gs://chromeos-image-archive.
745
Kuang-che Wu575dc442019-03-05 10:30:55 +0800746 Args:
747 board: ChromeOS board name
748
749 Returns:
750 list of (version, gs_path):
751 version: Chrome OS version in full format
752 gs_path: gs path of test image
753 """
754 result = []
755 for line in gsutil_ls(gs_archive_path.format(board=board)):
756 m = re.match(r'^gs:\S+(R\d+-\d+\.\d+\.\d+)', line)
757 if m:
758 full_version = m.group(1)
759 test_image = 'chromiumos_test_image.tar.xz'
760 assert line.endswith('/')
761 gs_path = line + test_image
762 result.append((full_version, gs_path))
763 return result
764
765
Kuang-che Wue1808402020-01-06 20:27:45 +0800766def has_test_image(board, version):
767 if is_cros_snapshot_version(version):
768 return bool(query_snapshot_buildbucket_id(board, version))
769
Kuang-che Wub529d2d2020-09-10 12:26:56 +0800770 try:
771 full_version = version_to_full(board, version)
772 except errors.ExternalError:
773 # version_to_full() is implemented by checking image, thus its failure
774 # means no image.
775 return False
Kuang-che Wuef3f37c2021-03-08 16:30:38 +0800776 path = (
Kuang-che Wue1808402020-01-06 20:27:45 +0800777 gs_archive_path.format(board=board) +
Kuang-che Wuef3f37c2021-03-08 16:30:38 +0800778 '/%s/chromiumos_test_image.tar.xz' % full_version)
Kuang-che Wue1808402020-01-06 20:27:45 +0800779
Kuang-che Wuef3f37c2021-03-08 16:30:38 +0800780 if gsutil_ls(path, ignore_errors=True):
781 return True
Kuang-che Wue1808402020-01-06 20:27:45 +0800782 return False
783
784
Kuang-che Wu575dc442019-03-05 10:30:55 +0800785def list_chromeos_prebuilt_versions(board,
786 old,
787 new,
788 only_good_build=True,
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800789 use_snapshot=False):
Kuang-che Wu575dc442019-03-05 10:30:55 +0800790 """Lists ChromeOS version numbers with prebuilt between given range
791
792 Args:
793 board: ChromeOS board name
794 old: start version (inclusive)
795 new: end version (inclusive)
796 only_good_build: only if test image is available
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800797 use_snapshot: return snapshot versions if found
Kuang-che Wu575dc442019-03-05 10:30:55 +0800798
799 Returns:
800 list of sorted version numbers (in full format) between [old, new] range
801 (inclusive).
802 """
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800803 old_short = version_to_short(old)
804 new_short = version_to_short(new)
Kuang-che Wu575dc442019-03-05 10:30:55 +0800805
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800806 rev_map = {
807 } # dict: short version -> list of (short/full or snapshot version, gs path)
Kuang-che Wu575dc442019-03-05 10:30:55 +0800808 for full_version, gs_path in list_prebuilt_from_image_archive(board):
809 short_version = version_to_short(full_version)
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800810 rev_map[short_version] = [(full_version, gs_path)]
Kuang-che Wu575dc442019-03-05 10:30:55 +0800811
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800812 if use_snapshot:
813 for major_version in range(
814 int(extract_major_version(old)),
815 int(extract_major_version(new)) + 1):
816 short_version = '%s.0.0' % major_version
Zheng-Jie Changc47af3a2019-11-11 17:28:58 +0800817 next_short_version = '%s.0.0' % (major_version + 1)
Zheng-Jie Changb8697042019-10-29 16:03:26 +0800818 # If current version is smaller than cutover, ignore it as it might not
819 # contain enough information for continuing android and chrome bisection.
820 if not util.is_version_lesseq(snapshot_cutover_version, short_version):
821 continue
Zheng-Jie Changc47af3a2019-11-11 17:28:58 +0800822
823 # Given the fact that snapshots are images between two release versions.
824 # Adding snapshots of 12345.0.0 should be treated as adding commits
825 # between [12345.0.0, 12346.0.0).
826 # So in the following lines we check two facts:
827 # 1) If 12346.0.0(next_short_version) is a version between old and new
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800828 if not util.is_direct_relative_version(next_short_version, old_short):
Zheng-Jie Changc47af3a2019-11-11 17:28:58 +0800829 continue
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800830 if not util.is_direct_relative_version(next_short_version, new_short):
Zheng-Jie Changc47af3a2019-11-11 17:28:58 +0800831 continue
832 # 2) If 12345.0.0(short_version) is a version between old and new
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800833 if not util.is_direct_relative_version(short_version, old_short):
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800834 continue
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800835 if not util.is_direct_relative_version(short_version, new_short):
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800836 continue
Zheng-Jie Changc47af3a2019-11-11 17:28:58 +0800837
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800838 snapshots = list_snapshots_from_image_archive(board, str(major_version))
839 if snapshots:
840 # if snapshots found, we can append them after the release version,
841 # so the prebuilt image list of this version will be
842 # release_image, snapshot1, snapshot2,...
Zheng-Jie Changb8697042019-10-29 16:03:26 +0800843 if short_version not in rev_map:
844 rev_map[short_version] = []
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800845 rev_map[short_version] += snapshots
Kuang-che Wu575dc442019-03-05 10:30:55 +0800846
847 result = []
848 for rev in sorted(rev_map, key=util.version_key_func):
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800849 if not util.is_direct_relative_version(new_short, rev):
Kuang-che Wu575dc442019-03-05 10:30:55 +0800850 continue
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800851 if not util.is_version_lesseq(old_short, rev):
Kuang-che Wu575dc442019-03-05 10:30:55 +0800852 continue
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800853 if not util.is_version_lesseq(rev, new_short):
Kuang-che Wu575dc442019-03-05 10:30:55 +0800854 continue
855
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800856 for version, gs_path in rev_map[rev]:
Kuang-che Wu575dc442019-03-05 10:30:55 +0800857
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800858 # version_to_full() and gsutil_ls() may take long time if versions are a
859 # lot. This is acceptable because we usually bisect only short range.
Kuang-che Wu575dc442019-03-05 10:30:55 +0800860
Zheng-Jie Changa331b872020-01-06 17:09:32 +0800861 if only_good_build and not is_cros_snapshot_version(version):
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800862 gs_result = gsutil_ls(gs_path, ignore_errors=True)
863 if not gs_result:
864 logger.warning('%s is not a good build, ignore', version)
865 continue
866 assert len(gs_result) == 1
867 m = re.search(r'(R\d+-\d+\.\d+\.\d+)', gs_result[0])
868 if not m:
869 logger.warning('format of image path is unexpected: %s', gs_result[0])
870 continue
Zheng-Jie Changa331b872020-01-06 17:09:32 +0800871 version = m.group(1)
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800872 elif is_cros_short_version(version):
873 version = version_to_full(board, version)
Kuang-che Wu575dc442019-03-05 10:30:55 +0800874
Zheng-Jie Changaea4fba2020-02-17 17:12:09 +0800875 if is_cros_version_lesseq(old, version) and is_cros_version_lesseq(
876 version, new):
877 result.append(version)
Kuang-che Wu575dc442019-03-05 10:30:55 +0800878
879 return result
880
881
Kuang-che Wu721e8902021-03-19 12:18:53 +0800882def search_snapshot_image(board, snapshot_version):
883 """Searches chromeos snapshot image.
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800884
885 Args:
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800886 board: ChromeOS board name
887 snapshot_version: ChromeOS snapshot version number
888
889 Returns:
Kuang-che Wu721e8902021-03-19 12:18:53 +0800890 ImageInfo object
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800891 """
892 assert is_cros_snapshot_version(snapshot_version)
Kuang-che Wu721e8902021-03-19 12:18:53 +0800893 image_info = ImageInfo()
894 gs_path = gs_archive_base + '{board}-snapshot/{snapshot_version}-*'.format(
895 board=board, snapshot_version=snapshot_version)
896 files = gsutil_ls(
897 gs_path + '/' + sample_partition_filename, ignore_errors=True)
898 if files:
899 image_info[ImageType.PARTITION_IMAGE] = files[0].replace(
900 sample_partition_filename, '')
901 files = gsutil_ls(gs_path + '/image.zip', ignore_errors=True)
902 if files:
903 image_info[ImageType.ZIP_FILE] = files[0]
904 return image_info
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800905
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800906
Kuang-che Wu721e8902021-03-19 12:18:53 +0800907def prepare_image_for_quick_provision(image_info):
908 path = image_info.get(ImageType.PARTITION_IMAGE)
909 if path and path.startswith(gs_archive_base):
910 return urllib.parse.urlparse(path).path[1:]
Zheng-Jie Changc47af3a2019-11-11 17:28:58 +0800911
Kuang-che Wu721e8902021-03-19 12:18:53 +0800912 logger.warning(
913 'image format or location are not supported by quick-provision: %s',
914 image_info)
Kuang-che Wua7ddf9b2019-11-25 18:59:57 +0800915 return None
Zheng-Jie Chang127c3302019-09-10 17:17:04 +0800916
917
Kuang-che Wu721e8902021-03-19 12:18:53 +0800918def _cache_path_for_download(chromeos_root, url):
919 cache_folder = os.path.join(chromeos_root, cached_images_dir)
920 os.makedirs(cache_folder, exist_ok=True)
921 name = urllib.parse.quote(url, safe='')
922 return os.path.join(cache_folder, name)
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800923
Kuang-che Wu721e8902021-03-19 12:18:53 +0800924
925def prepare_image_for_cros_flash(chromeos_root, image_info):
926 path = image_info.get(ImageType.DISK_IMAGE)
927 if path:
928 # local path
929 if '://' not in path:
930 return path
931
932 m = re.search(gs_archive_base + r'([^/]+)-[a-z]+/([^/]+)/', path)
933 if m:
934 return 'xbuddy://remote/%s/%s/test' % (m.group(1), m.group(2))
935 if path.startswith(gs_archive_base):
936 return path.replace('chromiumos_test_image.tar.xz', 'test')
937
938 # 'cros flash' doesn't support other gs bucket, download to local.
939 if path.startswith('gs://'):
940 cache_path = _cache_path_for_download(chromeos_root, path)
941 if os.path.exists(cache_path):
942 return cache_path
943 gsutil('cp', path, cache_path)
944 return cache_path
945
946 path = image_info.get(ImageType.PARTITION_IMAGE)
947 if path and path.startswith(gs_archive_base):
948 # newer 'cros flash' support partition images
949 # TODO(kcwu): update the commit hash after new flash is enabled
950 if git_util.is_ancestor_commit(
951 os.path.join(chromeos_root, 'chromite'), '153f91684a9a59', 'HEAD'):
952 return path
953
954 path = image_info.get(ImageType.ZIP_FILE)
955 if path:
956 cached_image_full_path = _cache_path_for_download(
957 chromeos_root, path + '.' + test_image_filename)
958 if os.path.exists(cached_image_full_path):
959 return cached_image_full_path
960
961 tmp_dir = tempfile.mkdtemp()
962 try:
963 if path.startswith('gs://'):
964 gsutil('cp', path, tmp_dir)
965 path = os.path.join(tmp_dir, os.path.basename(path))
966 assert os.path.exists(path)
967 util.check_call('unzip', '-j', path, test_image_filename, cwd=tmp_dir)
968 shutil.move(
969 os.path.join(tmp_dir, test_image_filename), cached_image_full_path)
970 finally:
971 shutil.rmtree(tmp_dir)
972
973 return cached_image_full_path
974
975 return None
976
977
978def quick_provision(chromeos_root, host, image_info):
979 # TODO(kimjae): Transition to using TLS ProvisionDut for F20.
980 logger.debug('quick_provision %s %s', host, image_info)
981 build = prepare_image_for_quick_provision(image_info)
982 if not build:
983 return False
984
985 autotest_path = os.path.join(chromeos_root_inside_chroot,
986 in_tree_autotest_dir)
987 quick_provision_cmd = [
988 'test_that', '--args',
989 "value='%s'" % build, host, 'provision_QuickProvision', '--autotest_dir',
990 autotest_path
991 ]
992 try:
993 cros_sdk(chromeos_root, *quick_provision_cmd)
994 except subprocess.CalledProcessError as e:
995 raise errors.ExternalError('quick-provision failed') from e
996 return True
997
998
999def verify_dut_version(host, board, version):
1000 if version:
1001 # In the past, cros flash may fail with returncode=0
1002 # So let's have an extra check.
1003 if is_cros_snapshot_version(version):
1004 builder_path = query_dut_lsb_release(host).get(
1005 'CHROMEOS_RELEASE_BUILDER_PATH', '')
1006 expect_prefix = '%s-snapshot/%s-' % (board, version)
1007 if not builder_path.startswith(expect_prefix):
1008 raise errors.ExternalError(
1009 'although provision succeeded, the OS builder path is '
1010 'unexpected: actual=%s expect=%s' % (builder_path, expect_prefix))
1011 else:
1012 expect_version = version_to_short(version)
1013 dut_version = query_dut_short_version(host)
1014 if dut_version != expect_version:
1015 raise errors.ExternalError(
1016 'although provision succeeded, the OS version is unexpected: '
1017 'actual=%s expect=%s' % (dut_version, expect_version))
1018
1019 # "cros flash" may terminate successfully but the DUT starts self-repairing
1020 # (b/130786578), so it's necessary to do sanity check.
1021 if not is_good_dut(host):
1022 raise errors.ExternalError(
1023 'although provision succeeded, the DUT is in bad state')
1024
1025
1026def provision_image(chromeos_root,
1027 host,
1028 board,
1029 image_info,
1030 version=None,
1031 clobber_stateful=False,
1032 disable_rootfs_verification=True,
1033 force_reboot_callback=None):
1034 # Try quick_provision first, but fallback to cros flash.
1035 # TODO(kcwu): only use quick_provision for DUTs in the lab
1036 try:
1037 if quick_provision(chromeos_root, host, image_info):
1038 verify_dut_version(host, board, version)
1039 return
1040 logger.debug('quick-provision is not supported; fallback to cros flash')
1041 except errors.ExternalError as e:
1042 logger.warning('quick-provision failed; fallback to cros flash: %s', e)
1043
1044 if not cros_flash(
1045 chromeos_root,
1046 host,
1047 image_info,
1048 clobber_stateful=clobber_stateful,
1049 disable_rootfs_verification=disable_rootfs_verification,
1050 force_reboot_callback=force_reboot_callback):
1051 raise errors.InternalError('unsupported image: ' + str(image_info))
1052 verify_dut_version(host, board, version)
1053
1054
1055def search_prebuilt_image(board, version):
1056 """Searches chromeos prebuilt image.
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001057
1058 Args:
Kuang-che Wu28980b22019-07-31 19:51:45 +08001059 chromeos_root: chromeos tree root
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001060 board: ChromeOS board name
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001061 version: ChromeOS version number in short or full format
1062
1063 Returns:
Kuang-che Wu721e8902021-03-19 12:18:53 +08001064 ImageInfo object
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001065 """
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001066 assert is_cros_version(version)
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001067 full_version = version_to_full(board, version)
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001068
Kuang-che Wu721e8902021-03-19 12:18:53 +08001069 image_info = ImageInfo()
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001070 gs_path = gs_archive_path.format(board=board) + '/' + full_version
Kuang-che Wu721e8902021-03-19 12:18:53 +08001071 if gsutil_ls(gs_path + '/chromiumos_test_image.tar.xz', ignore_errors=True):
1072 image_info[ImageType.DISK_IMAGE] = gs_path + '/chromiumos_test_image.tar.xz'
1073 if gsutil_ls(gs_path + '/' + sample_partition_filename, ignore_errors=True):
1074 image_info[ImageType.PARTITION_IMAGE] = gs_path
1075 return image_info
1076
1077
1078def search_image(board, version):
1079 if is_cros_snapshot_version(version):
1080 return search_snapshot_image(board, version)
1081 return search_prebuilt_image(board, version)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001082
1083
1084def cros_flash(chromeos_root,
1085 host,
Kuang-che Wu721e8902021-03-19 12:18:53 +08001086 image_info,
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001087 clobber_stateful=False,
Kuang-che Wu2ac9a922020-09-03 16:50:12 +08001088 disable_rootfs_verification=True,
1089 force_reboot_callback=None):
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001090 """Flash a DUT with given ChromeOS image.
1091
1092 This is implemented by 'cros flash' command line.
1093
1094 Args:
1095 chromeos_root: use 'cros flash' of which chromeos tree
1096 host: DUT address
1097 board: ChromeOS board name
Kuang-che Wu721e8902021-03-19 12:18:53 +08001098 image_info: ImageInfo object
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001099 version: ChromeOS version in short or full format
1100 clobber_stateful: Clobber stateful partition when performing update
1101 disable_rootfs_verification: Disable rootfs verification after update
1102 is completed
Kuang-che Wu2ac9a922020-09-03 16:50:12 +08001103 force_reboot_callback: powerful reboot hook (via servo)
Kuang-che Wu414d67f2019-05-28 11:28:57 +08001104
Kuang-che Wu721e8902021-03-19 12:18:53 +08001105 Returns:
1106 False for unsupported images
1107
Kuang-che Wu414d67f2019-05-28 11:28:57 +08001108 Raises:
1109 errors.ExternalError: cros flash failed
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001110 """
Kuang-che Wu721e8902021-03-19 12:18:53 +08001111 logger.info('cros_flash %s %s', host, image_info)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001112
1113 # Reboot is necessary because sometimes previous 'cros flash' failed and
1114 # entered a bad state.
Kuang-che Wu2ac9a922020-09-03 16:50:12 +08001115 reboot(host, force_reboot_callback=force_reboot_callback)
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001116
Kuang-che Wu020a1182020-09-08 17:17:22 +08001117 # Stop service ap-update-manager to prevent rebooting during auto update.
Kuang-che Wu2e0680b2020-08-19 22:41:28 +08001118 # The service is used in jetstream boards, but not other CrOS devices.
1119 if query_dut_os_release(host).get('GOOGLE_CRASH_ID') == 'Jetstream':
1120 try:
Kuang-che Wu29e2eaf2020-08-21 16:51:26 +08001121 # Sleep to wait ap-update-manager start, which may take up to 27 seconds.
1122 # For simplicity, we wait 60 seconds here, which is the timeout value of
1123 # jetstream_host.
1124 # https://chromium.googlesource.com/chromiumos/third_party/autotest
Kuang-che Wuebc2c362020-12-14 16:27:09 +08001125 # /+/HEAD/server/hosts/jetstream_host.py#27
Kuang-che Wu29e2eaf2020-08-21 16:51:26 +08001126 time.sleep(60)
Kuang-che Wu2e0680b2020-08-19 22:41:28 +08001127 util.ssh_cmd(host, 'stop', 'ap-update-manager')
1128 except subprocess.CalledProcessError:
1129 pass # not started; do nothing
1130
Kuang-che Wu721e8902021-03-19 12:18:53 +08001131 image_path = prepare_image_for_cros_flash(chromeos_root, image_info)
1132 if not image_path:
1133 return False
1134
Kuang-che Wu28980b22019-07-31 19:51:45 +08001135 # Handle relative path.
1136 if '://' not in image_path and not os.path.isabs(image_path):
1137 assert os.path.exists(os.path.join(chromeos_root, image_path))
1138 image_path = os.path.join(chromeos_root_inside_chroot, image_path)
1139
Kuang-che Wuf3d03ca2019-03-11 17:31:40 +08001140 args = [
Kuang-che Wu8a83c9b2021-01-11 14:52:15 +08001141 '--debug',
1142 '--no-ping',
1143 # Speed up for slow network connection.
1144 '--send-payload-in-parallel',
1145 host,
1146 image_path,
Kuang-che Wuf3d03ca2019-03-11 17:31:40 +08001147 ]
Kuang-che Wu8a83c9b2021-01-11 14:52:15 +08001148 # TODO(kcwu): remove this check if we don't need to support chromeos versions
1149 # earlier than Dec 2020.
1150 if git_util.is_ancestor_commit(
1151 os.path.join(chromeos_root, 'chromite'), '9ed30bc3ed292b', 'HEAD'):
1152 # To reduce disk usage on DUT.
1153 args.append('--no-copy-payloads-to-device')
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001154 if clobber_stateful:
1155 args.append('--clobber-stateful')
1156 if disable_rootfs_verification:
1157 args.append('--disable-rootfs-verification')
1158
Kuang-che Wu721e8902021-03-19 12:18:53 +08001159 # TODO(kcwu): remove this flag once the new flash code enabled by default
1160 if git_util.is_ancestor_commit(
1161 os.path.join(chromeos_root, 'chromite'), '153f91684a9a59', 'HEAD'):
1162 args.append('--exp-new-flash')
1163
Kuang-che Wu414d67f2019-05-28 11:28:57 +08001164 try:
1165 cros_sdk(chromeos_root, 'cros', 'flash', *args)
Kuang-che Wu6d91b8c2020-11-24 20:14:35 +08001166 except subprocess.CalledProcessError as e:
1167 raise errors.ExternalError('cros flash failed') from e
Kuang-che Wu721e8902021-03-19 12:18:53 +08001168 return True
Kuang-che Wu414d67f2019-05-28 11:28:57 +08001169
1170
Kuang-che Wu721e8902021-03-19 12:18:53 +08001171def provision_image_with_retry(chromeos_root,
1172 host,
1173 board,
1174 image_info,
1175 version=None,
1176 clobber_stateful=False,
1177 disable_rootfs_verification=True,
1178 repair_callback=None,
1179 force_reboot_callback=None):
Kuang-che Wu414d67f2019-05-28 11:28:57 +08001180 # 'cros flash' is not 100% reliable, retry if necessary.
1181 for attempt in range(2):
1182 if attempt > 0:
1183 logger.info('will retry 60 seconds later')
1184 time.sleep(60)
1185
1186 try:
Kuang-che Wu721e8902021-03-19 12:18:53 +08001187 provision_image(
Kuang-che Wu414d67f2019-05-28 11:28:57 +08001188 chromeos_root,
1189 host,
1190 board,
Kuang-che Wu721e8902021-03-19 12:18:53 +08001191 image_info,
Kuang-che Wu414d67f2019-05-28 11:28:57 +08001192 version=version,
1193 clobber_stateful=clobber_stateful,
Kuang-che Wu2ac9a922020-09-03 16:50:12 +08001194 disable_rootfs_verification=disable_rootfs_verification,
1195 force_reboot_callback=force_reboot_callback)
Zheng-Jie Changfc5d8742021-03-31 04:57:43 +08001196 workaround_b183567529(
1197 host, board, version, force_reboot_callback=force_reboot_callback)
Kuang-che Wu414d67f2019-05-28 11:28:57 +08001198 return True
1199 except errors.ExternalError:
1200 logger.exception('cros flash failed')
1201 if repair_callback and not repair_callback(host):
1202 logger.warning('not repaired, assume it is harmless')
1203 continue
1204 return False
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001205
1206
1207def version_info(board, version):
1208 """Query subcomponents version info of given version of ChromeOS
1209
1210 Args:
1211 board: ChromeOS board name
1212 version: ChromeOS version number in short or full format
1213
1214 Returns:
1215 dict of component and version info, including (if available):
1216 cros_short_version: ChromeOS version
1217 cros_full_version: ChromeOS version
1218 milestone: milestone of ChromeOS
1219 cr_version: Chrome version
Kuang-che Wu708310b2018-03-28 17:24:34 +08001220 android_build_id: Android build id
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001221 android_branch: Android branch, in format like 'git_nyc-mr1-arc'
1222 """
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08001223 if is_cros_snapshot_version(version):
Zheng-Jie Chang2b6d1472019-11-13 12:40:17 +08001224 api = buildbucket_util.BuildbucketApi()
Zheng-Jie Changb8697042019-10-29 16:03:26 +08001225 milestone, short_version, _ = snapshot_version_split(version)
1226 buildbucket_id = query_snapshot_buildbucket_id(board, version)
Zheng-Jie Chang597c38b2020-03-12 22:24:10 +08001227 data = api.get_build(int(buildbucket_id)).output.properties
Zheng-Jie Chang4fabff62019-12-08 21:54:35 +08001228 target_versions = json_format.MessageToDict(data['target_versions'])
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08001229 return {
Zheng-Jie Changb8697042019-10-29 16:03:26 +08001230 VERSION_KEY_MILESTONE: milestone,
1231 VERSION_KEY_CROS_FULL_VERSION: version,
1232 VERSION_KEY_CROS_SHORT_VERSION: short_version,
Zheng-Jie Chang2d1dd9b2019-12-07 23:30:20 +08001233 VERSION_KEY_CR_VERSION: target_versions.get('chromeVersion'),
1234 VERSION_KEY_ANDROID_BUILD_ID: target_versions.get('androidVersion'),
1235 VERSION_KEY_ANDROID_BRANCH: target_versions.get('androidBranchVersion'),
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08001236 }
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001237 info = {}
1238 full_version = version_to_full(board, version)
1239
1240 # Some boards may have only partial-metadata.json but no metadata.json.
1241 # e.g. caroline R60-9462.0.0
1242 # Let's try both.
1243 metadata = None
1244 for metadata_filename in ['metadata.json', 'partial-metadata.json']:
Kuang-che Wu0768b972019-10-05 15:18:59 +08001245 path = gs_archive_path.format(
1246 board=board) + '/%s/%s' % (full_version, metadata_filename)
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001247 metadata = gsutil('cat', path, ignore_errors=True)
1248 if metadata:
1249 o = json.loads(metadata)
1250 v = o['version']
1251 board_metadata = o['board-metadata'][board]
1252 info.update({
1253 VERSION_KEY_CROS_SHORT_VERSION: v['platform'],
1254 VERSION_KEY_CROS_FULL_VERSION: v['full'],
1255 VERSION_KEY_MILESTONE: v['milestone'],
1256 VERSION_KEY_CR_VERSION: v['chrome'],
1257 })
1258
1259 if 'android' in v:
Kuang-che Wu708310b2018-03-28 17:24:34 +08001260 info[VERSION_KEY_ANDROID_BUILD_ID] = v['android']
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001261 if 'android-branch' in v: # this appears since R58-9317.0.0
1262 info[VERSION_KEY_ANDROID_BRANCH] = v['android-branch']
1263 elif 'android-container-branch' in board_metadata:
1264 info[VERSION_KEY_ANDROID_BRANCH] = v['android-container-branch']
1265 break
1266 else:
1267 logger.error('Failed to read metadata from gs://chromeos-image-archive')
1268 logger.error(
1269 'Note, so far no quick way to look up version info for too old builds')
1270
1271 return info
Kuang-che Wu848b1af2018-02-01 20:59:36 +08001272
1273
1274def query_chrome_version(board, version):
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001275 """Queries chrome version of chromeos build.
Kuang-che Wu848b1af2018-02-01 20:59:36 +08001276
1277 Args:
1278 board: ChromeOS board name
1279 version: ChromeOS version number in short or full format
1280
1281 Returns:
1282 Chrome version number
1283 """
1284 info = version_info(board, version)
1285 return info['cr_version']
Kuang-che Wu708310b2018-03-28 17:24:34 +08001286
1287
1288def query_android_build_id(board, rev):
1289 info = version_info(board, rev)
1290 rev = info['android_build_id']
1291 return rev
1292
1293
1294def query_android_branch(board, rev):
1295 info = version_info(board, rev)
1296 rev = info['android_branch']
1297 return rev
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001298
1299
Kuang-che Wu3eb6b502018-06-06 16:15:18 +08001300def guess_chrome_version(board, rev):
1301 """Guess chrome version number.
1302
1303 Args:
1304 board: chromeos board name
1305 rev: chrome or chromeos version
1306
1307 Returns:
1308 chrome version number
1309 """
1310 if is_cros_version(rev):
1311 assert board, 'need to specify BOARD for cros version'
1312 rev = query_chrome_version(board, rev)
1313 assert cr_util.is_chrome_version(rev)
1314
1315 return rev
1316
1317
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001318def is_inside_chroot():
1319 """Returns True if we are inside chroot."""
1320 return os.path.exists('/etc/cros_chroot_version')
1321
1322
Kuang-che Wuad5ac7b2020-02-20 19:02:52 +08001323def convert_path_outside_chroot(chromeos_root, path):
1324 """Converts path in chroot to outside.
1325
1326 Args:
1327 chromeos_root: chromeos tree root
1328 path: path inside chroot; support starting with '~/'
1329
1330 Returns:
1331 The corresponding path outside chroot assuming the chroot is mounted
1332 """
1333 if path.startswith('~/'):
1334 path = path.replace('~', '/home/' + os.environ['USER'])
1335 assert '~' not in path, 'tilde (~) character is not fully supported'
1336
1337 assert os.path.isabs(path)
1338 assert path[0] == os.sep
1339 return os.path.join(chromeos_root, 'chroot', path[1:])
1340
1341
Kuang-che Wu25fec6f2021-01-28 12:40:43 +08001342def cros_sdk(chromeos_root,
1343 *args,
1344 chrome_root=None,
1345 env=None,
1346 log_stdout=True,
1347 stdin=None,
1348 stderr_callback=None,
1349 goma_dir=None):
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001350 """Run commands inside chromeos chroot.
1351
1352 Args:
1353 chromeos_root: chromeos tree root
1354 *args: command to run
Kuang-che Wu25fec6f2021-01-28 12:40:43 +08001355 chrome_root: pass to cros_sdk; mount this path into the SDK chroot
1356 env: (dict) environment variables for the command
1357 log_stdout: Whether write the stdout output of the child process to log.
1358 stdin: standard input file handle for the command
1359 stderr_callback: Callback function for stderr. Called once per line.
1360 goma_dir: Goma installed directory to mount into the chroot
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001361 """
1362 envs = []
Kuang-che Wu25fec6f2021-01-28 12:40:43 +08001363 if env:
1364 for k, v in env.items():
1365 assert re.match(r'^[A-Za-z_][A-Za-z0-9_]*$', k)
1366 envs.append('%s=%s' % (k, v))
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001367
1368 # Use --no-ns-pid to prevent cros_sdk change our pgid, otherwise subsequent
1369 # commands would be considered as background process.
Kuang-che Wu399d4662019-06-06 15:23:37 +08001370 prefix = ['chromite/bin/cros_sdk', '--no-ns-pid']
Kuang-che Wud4603d72018-11-29 17:51:21 +08001371
Kuang-che Wu25fec6f2021-01-28 12:40:43 +08001372 if chrome_root:
1373 prefix += ['--chrome_root', chrome_root]
1374 if goma_dir:
1375 prefix += ['--goma_dir', goma_dir]
Kuang-che Wud4603d72018-11-29 17:51:21 +08001376
Kuang-che Wu399d4662019-06-06 15:23:37 +08001377 prefix += envs + ['--']
Kuang-che Wud4603d72018-11-29 17:51:21 +08001378
Kuang-che Wu399d4662019-06-06 15:23:37 +08001379 # In addition to the output of command we are interested, cros_sdk may
1380 # generate its own messages. For example, chroot creation messages if we run
1381 # cros_sdk the first time.
1382 # This is the hack to run dummy command once, so we can get clean output for
1383 # the command we are interested.
1384 cmd = prefix + ['true']
Kuang-che Wu62677012020-07-13 14:25:18 +08001385 try:
1386 util.check_call(*cmd, cwd=chromeos_root)
1387 except subprocess.CalledProcessError:
1388 logger.exception('cros_sdk init/update failed')
1389 raise
Kuang-che Wu399d4662019-06-06 15:23:37 +08001390
1391 cmd = prefix + list(args)
Kuang-che Wu9890ce82018-07-07 15:14:10 +08001392 return util.check_output(
1393 *cmd,
1394 cwd=chromeos_root,
Kuang-che Wu25fec6f2021-01-28 12:40:43 +08001395 log_stdout=log_stdout,
1396 stdin=stdin,
1397 stderr_callback=stderr_callback)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001398
1399
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001400def create_chroot(chromeos_root):
Kuang-che Wuad5ac7b2020-02-20 19:02:52 +08001401 """Creates ChromeOS chroot if necessary.
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001402
1403 Args:
1404 chromeos_root: chromeos tree root
1405 """
1406 if os.path.exists(os.path.join(chromeos_root, 'chroot')):
1407 return
1408 if os.path.exists(os.path.join(chromeos_root, 'chroot.img')):
1409 return
1410
1411 util.check_output('chromite/bin/cros_sdk', '--create', cwd=chromeos_root)
1412
1413
Kuang-che Wuad5ac7b2020-02-20 19:02:52 +08001414def mount_chroot(chromeos_root):
1415 """Creates ChromeOS chroot if necessary.
1416
1417 Args:
1418 chromeos_root: chromeos tree root
1419 """
1420 # An arbitrary file must exist in chroot.
1421 path = convert_path_outside_chroot(chromeos_root, '/bin/ls')
1422
1423 # Not created or mounted yet.
1424 if not os.path.exists(path):
1425 create_chroot(chromeos_root)
1426 # After this command, the chroot is mounted.
1427 cros_sdk(chromeos_root, 'true')
1428 assert os.path.exists(path)
1429
1430
1431def copy_into_chroot(chromeos_root, src, dst, overwrite=True):
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001432 """Copies file into chromeos chroot.
1433
Kuang-che Wuad5ac7b2020-02-20 19:02:52 +08001434 The side effect is chroot created and mounted.
1435
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001436 Args:
1437 chromeos_root: chromeos tree root
1438 src: path outside chroot
1439 dst: path inside chroot
Kuang-che Wu020a1182020-09-08 17:17:22 +08001440 overwrite: overwrite if dst already exists
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001441 """
Kuang-che Wuad5ac7b2020-02-20 19:02:52 +08001442 mount_chroot(chromeos_root)
1443 src = os.path.expanduser(src)
1444 dst_outside = convert_path_outside_chroot(chromeos_root, dst)
1445 if not overwrite and os.path.exists(dst_outside):
1446 return
1447
1448 # Haven't support directory or special files yet.
1449 assert os.path.isfile(src)
1450 assert os.path.isfile(dst_outside) or not os.path.exists(dst_outside)
1451
1452 dirname = os.path.dirname(dst_outside)
1453 if not os.path.exists(dirname):
1454 os.makedirs(dirname)
1455 shutil.copy(src, dst_outside)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001456
1457
Kuang-che Wu721e8902021-03-19 12:18:53 +08001458def _copy_template_files(src, dst):
1459 if not os.path.exists(src):
1460 return
1461
1462 def copy_if_nonexistent(src, dst):
1463 if not os.path.exists(dst):
1464 shutil.copy2(src, dst)
1465
1466 shutil.copytree(
1467 src, dst, dirs_exist_ok=True, copy_function=copy_if_nonexistent)
1468
1469
1470def override_autotest_config(autotest_dir):
1471 shadow_config_path = os.path.join(autotest_dir, 'shadow_config.ini')
1472 if not os.path.exists(shadow_config_path):
1473 with open(shadow_config_path, 'w') as f:
1474 f.write(autotest_shadow_config)
1475
1476
Kuang-che Wuad5ac7b2020-02-20 19:02:52 +08001477def prepare_chroot(chromeos_root):
1478 mount_chroot(chromeos_root)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001479
Kuang-che Wuad5ac7b2020-02-20 19:02:52 +08001480 # Work around b/149077936:
1481 # The creds file is copied into the chroot since 12866.0.0.
1482 # But earlier versions need this file as well because of cipd ACL change.
1483 creds_path = '~/.config/chrome_infra/auth/creds.json'
1484 if os.path.exists(os.path.expanduser(creds_path)):
1485 copy_into_chroot(chromeos_root, creds_path, creds_path, overwrite=False)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001486
Kuang-che Wu721e8902021-03-19 12:18:53 +08001487 # quick-provision requires special config for autotest.
1488 override_autotest_config(os.path.join(chromeos_root, in_tree_autotest_dir))
1489
1490 # Copy optional configure files into the home directory inside chromeos
1491 # chroot. For example, quick-provision may need special ssh config.
1492 assert os.environ.get('USER')
1493 _copy_template_files(
1494 os.path.join(common.BISECT_KIT_ROOT, 'cros_template_files', 'at_home'),
1495 os.path.join(chromeos_root, 'chroot', 'home', os.environ['USER']))
1496
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001497
Kuang-che Wu9890ce82018-07-07 15:14:10 +08001498def check_if_need_recreate_chroot(stdout, stderr):
1499 """Analyze build log and determine if chroot should be recreated.
1500
1501 Args:
1502 stdout: stdout output of build
1503 stderr: stderr output of build
1504
1505 Returns:
1506 the reason if chroot needs recreated; None otherwise
1507 """
Kuang-che Wu74768d32018-09-07 12:03:24 +08001508 if re.search(
1509 r"The current version of portage supports EAPI '\d+'. "
Kuang-che Wuae6824b2019-08-27 22:20:01 +08001510 'You must upgrade', stderr):
Kuang-che Wu9890ce82018-07-07 15:14:10 +08001511 return 'EAPI version mismatch'
1512
Kuang-che Wu5ac81322018-11-26 14:04:06 +08001513 if 'Chroot is too new. Consider running:' in stderr:
1514 return 'chroot version is too new'
1515
1516 # old message before Oct 2018
Kuang-che Wu9890ce82018-07-07 15:14:10 +08001517 if 'Chroot version is too new. Consider running cros_sdk --replace' in stderr:
1518 return 'chroot version is too new'
1519
Kuang-che Wu6fe987f2018-08-28 15:24:20 +08001520 # https://groups.google.com/a/chromium.org/forum/#!msg/chromium-os-dev/uzwT5APspB4/NFakFyCIDwAJ
1521 if "undefined reference to 'std::__1::basic_string" in stdout:
1522 return 'might be due to compiler change'
1523
Kuang-che Wu94e3b452019-11-21 12:49:18 +08001524 # Detect failures due to file collisions.
1525 # For example, kernel uprev from 3.x to 4.x, they are two separate packages
1526 # and conflict with each other. Other possible cases are package renaming or
1527 # refactoring. Let's recreate chroot to work around them.
1528 if 'Detected file collision' in stdout:
1529 # Using wildcard between words because the text wraps to the next line
1530 # depending on length of package name and each line is prefixed with
1531 # package name.
1532 # Using ".{,100}" instead of ".*" to prevent regex matching time explodes
1533 # exponentially. 100 is chosen arbitrarily. It should be longer than any
1534 # package name (65 now).
1535 m = re.search(
1536 r'Package (\S+).{,100}NOT.{,100}merged.{,100}'
1537 r'due.{,100}to.{,100}file.{,100}collisions', stdout, re.S)
1538 if m:
1539 return 'failed to install package due to file collision: ' + m.group(1)
Kuang-che Wu356c3522019-11-19 16:11:05 +08001540
Kuang-che Wu9890ce82018-07-07 15:14:10 +08001541 return None
1542
1543
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001544def build_packages(chromeos_root,
1545 board,
1546 chrome_root=None,
1547 goma_dir=None,
1548 afdo_use=False):
Kuang-che Wu28980b22019-07-31 19:51:45 +08001549 """Build ChromeOS packages.
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001550
1551 Args:
1552 chromeos_root: chromeos tree root
1553 board: ChromeOS board name
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001554 chrome_root: Chrome tree root. If specified, build chrome using the
1555 provided tree
1556 goma_dir: Goma installed directory to mount into the chroot. If specified,
1557 build chrome with goma.
1558 afdo_use: build chrome with AFDO optimization
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001559 """
Zheng-Jie Changffd49462019-12-16 12:15:18 +08001560
1561 def has_build_package_argument(argument):
1562 stderr_lines = []
1563 try:
1564 util.check_call(
1565 'src/scripts/build_packages',
1566 '--help',
1567 cwd=chromeos_root,
1568 stderr_callback=stderr_lines.append)
1569 except subprocess.CalledProcessError:
1570 help_output = ''.join(stderr_lines)
1571 return '--[no]%s' % argument in help_output
1572
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001573 common_env = {
1574 'USE': '-cros-debug chrome_internal',
1575 'FEATURES': 'separatedebug',
1576 }
Kuang-che Wu9890ce82018-07-07 15:14:10 +08001577 stderr_lines = []
1578 try:
Kuang-che Wufb553102018-10-02 18:14:29 +08001579 with locking.lock_file(locking.LOCK_FILE_FOR_BUILD):
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001580 env = common_env.copy()
1581 env['FEATURES'] += ' -separatedebug splitdebug'
Kuang-che Wufb553102018-10-02 18:14:29 +08001582 cros_sdk(
1583 chromeos_root,
Kuang-che Wu28980b22019-07-31 19:51:45 +08001584 './update_chroot',
1585 '--toolchain_boards',
Kuang-che Wufb553102018-10-02 18:14:29 +08001586 board,
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001587 env=env,
Kuang-che Wu28980b22019-07-31 19:51:45 +08001588 stderr_callback=stderr_lines.append)
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001589
1590 env = common_env.copy()
1591 cmd = [
Kuang-che Wu28980b22019-07-31 19:51:45 +08001592 './build_packages',
1593 '--board',
1594 board,
1595 '--withdev',
1596 '--noworkon',
1597 '--skip_chroot_upgrade',
1598 '--accept_licenses=@CHROMEOS',
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001599 ]
Zheng-Jie Changffd49462019-12-16 12:15:18 +08001600
1601 # `use_any_chrome` flag is default on and will force to use a chrome
1602 # prebuilt even if the version doesn't match.
1603
1604 # As this argument is landed in 12681, we should check if the argument
1605 # exists before adding this.
1606 if has_build_package_argument('use_any_chrome'):
1607 cmd.append('--nouse_any_chrome')
1608
Kuang-che Wua9a20bb2019-09-05 22:24:04 +08001609 if goma_dir:
1610 # Tell build_packages to start and stop goma
1611 cmd.append('--run_goma')
1612 env['USE_GOMA'] = 'true'
1613 if afdo_use:
1614 env['USE'] += ' afdo_use'
1615 cros_sdk(
1616 chromeos_root,
1617 *cmd,
1618 env=env,
1619 chrome_root=chrome_root,
1620 stderr_callback=stderr_lines.append,
1621 goma_dir=goma_dir)
Kuang-che Wu9890ce82018-07-07 15:14:10 +08001622 except subprocess.CalledProcessError as e:
1623 # Detect failures due to incompatibility between chroot and source tree. If
1624 # so, notify the caller to recreate chroot and retry.
1625 reason = check_if_need_recreate_chroot(e.output, ''.join(stderr_lines))
1626 if reason:
Kuang-che Wu6d91b8c2020-11-24 20:14:35 +08001627 raise NeedRecreateChrootException(reason) from e
Kuang-che Wu9890ce82018-07-07 15:14:10 +08001628
1629 # For other failures, don't know how to handle. Just bail out.
1630 raise
1631
Kuang-che Wu28980b22019-07-31 19:51:45 +08001632
1633def build_image(chromeos_root, board):
1634 """Build ChromeOS image.
1635
1636 Args:
1637 chromeos_root: chromeos tree root
1638 board: ChromeOS board name
1639
1640 Returns:
1641 image folder; relative to chromeos_root
1642 """
1643 stderr_lines = []
1644 try:
1645 with locking.lock_file(locking.LOCK_FILE_FOR_BUILD):
1646 cros_sdk(
1647 chromeos_root,
1648 './build_image',
1649 '--board',
1650 board,
1651 '--noenable_rootfs_verification',
1652 'test',
1653 env={
1654 'USE': '-cros-debug chrome_internal',
1655 'FEATURES': 'separatedebug',
1656 },
1657 stderr_callback=stderr_lines.append)
1658 except subprocess.CalledProcessError as e:
1659 # Detect failures due to incompatibility between chroot and source tree. If
1660 # so, notify the caller to recreate chroot and retry.
1661 reason = check_if_need_recreate_chroot(e.output, ''.join(stderr_lines))
1662 if reason:
Kuang-che Wu6d91b8c2020-11-24 20:14:35 +08001663 raise NeedRecreateChrootException(reason) from e
Kuang-che Wu28980b22019-07-31 19:51:45 +08001664
1665 # For other failures, don't know how to handle. Just bail out.
1666 raise
1667
Kuang-che Wu721e8902021-03-19 12:18:53 +08001668 image_symlink = os.path.join(chromeos_root, build_images_dir, board, 'latest')
Kuang-che Wu28980b22019-07-31 19:51:45 +08001669 assert os.path.exists(image_symlink)
1670 image_name = os.readlink(image_symlink)
Kuang-che Wu721e8902021-03-19 12:18:53 +08001671 image_folder = os.path.join(build_images_dir, board, image_name)
Kuang-che Wu28980b22019-07-31 19:51:45 +08001672 assert os.path.exists(
1673 os.path.join(chromeos_root, image_folder, test_image_filename))
1674 return image_folder
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001675
1676
Zheng-Jie Changfc5d8742021-03-31 04:57:43 +08001677def workaround_b183567529(host, board, version=None,
1678 force_reboot_callback=None):
1679 """Workaround for volteer failure.
1680
1681 See b/183567529#comment8 and b/183020319#comment26 for more details.
1682 """
1683 broken_range = [
1684 ('13836.0.0', '13854.0.0'),
1685 ('13816.13.0', '13816.19.0'),
1686 ]
1687 if board != 'volteer' or not version:
1688 return
1689
1690 if is_cros_short_version(version):
1691 short_version = version
1692 else:
1693 _, short_version = version_split(version)
1694 for old, new in broken_range:
1695 if util.is_version_lesseq(old, short_version) and util.is_version_lesseq(
1696 short_version,
1697 new) and (util.is_direct_relative_version(old, short_version) and
1698 util.is_direct_relative_version(short_version, new)):
1699 logger.info('applying b183567529 cbi patch for volteer')
1700 cbi_override = os.path.join(
1701 common.BISECT_KIT_ROOT,
1702 'patching/b183567529/volteer-cbi-override.conf')
1703 util.scp_cmd(cbi_override, 'root@%s:/etc/init/' % host)
1704
1705 patch_script = os.path.join(common.BISECT_KIT_ROOT,
1706 'patching/b183567529/test_cbi_script.sh')
1707 util.check_output(patch_script, host)
1708 reboot(host, force_reboot_callback)
1709 break
1710
1711
Kuang-che Wu23192ad2020-03-11 18:12:46 +08001712class AutotestControlInfo:
Kuang-che Wub9705bd2018-06-28 17:59:18 +08001713 """Parsed content of autotest control file.
1714
1715 Attributes:
1716 name: test name
1717 path: control file path
1718 variables: dict of top-level control variables. Sample keys: NAME, AUTHOR,
1719 DOC, ATTRIBUTES, DEPENDENCIES, etc.
1720 """
1721
1722 def __init__(self, path, variables):
Kuang-che Wu25fec6f2021-01-28 12:40:43 +08001723 assert 'NAME' in variables, 'invalid control file'
Kuang-che Wub9705bd2018-06-28 17:59:18 +08001724 self.name = variables['NAME']
1725 self.path = path
1726 self.variables = variables
1727
1728
1729def parse_autotest_control_file(path):
1730 """Parses autotest control file.
1731
1732 This only parses simple top-level string assignments.
1733
1734 Returns:
1735 AutotestControlInfo object
1736 """
1737 variables = {}
Kuang-che Wua5723492019-11-25 20:59:34 +08001738 with open(path) as f:
1739 code = ast.parse(f.read())
Kuang-che Wub9705bd2018-06-28 17:59:18 +08001740 for stmt in code.body:
1741 # Skip if not simple "NAME = *" assignment.
1742 if not (isinstance(stmt, ast.Assign) and len(stmt.targets) == 1 and
1743 isinstance(stmt.targets[0], ast.Name)):
1744 continue
1745
1746 # Only support string value.
1747 if isinstance(stmt.value, ast.Str):
1748 variables[stmt.targets[0].id] = stmt.value.s
1749
1750 return AutotestControlInfo(path, variables)
1751
1752
1753def enumerate_autotest_control_files(autotest_dir):
1754 """Enumerate autotest control files.
1755
1756 Args:
1757 autotest_dir: autotest folder
1758
1759 Returns:
1760 list of paths to control files
1761 """
1762 # Where to find control files. Relative to autotest_dir.
1763 subpaths = [
1764 'server/site_tests',
1765 'client/site_tests',
1766 'server/tests',
1767 'client/tests',
1768 ]
1769
Kuang-che Wud6df63c2020-09-20 01:29:55 +08001770 denylist = ['site-packages', 'venv', 'results', 'logs', 'containers']
Kuang-che Wub9705bd2018-06-28 17:59:18 +08001771 result = []
1772 for subpath in subpaths:
1773 path = os.path.join(autotest_dir, subpath)
1774 for root, dirs, files in os.walk(path):
1775
Kuang-che Wud6df63c2020-09-20 01:29:55 +08001776 for deny in denylist:
1777 if deny in dirs:
1778 dirs.remove(deny)
Kuang-che Wub9705bd2018-06-28 17:59:18 +08001779
1780 for filename in files:
1781 if filename == 'control' or filename.startswith('control.'):
1782 result.append(os.path.join(root, filename))
1783
1784 return result
1785
1786
1787def get_autotest_test_info(autotest_dir, test_name):
1788 """Get metadata of given test.
1789
1790 Args:
1791 autotest_dir: autotest folder
1792 test_name: test name
1793
1794 Returns:
1795 AutotestControlInfo object. None if test not found.
1796 """
1797 for control_file in enumerate_autotest_control_files(autotest_dir):
Zheng-Jie Chang1504a552020-02-20 16:38:57 +08001798 try:
1799 info = parse_autotest_control_file(control_file)
1800 except SyntaxError:
1801 logger.warning('%s is not parsable, ignore', control_file)
1802 continue
1803
Kuang-che Wub9705bd2018-06-28 17:59:18 +08001804 if info.name == test_name:
1805 return info
1806 return None
1807
1808
Kuang-che Wu9d14c162020-11-03 19:35:18 +08001809def _get_overlay_name(overlay):
1810 path = os.path.join(overlay, 'metadata', 'layout.conf')
1811 if os.path.exists(path):
1812 with open(path) as f:
1813 for line in f:
1814 m = re.search(r'repo-name\s*=\s*(\S+)', line)
1815 if m:
1816 return m.group(1)
1817
1818 path = os.path.join(overlay, 'profiles', 'repo_name')
1819 if os.path.exists(path):
1820 with open(path) as f:
1821 return f.readline().rstrip()
1822
1823 return None
1824
1825
1826def parse_chromeos_overlays(chromeos_root):
1827 # ref: chromite's lib/portage_util.py ListOverlays().
1828 overlays = {}
1829 paths = ['src/overlays', 'src/private-overlays']
1830
1831 for path in paths:
1832 path = os.path.join(chromeos_root, path, 'overlay-*')
1833 for overlay in sorted(glob.glob(path)):
1834 name = _get_overlay_name(overlay)
1835 if not name:
1836 continue
1837 # Special cases which have variant boards.
1838 if name in ['auron', 'guado', 'nyan', 'veyron']:
1839 continue
1840
1841 path = os.path.join(overlay, 'metadata', 'layout.conf')
1842 masters = []
1843 if os.path.exists(path):
1844 with open(path) as f:
1845 for line in f:
1846 m = re.search(r'masters\s*=(.*)', line)
1847 if m:
1848 masters = m.group(1).split()
1849 overlays[name] = masters
1850 return overlays
1851
1852
1853def resolve_basic_boards(overlays):
1854
1855 def normalize(name):
1856 return name.replace('-private', '')
1857
1858 def resolve(name):
1859 result = set()
1860 for parent in overlays[name]:
1861 assert parent != name, 'recursive overlays definition?'
1862 if parent not in overlays:
1863 continue
1864 for basic in resolve(parent):
1865 result.add(basic)
1866 if not result:
1867 result.add(name)
1868 return set(map(normalize, result))
1869
1870 result = {}
1871 for name in overlays:
1872 board = normalize(name)
1873 basic = resolve(name)
1874 assert len(basic) == 1
1875 basic_board = basic.pop()
1876 result[board] = basic_board
1877 return result
1878
1879
Zheng-Jie Chang868c1752020-01-21 14:42:41 +08001880def detect_branch_level(branch):
1881 """Given a branch name of manifest-internal, detect it's branch level.
1882
1883 level1: if ChromeOS version is x.0.0
1884 level2: if ChromeOS version is x.x.0
1885 level3: if ChromeOS version is x.x.x
1886 Where x is an non-zero integer.
1887
1888 Args:
1889 branch: branch name or ref name in manifest-internal
1890
1891 Returns:
1892 An integer indicates the branch level, or zero if not detectable.
1893 """
1894 level1 = r'^(refs\/\S+(\/\S+)?/)?master$'
1895 level2 = r'^\S+-(\d+)(\.0)?\.B$'
1896 level3 = r'^\S+-(\d+)\.(\d+)(\.0)?\.B$'
1897
1898 if re.match(level1, branch):
1899 return 1
1900 if re.match(level2, branch):
1901 return 2
1902 if re.match(level3, branch):
1903 return 3
1904 return 0
1905
1906
Zheng-Jie Chang5f9ae4e2020-02-07 14:26:06 +08001907def get_crosland_link(old, new):
1908 """Generates crosland link between two versions.
1909
1910 Args:
1911 old: ChromeOS version
1912 new: ChromeOS version
1913
1914 Returns:
1915 A crosland url.
1916 """
1917
1918 def version_to_url_parameter(ver):
1919 if is_cros_snapshot_version(ver):
1920 return snapshot_version_split(ver)[2]
1921 return version_to_short(ver)
1922
1923 old_parameter = version_to_url_parameter(old)
1924 new_parameter = version_to_url_parameter(new)
1925 return CROSLAND_URL_TEMPLATE % (old_parameter, new_parameter)
1926
1927
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08001928class ChromeOSSpecManager(codechange.SpecManager):
1929 """Repo manifest related operations.
1930
1931 This class enumerates chromeos manifest files, parses them,
1932 and sync to disk state according to them.
1933 """
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001934
1935 def __init__(self, config):
1936 self.config = config
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08001937 self.manifest_dir = os.path.join(self.config['chromeos_root'], '.repo',
1938 'manifests')
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08001939 self.manifest_internal_dir = os.path.join(self.config['chromeos_mirror'],
1940 'manifest-internal.git')
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08001941 self.historical_manifest_git_dir = os.path.join(
Kuang-che Wud8fc9572018-10-03 21:00:41 +08001942 self.config['chromeos_mirror'], 'chromeos/manifest-versions.git')
Kuang-che Wu0fff9882020-12-14 17:37:31 +08001943 self.historical_manifest_branch_name = 'refs/heads/master'
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08001944 if not os.path.exists(self.historical_manifest_git_dir):
Kuang-che Wue121fae2018-11-09 16:18:39 +08001945 raise errors.InternalError('Manifest snapshots should be cloned into %s' %
1946 self.historical_manifest_git_dir)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08001947
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08001948 def lookup_snapshot_manifest_revisions(self, old, new):
1949 """Get manifest commits between snapshot versions.
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08001950
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08001951 Returns:
1952 list of (timestamp, commit_id, snapshot_id):
1953 timestamp: integer unix timestamp
1954 commit_id: a string indicates commit hash
1955 snapshot_id: a string indicates snapshot id
1956 """
1957 assert is_cros_snapshot_version(old)
1958 assert is_cros_snapshot_version(new)
1959
1960 gs_path = (
Zheng-Jie Chang54020832020-04-21 15:52:48 +08001961 'gs://chromeos-image-archive/{board}-snapshot/{version}-*/image.zip')
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08001962 # Try to guess the commit time of a snapshot manifest, it is usually a few
1963 # minutes different between snapshot manifest commit and image.zip
1964 # generate.
1965 try:
Kuang-che Wu876a5382020-10-27 14:24:58 +08001966 old_timestamp = gsutil_stat_creation_time(
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08001967 gs_path.format(board=self.config['board'], version=old)) - 86400
1968 except subprocess.CalledProcessError:
1969 old_timestamp = None
1970 try:
Kuang-che Wu876a5382020-10-27 14:24:58 +08001971 new_timestamp = gsutil_stat_creation_time(
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08001972 gs_path.format(board=self.config['board'], version=new)) + 86400
1973 # 1558657989 is snapshot_id 5982's commit time, this ensures every time
1974 # we can find snapshot 5982
1975 # snapshot_id <= 5982 has different commit message format, so we need
1976 # to identify its id in different ways, see below comment for more info.
1977 new_timestamp = max(new_timestamp, 1558657989 + 1)
1978 except subprocess.CalledProcessError:
1979 new_timestamp = None
1980 result = []
1981 _, _, old_snapshot_id = snapshot_version_split(old)
1982 _, _, new_snapshot_id = snapshot_version_split(new)
1983 repo = self.manifest_internal_dir
1984 path = 'snapshot.xml'
1985 branch = 'snapshot'
1986 commits = git_util.get_history(
1987 repo,
1988 path,
1989 branch,
1990 after=old_timestamp,
1991 before=new_timestamp,
1992 with_subject=True)
1993
1994 # Unfortunately, we can not identify snapshot_id <= 5982 from its commit
1995 # subject, as their subjects are all `Annealing manifest snapshot.`.
1996 # So instead we count the snapshot_id manually.
1997 count = 5982
1998 # There are two snapshot_id = 2633 in commit history, ignore the former
1999 # one.
2000 ignore_list = ['95c8526a7f0798d02f692010669dcbd5a152439a']
2001 # We examine the commits in reverse order as there are some testing
2002 # commits before snapshot_id=2, this method works fine after
2003 # snapshot 2, except snapshot 2633
2004 for commit in reversed(commits):
2005 msg = commit[2]
2006 if commit[1] in ignore_list:
2007 continue
2008
2009 match = re.match(r'^annealing manifest snapshot (\d+)', msg)
2010 if match:
2011 snapshot_id = match.group(1)
2012 elif 'Annealing manifest snapshot' in msg:
2013 snapshot_id = str(count)
2014 count -= 1
2015 else:
2016 continue
Zheng-Jie Chang34595ea2020-03-10 13:04:22 +08002017 # b/151054108: snapshot version in [29288, 29439] is broken
2018 if 29288 <= int(snapshot_id) <= 29439:
2019 continue
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002020 if int(old_snapshot_id) <= int(snapshot_id) <= int(new_snapshot_id):
2021 result.append((commit[0], commit[1], snapshot_id))
2022 # We find commits in reversed order, now reverse it again to chronological
2023 # order.
2024 return list(reversed(result))
2025
2026 def lookup_build_timestamp(self, rev):
2027 assert is_cros_full_version(rev) or is_cros_snapshot_version(rev)
2028 if is_cros_full_version(rev):
2029 return self.lookup_release_build_timestamp(rev)
Kuang-che Wua7ddf9b2019-11-25 18:59:57 +08002030 return self.lookup_snapshot_build_timestamp(rev)
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002031
2032 def lookup_snapshot_build_timestamp(self, rev):
2033 assert is_cros_snapshot_version(rev)
2034 return int(self.lookup_snapshot_manifest_revisions(rev, rev)[0][0])
2035
2036 def lookup_release_build_timestamp(self, rev):
2037 assert is_cros_full_version(rev)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002038 milestone, short_version = version_split(rev)
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002039 path = os.path.join('buildspecs', milestone, short_version + '.xml')
2040 try:
2041 timestamp = git_util.get_commit_time(self.historical_manifest_git_dir,
Kuang-che Wu0fff9882020-12-14 17:37:31 +08002042 self.historical_manifest_branch_name,
2043 path)
Kuang-che Wu6d91b8c2020-11-24 20:14:35 +08002044 except ValueError as e:
2045 raise errors.InternalError(
2046 '%s does not have %s' %
2047 (self.historical_manifest_git_dir, path)) from e
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002048 return timestamp
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002049
Zheng-Jie Chang868c1752020-01-21 14:42:41 +08002050 def detect_float_spec_branch_level(self, spec):
2051 results = [
2052 detect_branch_level(branch) for branch in git_util.get_branches(
2053 self.manifest_dir, commit=spec.name)
2054 ]
2055 results = [x for x in results if x > 0]
2056 return min(results) if results else 0
2057
2058 def branch_between_float_specs(self, old_spec, new_spec):
2059 if old_spec.spec_type != codechange.SPEC_FLOAT:
2060 return False
2061 if new_spec.spec_type != codechange.SPEC_FLOAT:
2062 return False
2063
2064 level_old = self.detect_float_spec_branch_level(old_spec)
2065 level_new = self.detect_float_spec_branch_level(new_spec)
2066
2067 if not level_old or not level_new:
Kuang-che Wuebc2c362020-12-14 16:27:09 +08002068 logger.warning('branch level detect failed, assume not branched')
Zheng-Jie Chang868c1752020-01-21 14:42:41 +08002069 return False
2070 return level_old != level_new
2071
Kuang-che Wud558a042020-06-06 02:11:00 +08002072 def _determine_float_branch(self, old, new, fixed_specs):
2073 # There is no revision tag in snapshot's xml. We know snapshot
2074 # builds are on master branch.
2075 master_refname = 'refs/remotes/origin/master'
Zheng-Jie Chang3e191962020-02-06 14:25:31 +08002076 if fixed_specs[0].revision:
2077 old_branches = git_util.get_branches(
Kuang-che Wud558a042020-06-06 02:11:00 +08002078 self.manifest_dir, commit=fixed_specs[0].revision, remote=True)
2079 else:
2080 old_branches = [master_refname]
2081
Zheng-Jie Chang3e191962020-02-06 14:25:31 +08002082 if fixed_specs[-1].revision:
2083 new_branches = git_util.get_branches(
Kuang-che Wud558a042020-06-06 02:11:00 +08002084 self.manifest_dir, commit=fixed_specs[-1].revision, remote=True)
Zheng-Jie Chang3e191962020-02-06 14:25:31 +08002085 else:
Kuang-che Wud558a042020-06-06 02:11:00 +08002086 new_branches = [master_refname]
Zheng-Jie Chang3e191962020-02-06 14:25:31 +08002087
Kuang-che Wud558a042020-06-06 02:11:00 +08002088 common_branches = list(set(old_branches) & set(new_branches))
2089 assert common_branches, '%s and %s are not on common branches?' % (old, new)
2090
2091 if len(common_branches) == 1:
2092 return common_branches[0]
2093
2094 # There are more than one common branches, use heuristic to tie breaking.
2095 # The heuristic is simple: choice the branch with "smallest" number.
2096 # "Smaller" means the more major branch (not branched) or branched later.
2097 #
2098 # Following is the commit graph of manifest-internal repo. It shows many
2099 # interesting cases.
2100 #
2101 # 84/13021.0.0 84/13022.0.0 84/13024.0.0
2102 # --A--+---X--------------X------B-------X-----------> master
2103 # \
2104 # \ 83/13020.1.0 83/13020.56.0 83/13020.68.0
2105 # C---X----D--+-------X-------+--------X-----> release-R83-13020.B
2106 # \ \
2107 # \ E------------> stabilize-13020.67.B
2108 # \ 83/13020.55.1
2109 # F-----X--------------------> stabilize-13020.55.B
2110 #
2111 # How to read this graph:
2112 # - Time goes from left to right. Branch names are on the right side of
2113 # arrows.
2114 # - Letters A-F are manifest commits.
2115 # - Marker X means release image build at that time, the version numbers
2116 # are labeled above the X marker.
2117 # For example,
2118 # 1) 13021.0.0 release is based on manifest A, which is on all branches
2119 # shown on the graph.
2120 # We know 13021.0.0 is on master (and R84 branch later, not shown in
2121 # this graph), not on 13020* branches.
2122 # 2) 13020.56.0 release is based on manifest D, which is on 3 branches
2123 # (R83-13020.B, 13020.67.B, and 13020.55.B).
2124 # We know 13020.56.0 is on R83-13020.B and 13020.67.B, but not
2125 # 13020.55.B.
2126 #
2127 # There is an important property here. Every time a new branch is created,
2128 # there will always be a commit (like C, E, and F) to fix "revision" field
2129 # in the manifest file. In other words, xxxxx.1.0 is impossible based on
2130 # manifest on master branch. xxxxx.yy.1 is impossible based on manifest on
2131 # xxxxx.B branch.
2132 #
2133 # With such property, among the branches containing the given manifest
2134 # file, the branch with "smallest" number guarantees where the release is.
2135
2136 def branch_key(s):
2137 if s == master_refname:
2138 return 0, 0, 0
2139 m = re.search(r'-(\d+)\.B$', s)
2140 if m:
2141 return int(m.group(1)), 0, 0
2142 m = re.search(r'-(\d+)\.(\d+)\.B$', s)
2143 if m:
2144 return int(m.group(1)), int(m.group(2)), 0
2145 m = re.search(r'-(\d+)\.(\d+)\.(\d+)\.B$', s)
2146 if m:
2147 return int(m.group(1)), int(m.group(2)), int(m.group(3))
2148
2149 logger.warning('unexpected branch name: %s', s)
2150 return (sys.maxsize, sys.maxsize, sys.maxsize, s)
2151
2152 common_branches.sort(key=branch_key)
2153 return common_branches[0]
2154
2155 def collect_float_spec(self, old, new, fixed_specs=None):
2156 assert fixed_specs
2157 branch = self._determine_float_branch(old, new, fixed_specs)
2158 logger.debug('float branch=%s', branch)
Zheng-Jie Changd968f552020-01-16 13:31:57 +08002159
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002160 old_timestamp = self.lookup_build_timestamp(old)
2161 new_timestamp = self.lookup_build_timestamp(new)
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002162 # snapshot time is different from commit time
2163 # usually it's a few minutes different
2164 # 30 minutes should be safe in most cases
2165 if is_cros_snapshot_version(old):
2166 old_timestamp = old_timestamp - 1800
2167 if is_cros_snapshot_version(new):
2168 new_timestamp = new_timestamp + 1800
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002169
Zheng-Jie Chang1ace3012020-02-15 04:51:05 +08002170 # TODO(zjchang): add logic to combine symlink target's (full.xml) history
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002171 result = []
Zheng-Jie Chang1ace3012020-02-15 04:51:05 +08002172 path = 'default.xml'
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002173 parser = repo_util.ManifestParser(self.manifest_dir)
2174 for timestamp, git_rev in parser.enumerate_manifest_commits(
Zheng-Jie Changd968f552020-01-16 13:31:57 +08002175 old_timestamp, new_timestamp, path, branch=branch):
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002176 result.append(
2177 codechange.Spec(codechange.SPEC_FLOAT, git_rev, timestamp, path))
2178 return result
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002179
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002180 def collect_fixed_spec(self, old, new):
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002181 assert is_cros_full_version(old) or is_cros_snapshot_version(old)
2182 assert is_cros_full_version(new) or is_cros_snapshot_version(new)
2183
2184 # case 1: if both are snapshot, return a list of snapshot
2185 if is_cros_snapshot_version(old) and is_cros_snapshot_version(new):
2186 return self.collect_snapshot_specs(old, new)
2187
2188 # case 2: if both are release version
2189 # return a list of release version
2190 if is_cros_full_version(old) and is_cros_full_version(new):
2191 return self.collect_release_specs(old, new)
2192
2193 # case 3: return a list of release version and append a snapshot
2194 # before or at the end
Zheng-Jie Changc47af3a2019-11-11 17:28:58 +08002195 result = self.collect_release_specs(
2196 version_to_full(self.config['board'], old),
2197 version_to_full(self.config['board'], new))
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002198 if is_cros_snapshot_version(old):
Zheng-Jie Changc47af3a2019-11-11 17:28:58 +08002199 result = self.collect_snapshot_specs(old, old) + result[1:]
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002200 elif is_cros_snapshot_version(new):
Zheng-Jie Chang5cd62dd2019-12-09 13:21:12 +08002201 result += self.collect_snapshot_specs(new, new)
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002202 return result
2203
2204 def collect_snapshot_specs(self, old, new):
2205 assert is_cros_snapshot_version(old)
2206 assert is_cros_snapshot_version(new)
2207
2208 def guess_snapshot_version(board, snapshot_id, old, new):
2209 if old.endswith('-' + snapshot_id):
2210 return old
2211 if new.endswith('-' + snapshot_id):
2212 return new
Zheng-Jie Chang54020832020-04-21 15:52:48 +08002213 gs_path = ('gs://chromeos-image-archive/{board}-snapshot/'
Kuang-che Wuf791afa2019-10-28 19:53:26 +08002214 'R*-{snapshot_id}-*'.format(
2215 board=board, snapshot_id=snapshot_id))
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002216 for line in gsutil_ls(gs_path, ignore_errors=True):
2217 m = re.match(r'^gs:\S+(R\d+-\d+\.\d+\.\d+-\d+)\S+', line)
2218 if m:
2219 return m.group(1)
Zheng-Jie Chang026cd5d2019-12-04 12:13:01 +08002220 return None
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002221
2222 result = []
2223 path = 'snapshot.xml'
2224 revisions = self.lookup_snapshot_manifest_revisions(old, new)
Kuang-che Wuf791afa2019-10-28 19:53:26 +08002225 for timestamp, _git_rev, snapshot_id in revisions:
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002226 snapshot_version = guess_snapshot_version(self.config['board'],
2227 snapshot_id, old, new)
Zheng-Jie Chang026cd5d2019-12-04 12:13:01 +08002228 if snapshot_version:
2229 result.append(
2230 codechange.Spec(codechange.SPEC_FIXED, snapshot_version, timestamp,
2231 path))
2232 else:
2233 logger.warning('snapshot id %s is not found, ignore', snapshot_id)
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002234 return result
2235
2236 def collect_release_specs(self, old, new):
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002237 assert is_cros_full_version(old)
2238 assert is_cros_full_version(new)
2239 old_milestone, old_short_version = version_split(old)
2240 new_milestone, new_short_version = version_split(new)
2241
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002242 result = []
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002243 for milestone in git_util.list_dir_from_revision(
Kuang-che Wu0fff9882020-12-14 17:37:31 +08002244 self.historical_manifest_git_dir, self.historical_manifest_branch_name,
2245 'buildspecs'):
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002246 if not milestone.isdigit():
2247 continue
2248 if not int(old_milestone) <= int(milestone) <= int(new_milestone):
2249 continue
2250
Kuang-che Wu74768d32018-09-07 12:03:24 +08002251 files = git_util.list_dir_from_revision(
Kuang-che Wu0fff9882020-12-14 17:37:31 +08002252 self.historical_manifest_git_dir,
2253 self.historical_manifest_branch_name,
Kuang-che Wu74768d32018-09-07 12:03:24 +08002254 os.path.join('buildspecs', milestone))
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002255
2256 for fn in files:
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002257 path = os.path.join('buildspecs', milestone, fn)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002258 short_version, ext = os.path.splitext(fn)
2259 if ext != '.xml':
2260 continue
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002261 if (util.is_version_lesseq(old_short_version, short_version) and
2262 util.is_version_lesseq(short_version, new_short_version) and
2263 util.is_direct_relative_version(short_version, new_short_version)):
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002264 rev = make_cros_full_version(milestone, short_version)
Kuang-che Wu0fff9882020-12-14 17:37:31 +08002265 timestamp = git_util.get_commit_time(
2266 self.historical_manifest_git_dir,
2267 self.historical_manifest_branch_name, path)
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002268 result.append(
2269 codechange.Spec(codechange.SPEC_FIXED, rev, timestamp, path))
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002270
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002271 def version_key_func(spec):
2272 _milestone, short_version = version_split(spec.name)
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002273 return util.version_key_func(short_version)
2274
2275 result.sort(key=version_key_func)
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002276 assert result[0].name == old
2277 assert result[-1].name == new
Kuang-che Wubfc4a642018-04-19 11:54:08 +08002278 return result
2279
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002280 def get_manifest(self, rev):
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002281 assert is_cros_full_version(rev) or is_cros_snapshot_version(rev)
2282 if is_cros_full_version(rev):
2283 milestone, short_version = version_split(rev)
2284 path = os.path.join('buildspecs', milestone, '%s.xml' % short_version)
2285 manifest = git_util.get_file_from_revision(
Kuang-che Wu0fff9882020-12-14 17:37:31 +08002286 self.historical_manifest_git_dir,
2287 self.historical_manifest_branch_name, path)
Zheng-Jie Chang127c3302019-09-10 17:17:04 +08002288 else:
2289 revisions = self.lookup_snapshot_manifest_revisions(rev, rev)
2290 commit_id = revisions[0][1]
2291 manifest = git_util.get_file_from_revision(self.manifest_internal_dir,
2292 commit_id, 'snapshot.xml')
Zheng-Jie Chang0fc704b2019-12-09 18:43:38 +08002293 return manifest
2294
2295 def get_manifest_file(self, rev):
2296 assert is_cros_full_version(rev) or is_cros_snapshot_version(rev)
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002297 manifest_name = 'manifest_%s.xml' % rev
2298 manifest_path = os.path.join(self.manifest_dir, manifest_name)
2299 with open(manifest_path, 'w') as f:
Zheng-Jie Chang0fc704b2019-12-09 18:43:38 +08002300 f.write(self.get_manifest(rev))
Zheng-Jie Changec368b52020-03-02 16:25:25 +08002301
2302 # workaround for b/150572399
2303 # for chromeOS version < 12931.0.0, manifests are included from incorrect
2304 # folder .repo instead of.repo/manifests
2305 if is_cros_version_lesseq(rev, '12931.0.0'):
2306 repo_path = os.path.join(self.config['chromeos_root'], '.repo')
2307 manifest_patch_path = os.path.join(repo_path, manifest_name)
2308 with open(manifest_patch_path, 'w') as f:
2309 f.write(self.get_manifest(rev))
2310
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002311 return manifest_name
2312
2313 def parse_spec(self, spec):
2314 parser = repo_util.ManifestParser(self.manifest_dir)
2315 if spec.spec_type == codechange.SPEC_FIXED:
Zheng-Jie Chang0fc704b2019-12-09 18:43:38 +08002316 manifest_name = self.get_manifest_file(spec.name)
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002317 manifest_path = os.path.join(self.manifest_dir, manifest_name)
Kuang-che Wua5723492019-11-25 20:59:34 +08002318 with open(manifest_path) as f:
2319 content = f.read()
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002320 root = parser.parse_single_xml(content, allow_include=False)
2321 else:
2322 root = parser.parse_xml_recursive(spec.name, spec.path)
2323
2324 spec.entries = parser.process_parsed_result(root)
2325 if spec.spec_type == codechange.SPEC_FIXED:
Kuang-che Wufe1e88a2019-09-10 21:52:25 +08002326 if not spec.is_static():
Kuang-che Wud1b74152020-05-20 08:46:46 +08002327 raise ValueError('fixed spec %r has unexpected floating entries' %
2328 spec.name)
Zheng-Jie Changd968f552020-01-16 13:31:57 +08002329 spec.revision = root.get('revision')
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002330
2331 def sync_disk_state(self, rev):
Zheng-Jie Chang0fc704b2019-12-09 18:43:38 +08002332 manifest_name = self.get_manifest_file(rev)
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002333
2334 # For ChromeOS, mark_as_stable step requires 'repo init -m', which sticks
2335 # manifest. 'repo sync -m' is not enough
2336 repo_util.init(
2337 self.config['chromeos_root'],
2338 'https://chrome-internal.googlesource.com/chromeos/manifest-internal',
2339 manifest_name=manifest_name,
2340 repo_url='https://chromium.googlesource.com/external/repo.git',
Kuang-che Wud8fc9572018-10-03 21:00:41 +08002341 reference=self.config['chromeos_mirror'],
Zheng-Jie Chang85529ad2020-03-06 18:40:18 +08002342 # b/150753074: moblab is in non-default group and causes mark_as_stable
2343 # fail
Kuang-che Wu084eef22020-03-11 18:29:48 +08002344 groups='default,moblab,platform-linux',
Kuang-che Wue4bae0b2018-07-19 12:10:14 +08002345 )
2346
2347 # Note, don't sync with current_branch=True for chromeos. One of its
2348 # build steps (inside mark_as_stable) executes "git describe" which
2349 # needs git tag information.
2350 repo_util.sync(self.config['chromeos_root'])