Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 2 | # 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 | |
| 7 | Terminology 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 Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 10 | snapshot_version: ChromeOS version number with milestone and snapshot id, |
| 11 | like "R62-9876.0.0-12345". |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 12 | version: if not specified, it could be in short or full format. |
| 13 | """ |
| 14 | |
| 15 | from __future__ import print_function |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 16 | import ast |
Kuang-che Wu | 72b5a57 | 2019-10-29 20:37:57 +0800 | [diff] [blame] | 17 | import calendar |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 18 | import datetime |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 19 | import errno |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 20 | import glob |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 21 | import json |
| 22 | import logging |
| 23 | import os |
| 24 | import re |
Kuang-che Wu | ad5ac7b | 2020-02-20 19:02:52 +0800 | [diff] [blame] | 25 | import shutil |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 26 | import subprocess |
Kuang-che Wu | d558a04 | 2020-06-06 02:11:00 +0800 | [diff] [blame] | 27 | import sys |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 28 | import time |
| 29 | |
Zheng-Jie Chang | 4fabff6 | 2019-12-08 21:54:35 +0800 | [diff] [blame] | 30 | from google.protobuf import json_format |
| 31 | |
Zheng-Jie Chang | 2b6d147 | 2019-11-13 12:40:17 +0800 | [diff] [blame] | 32 | from bisect_kit import buildbucket_util |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 33 | from bisect_kit import cli |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 34 | from bisect_kit import codechange |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 35 | from bisect_kit import cr_util |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 36 | from bisect_kit import errors |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 37 | from bisect_kit import git_util |
Kuang-che Wu | fb55310 | 2018-10-02 18:14:29 +0800 | [diff] [blame] | 38 | from bisect_kit import locking |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 39 | from bisect_kit import repo_util |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 40 | from bisect_kit import util |
| 41 | |
| 42 | logger = logging.getLogger(__name__) |
| 43 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 44 | re_chromeos_full_version = r'^R\d+-\d+\.\d+\.\d+$' |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 45 | re_chromeos_short_version = r'^\d+\.\d+\.\d+$' |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 46 | re_chromeos_snapshot_version = r'^R\d+-\d+\.\d+\.\d+-\d+$' |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 47 | |
| 48 | gs_archive_path = 'gs://chromeos-image-archive/{board}-release' |
| 49 | gs_release_path = ( |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 50 | 'gs://chromeos-releases/{channel}-channel/{boardpath}/{short_version}') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 51 | |
| 52 | # Assume gsutil is in PATH. |
| 53 | gsutil_bin = 'gsutil' |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 54 | |
| 55 | # Since snapshots with version >= 12618.0.0 have android and chrome version |
| 56 | # info. |
| 57 | snapshot_cutover_version = '12618.0.0' |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 58 | |
Zheng-Jie Chang | dbe1f8e | 2021-01-26 12:33:32 +0800 | [diff] [blame^] | 59 | # http://crbug.com/1170601, small snapshot ids should be ignored |
| 60 | # 21000 is R80-12617.0.0 |
| 61 | snapshot_cutover_id = 21000 |
| 62 | |
Zheng-Jie Chang | 61a645a | 2020-04-29 10:12:13 +0800 | [diff] [blame] | 63 | # current earliest buildbucket buildable versions |
| 64 | # picked from https://crrev.com/c/2072618 |
| 65 | buildbucket_cutover_versions = [ |
| 66 | '12931.0.0', |
| 67 | '12871.26.0', # R81 |
| 68 | '12871.24.2', # stabilize-12871.24.B |
| 69 | '12812.10.0', # factory-excelsior-12812.B |
| 70 | '12768.14.0', # firmware-servo-12768.B |
| 71 | '12739.85.0', # R80 |
| 72 | '12739.67.1', # stabilize-excelsior-12739.67.B |
| 73 | '12692.36.0', # factory-hatch-12692.B |
| 74 | '12672.104.0', # firmware-hatch-12672.B |
| 75 | '12607.110.0', # R79 |
| 76 | '12607.83.2', # stabilize-quickfix-12607.83.B |
| 77 | '12587.59.0', # factory-kukui-12587.B |
| 78 | '12573.78.0', # firmware-kukui-12573.B |
| 79 | '12499.96.0', # R78 |
| 80 | '12422.33.0', # firmware-mistral-12422.B |
| 81 | '12371.190.0', # R77 |
| 82 | '12361.38.0', # factory-mistral-12361.B |
| 83 | '12200.65.0', # firmware-sarien-12200.B |
| 84 | '12105.128.0', # R75 |
| 85 | '12033.82.0', # factory-sarien-12033.B |
| 86 | ] |
| 87 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 88 | chromeos_root_inside_chroot = '/mnt/host/source' |
| 89 | # relative to chromeos_root |
Kuang-che Wu | 7f82c6f | 2019-08-12 14:29:28 +0800 | [diff] [blame] | 90 | prebuilt_autotest_dir = 'tmp/autotest-prebuilt' |
Kuang-che Wu | 5963ebf | 2020-10-21 09:01:04 +0800 | [diff] [blame] | 91 | prebuilt_tast_dir = 'tmp/tast-prebuilt' |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 92 | # Relative to chromeos root. Images are cached_images_dir/$board/$image_name. |
| 93 | cached_images_dir = 'src/build/images' |
| 94 | test_image_filename = 'chromiumos_test_image.bin' |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 95 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 96 | VERSION_KEY_CROS_SHORT_VERSION = 'cros_short_version' |
| 97 | VERSION_KEY_CROS_FULL_VERSION = 'cros_full_version' |
| 98 | VERSION_KEY_MILESTONE = 'milestone' |
| 99 | VERSION_KEY_CR_VERSION = 'cr_version' |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 100 | VERSION_KEY_ANDROID_BUILD_ID = 'android_build_id' |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 101 | VERSION_KEY_ANDROID_BRANCH = 'android_branch' |
Zheng-Jie Chang | 5f9ae4e | 2020-02-07 14:26:06 +0800 | [diff] [blame] | 102 | CROSLAND_URL_TEMPLATE = 'https://crosland.corp.google.com/log/%s..%s' |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 103 | |
| 104 | |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 105 | class NeedRecreateChrootException(Exception): |
| 106 | """Failed to build ChromeOS because of chroot mismatch or corruption""" |
| 107 | |
| 108 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 109 | def is_cros_short_version(s): |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 110 | """Determines if `s` is chromeos short version. |
| 111 | |
| 112 | This function doesn't accept version number of local build. |
| 113 | """ |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 114 | return bool(re.match(re_chromeos_short_version, s)) |
| 115 | |
| 116 | |
| 117 | def is_cros_full_version(s): |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 118 | """Determines if `s` is chromeos full version. |
| 119 | |
| 120 | This function doesn't accept version number of local build. |
| 121 | """ |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 122 | return bool(re.match(re_chromeos_full_version, s)) |
| 123 | |
| 124 | |
| 125 | def is_cros_version(s): |
| 126 | """Determines if `s` is chromeos version (either short or full)""" |
| 127 | return is_cros_short_version(s) or is_cros_full_version(s) |
| 128 | |
| 129 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 130 | def is_cros_snapshot_version(s): |
| 131 | """Determines if `s` is chromeos snapshot version""" |
| 132 | return bool(re.match(re_chromeos_snapshot_version, s)) |
| 133 | |
| 134 | |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 135 | def is_cros_version_lesseq(ver1, ver2): |
| 136 | """Determines if ver1 is less or equal to ver2. |
| 137 | |
| 138 | Args: |
| 139 | ver1: a Chrome OS version in short, full, or snapshot format. |
| 140 | ver2: a Chrome OS version in short, full, or snapshot format. |
| 141 | |
| 142 | Returns: |
Zheng-Jie Chang | 61a645a | 2020-04-29 10:12:13 +0800 | [diff] [blame] | 143 | True if ver1 is less or equal to ver2. |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 144 | """ |
| 145 | assert is_cros_version(ver1) or is_cros_snapshot_version(ver1) |
| 146 | assert is_cros_version(ver2) or is_cros_snapshot_version(ver2) |
| 147 | |
| 148 | ver1 = [int(x) for x in re.split(r'[.-]', ver1) if not x.startswith('R')] |
| 149 | ver2 = [int(x) for x in re.split(r'[.-]', ver2) if not x.startswith('R')] |
| 150 | return ver1 <= ver2 |
| 151 | |
| 152 | |
Zheng-Jie Chang | 61a645a | 2020-04-29 10:12:13 +0800 | [diff] [blame] | 153 | def is_buildbucket_buildable(version): |
| 154 | """Determines if a version is buildable on buildbucket.""" |
| 155 | short_version = version_to_short(version) |
| 156 | # If given version is child of any cutover, then it's buildable |
| 157 | return any([ |
| 158 | util.is_direct_relative_version(x, short_version) and |
| 159 | is_cros_version_lesseq(x, version) for x in buildbucket_cutover_versions |
| 160 | ]) |
| 161 | |
| 162 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 163 | def make_cros_full_version(milestone, short_version): |
| 164 | """Makes full_version from milestone and short_version""" |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 165 | assert milestone |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 166 | return 'R%s-%s' % (milestone, short_version) |
| 167 | |
| 168 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 169 | def make_cros_snapshot_version(milestone, short_version, snapshot_id): |
| 170 | """Makes snapshot version from milestone, short_version and snapshot id""" |
| 171 | return 'R%s-%s-%s' % (milestone, short_version, snapshot_id) |
| 172 | |
| 173 | |
| 174 | def version_split(version): |
| 175 | """Splits full_version or snapshot_version into milestone and short_version""" |
| 176 | assert is_cros_full_version(version) or is_cros_snapshot_version(version) |
| 177 | if is_cros_snapshot_version(version): |
| 178 | return snapshot_version_split(version)[0:2] |
| 179 | milestone, short_version = version.split('-') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 180 | return milestone[1:], short_version |
| 181 | |
| 182 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 183 | def snapshot_version_split(snapshot_version): |
| 184 | """Splits snapshot_version into milestone, short_version and snapshot_id""" |
| 185 | assert is_cros_snapshot_version(snapshot_version) |
| 186 | milestone, shot_version, snapshot_id = snapshot_version.split('-') |
| 187 | return milestone[1:], shot_version, snapshot_id |
| 188 | |
| 189 | |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 190 | def query_snapshot_buildbucket_id(board, snapshot_version): |
| 191 | """Query buildbucket id of a snapshot""" |
| 192 | assert is_cros_snapshot_version(snapshot_version) |
Zheng-Jie Chang | 5402083 | 2020-04-21 15:52:48 +0800 | [diff] [blame] | 193 | path = ('gs://chromeos-image-archive/{board}-snapshot' |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 194 | '/{snapshot_version}-*/image.zip') |
| 195 | output = gsutil_ls( |
| 196 | '-d', |
| 197 | path.format(board=board, snapshot_version=snapshot_version), |
| 198 | ignore_errors=True) |
| 199 | for line in output: |
Zheng-Jie Chang | 5402083 | 2020-04-21 15:52:48 +0800 | [diff] [blame] | 200 | m = re.match(r'.*-snapshot/R\d+-\d+\.\d+\.\d+-\d+-(.+)/image\.zip', line) |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 201 | if m: |
| 202 | return m.group(1) |
| 203 | return None |
| 204 | |
| 205 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 206 | def argtype_cros_version(s): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 207 | if (not is_cros_version(s)) and (not is_cros_snapshot_version(s)): |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 208 | msg = 'invalid cros version' |
Kuang-che Wu | ce2f3be | 2019-10-28 19:44:54 +0800 | [diff] [blame] | 209 | raise cli.ArgTypeError(msg, '9876.0.0, R62-9876.0.0 or R77-12369.0.0-11681') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 210 | return s |
| 211 | |
| 212 | |
| 213 | def query_dut_lsb_release(host): |
| 214 | """Query /etc/lsb-release of given DUT |
| 215 | |
| 216 | Args: |
| 217 | host: the DUT address |
| 218 | |
| 219 | Returns: |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 220 | dict for keys and values of /etc/lsb-release. |
| 221 | |
| 222 | Raises: |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 223 | errors.SshConnectionError: cannot connect to host |
| 224 | errors.ExternalError: lsb-release file doesn't exist |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 225 | """ |
| 226 | try: |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 227 | output = util.ssh_cmd(host, 'cat', '/etc/lsb-release') |
Kuang-che Wu | 6d91b8c | 2020-11-24 20:14:35 +0800 | [diff] [blame] | 228 | except subprocess.CalledProcessError as e: |
| 229 | raise errors.ExternalError( |
| 230 | 'unable to read /etc/lsb-release; not a DUT') from e |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 231 | return dict(re.findall(r'^(\w+)=(.*)$', output, re.M)) |
| 232 | |
| 233 | |
Kuang-che Wu | f134ebf | 2020-08-18 21:06:47 +0800 | [diff] [blame] | 234 | def query_dut_os_release(host): |
| 235 | """Query /etc/os-release of given DUT |
| 236 | |
| 237 | Args: |
| 238 | host: the DUT address |
| 239 | |
| 240 | Returns: |
| 241 | dict for keys and values of /etc/os-release. |
| 242 | |
| 243 | Raises: |
| 244 | errors.SshConnectionError: cannot connect to host |
| 245 | errors.ExternalError: lsb-release file doesn't exist |
| 246 | """ |
| 247 | try: |
| 248 | output = util.ssh_cmd(host, 'cat', '/etc/os-release') |
Kuang-che Wu | 6d91b8c | 2020-11-24 20:14:35 +0800 | [diff] [blame] | 249 | except subprocess.CalledProcessError as e: |
| 250 | raise errors.ExternalError( |
| 251 | 'unable to read /etc/os-release; not a DUT') from e |
Kuang-che Wu | f134ebf | 2020-08-18 21:06:47 +0800 | [diff] [blame] | 252 | return dict(re.findall(r'^(\w+)=(.*)$', output, re.M)) |
| 253 | |
| 254 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 255 | def is_dut(host): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 256 | """Determines whether a host is a chromeos device. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 257 | |
| 258 | Args: |
| 259 | host: the DUT address |
| 260 | |
| 261 | Returns: |
| 262 | True if the host is a chromeos device. |
| 263 | """ |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 264 | try: |
Kuang-che Wu | f134ebf | 2020-08-18 21:06:47 +0800 | [diff] [blame] | 265 | return query_dut_os_release(host).get('ID') in [ |
| 266 | 'chromiumos', |
| 267 | 'chromeos', |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 268 | ] |
| 269 | except (errors.ExternalError, errors.SshConnectionError): |
| 270 | return False |
| 271 | |
| 272 | |
| 273 | def is_good_dut(host): |
| 274 | if not is_dut(host): |
| 275 | return False |
| 276 | |
| 277 | # Sometimes python is broken after 'cros flash'. |
| 278 | try: |
| 279 | util.ssh_cmd(host, 'python', '-c', '1') |
| 280 | return True |
| 281 | except (subprocess.CalledProcessError, errors.SshConnectionError): |
| 282 | return False |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 283 | |
| 284 | |
| 285 | def query_dut_board(host): |
| 286 | """Query board name of a given DUT""" |
| 287 | return query_dut_lsb_release(host).get('CHROMEOS_RELEASE_BOARD') |
| 288 | |
| 289 | |
| 290 | def query_dut_short_version(host): |
Kuang-che Wu | acb6efd | 2018-04-25 18:52:58 +0800 | [diff] [blame] | 291 | """Query short version of a given DUT. |
| 292 | |
| 293 | This function may return version of local build, which |
| 294 | is_cros_short_version() is false. |
| 295 | """ |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 296 | return query_dut_lsb_release(host).get('CHROMEOS_RELEASE_VERSION') |
| 297 | |
| 298 | |
Zheng-Jie Chang | 2608454 | 2020-04-29 06:34:33 +0800 | [diff] [blame] | 299 | def query_dut_prebuilt_version(host): |
| 300 | """Return a snapshot version or short version of a given DUT. |
| 301 | |
| 302 | Args: |
| 303 | host: dut host |
| 304 | |
| 305 | Returns: |
| 306 | Snapshot version or short version. |
| 307 | """ |
| 308 | lsb_release = query_dut_lsb_release(host) |
| 309 | release_version = lsb_release.get('CHROMEOS_RELEASE_VERSION') |
| 310 | builder_path = lsb_release.get('CHROMEOS_RELEASE_BUILDER_PATH') |
| 311 | match = re.match(r'\S+-(?:snapshot|postsubmit)/(R\d+-\d+\.\d+\.\d+-\d+)-\d+', |
| 312 | builder_path) |
| 313 | if match: |
| 314 | return match.group(1) |
| 315 | return release_version |
| 316 | |
| 317 | |
Zheng-Jie Chang | 181be6f | 2020-03-17 16:16:08 +0800 | [diff] [blame] | 318 | def query_dut_is_by_official_builder(host): |
Kuang-che Wu | c092bd5 | 2021-01-05 14:25:56 +0800 | [diff] [blame] | 319 | """Query if given DUT is build by official builder""" |
| 320 | build_type = query_dut_lsb_release(host).get('CHROMEOS_RELEASE_BUILD_TYPE', |
| 321 | '') |
| 322 | build_type = build_type.split(' - ')[0] |
| 323 | assert build_type in ('Official Build', 'Continuous Builder', |
| 324 | 'Developer Build', |
| 325 | 'Test Build'), 'unknown build type (%s)' % build_type |
| 326 | return build_type in ['Official Build', 'Continuous Builder'] |
Zheng-Jie Chang | 181be6f | 2020-03-17 16:16:08 +0800 | [diff] [blame] | 327 | |
| 328 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 329 | def query_dut_boot_id(host, connect_timeout=None): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 330 | """Query boot id. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 331 | |
| 332 | Args: |
| 333 | host: DUT address |
| 334 | connect_timeout: connection timeout |
| 335 | |
| 336 | Returns: |
| 337 | boot uuid |
| 338 | """ |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 339 | return util.ssh_cmd( |
| 340 | host, |
| 341 | 'cat', |
| 342 | '/proc/sys/kernel/random/boot_id', |
| 343 | connect_timeout=connect_timeout).strip() |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 344 | |
| 345 | |
Kuang-che Wu | 2ac9a92 | 2020-09-03 16:50:12 +0800 | [diff] [blame] | 346 | def reboot(host, force_reboot_callback=None): |
| 347 | """Reboot a DUT and verify. |
| 348 | |
| 349 | Args: |
| 350 | host: DUT address |
| 351 | force_reboot_callback: powerful reboot hook (via servo). This will be |
| 352 | invoked if normal reboot failed. |
| 353 | """ |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 354 | logger.debug('reboot %s', host) |
Kuang-che Wu | 2534bab | 2020-10-23 17:37:16 +0800 | [diff] [blame] | 355 | boot_id = None |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 356 | try: |
Kuang-che Wu | 2534bab | 2020-10-23 17:37:16 +0800 | [diff] [blame] | 357 | boot_id = query_dut_boot_id(host) |
Kuang-che Wu | 2ac9a92 | 2020-09-03 16:50:12 +0800 | [diff] [blame] | 358 | |
Kuang-che Wu | 2534bab | 2020-10-23 17:37:16 +0800 | [diff] [blame] | 359 | try: |
| 360 | util.ssh_cmd(host, 'reboot') |
| 361 | except errors.SshConnectionError: |
| 362 | # Depends on timing, ssh may return failure due to broken pipe, which is |
| 363 | # working as intended. Ignore such kind of errors. |
| 364 | pass |
| 365 | |
Kuang-che Wu | 2ac9a92 | 2020-09-03 16:50:12 +0800 | [diff] [blame] | 366 | wait_reboot_done(host, boot_id) |
| 367 | except (errors.SshConnectionError, errors.ExternalError): |
| 368 | if force_reboot_callback and force_reboot_callback(host): |
| 369 | wait_reboot_done(host, boot_id) |
| 370 | return |
| 371 | raise |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 372 | |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 373 | |
| 374 | def wait_reboot_done(host, boot_id): |
Kuang-che Wu | 4fe945b | 2018-03-31 16:46:38 +0800 | [diff] [blame] | 375 | # For dev-mode test image, the reboot time is roughly at least 16 seconds |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 376 | # (dev screen short delay) or more (long delay). |
| 377 | time.sleep(15) |
| 378 | for _ in range(100): |
| 379 | try: |
| 380 | # During boot, DUT does not response and thus ssh may hang a while. So |
| 381 | # set a connect timeout. 3 seconds are enough and 2 are not. It's okay to |
| 382 | # set tight limit because it's inside retry loop. |
| 383 | assert boot_id != query_dut_boot_id(host, connect_timeout=3) |
| 384 | return |
Kuang-che Wu | 5f662e8 | 2019-03-05 11:49:56 +0800 | [diff] [blame] | 385 | except errors.SshConnectionError: |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 386 | logger.debug('reboot not ready? sleep wait 1 sec') |
| 387 | time.sleep(1) |
| 388 | |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 389 | raise errors.ExternalError('reboot failed?') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 390 | |
| 391 | |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 392 | def gs_release_boardpath(board): |
| 393 | """Normalizes board name for gs://chromeos-releases/ |
| 394 | |
| 395 | This follows behavior of PushImage() in chromite/scripts/pushimage.py |
| 396 | Note, only gs://chromeos-releases/ needs normalization, |
| 397 | gs://chromeos-image-archive does not. |
| 398 | |
| 399 | Args: |
| 400 | board: ChromeOS board name |
| 401 | |
| 402 | Returns: |
| 403 | normalized board name |
| 404 | """ |
| 405 | return board.replace('_', '-') |
| 406 | |
| 407 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 408 | def gsutil(*args, **kwargs): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 409 | """gsutil command line wrapper. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 410 | |
| 411 | Args: |
| 412 | args: command line arguments passed to gsutil |
| 413 | kwargs: |
| 414 | ignore_errors: if true, return '' for failures, for example 'gsutil ls' |
| 415 | but the path not found. |
| 416 | |
| 417 | Returns: |
| 418 | stdout of gsutil |
| 419 | |
| 420 | Raises: |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 421 | errors.ExternalError: gsutil failed to run |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 422 | subprocess.CalledProcessError: command failed |
| 423 | """ |
| 424 | stderr_lines = [] |
| 425 | try: |
| 426 | return util.check_output( |
| 427 | gsutil_bin, *args, stderr_callback=stderr_lines.append) |
| 428 | except subprocess.CalledProcessError as e: |
| 429 | stderr = ''.join(stderr_lines) |
| 430 | if re.search(r'ServiceException:.* does not have .*access', stderr): |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 431 | raise errors.ExternalError( |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 432 | 'gsutil failed due to permission. ' + |
| 433 | 'Run "%s config" and follow its instruction. ' % gsutil_bin + |
| 434 | 'Fill any string if it asks for project-id') |
| 435 | if kwargs.get('ignore_errors'): |
| 436 | return '' |
| 437 | raise |
| 438 | except OSError as e: |
| 439 | if e.errno == errno.ENOENT: |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 440 | raise errors.ExternalError( |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 441 | 'Unable to run %s. gsutil is not installed or not in PATH?' % |
| 442 | gsutil_bin) |
| 443 | raise |
| 444 | |
| 445 | |
| 446 | def gsutil_ls(*args, **kwargs): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 447 | """gsutil ls. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 448 | |
| 449 | Args: |
| 450 | args: arguments passed to 'gsutil ls' |
| 451 | kwargs: extra parameters, where |
Kuang-che Wu | 4fe945b | 2018-03-31 16:46:38 +0800 | [diff] [blame] | 452 | ignore_errors: if true, return empty list instead of raising |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 453 | exception, ex. path not found. |
| 454 | |
| 455 | Returns: |
| 456 | list of 'gsutil ls' result. One element for one line of gsutil output. |
| 457 | |
| 458 | Raises: |
| 459 | subprocess.CalledProcessError: gsutil failed, usually means path not found |
| 460 | """ |
| 461 | return gsutil('ls', *args, **kwargs).splitlines() |
| 462 | |
| 463 | |
Kuang-che Wu | 876a538 | 2020-10-27 14:24:58 +0800 | [diff] [blame] | 464 | def gsutil_stat_creation_time(*args, **kwargs): |
| 465 | """Returns the creation time of a file or multiple files. |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 466 | |
| 467 | Args: |
| 468 | args: arguments passed to 'gsutil stat'. |
| 469 | kwargs: extra parameters for gsutil. |
| 470 | |
| 471 | Returns: |
Kuang-che Wu | 876a538 | 2020-10-27 14:24:58 +0800 | [diff] [blame] | 472 | A integer indicates the creation timestamp. |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 473 | |
| 474 | Raises: |
| 475 | subprocess.CalledProcessError: gsutil failed, usually means path not found |
Kuang-che Wu | 876a538 | 2020-10-27 14:24:58 +0800 | [diff] [blame] | 476 | errors.ExternalError: creation time is not found |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 477 | """ |
| 478 | result = -1 |
| 479 | # Currently we believe stat always returns a UTC time, and strptime also |
| 480 | # parses a UTC time by default. |
| 481 | time_format = '%a, %d %b %Y %H:%M:%S GMT' |
| 482 | |
| 483 | for line in gsutil('stat', *args, **kwargs).splitlines(): |
| 484 | if ':' not in line: |
| 485 | continue |
Kuang-che Wu | c89f2a2 | 2019-11-26 15:30:50 +0800 | [diff] [blame] | 486 | key, value = line.split(':', 1) |
| 487 | key, value = key.strip(), value.strip() |
Kuang-che Wu | 876a538 | 2020-10-27 14:24:58 +0800 | [diff] [blame] | 488 | if key != 'Creation time': |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 489 | continue |
| 490 | dt = datetime.datetime.strptime(value, time_format) |
Kuang-che Wu | 72b5a57 | 2019-10-29 20:37:57 +0800 | [diff] [blame] | 491 | unixtime = int(calendar.timegm(dt.utctimetuple())) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 492 | result = max(result, unixtime) |
| 493 | |
| 494 | if result == -1: |
Kuang-che Wu | 876a538 | 2020-10-27 14:24:58 +0800 | [diff] [blame] | 495 | raise errors.ExternalError("didn't find creation time") |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 496 | return result |
| 497 | |
| 498 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 499 | def query_milestone_by_version(board, short_version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 500 | """Query milestone by ChromeOS version number. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 501 | |
| 502 | Args: |
| 503 | board: ChromeOS board name |
| 504 | short_version: ChromeOS version number in short format, ex. 9300.0.0 |
| 505 | |
| 506 | Returns: |
| 507 | ChromeOS milestone number (string). For example, '58' for '9300.0.0'. |
| 508 | None if failed. |
| 509 | """ |
| 510 | path = gs_archive_path.format(board=board) + '/R*-' + short_version |
| 511 | for line in gsutil_ls('-d', path, ignore_errors=True): |
| 512 | m = re.search(r'/R(\d+)-', line) |
| 513 | if not m: |
| 514 | continue |
| 515 | return m.group(1) |
| 516 | |
| 517 | for channel in ['canary', 'dev', 'beta', 'stable']: |
| 518 | path = gs_release_path.format( |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 519 | channel=channel, |
| 520 | boardpath=gs_release_boardpath(board), |
| 521 | short_version=short_version) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 522 | for line in gsutil_ls(path, ignore_errors=True): |
| 523 | m = re.search(r'\bR(\d+)-' + short_version, line) |
| 524 | if not m: |
| 525 | continue |
| 526 | return m.group(1) |
| 527 | |
Zheng-Jie Chang | 4e25a8d | 2020-01-08 16:00:13 +0800 | [diff] [blame] | 528 | logger.debug('unable to query milestone of %s for %s', short_version, board) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 529 | return None |
| 530 | |
| 531 | |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 532 | def list_board_names(chromeos_root): |
| 533 | """List board names. |
| 534 | |
| 535 | Args: |
| 536 | chromeos_root: chromeos tree root |
| 537 | |
| 538 | Returns: |
| 539 | list of board names |
| 540 | """ |
| 541 | # Following logic is simplified from chromite/lib/portage_util.py |
| 542 | cros_list_overlays = os.path.join(chromeos_root, |
| 543 | 'chromite/bin/cros_list_overlays') |
| 544 | overlays = util.check_output(cros_list_overlays).splitlines() |
| 545 | result = set() |
| 546 | for overlay in overlays: |
| 547 | conf_file = os.path.join(overlay, 'metadata', 'layout.conf') |
| 548 | name = None |
| 549 | if os.path.exists(conf_file): |
| 550 | for line in open(conf_file): |
| 551 | m = re.match(r'^repo-name\s*=\s*(\S+)\s*$', line) |
| 552 | if m: |
| 553 | name = m.group(1) |
| 554 | break |
| 555 | |
| 556 | if not name: |
| 557 | name_file = os.path.join(overlay, 'profiles', 'repo_name') |
| 558 | if os.path.exists(name_file): |
Kuang-che Wu | a572349 | 2019-11-25 20:59:34 +0800 | [diff] [blame] | 559 | with open(name_file) as f: |
| 560 | name = f.read().strip() |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 561 | |
| 562 | if name: |
| 563 | name = re.sub(r'-private$', '', name) |
| 564 | result.add(name) |
| 565 | |
| 566 | return list(result) |
| 567 | |
| 568 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 569 | def recognize_version(board, version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 570 | """Recognize ChromeOS version. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 571 | |
| 572 | Args: |
| 573 | board: ChromeOS board name |
| 574 | version: ChromeOS version number in short or full format |
| 575 | |
| 576 | Returns: |
| 577 | (milestone, version in short format) |
| 578 | """ |
| 579 | if is_cros_short_version(version): |
| 580 | milestone = query_milestone_by_version(board, version) |
| 581 | short_version = version |
| 582 | else: |
| 583 | milestone, short_version = version_split(version) |
| 584 | return milestone, short_version |
| 585 | |
| 586 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 587 | def extract_major_version(version): |
| 588 | """Converts a version to its major version. |
| 589 | |
| 590 | Args: |
Kuang-che Wu | 9501f34 | 2019-11-15 17:15:21 +0800 | [diff] [blame] | 591 | version: ChromeOS version number or snapshot version |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 592 | |
| 593 | Returns: |
| 594 | major version number in string format |
| 595 | """ |
| 596 | version = version_to_short(version) |
| 597 | m = re.match(r'^(\d+)\.\d+\.\d+$', version) |
| 598 | return m.group(1) |
| 599 | |
| 600 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 601 | def version_to_short(version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 602 | """Convert ChromeOS version number to short format. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 603 | |
| 604 | Args: |
| 605 | version: ChromeOS version number in short or full format |
| 606 | |
| 607 | Returns: |
| 608 | version number in short format |
| 609 | """ |
| 610 | if is_cros_short_version(version): |
| 611 | return version |
| 612 | _, short_version = version_split(version) |
| 613 | return short_version |
| 614 | |
| 615 | |
| 616 | def version_to_full(board, version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 617 | """Convert ChromeOS version number to full format. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 618 | |
| 619 | Args: |
| 620 | board: ChromeOS board name |
| 621 | version: ChromeOS version number in short or full format |
| 622 | |
| 623 | Returns: |
| 624 | version number in full format |
| 625 | """ |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 626 | if is_cros_snapshot_version(version): |
| 627 | milestone, short_version, _ = snapshot_version_split(version) |
| 628 | return make_cros_full_version(milestone, short_version) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 629 | if is_cros_full_version(version): |
| 630 | return version |
| 631 | milestone = query_milestone_by_version(board, version) |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 632 | if not milestone: |
| 633 | raise errors.ExternalError('incorrect board=%s or version=%s ?' % |
| 634 | (board, version)) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 635 | return make_cros_full_version(milestone, version) |
| 636 | |
| 637 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 638 | def list_snapshots_from_image_archive(board, major_version): |
Kuang-che Wu | 9501f34 | 2019-11-15 17:15:21 +0800 | [diff] [blame] | 639 | """List ChromeOS snapshot image available from gs://chromeos-image-archive. |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 640 | |
| 641 | Args: |
| 642 | board: ChromeOS board |
| 643 | major_version: ChromeOS major version |
| 644 | |
| 645 | Returns: |
| 646 | list of (version, gs_path): |
| 647 | version: Chrome OS snapshot version |
| 648 | gs_path: gs path of test image |
| 649 | """ |
| 650 | |
Zheng-Jie Chang | 43a0841 | 2019-12-05 17:05:45 +0800 | [diff] [blame] | 651 | def extract_snapshot_id(result): |
| 652 | m = re.match(r'^R\d+-\d+\.\d+\.\d+-(\d+)', result[0]) |
| 653 | assert m |
| 654 | return int(m.group(1)) |
| 655 | |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 656 | short_version = '%s.0.0' % major_version |
| 657 | milestone = query_milestone_by_version(board, short_version) |
| 658 | if not milestone: |
| 659 | milestone = '*' |
| 660 | |
Zheng-Jie Chang | 5402083 | 2020-04-21 15:52:48 +0800 | [diff] [blame] | 661 | path = ('gs://chromeos-image-archive/{board}-snapshot/R{milestone}-' |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 662 | '{short_version}-*/image.zip') |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 663 | result = [] |
| 664 | output = gsutil_ls( |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 665 | path.format( |
| 666 | board=board, milestone=milestone, short_version=short_version), |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 667 | ignore_errors=True) |
| 668 | |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 669 | for gs_path in sorted(output): |
| 670 | m = re.match(r'^gs:\S+(R\d+-\d+\.\d+\.\d+-\d+)', gs_path) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 671 | if m: |
| 672 | snapshot_version = m.group(1) |
Zheng-Jie Chang | 43a0841 | 2019-12-05 17:05:45 +0800 | [diff] [blame] | 673 | # we should skip if there is duplicate snapshot |
| 674 | if result and result[-1][0] == snapshot_version: |
| 675 | continue |
Zheng-Jie Chang | 1ae64c3 | 2020-03-10 14:08:28 +0800 | [diff] [blame] | 676 | |
Zheng-Jie Chang | 1ae64c3 | 2020-03-10 14:08:28 +0800 | [diff] [blame] | 677 | _, _, snapshot_id = snapshot_version_split(snapshot_version) |
Zheng-Jie Chang | dbe1f8e | 2021-01-26 12:33:32 +0800 | [diff] [blame^] | 678 | |
| 679 | # crbug/1170601: ignore small snapshot ids |
| 680 | if int(snapshot_id) <= snapshot_cutover_id: |
| 681 | continue |
| 682 | |
| 683 | # b/151054108: snapshot version in [29288, 29439] is broken |
Zheng-Jie Chang | 1ae64c3 | 2020-03-10 14:08:28 +0800 | [diff] [blame] | 684 | if 29288 <= int(snapshot_id) <= 29439: |
| 685 | continue |
| 686 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 687 | result.append((snapshot_version, gs_path)) |
Zheng-Jie Chang | 43a0841 | 2019-12-05 17:05:45 +0800 | [diff] [blame] | 688 | |
| 689 | # sort by its snapshot_id |
| 690 | result.sort(key=extract_snapshot_id) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 691 | return result |
| 692 | |
| 693 | |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 694 | def list_prebuilt_from_image_archive(board): |
| 695 | """Lists ChromeOS prebuilt image available from gs://chromeos-image-archive. |
| 696 | |
| 697 | gs://chromeos-image-archive contains only recent builds (in two years). |
| 698 | We prefer this function to list_prebuilt_from_chromeos_releases() because |
| 699 | - this is what "cros flash" supports directly. |
| 700 | - the paths have milestone information, so we don't need to do slow query |
| 701 | by ourselves. |
| 702 | |
| 703 | Args: |
| 704 | board: ChromeOS board name |
| 705 | |
| 706 | Returns: |
| 707 | list of (version, gs_path): |
| 708 | version: Chrome OS version in full format |
| 709 | gs_path: gs path of test image |
| 710 | """ |
| 711 | result = [] |
| 712 | for line in gsutil_ls(gs_archive_path.format(board=board)): |
| 713 | m = re.match(r'^gs:\S+(R\d+-\d+\.\d+\.\d+)', line) |
| 714 | if m: |
| 715 | full_version = m.group(1) |
| 716 | test_image = 'chromiumos_test_image.tar.xz' |
| 717 | assert line.endswith('/') |
| 718 | gs_path = line + test_image |
| 719 | result.append((full_version, gs_path)) |
| 720 | return result |
| 721 | |
| 722 | |
| 723 | def list_prebuilt_from_chromeos_releases(board): |
| 724 | """Lists ChromeOS versions available from gs://chromeos-releases. |
| 725 | |
| 726 | gs://chromeos-releases contains more builds. However, 'cros flash' doesn't |
| 727 | support it. |
| 728 | |
| 729 | Args: |
| 730 | board: ChromeOS board name |
| 731 | |
| 732 | Returns: |
| 733 | list of (version, gs_path): |
| 734 | version: Chrome OS version in short format |
| 735 | gs_path: gs path of test image (with wildcard) |
| 736 | """ |
| 737 | result = [] |
| 738 | for line in gsutil_ls( |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 739 | gs_release_path.format( |
| 740 | channel='*', boardpath=gs_release_boardpath(board), short_version=''), |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 741 | ignore_errors=True): |
| 742 | m = re.match(r'gs:\S+/(\d+\.\d+\.\d+)/$', line) |
| 743 | if m: |
| 744 | short_version = m.group(1) |
| 745 | test_image = 'ChromeOS-test-R*-{short_version}-{board}.tar.xz'.format( |
| 746 | short_version=short_version, board=board) |
| 747 | gs_path = line + test_image |
| 748 | result.append((short_version, gs_path)) |
| 749 | return result |
| 750 | |
| 751 | |
Kuang-che Wu | e180840 | 2020-01-06 20:27:45 +0800 | [diff] [blame] | 752 | def has_test_image(board, version): |
| 753 | if is_cros_snapshot_version(version): |
| 754 | return bool(query_snapshot_buildbucket_id(board, version)) |
| 755 | |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 756 | try: |
| 757 | full_version = version_to_full(board, version) |
| 758 | except errors.ExternalError: |
| 759 | # version_to_full() is implemented by checking image, thus its failure |
| 760 | # means no image. |
| 761 | return False |
Kuang-che Wu | e180840 | 2020-01-06 20:27:45 +0800 | [diff] [blame] | 762 | short_version = version_to_short(version) |
| 763 | paths = [ |
| 764 | gs_archive_path.format(board=board) + |
| 765 | '/%s/chromiumos_test_image.tar.xz' % full_version, |
| 766 | gs_release_path.format( |
| 767 | channel='*', |
| 768 | boardpath=gs_release_boardpath(board), |
| 769 | short_version=short_version), |
| 770 | ] |
| 771 | |
| 772 | for path in paths: |
| 773 | if gsutil_ls(path, ignore_errors=True): |
| 774 | return True |
| 775 | return False |
| 776 | |
| 777 | |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 778 | def list_chromeos_prebuilt_versions(board, |
| 779 | old, |
| 780 | new, |
| 781 | only_good_build=True, |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 782 | include_older_build=True, |
| 783 | use_snapshot=False): |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 784 | """Lists ChromeOS version numbers with prebuilt between given range |
| 785 | |
| 786 | Args: |
| 787 | board: ChromeOS board name |
| 788 | old: start version (inclusive) |
| 789 | new: end version (inclusive) |
| 790 | only_good_build: only if test image is available |
| 791 | include_older_build: include prebuilt in gs://chromeos-releases |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 792 | use_snapshot: return snapshot versions if found |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 793 | |
| 794 | Returns: |
| 795 | list of sorted version numbers (in full format) between [old, new] range |
| 796 | (inclusive). |
| 797 | """ |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 798 | old_short = version_to_short(old) |
| 799 | new_short = version_to_short(new) |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 800 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 801 | rev_map = { |
| 802 | } # dict: short version -> list of (short/full or snapshot version, gs path) |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 803 | for full_version, gs_path in list_prebuilt_from_image_archive(board): |
| 804 | short_version = version_to_short(full_version) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 805 | rev_map[short_version] = [(full_version, gs_path)] |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 806 | |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 807 | if include_older_build and old_short not in rev_map: |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 808 | for short_version, gs_path in list_prebuilt_from_chromeos_releases(board): |
| 809 | if short_version not in rev_map: |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 810 | rev_map[short_version] = [(short_version, gs_path)] |
| 811 | |
| 812 | 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 Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 817 | next_short_version = '%s.0.0' % (major_version + 1) |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 818 | # 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 Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 822 | |
| 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 Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 828 | if not util.is_direct_relative_version(next_short_version, old_short): |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 829 | continue |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 830 | if not util.is_direct_relative_version(next_short_version, new_short): |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 831 | continue |
| 832 | # 2) If 12345.0.0(short_version) is a version between old and new |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 833 | if not util.is_direct_relative_version(short_version, old_short): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 834 | continue |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 835 | if not util.is_direct_relative_version(short_version, new_short): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 836 | continue |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 837 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 838 | 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 Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 843 | if short_version not in rev_map: |
| 844 | rev_map[short_version] = [] |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 845 | rev_map[short_version] += snapshots |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 846 | |
| 847 | result = [] |
| 848 | for rev in sorted(rev_map, key=util.version_key_func): |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 849 | if not util.is_direct_relative_version(new_short, rev): |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 850 | continue |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 851 | if not util.is_version_lesseq(old_short, rev): |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 852 | continue |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 853 | if not util.is_version_lesseq(rev, new_short): |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 854 | continue |
| 855 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 856 | for version, gs_path in rev_map[rev]: |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 857 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 858 | # 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 Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 860 | |
Zheng-Jie Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 861 | if only_good_build and not is_cros_snapshot_version(version): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 862 | 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 Chang | a331b87 | 2020-01-06 17:09:32 +0800 | [diff] [blame] | 871 | version = m.group(1) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 872 | elif is_cros_short_version(version): |
| 873 | version = version_to_full(board, version) |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 874 | |
Zheng-Jie Chang | aea4fba | 2020-02-17 17:12:09 +0800 | [diff] [blame] | 875 | if is_cros_version_lesseq(old, version) and is_cros_version_lesseq( |
| 876 | version, new): |
| 877 | result.append(version) |
Kuang-che Wu | 575dc44 | 2019-03-05 10:30:55 +0800 | [diff] [blame] | 878 | |
| 879 | return result |
| 880 | |
| 881 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 882 | def prepare_snapshot_image(chromeos_root, board, snapshot_version): |
| 883 | """Prepare chromeos snapshot image. |
| 884 | |
| 885 | Args: |
| 886 | chromeos_root: chromeos tree root |
| 887 | board: ChromeOS board name |
| 888 | snapshot_version: ChromeOS snapshot version number |
| 889 | |
| 890 | Returns: |
| 891 | local file path of test image relative to chromeos_root |
| 892 | """ |
| 893 | assert is_cros_snapshot_version(snapshot_version) |
| 894 | milestone, short_version, snapshot_id = snapshot_version_split( |
| 895 | snapshot_version) |
| 896 | full_version = make_cros_full_version(milestone, short_version) |
| 897 | tmp_dir = os.path.join( |
| 898 | chromeos_root, 'tmp', |
| 899 | 'ChromeOS-test-%s-%s-%s' % (full_version, board, snapshot_id)) |
| 900 | if not os.path.exists(tmp_dir): |
| 901 | os.makedirs(tmp_dir) |
| 902 | |
Zheng-Jie Chang | 5402083 | 2020-04-21 15:52:48 +0800 | [diff] [blame] | 903 | gs_path = ('gs://chromeos-image-archive/{board}-snapshot/' + |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 904 | '{snapshot_version}-*/image.zip') |
| 905 | gs_path = gs_path.format(board=board, snapshot_version=snapshot_version) |
| 906 | |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 907 | full_path = os.path.join(tmp_dir, test_image_filename) |
| 908 | rel_path = os.path.relpath(full_path, chromeos_root) |
| 909 | if os.path.exists(full_path): |
| 910 | return rel_path |
| 911 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 912 | files = gsutil_ls(gs_path, ignore_errors=True) |
Zheng-Jie Chang | eb7308f | 2019-12-05 14:25:05 +0800 | [diff] [blame] | 913 | if len(files) >= 1: |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 914 | gs_path = files[0] |
| 915 | gsutil('cp', gs_path, tmp_dir) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 916 | util.check_call( |
| 917 | 'unzip', '-j', 'image.zip', test_image_filename, cwd=tmp_dir) |
| 918 | os.remove(os.path.join(tmp_dir, 'image.zip')) |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 919 | return rel_path |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 920 | |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 921 | assert False |
Kuang-che Wu | a7ddf9b | 2019-11-25 18:59:57 +0800 | [diff] [blame] | 922 | return None |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 923 | |
| 924 | |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 925 | def prepare_prebuilt_image(chromeos_root, board, version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 926 | """Prepare chromeos prebuilt image. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 927 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 928 | It searches for xbuddy image which "cros flash" can use, or fetch image to |
| 929 | local disk. |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 930 | |
| 931 | Args: |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 932 | chromeos_root: chromeos tree root |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 933 | board: ChromeOS board name |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 934 | version: ChromeOS version number in short or full format |
| 935 | |
| 936 | Returns: |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 937 | xbuddy path or file path (relative to chromeos_root) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 938 | """ |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 939 | assert is_cros_version(version) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 940 | full_version = version_to_full(board, version) |
| 941 | short_version = version_to_short(full_version) |
| 942 | |
| 943 | image_path = None |
| 944 | gs_path = gs_archive_path.format(board=board) + '/' + full_version |
| 945 | if gsutil_ls('-d', gs_path, ignore_errors=True): |
| 946 | image_path = 'xbuddy://remote/{board}/{full_version}/test'.format( |
| 947 | board=board, full_version=full_version) |
| 948 | else: |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 949 | tmp_dir = os.path.join(chromeos_root, 'tmp', |
| 950 | 'ChromeOS-test-%s-%s' % (full_version, board)) |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 951 | full_path = os.path.join(tmp_dir, test_image_filename) |
| 952 | rel_path = os.path.relpath(full_path, chromeos_root) |
| 953 | if os.path.exists(full_path): |
| 954 | return rel_path |
| 955 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 956 | if not os.path.exists(tmp_dir): |
| 957 | os.makedirs(tmp_dir) |
| 958 | # gs://chromeos-releases may have more old images than |
Kuang-che Wu | 4fe945b | 2018-03-31 16:46:38 +0800 | [diff] [blame] | 959 | # gs://chromeos-image-archive, but 'cros flash' doesn't support it. We have |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 960 | # to fetch the image by ourselves |
| 961 | for channel in ['canary', 'dev', 'beta', 'stable']: |
| 962 | fn = 'ChromeOS-test-{full_version}-{board}.tar.xz'.format( |
| 963 | full_version=full_version, board=board) |
| 964 | gs_path = gs_release_path.format( |
Kuang-che Wu | 80bf6a5 | 2019-05-31 12:48:06 +0800 | [diff] [blame] | 965 | channel=channel, |
| 966 | boardpath=gs_release_boardpath(board), |
| 967 | short_version=short_version) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 968 | gs_path += '/' + fn |
| 969 | if gsutil_ls(gs_path, ignore_errors=True): |
| 970 | # TODO(kcwu): delete tmp |
| 971 | gsutil('cp', gs_path, tmp_dir) |
| 972 | util.check_call('tar', 'Jxvf', fn, cwd=tmp_dir) |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 973 | image_path = os.path.relpath(full_path, chromeos_root) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 974 | break |
| 975 | |
| 976 | assert image_path |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 977 | return image_path |
| 978 | |
| 979 | |
| 980 | def cros_flash(chromeos_root, |
| 981 | host, |
| 982 | board, |
| 983 | image_path, |
| 984 | version=None, |
| 985 | clobber_stateful=False, |
Kuang-che Wu | 2ac9a92 | 2020-09-03 16:50:12 +0800 | [diff] [blame] | 986 | disable_rootfs_verification=True, |
| 987 | force_reboot_callback=None): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 988 | """Flash a DUT with given ChromeOS image. |
| 989 | |
| 990 | This is implemented by 'cros flash' command line. |
| 991 | |
| 992 | Args: |
| 993 | chromeos_root: use 'cros flash' of which chromeos tree |
| 994 | host: DUT address |
| 995 | board: ChromeOS board name |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 996 | image_path: chromeos image xbuddy path or file path. For relative |
| 997 | path, it should be relative to chromeos_root. |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 998 | version: ChromeOS version in short or full format |
| 999 | clobber_stateful: Clobber stateful partition when performing update |
| 1000 | disable_rootfs_verification: Disable rootfs verification after update |
| 1001 | is completed |
Kuang-che Wu | 2ac9a92 | 2020-09-03 16:50:12 +0800 | [diff] [blame] | 1002 | force_reboot_callback: powerful reboot hook (via servo) |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 1003 | |
| 1004 | Raises: |
| 1005 | errors.ExternalError: cros flash failed |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1006 | """ |
| 1007 | logger.info('cros_flash %s %s %s %s', host, board, version, image_path) |
| 1008 | |
| 1009 | # Reboot is necessary because sometimes previous 'cros flash' failed and |
| 1010 | # entered a bad state. |
Kuang-che Wu | 2ac9a92 | 2020-09-03 16:50:12 +0800 | [diff] [blame] | 1011 | reboot(host, force_reboot_callback=force_reboot_callback) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 1012 | |
Kuang-che Wu | 020a118 | 2020-09-08 17:17:22 +0800 | [diff] [blame] | 1013 | # Stop service ap-update-manager to prevent rebooting during auto update. |
Kuang-che Wu | 2e0680b | 2020-08-19 22:41:28 +0800 | [diff] [blame] | 1014 | # The service is used in jetstream boards, but not other CrOS devices. |
| 1015 | if query_dut_os_release(host).get('GOOGLE_CRASH_ID') == 'Jetstream': |
| 1016 | try: |
Kuang-che Wu | 29e2eaf | 2020-08-21 16:51:26 +0800 | [diff] [blame] | 1017 | # Sleep to wait ap-update-manager start, which may take up to 27 seconds. |
| 1018 | # For simplicity, we wait 60 seconds here, which is the timeout value of |
| 1019 | # jetstream_host. |
| 1020 | # https://chromium.googlesource.com/chromiumos/third_party/autotest |
Kuang-che Wu | ebc2c36 | 2020-12-14 16:27:09 +0800 | [diff] [blame] | 1021 | # /+/HEAD/server/hosts/jetstream_host.py#27 |
Kuang-che Wu | 29e2eaf | 2020-08-21 16:51:26 +0800 | [diff] [blame] | 1022 | time.sleep(60) |
Kuang-che Wu | 2e0680b | 2020-08-19 22:41:28 +0800 | [diff] [blame] | 1023 | util.ssh_cmd(host, 'stop', 'ap-update-manager') |
| 1024 | except subprocess.CalledProcessError: |
| 1025 | pass # not started; do nothing |
| 1026 | |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1027 | # Handle relative path. |
| 1028 | if '://' not in image_path and not os.path.isabs(image_path): |
| 1029 | assert os.path.exists(os.path.join(chromeos_root, image_path)) |
| 1030 | image_path = os.path.join(chromeos_root_inside_chroot, image_path) |
| 1031 | |
Kuang-che Wu | f3d03ca | 2019-03-11 17:31:40 +0800 | [diff] [blame] | 1032 | args = [ |
Kuang-che Wu | 8a83c9b | 2021-01-11 14:52:15 +0800 | [diff] [blame] | 1033 | '--debug', |
| 1034 | '--no-ping', |
| 1035 | # Speed up for slow network connection. |
| 1036 | '--send-payload-in-parallel', |
| 1037 | host, |
| 1038 | image_path, |
Kuang-che Wu | f3d03ca | 2019-03-11 17:31:40 +0800 | [diff] [blame] | 1039 | ] |
Kuang-che Wu | 8a83c9b | 2021-01-11 14:52:15 +0800 | [diff] [blame] | 1040 | # TODO(kcwu): remove this check if we don't need to support chromeos versions |
| 1041 | # earlier than Dec 2020. |
| 1042 | if git_util.is_ancestor_commit( |
| 1043 | os.path.join(chromeos_root, 'chromite'), '9ed30bc3ed292b', 'HEAD'): |
| 1044 | # To reduce disk usage on DUT. |
| 1045 | args.append('--no-copy-payloads-to-device') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 1046 | if clobber_stateful: |
| 1047 | args.append('--clobber-stateful') |
| 1048 | if disable_rootfs_verification: |
| 1049 | args.append('--disable-rootfs-verification') |
| 1050 | |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 1051 | try: |
| 1052 | cros_sdk(chromeos_root, 'cros', 'flash', *args) |
Kuang-che Wu | 6d91b8c | 2020-11-24 20:14:35 +0800 | [diff] [blame] | 1053 | except subprocess.CalledProcessError as e: |
| 1054 | raise errors.ExternalError('cros flash failed') from e |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 1055 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1056 | if version: |
| 1057 | # In the past, cros flash may fail with returncode=0 |
| 1058 | # So let's have an extra check. |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1059 | if is_cros_snapshot_version(version): |
| 1060 | builder_path = query_dut_lsb_release(host).get( |
| 1061 | 'CHROMEOS_RELEASE_BUILDER_PATH', '') |
Zheng-Jie Chang | 5402083 | 2020-04-21 15:52:48 +0800 | [diff] [blame] | 1062 | expect_prefix = '%s-snapshot/%s-' % (board, version) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1063 | if not builder_path.startswith(expect_prefix): |
| 1064 | raise errors.ExternalError( |
| 1065 | 'although cros flash succeeded, the OS builder path is ' |
| 1066 | 'unexpected: actual=%s expect=%s' % (builder_path, expect_prefix)) |
| 1067 | else: |
| 1068 | expect_version = version_to_short(version) |
| 1069 | dut_version = query_dut_short_version(host) |
| 1070 | if dut_version != expect_version: |
| 1071 | raise errors.ExternalError( |
| 1072 | 'although cros flash succeeded, the OS version is unexpected: ' |
| 1073 | 'actual=%s expect=%s' % (dut_version, expect_version)) |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 1074 | |
Kuang-che Wu | 4a81ea7 | 2019-10-05 15:35:17 +0800 | [diff] [blame] | 1075 | # "cros flash" may terminate successfully but the DUT starts self-repairing |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 1076 | # (b/130786578), so it's necessary to do sanity check. |
| 1077 | if not is_good_dut(host): |
| 1078 | raise errors.ExternalError( |
| 1079 | 'although cros flash succeeded, the DUT is in bad state') |
| 1080 | |
| 1081 | |
| 1082 | def cros_flash_with_retry(chromeos_root, |
| 1083 | host, |
| 1084 | board, |
| 1085 | image_path, |
| 1086 | version=None, |
| 1087 | clobber_stateful=False, |
| 1088 | disable_rootfs_verification=True, |
Kuang-che Wu | 2ac9a92 | 2020-09-03 16:50:12 +0800 | [diff] [blame] | 1089 | repair_callback=None, |
| 1090 | force_reboot_callback=None): |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 1091 | # 'cros flash' is not 100% reliable, retry if necessary. |
| 1092 | for attempt in range(2): |
| 1093 | if attempt > 0: |
| 1094 | logger.info('will retry 60 seconds later') |
| 1095 | time.sleep(60) |
| 1096 | |
| 1097 | try: |
| 1098 | cros_flash( |
| 1099 | chromeos_root, |
| 1100 | host, |
| 1101 | board, |
| 1102 | image_path, |
| 1103 | version=version, |
| 1104 | clobber_stateful=clobber_stateful, |
Kuang-che Wu | 2ac9a92 | 2020-09-03 16:50:12 +0800 | [diff] [blame] | 1105 | disable_rootfs_verification=disable_rootfs_verification, |
| 1106 | force_reboot_callback=force_reboot_callback) |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 1107 | return True |
| 1108 | except errors.ExternalError: |
| 1109 | logger.exception('cros flash failed') |
| 1110 | if repair_callback and not repair_callback(host): |
| 1111 | logger.warning('not repaired, assume it is harmless') |
| 1112 | continue |
| 1113 | return False |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 1114 | |
| 1115 | |
| 1116 | def version_info(board, version): |
| 1117 | """Query subcomponents version info of given version of ChromeOS |
| 1118 | |
| 1119 | Args: |
| 1120 | board: ChromeOS board name |
| 1121 | version: ChromeOS version number in short or full format |
| 1122 | |
| 1123 | Returns: |
| 1124 | dict of component and version info, including (if available): |
| 1125 | cros_short_version: ChromeOS version |
| 1126 | cros_full_version: ChromeOS version |
| 1127 | milestone: milestone of ChromeOS |
| 1128 | cr_version: Chrome version |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 1129 | android_build_id: Android build id |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 1130 | android_branch: Android branch, in format like 'git_nyc-mr1-arc' |
| 1131 | """ |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1132 | if is_cros_snapshot_version(version): |
Zheng-Jie Chang | 2b6d147 | 2019-11-13 12:40:17 +0800 | [diff] [blame] | 1133 | api = buildbucket_util.BuildbucketApi() |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 1134 | milestone, short_version, _ = snapshot_version_split(version) |
| 1135 | buildbucket_id = query_snapshot_buildbucket_id(board, version) |
Zheng-Jie Chang | 597c38b | 2020-03-12 22:24:10 +0800 | [diff] [blame] | 1136 | data = api.get_build(int(buildbucket_id)).output.properties |
Zheng-Jie Chang | 4fabff6 | 2019-12-08 21:54:35 +0800 | [diff] [blame] | 1137 | target_versions = json_format.MessageToDict(data['target_versions']) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1138 | return { |
Zheng-Jie Chang | b869704 | 2019-10-29 16:03:26 +0800 | [diff] [blame] | 1139 | VERSION_KEY_MILESTONE: milestone, |
| 1140 | VERSION_KEY_CROS_FULL_VERSION: version, |
| 1141 | VERSION_KEY_CROS_SHORT_VERSION: short_version, |
Zheng-Jie Chang | 2d1dd9b | 2019-12-07 23:30:20 +0800 | [diff] [blame] | 1142 | VERSION_KEY_CR_VERSION: target_versions.get('chromeVersion'), |
| 1143 | VERSION_KEY_ANDROID_BUILD_ID: target_versions.get('androidVersion'), |
| 1144 | VERSION_KEY_ANDROID_BRANCH: target_versions.get('androidBranchVersion'), |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1145 | } |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 1146 | info = {} |
| 1147 | full_version = version_to_full(board, version) |
| 1148 | |
| 1149 | # Some boards may have only partial-metadata.json but no metadata.json. |
| 1150 | # e.g. caroline R60-9462.0.0 |
| 1151 | # Let's try both. |
| 1152 | metadata = None |
| 1153 | for metadata_filename in ['metadata.json', 'partial-metadata.json']: |
Kuang-che Wu | 0768b97 | 2019-10-05 15:18:59 +0800 | [diff] [blame] | 1154 | path = gs_archive_path.format( |
| 1155 | board=board) + '/%s/%s' % (full_version, metadata_filename) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 1156 | metadata = gsutil('cat', path, ignore_errors=True) |
| 1157 | if metadata: |
| 1158 | o = json.loads(metadata) |
| 1159 | v = o['version'] |
| 1160 | board_metadata = o['board-metadata'][board] |
| 1161 | info.update({ |
| 1162 | VERSION_KEY_CROS_SHORT_VERSION: v['platform'], |
| 1163 | VERSION_KEY_CROS_FULL_VERSION: v['full'], |
| 1164 | VERSION_KEY_MILESTONE: v['milestone'], |
| 1165 | VERSION_KEY_CR_VERSION: v['chrome'], |
| 1166 | }) |
| 1167 | |
| 1168 | if 'android' in v: |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 1169 | info[VERSION_KEY_ANDROID_BUILD_ID] = v['android'] |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 1170 | if 'android-branch' in v: # this appears since R58-9317.0.0 |
| 1171 | info[VERSION_KEY_ANDROID_BRANCH] = v['android-branch'] |
| 1172 | elif 'android-container-branch' in board_metadata: |
| 1173 | info[VERSION_KEY_ANDROID_BRANCH] = v['android-container-branch'] |
| 1174 | break |
| 1175 | else: |
| 1176 | logger.error('Failed to read metadata from gs://chromeos-image-archive') |
| 1177 | logger.error( |
| 1178 | 'Note, so far no quick way to look up version info for too old builds') |
| 1179 | |
| 1180 | return info |
Kuang-che Wu | 848b1af | 2018-02-01 20:59:36 +0800 | [diff] [blame] | 1181 | |
| 1182 | |
| 1183 | def query_chrome_version(board, version): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1184 | """Queries chrome version of chromeos build. |
Kuang-che Wu | 848b1af | 2018-02-01 20:59:36 +0800 | [diff] [blame] | 1185 | |
| 1186 | Args: |
| 1187 | board: ChromeOS board name |
| 1188 | version: ChromeOS version number in short or full format |
| 1189 | |
| 1190 | Returns: |
| 1191 | Chrome version number |
| 1192 | """ |
| 1193 | info = version_info(board, version) |
| 1194 | return info['cr_version'] |
Kuang-che Wu | 708310b | 2018-03-28 17:24:34 +0800 | [diff] [blame] | 1195 | |
| 1196 | |
| 1197 | def query_android_build_id(board, rev): |
| 1198 | info = version_info(board, rev) |
| 1199 | rev = info['android_build_id'] |
| 1200 | return rev |
| 1201 | |
| 1202 | |
| 1203 | def query_android_branch(board, rev): |
| 1204 | info = version_info(board, rev) |
| 1205 | rev = info['android_branch'] |
| 1206 | return rev |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1207 | |
| 1208 | |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 1209 | def guess_chrome_version(board, rev): |
| 1210 | """Guess chrome version number. |
| 1211 | |
| 1212 | Args: |
| 1213 | board: chromeos board name |
| 1214 | rev: chrome or chromeos version |
| 1215 | |
| 1216 | Returns: |
| 1217 | chrome version number |
| 1218 | """ |
| 1219 | if is_cros_version(rev): |
| 1220 | assert board, 'need to specify BOARD for cros version' |
| 1221 | rev = query_chrome_version(board, rev) |
| 1222 | assert cr_util.is_chrome_version(rev) |
| 1223 | |
| 1224 | return rev |
| 1225 | |
| 1226 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1227 | def is_inside_chroot(): |
| 1228 | """Returns True if we are inside chroot.""" |
| 1229 | return os.path.exists('/etc/cros_chroot_version') |
| 1230 | |
| 1231 | |
Kuang-che Wu | ad5ac7b | 2020-02-20 19:02:52 +0800 | [diff] [blame] | 1232 | def convert_path_outside_chroot(chromeos_root, path): |
| 1233 | """Converts path in chroot to outside. |
| 1234 | |
| 1235 | Args: |
| 1236 | chromeos_root: chromeos tree root |
| 1237 | path: path inside chroot; support starting with '~/' |
| 1238 | |
| 1239 | Returns: |
| 1240 | The corresponding path outside chroot assuming the chroot is mounted |
| 1241 | """ |
| 1242 | if path.startswith('~/'): |
| 1243 | path = path.replace('~', '/home/' + os.environ['USER']) |
| 1244 | assert '~' not in path, 'tilde (~) character is not fully supported' |
| 1245 | |
| 1246 | assert os.path.isabs(path) |
| 1247 | assert path[0] == os.sep |
| 1248 | return os.path.join(chromeos_root, 'chroot', path[1:]) |
| 1249 | |
| 1250 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1251 | def cros_sdk(chromeos_root, *args, **kwargs): |
| 1252 | """Run commands inside chromeos chroot. |
| 1253 | |
| 1254 | Args: |
| 1255 | chromeos_root: chromeos tree root |
| 1256 | *args: command to run |
| 1257 | **kwargs: |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 1258 | chrome_root: pass to cros_sdk; mount this path into the SDK chroot |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1259 | env: (dict) environment variables for the command |
Kuang-che Wu | bcafc55 | 2019-08-15 15:27:02 +0800 | [diff] [blame] | 1260 | log_stdout: Whether write the stdout output of the child process to log. |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1261 | stdin: standard input file handle for the command |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1262 | stderr_callback: Callback function for stderr. Called once per line. |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1263 | goma_dir: Goma installed directory to mount into the chroot |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1264 | """ |
| 1265 | envs = [] |
| 1266 | for k, v in kwargs.get('env', {}).items(): |
| 1267 | assert re.match(r'^[A-Za-z_][A-Za-z0-9_]*$', k) |
| 1268 | envs.append('%s=%s' % (k, v)) |
| 1269 | |
| 1270 | # Use --no-ns-pid to prevent cros_sdk change our pgid, otherwise subsequent |
| 1271 | # commands would be considered as background process. |
Kuang-che Wu | 399d466 | 2019-06-06 15:23:37 +0800 | [diff] [blame] | 1272 | prefix = ['chromite/bin/cros_sdk', '--no-ns-pid'] |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 1273 | |
| 1274 | if kwargs.get('chrome_root'): |
Kuang-che Wu | 399d466 | 2019-06-06 15:23:37 +0800 | [diff] [blame] | 1275 | prefix += ['--chrome_root', kwargs['chrome_root']] |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1276 | if kwargs.get('goma_dir'): |
| 1277 | prefix += ['--goma_dir', kwargs['goma_dir']] |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 1278 | |
Kuang-che Wu | 399d466 | 2019-06-06 15:23:37 +0800 | [diff] [blame] | 1279 | prefix += envs + ['--'] |
Kuang-che Wu | d4603d7 | 2018-11-29 17:51:21 +0800 | [diff] [blame] | 1280 | |
Kuang-che Wu | 399d466 | 2019-06-06 15:23:37 +0800 | [diff] [blame] | 1281 | # In addition to the output of command we are interested, cros_sdk may |
| 1282 | # generate its own messages. For example, chroot creation messages if we run |
| 1283 | # cros_sdk the first time. |
| 1284 | # This is the hack to run dummy command once, so we can get clean output for |
| 1285 | # the command we are interested. |
| 1286 | cmd = prefix + ['true'] |
Kuang-che Wu | 6267701 | 2020-07-13 14:25:18 +0800 | [diff] [blame] | 1287 | try: |
| 1288 | util.check_call(*cmd, cwd=chromeos_root) |
| 1289 | except subprocess.CalledProcessError: |
| 1290 | logger.exception('cros_sdk init/update failed') |
| 1291 | raise |
Kuang-che Wu | 399d466 | 2019-06-06 15:23:37 +0800 | [diff] [blame] | 1292 | |
| 1293 | cmd = prefix + list(args) |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1294 | return util.check_output( |
| 1295 | *cmd, |
| 1296 | cwd=chromeos_root, |
Kuang-che Wu | bcafc55 | 2019-08-15 15:27:02 +0800 | [diff] [blame] | 1297 | log_stdout=kwargs.get('log_stdout', True), |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1298 | stdin=kwargs.get('stdin'), |
| 1299 | stderr_callback=kwargs.get('stderr_callback')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1300 | |
| 1301 | |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1302 | def create_chroot(chromeos_root): |
Kuang-che Wu | ad5ac7b | 2020-02-20 19:02:52 +0800 | [diff] [blame] | 1303 | """Creates ChromeOS chroot if necessary. |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1304 | |
| 1305 | Args: |
| 1306 | chromeos_root: chromeos tree root |
| 1307 | """ |
| 1308 | if os.path.exists(os.path.join(chromeos_root, 'chroot')): |
| 1309 | return |
| 1310 | if os.path.exists(os.path.join(chromeos_root, 'chroot.img')): |
| 1311 | return |
| 1312 | |
| 1313 | util.check_output('chromite/bin/cros_sdk', '--create', cwd=chromeos_root) |
| 1314 | |
| 1315 | |
Kuang-che Wu | ad5ac7b | 2020-02-20 19:02:52 +0800 | [diff] [blame] | 1316 | def mount_chroot(chromeos_root): |
| 1317 | """Creates ChromeOS chroot if necessary. |
| 1318 | |
| 1319 | Args: |
| 1320 | chromeos_root: chromeos tree root |
| 1321 | """ |
| 1322 | # An arbitrary file must exist in chroot. |
| 1323 | path = convert_path_outside_chroot(chromeos_root, '/bin/ls') |
| 1324 | |
| 1325 | # Not created or mounted yet. |
| 1326 | if not os.path.exists(path): |
| 1327 | create_chroot(chromeos_root) |
| 1328 | # After this command, the chroot is mounted. |
| 1329 | cros_sdk(chromeos_root, 'true') |
| 1330 | assert os.path.exists(path) |
| 1331 | |
| 1332 | |
| 1333 | def copy_into_chroot(chromeos_root, src, dst, overwrite=True): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1334 | """Copies file into chromeos chroot. |
| 1335 | |
Kuang-che Wu | ad5ac7b | 2020-02-20 19:02:52 +0800 | [diff] [blame] | 1336 | The side effect is chroot created and mounted. |
| 1337 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1338 | Args: |
| 1339 | chromeos_root: chromeos tree root |
| 1340 | src: path outside chroot |
| 1341 | dst: path inside chroot |
Kuang-che Wu | 020a118 | 2020-09-08 17:17:22 +0800 | [diff] [blame] | 1342 | overwrite: overwrite if dst already exists |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1343 | """ |
Kuang-che Wu | ad5ac7b | 2020-02-20 19:02:52 +0800 | [diff] [blame] | 1344 | mount_chroot(chromeos_root) |
| 1345 | src = os.path.expanduser(src) |
| 1346 | dst_outside = convert_path_outside_chroot(chromeos_root, dst) |
| 1347 | if not overwrite and os.path.exists(dst_outside): |
| 1348 | return |
| 1349 | |
| 1350 | # Haven't support directory or special files yet. |
| 1351 | assert os.path.isfile(src) |
| 1352 | assert os.path.isfile(dst_outside) or not os.path.exists(dst_outside) |
| 1353 | |
| 1354 | dirname = os.path.dirname(dst_outside) |
| 1355 | if not os.path.exists(dirname): |
| 1356 | os.makedirs(dirname) |
| 1357 | shutil.copy(src, dst_outside) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1358 | |
| 1359 | |
Kuang-che Wu | ad5ac7b | 2020-02-20 19:02:52 +0800 | [diff] [blame] | 1360 | def prepare_chroot(chromeos_root): |
| 1361 | mount_chroot(chromeos_root) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1362 | |
Kuang-che Wu | ad5ac7b | 2020-02-20 19:02:52 +0800 | [diff] [blame] | 1363 | # Work around b/149077936: |
| 1364 | # The creds file is copied into the chroot since 12866.0.0. |
| 1365 | # But earlier versions need this file as well because of cipd ACL change. |
| 1366 | creds_path = '~/.config/chrome_infra/auth/creds.json' |
| 1367 | if os.path.exists(os.path.expanduser(creds_path)): |
| 1368 | copy_into_chroot(chromeos_root, creds_path, creds_path, overwrite=False) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1369 | |
| 1370 | |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1371 | def check_if_need_recreate_chroot(stdout, stderr): |
| 1372 | """Analyze build log and determine if chroot should be recreated. |
| 1373 | |
| 1374 | Args: |
| 1375 | stdout: stdout output of build |
| 1376 | stderr: stderr output of build |
| 1377 | |
| 1378 | Returns: |
| 1379 | the reason if chroot needs recreated; None otherwise |
| 1380 | """ |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 1381 | if re.search( |
| 1382 | r"The current version of portage supports EAPI '\d+'. " |
Kuang-che Wu | ae6824b | 2019-08-27 22:20:01 +0800 | [diff] [blame] | 1383 | 'You must upgrade', stderr): |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1384 | return 'EAPI version mismatch' |
| 1385 | |
Kuang-che Wu | 5ac8132 | 2018-11-26 14:04:06 +0800 | [diff] [blame] | 1386 | if 'Chroot is too new. Consider running:' in stderr: |
| 1387 | return 'chroot version is too new' |
| 1388 | |
| 1389 | # old message before Oct 2018 |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1390 | if 'Chroot version is too new. Consider running cros_sdk --replace' in stderr: |
| 1391 | return 'chroot version is too new' |
| 1392 | |
Kuang-che Wu | 6fe987f | 2018-08-28 15:24:20 +0800 | [diff] [blame] | 1393 | # https://groups.google.com/a/chromium.org/forum/#!msg/chromium-os-dev/uzwT5APspB4/NFakFyCIDwAJ |
| 1394 | if "undefined reference to 'std::__1::basic_string" in stdout: |
| 1395 | return 'might be due to compiler change' |
| 1396 | |
Kuang-che Wu | 94e3b45 | 2019-11-21 12:49:18 +0800 | [diff] [blame] | 1397 | # Detect failures due to file collisions. |
| 1398 | # For example, kernel uprev from 3.x to 4.x, they are two separate packages |
| 1399 | # and conflict with each other. Other possible cases are package renaming or |
| 1400 | # refactoring. Let's recreate chroot to work around them. |
| 1401 | if 'Detected file collision' in stdout: |
| 1402 | # Using wildcard between words because the text wraps to the next line |
| 1403 | # depending on length of package name and each line is prefixed with |
| 1404 | # package name. |
| 1405 | # Using ".{,100}" instead of ".*" to prevent regex matching time explodes |
| 1406 | # exponentially. 100 is chosen arbitrarily. It should be longer than any |
| 1407 | # package name (65 now). |
| 1408 | m = re.search( |
| 1409 | r'Package (\S+).{,100}NOT.{,100}merged.{,100}' |
| 1410 | r'due.{,100}to.{,100}file.{,100}collisions', stdout, re.S) |
| 1411 | if m: |
| 1412 | return 'failed to install package due to file collision: ' + m.group(1) |
Kuang-che Wu | 356c352 | 2019-11-19 16:11:05 +0800 | [diff] [blame] | 1413 | |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1414 | return None |
| 1415 | |
| 1416 | |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1417 | def build_packages(chromeos_root, |
| 1418 | board, |
| 1419 | chrome_root=None, |
| 1420 | goma_dir=None, |
| 1421 | afdo_use=False): |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1422 | """Build ChromeOS packages. |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1423 | |
| 1424 | Args: |
| 1425 | chromeos_root: chromeos tree root |
| 1426 | board: ChromeOS board name |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1427 | chrome_root: Chrome tree root. If specified, build chrome using the |
| 1428 | provided tree |
| 1429 | goma_dir: Goma installed directory to mount into the chroot. If specified, |
| 1430 | build chrome with goma. |
| 1431 | afdo_use: build chrome with AFDO optimization |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1432 | """ |
Zheng-Jie Chang | ffd4946 | 2019-12-16 12:15:18 +0800 | [diff] [blame] | 1433 | |
| 1434 | def has_build_package_argument(argument): |
| 1435 | stderr_lines = [] |
| 1436 | try: |
| 1437 | util.check_call( |
| 1438 | 'src/scripts/build_packages', |
| 1439 | '--help', |
| 1440 | cwd=chromeos_root, |
| 1441 | stderr_callback=stderr_lines.append) |
| 1442 | except subprocess.CalledProcessError: |
| 1443 | help_output = ''.join(stderr_lines) |
| 1444 | return '--[no]%s' % argument in help_output |
| 1445 | |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1446 | common_env = { |
| 1447 | 'USE': '-cros-debug chrome_internal', |
| 1448 | 'FEATURES': 'separatedebug', |
| 1449 | } |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1450 | stderr_lines = [] |
| 1451 | try: |
Kuang-che Wu | fb55310 | 2018-10-02 18:14:29 +0800 | [diff] [blame] | 1452 | with locking.lock_file(locking.LOCK_FILE_FOR_BUILD): |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1453 | env = common_env.copy() |
| 1454 | env['FEATURES'] += ' -separatedebug splitdebug' |
Kuang-che Wu | fb55310 | 2018-10-02 18:14:29 +0800 | [diff] [blame] | 1455 | cros_sdk( |
| 1456 | chromeos_root, |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1457 | './update_chroot', |
| 1458 | '--toolchain_boards', |
Kuang-che Wu | fb55310 | 2018-10-02 18:14:29 +0800 | [diff] [blame] | 1459 | board, |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1460 | env=env, |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1461 | stderr_callback=stderr_lines.append) |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1462 | |
| 1463 | env = common_env.copy() |
| 1464 | cmd = [ |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1465 | './build_packages', |
| 1466 | '--board', |
| 1467 | board, |
| 1468 | '--withdev', |
| 1469 | '--noworkon', |
| 1470 | '--skip_chroot_upgrade', |
| 1471 | '--accept_licenses=@CHROMEOS', |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1472 | ] |
Zheng-Jie Chang | ffd4946 | 2019-12-16 12:15:18 +0800 | [diff] [blame] | 1473 | |
| 1474 | # `use_any_chrome` flag is default on and will force to use a chrome |
| 1475 | # prebuilt even if the version doesn't match. |
| 1476 | |
| 1477 | # As this argument is landed in 12681, we should check if the argument |
| 1478 | # exists before adding this. |
| 1479 | if has_build_package_argument('use_any_chrome'): |
| 1480 | cmd.append('--nouse_any_chrome') |
| 1481 | |
Kuang-che Wu | a9a20bb | 2019-09-05 22:24:04 +0800 | [diff] [blame] | 1482 | if goma_dir: |
| 1483 | # Tell build_packages to start and stop goma |
| 1484 | cmd.append('--run_goma') |
| 1485 | env['USE_GOMA'] = 'true' |
| 1486 | if afdo_use: |
| 1487 | env['USE'] += ' afdo_use' |
| 1488 | cros_sdk( |
| 1489 | chromeos_root, |
| 1490 | *cmd, |
| 1491 | env=env, |
| 1492 | chrome_root=chrome_root, |
| 1493 | stderr_callback=stderr_lines.append, |
| 1494 | goma_dir=goma_dir) |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1495 | except subprocess.CalledProcessError as e: |
| 1496 | # Detect failures due to incompatibility between chroot and source tree. If |
| 1497 | # so, notify the caller to recreate chroot and retry. |
| 1498 | reason = check_if_need_recreate_chroot(e.output, ''.join(stderr_lines)) |
| 1499 | if reason: |
Kuang-che Wu | 6d91b8c | 2020-11-24 20:14:35 +0800 | [diff] [blame] | 1500 | raise NeedRecreateChrootException(reason) from e |
Kuang-che Wu | 9890ce8 | 2018-07-07 15:14:10 +0800 | [diff] [blame] | 1501 | |
| 1502 | # For other failures, don't know how to handle. Just bail out. |
| 1503 | raise |
| 1504 | |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1505 | |
| 1506 | def build_image(chromeos_root, board): |
| 1507 | """Build ChromeOS image. |
| 1508 | |
| 1509 | Args: |
| 1510 | chromeos_root: chromeos tree root |
| 1511 | board: ChromeOS board name |
| 1512 | |
| 1513 | Returns: |
| 1514 | image folder; relative to chromeos_root |
| 1515 | """ |
| 1516 | stderr_lines = [] |
| 1517 | try: |
| 1518 | with locking.lock_file(locking.LOCK_FILE_FOR_BUILD): |
| 1519 | cros_sdk( |
| 1520 | chromeos_root, |
| 1521 | './build_image', |
| 1522 | '--board', |
| 1523 | board, |
| 1524 | '--noenable_rootfs_verification', |
| 1525 | 'test', |
| 1526 | env={ |
| 1527 | 'USE': '-cros-debug chrome_internal', |
| 1528 | 'FEATURES': 'separatedebug', |
| 1529 | }, |
| 1530 | stderr_callback=stderr_lines.append) |
| 1531 | except subprocess.CalledProcessError as e: |
| 1532 | # Detect failures due to incompatibility between chroot and source tree. If |
| 1533 | # so, notify the caller to recreate chroot and retry. |
| 1534 | reason = check_if_need_recreate_chroot(e.output, ''.join(stderr_lines)) |
| 1535 | if reason: |
Kuang-che Wu | 6d91b8c | 2020-11-24 20:14:35 +0800 | [diff] [blame] | 1536 | raise NeedRecreateChrootException(reason) from e |
Kuang-che Wu | 28980b2 | 2019-07-31 19:51:45 +0800 | [diff] [blame] | 1537 | |
| 1538 | # For other failures, don't know how to handle. Just bail out. |
| 1539 | raise |
| 1540 | |
| 1541 | image_symlink = os.path.join(chromeos_root, cached_images_dir, board, |
| 1542 | 'latest') |
| 1543 | assert os.path.exists(image_symlink) |
| 1544 | image_name = os.readlink(image_symlink) |
| 1545 | image_folder = os.path.join(cached_images_dir, board, image_name) |
| 1546 | assert os.path.exists( |
| 1547 | os.path.join(chromeos_root, image_folder, test_image_filename)) |
| 1548 | return image_folder |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1549 | |
| 1550 | |
Kuang-che Wu | 23192ad | 2020-03-11 18:12:46 +0800 | [diff] [blame] | 1551 | class AutotestControlInfo: |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 1552 | """Parsed content of autotest control file. |
| 1553 | |
| 1554 | Attributes: |
| 1555 | name: test name |
| 1556 | path: control file path |
| 1557 | variables: dict of top-level control variables. Sample keys: NAME, AUTHOR, |
| 1558 | DOC, ATTRIBUTES, DEPENDENCIES, etc. |
| 1559 | """ |
| 1560 | |
| 1561 | def __init__(self, path, variables): |
| 1562 | self.name = variables['NAME'] |
| 1563 | self.path = path |
| 1564 | self.variables = variables |
| 1565 | |
| 1566 | |
| 1567 | def parse_autotest_control_file(path): |
| 1568 | """Parses autotest control file. |
| 1569 | |
| 1570 | This only parses simple top-level string assignments. |
| 1571 | |
| 1572 | Returns: |
| 1573 | AutotestControlInfo object |
| 1574 | """ |
| 1575 | variables = {} |
Kuang-che Wu | a572349 | 2019-11-25 20:59:34 +0800 | [diff] [blame] | 1576 | with open(path) as f: |
| 1577 | code = ast.parse(f.read()) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 1578 | for stmt in code.body: |
| 1579 | # Skip if not simple "NAME = *" assignment. |
| 1580 | if not (isinstance(stmt, ast.Assign) and len(stmt.targets) == 1 and |
| 1581 | isinstance(stmt.targets[0], ast.Name)): |
| 1582 | continue |
| 1583 | |
| 1584 | # Only support string value. |
| 1585 | if isinstance(stmt.value, ast.Str): |
| 1586 | variables[stmt.targets[0].id] = stmt.value.s |
| 1587 | |
| 1588 | return AutotestControlInfo(path, variables) |
| 1589 | |
| 1590 | |
| 1591 | def enumerate_autotest_control_files(autotest_dir): |
| 1592 | """Enumerate autotest control files. |
| 1593 | |
| 1594 | Args: |
| 1595 | autotest_dir: autotest folder |
| 1596 | |
| 1597 | Returns: |
| 1598 | list of paths to control files |
| 1599 | """ |
| 1600 | # Where to find control files. Relative to autotest_dir. |
| 1601 | subpaths = [ |
| 1602 | 'server/site_tests', |
| 1603 | 'client/site_tests', |
| 1604 | 'server/tests', |
| 1605 | 'client/tests', |
| 1606 | ] |
| 1607 | |
Kuang-che Wu | d6df63c | 2020-09-20 01:29:55 +0800 | [diff] [blame] | 1608 | denylist = ['site-packages', 'venv', 'results', 'logs', 'containers'] |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 1609 | result = [] |
| 1610 | for subpath in subpaths: |
| 1611 | path = os.path.join(autotest_dir, subpath) |
| 1612 | for root, dirs, files in os.walk(path): |
| 1613 | |
Kuang-che Wu | d6df63c | 2020-09-20 01:29:55 +0800 | [diff] [blame] | 1614 | for deny in denylist: |
| 1615 | if deny in dirs: |
| 1616 | dirs.remove(deny) |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 1617 | |
| 1618 | for filename in files: |
| 1619 | if filename == 'control' or filename.startswith('control.'): |
| 1620 | result.append(os.path.join(root, filename)) |
| 1621 | |
| 1622 | return result |
| 1623 | |
| 1624 | |
| 1625 | def get_autotest_test_info(autotest_dir, test_name): |
| 1626 | """Get metadata of given test. |
| 1627 | |
| 1628 | Args: |
| 1629 | autotest_dir: autotest folder |
| 1630 | test_name: test name |
| 1631 | |
| 1632 | Returns: |
| 1633 | AutotestControlInfo object. None if test not found. |
| 1634 | """ |
| 1635 | for control_file in enumerate_autotest_control_files(autotest_dir): |
Zheng-Jie Chang | 1504a55 | 2020-02-20 16:38:57 +0800 | [diff] [blame] | 1636 | try: |
| 1637 | info = parse_autotest_control_file(control_file) |
| 1638 | except SyntaxError: |
| 1639 | logger.warning('%s is not parsable, ignore', control_file) |
| 1640 | continue |
| 1641 | |
Kuang-che Wu | b9705bd | 2018-06-28 17:59:18 +0800 | [diff] [blame] | 1642 | if info.name == test_name: |
| 1643 | return info |
| 1644 | return None |
| 1645 | |
| 1646 | |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 1647 | def _get_overlay_name(overlay): |
| 1648 | path = os.path.join(overlay, 'metadata', 'layout.conf') |
| 1649 | if os.path.exists(path): |
| 1650 | with open(path) as f: |
| 1651 | for line in f: |
| 1652 | m = re.search(r'repo-name\s*=\s*(\S+)', line) |
| 1653 | if m: |
| 1654 | return m.group(1) |
| 1655 | |
| 1656 | path = os.path.join(overlay, 'profiles', 'repo_name') |
| 1657 | if os.path.exists(path): |
| 1658 | with open(path) as f: |
| 1659 | return f.readline().rstrip() |
| 1660 | |
| 1661 | return None |
| 1662 | |
| 1663 | |
| 1664 | def parse_chromeos_overlays(chromeos_root): |
| 1665 | # ref: chromite's lib/portage_util.py ListOverlays(). |
| 1666 | overlays = {} |
| 1667 | paths = ['src/overlays', 'src/private-overlays'] |
| 1668 | |
| 1669 | for path in paths: |
| 1670 | path = os.path.join(chromeos_root, path, 'overlay-*') |
| 1671 | for overlay in sorted(glob.glob(path)): |
| 1672 | name = _get_overlay_name(overlay) |
| 1673 | if not name: |
| 1674 | continue |
| 1675 | # Special cases which have variant boards. |
| 1676 | if name in ['auron', 'guado', 'nyan', 'veyron']: |
| 1677 | continue |
| 1678 | |
| 1679 | path = os.path.join(overlay, 'metadata', 'layout.conf') |
| 1680 | masters = [] |
| 1681 | if os.path.exists(path): |
| 1682 | with open(path) as f: |
| 1683 | for line in f: |
| 1684 | m = re.search(r'masters\s*=(.*)', line) |
| 1685 | if m: |
| 1686 | masters = m.group(1).split() |
| 1687 | overlays[name] = masters |
| 1688 | return overlays |
| 1689 | |
| 1690 | |
| 1691 | def resolve_basic_boards(overlays): |
| 1692 | |
| 1693 | def normalize(name): |
| 1694 | return name.replace('-private', '') |
| 1695 | |
| 1696 | def resolve(name): |
| 1697 | result = set() |
| 1698 | for parent in overlays[name]: |
| 1699 | assert parent != name, 'recursive overlays definition?' |
| 1700 | if parent not in overlays: |
| 1701 | continue |
| 1702 | for basic in resolve(parent): |
| 1703 | result.add(basic) |
| 1704 | if not result: |
| 1705 | result.add(name) |
| 1706 | return set(map(normalize, result)) |
| 1707 | |
| 1708 | result = {} |
| 1709 | for name in overlays: |
| 1710 | board = normalize(name) |
| 1711 | basic = resolve(name) |
| 1712 | assert len(basic) == 1 |
| 1713 | basic_board = basic.pop() |
| 1714 | result[board] = basic_board |
| 1715 | return result |
| 1716 | |
| 1717 | |
Zheng-Jie Chang | 868c175 | 2020-01-21 14:42:41 +0800 | [diff] [blame] | 1718 | def detect_branch_level(branch): |
| 1719 | """Given a branch name of manifest-internal, detect it's branch level. |
| 1720 | |
| 1721 | level1: if ChromeOS version is x.0.0 |
| 1722 | level2: if ChromeOS version is x.x.0 |
| 1723 | level3: if ChromeOS version is x.x.x |
| 1724 | Where x is an non-zero integer. |
| 1725 | |
| 1726 | Args: |
| 1727 | branch: branch name or ref name in manifest-internal |
| 1728 | |
| 1729 | Returns: |
| 1730 | An integer indicates the branch level, or zero if not detectable. |
| 1731 | """ |
| 1732 | level1 = r'^(refs\/\S+(\/\S+)?/)?master$' |
| 1733 | level2 = r'^\S+-(\d+)(\.0)?\.B$' |
| 1734 | level3 = r'^\S+-(\d+)\.(\d+)(\.0)?\.B$' |
| 1735 | |
| 1736 | if re.match(level1, branch): |
| 1737 | return 1 |
| 1738 | if re.match(level2, branch): |
| 1739 | return 2 |
| 1740 | if re.match(level3, branch): |
| 1741 | return 3 |
| 1742 | return 0 |
| 1743 | |
| 1744 | |
Zheng-Jie Chang | 5f9ae4e | 2020-02-07 14:26:06 +0800 | [diff] [blame] | 1745 | def get_crosland_link(old, new): |
| 1746 | """Generates crosland link between two versions. |
| 1747 | |
| 1748 | Args: |
| 1749 | old: ChromeOS version |
| 1750 | new: ChromeOS version |
| 1751 | |
| 1752 | Returns: |
| 1753 | A crosland url. |
| 1754 | """ |
| 1755 | |
| 1756 | def version_to_url_parameter(ver): |
| 1757 | if is_cros_snapshot_version(ver): |
| 1758 | return snapshot_version_split(ver)[2] |
| 1759 | return version_to_short(ver) |
| 1760 | |
| 1761 | old_parameter = version_to_url_parameter(old) |
| 1762 | new_parameter = version_to_url_parameter(new) |
| 1763 | return CROSLAND_URL_TEMPLATE % (old_parameter, new_parameter) |
| 1764 | |
| 1765 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1766 | class ChromeOSSpecManager(codechange.SpecManager): |
| 1767 | """Repo manifest related operations. |
| 1768 | |
| 1769 | This class enumerates chromeos manifest files, parses them, |
| 1770 | and sync to disk state according to them. |
| 1771 | """ |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1772 | |
| 1773 | def __init__(self, config): |
| 1774 | self.config = config |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1775 | self.manifest_dir = os.path.join(self.config['chromeos_root'], '.repo', |
| 1776 | 'manifests') |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1777 | self.manifest_internal_dir = os.path.join(self.config['chromeos_mirror'], |
| 1778 | 'manifest-internal.git') |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1779 | self.historical_manifest_git_dir = os.path.join( |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 1780 | self.config['chromeos_mirror'], 'chromeos/manifest-versions.git') |
Kuang-che Wu | 0fff988 | 2020-12-14 17:37:31 +0800 | [diff] [blame] | 1781 | self.historical_manifest_branch_name = 'refs/heads/master' |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1782 | if not os.path.exists(self.historical_manifest_git_dir): |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 1783 | raise errors.InternalError('Manifest snapshots should be cloned into %s' % |
| 1784 | self.historical_manifest_git_dir) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1785 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1786 | def lookup_snapshot_manifest_revisions(self, old, new): |
| 1787 | """Get manifest commits between snapshot versions. |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1788 | |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1789 | Returns: |
| 1790 | list of (timestamp, commit_id, snapshot_id): |
| 1791 | timestamp: integer unix timestamp |
| 1792 | commit_id: a string indicates commit hash |
| 1793 | snapshot_id: a string indicates snapshot id |
| 1794 | """ |
| 1795 | assert is_cros_snapshot_version(old) |
| 1796 | assert is_cros_snapshot_version(new) |
| 1797 | |
| 1798 | gs_path = ( |
Zheng-Jie Chang | 5402083 | 2020-04-21 15:52:48 +0800 | [diff] [blame] | 1799 | 'gs://chromeos-image-archive/{board}-snapshot/{version}-*/image.zip') |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1800 | # Try to guess the commit time of a snapshot manifest, it is usually a few |
| 1801 | # minutes different between snapshot manifest commit and image.zip |
| 1802 | # generate. |
| 1803 | try: |
Kuang-che Wu | 876a538 | 2020-10-27 14:24:58 +0800 | [diff] [blame] | 1804 | old_timestamp = gsutil_stat_creation_time( |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1805 | gs_path.format(board=self.config['board'], version=old)) - 86400 |
| 1806 | except subprocess.CalledProcessError: |
| 1807 | old_timestamp = None |
| 1808 | try: |
Kuang-che Wu | 876a538 | 2020-10-27 14:24:58 +0800 | [diff] [blame] | 1809 | new_timestamp = gsutil_stat_creation_time( |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1810 | gs_path.format(board=self.config['board'], version=new)) + 86400 |
| 1811 | # 1558657989 is snapshot_id 5982's commit time, this ensures every time |
| 1812 | # we can find snapshot 5982 |
| 1813 | # snapshot_id <= 5982 has different commit message format, so we need |
| 1814 | # to identify its id in different ways, see below comment for more info. |
| 1815 | new_timestamp = max(new_timestamp, 1558657989 + 1) |
| 1816 | except subprocess.CalledProcessError: |
| 1817 | new_timestamp = None |
| 1818 | result = [] |
| 1819 | _, _, old_snapshot_id = snapshot_version_split(old) |
| 1820 | _, _, new_snapshot_id = snapshot_version_split(new) |
| 1821 | repo = self.manifest_internal_dir |
| 1822 | path = 'snapshot.xml' |
| 1823 | branch = 'snapshot' |
| 1824 | commits = git_util.get_history( |
| 1825 | repo, |
| 1826 | path, |
| 1827 | branch, |
| 1828 | after=old_timestamp, |
| 1829 | before=new_timestamp, |
| 1830 | with_subject=True) |
| 1831 | |
| 1832 | # Unfortunately, we can not identify snapshot_id <= 5982 from its commit |
| 1833 | # subject, as their subjects are all `Annealing manifest snapshot.`. |
| 1834 | # So instead we count the snapshot_id manually. |
| 1835 | count = 5982 |
| 1836 | # There are two snapshot_id = 2633 in commit history, ignore the former |
| 1837 | # one. |
| 1838 | ignore_list = ['95c8526a7f0798d02f692010669dcbd5a152439a'] |
| 1839 | # We examine the commits in reverse order as there are some testing |
| 1840 | # commits before snapshot_id=2, this method works fine after |
| 1841 | # snapshot 2, except snapshot 2633 |
| 1842 | for commit in reversed(commits): |
| 1843 | msg = commit[2] |
| 1844 | if commit[1] in ignore_list: |
| 1845 | continue |
| 1846 | |
| 1847 | match = re.match(r'^annealing manifest snapshot (\d+)', msg) |
| 1848 | if match: |
| 1849 | snapshot_id = match.group(1) |
| 1850 | elif 'Annealing manifest snapshot' in msg: |
| 1851 | snapshot_id = str(count) |
| 1852 | count -= 1 |
| 1853 | else: |
| 1854 | continue |
Zheng-Jie Chang | 34595ea | 2020-03-10 13:04:22 +0800 | [diff] [blame] | 1855 | # b/151054108: snapshot version in [29288, 29439] is broken |
| 1856 | if 29288 <= int(snapshot_id) <= 29439: |
| 1857 | continue |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1858 | if int(old_snapshot_id) <= int(snapshot_id) <= int(new_snapshot_id): |
| 1859 | result.append((commit[0], commit[1], snapshot_id)) |
| 1860 | # We find commits in reversed order, now reverse it again to chronological |
| 1861 | # order. |
| 1862 | return list(reversed(result)) |
| 1863 | |
| 1864 | def lookup_build_timestamp(self, rev): |
| 1865 | assert is_cros_full_version(rev) or is_cros_snapshot_version(rev) |
| 1866 | if is_cros_full_version(rev): |
| 1867 | return self.lookup_release_build_timestamp(rev) |
Kuang-che Wu | a7ddf9b | 2019-11-25 18:59:57 +0800 | [diff] [blame] | 1868 | return self.lookup_snapshot_build_timestamp(rev) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 1869 | |
| 1870 | def lookup_snapshot_build_timestamp(self, rev): |
| 1871 | assert is_cros_snapshot_version(rev) |
| 1872 | return int(self.lookup_snapshot_manifest_revisions(rev, rev)[0][0]) |
| 1873 | |
| 1874 | def lookup_release_build_timestamp(self, rev): |
| 1875 | assert is_cros_full_version(rev) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1876 | milestone, short_version = version_split(rev) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1877 | path = os.path.join('buildspecs', milestone, short_version + '.xml') |
| 1878 | try: |
| 1879 | timestamp = git_util.get_commit_time(self.historical_manifest_git_dir, |
Kuang-che Wu | 0fff988 | 2020-12-14 17:37:31 +0800 | [diff] [blame] | 1880 | self.historical_manifest_branch_name, |
| 1881 | path) |
Kuang-che Wu | 6d91b8c | 2020-11-24 20:14:35 +0800 | [diff] [blame] | 1882 | except ValueError as e: |
| 1883 | raise errors.InternalError( |
| 1884 | '%s does not have %s' % |
| 1885 | (self.historical_manifest_git_dir, path)) from e |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1886 | return timestamp |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 1887 | |
Zheng-Jie Chang | 868c175 | 2020-01-21 14:42:41 +0800 | [diff] [blame] | 1888 | def detect_float_spec_branch_level(self, spec): |
| 1889 | results = [ |
| 1890 | detect_branch_level(branch) for branch in git_util.get_branches( |
| 1891 | self.manifest_dir, commit=spec.name) |
| 1892 | ] |
| 1893 | results = [x for x in results if x > 0] |
| 1894 | return min(results) if results else 0 |
| 1895 | |
| 1896 | def branch_between_float_specs(self, old_spec, new_spec): |
| 1897 | if old_spec.spec_type != codechange.SPEC_FLOAT: |
| 1898 | return False |
| 1899 | if new_spec.spec_type != codechange.SPEC_FLOAT: |
| 1900 | return False |
| 1901 | |
| 1902 | level_old = self.detect_float_spec_branch_level(old_spec) |
| 1903 | level_new = self.detect_float_spec_branch_level(new_spec) |
| 1904 | |
| 1905 | if not level_old or not level_new: |
Kuang-che Wu | ebc2c36 | 2020-12-14 16:27:09 +0800 | [diff] [blame] | 1906 | logger.warning('branch level detect failed, assume not branched') |
Zheng-Jie Chang | 868c175 | 2020-01-21 14:42:41 +0800 | [diff] [blame] | 1907 | return False |
| 1908 | return level_old != level_new |
| 1909 | |
Kuang-che Wu | d558a04 | 2020-06-06 02:11:00 +0800 | [diff] [blame] | 1910 | def _determine_float_branch(self, old, new, fixed_specs): |
| 1911 | # There is no revision tag in snapshot's xml. We know snapshot |
| 1912 | # builds are on master branch. |
| 1913 | master_refname = 'refs/remotes/origin/master' |
Zheng-Jie Chang | 3e19196 | 2020-02-06 14:25:31 +0800 | [diff] [blame] | 1914 | if fixed_specs[0].revision: |
| 1915 | old_branches = git_util.get_branches( |
Kuang-che Wu | d558a04 | 2020-06-06 02:11:00 +0800 | [diff] [blame] | 1916 | self.manifest_dir, commit=fixed_specs[0].revision, remote=True) |
| 1917 | else: |
| 1918 | old_branches = [master_refname] |
| 1919 | |
Zheng-Jie Chang | 3e19196 | 2020-02-06 14:25:31 +0800 | [diff] [blame] | 1920 | if fixed_specs[-1].revision: |
| 1921 | new_branches = git_util.get_branches( |
Kuang-che Wu | d558a04 | 2020-06-06 02:11:00 +0800 | [diff] [blame] | 1922 | self.manifest_dir, commit=fixed_specs[-1].revision, remote=True) |
Zheng-Jie Chang | 3e19196 | 2020-02-06 14:25:31 +0800 | [diff] [blame] | 1923 | else: |
Kuang-che Wu | d558a04 | 2020-06-06 02:11:00 +0800 | [diff] [blame] | 1924 | new_branches = [master_refname] |
Zheng-Jie Chang | 3e19196 | 2020-02-06 14:25:31 +0800 | [diff] [blame] | 1925 | |
Kuang-che Wu | d558a04 | 2020-06-06 02:11:00 +0800 | [diff] [blame] | 1926 | common_branches = list(set(old_branches) & set(new_branches)) |
| 1927 | assert common_branches, '%s and %s are not on common branches?' % (old, new) |
| 1928 | |
| 1929 | if len(common_branches) == 1: |
| 1930 | return common_branches[0] |
| 1931 | |
| 1932 | # There are more than one common branches, use heuristic to tie breaking. |
| 1933 | # The heuristic is simple: choice the branch with "smallest" number. |
| 1934 | # "Smaller" means the more major branch (not branched) or branched later. |
| 1935 | # |
| 1936 | # Following is the commit graph of manifest-internal repo. It shows many |
| 1937 | # interesting cases. |
| 1938 | # |
| 1939 | # 84/13021.0.0 84/13022.0.0 84/13024.0.0 |
| 1940 | # --A--+---X--------------X------B-------X-----------> master |
| 1941 | # \ |
| 1942 | # \ 83/13020.1.0 83/13020.56.0 83/13020.68.0 |
| 1943 | # C---X----D--+-------X-------+--------X-----> release-R83-13020.B |
| 1944 | # \ \ |
| 1945 | # \ E------------> stabilize-13020.67.B |
| 1946 | # \ 83/13020.55.1 |
| 1947 | # F-----X--------------------> stabilize-13020.55.B |
| 1948 | # |
| 1949 | # How to read this graph: |
| 1950 | # - Time goes from left to right. Branch names are on the right side of |
| 1951 | # arrows. |
| 1952 | # - Letters A-F are manifest commits. |
| 1953 | # - Marker X means release image build at that time, the version numbers |
| 1954 | # are labeled above the X marker. |
| 1955 | # For example, |
| 1956 | # 1) 13021.0.0 release is based on manifest A, which is on all branches |
| 1957 | # shown on the graph. |
| 1958 | # We know 13021.0.0 is on master (and R84 branch later, not shown in |
| 1959 | # this graph), not on 13020* branches. |
| 1960 | # 2) 13020.56.0 release is based on manifest D, which is on 3 branches |
| 1961 | # (R83-13020.B, 13020.67.B, and 13020.55.B). |
| 1962 | # We know 13020.56.0 is on R83-13020.B and 13020.67.B, but not |
| 1963 | # 13020.55.B. |
| 1964 | # |
| 1965 | # There is an important property here. Every time a new branch is created, |
| 1966 | # there will always be a commit (like C, E, and F) to fix "revision" field |
| 1967 | # in the manifest file. In other words, xxxxx.1.0 is impossible based on |
| 1968 | # manifest on master branch. xxxxx.yy.1 is impossible based on manifest on |
| 1969 | # xxxxx.B branch. |
| 1970 | # |
| 1971 | # With such property, among the branches containing the given manifest |
| 1972 | # file, the branch with "smallest" number guarantees where the release is. |
| 1973 | |
| 1974 | def branch_key(s): |
| 1975 | if s == master_refname: |
| 1976 | return 0, 0, 0 |
| 1977 | m = re.search(r'-(\d+)\.B$', s) |
| 1978 | if m: |
| 1979 | return int(m.group(1)), 0, 0 |
| 1980 | m = re.search(r'-(\d+)\.(\d+)\.B$', s) |
| 1981 | if m: |
| 1982 | return int(m.group(1)), int(m.group(2)), 0 |
| 1983 | m = re.search(r'-(\d+)\.(\d+)\.(\d+)\.B$', s) |
| 1984 | if m: |
| 1985 | return int(m.group(1)), int(m.group(2)), int(m.group(3)) |
| 1986 | |
| 1987 | logger.warning('unexpected branch name: %s', s) |
| 1988 | return (sys.maxsize, sys.maxsize, sys.maxsize, s) |
| 1989 | |
| 1990 | common_branches.sort(key=branch_key) |
| 1991 | return common_branches[0] |
| 1992 | |
| 1993 | def collect_float_spec(self, old, new, fixed_specs=None): |
| 1994 | assert fixed_specs |
| 1995 | branch = self._determine_float_branch(old, new, fixed_specs) |
| 1996 | logger.debug('float branch=%s', branch) |
Zheng-Jie Chang | d968f55 | 2020-01-16 13:31:57 +0800 | [diff] [blame] | 1997 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 1998 | old_timestamp = self.lookup_build_timestamp(old) |
| 1999 | new_timestamp = self.lookup_build_timestamp(new) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2000 | # snapshot time is different from commit time |
| 2001 | # usually it's a few minutes different |
| 2002 | # 30 minutes should be safe in most cases |
| 2003 | if is_cros_snapshot_version(old): |
| 2004 | old_timestamp = old_timestamp - 1800 |
| 2005 | if is_cros_snapshot_version(new): |
| 2006 | new_timestamp = new_timestamp + 1800 |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2007 | |
Zheng-Jie Chang | 1ace301 | 2020-02-15 04:51:05 +0800 | [diff] [blame] | 2008 | # TODO(zjchang): add logic to combine symlink target's (full.xml) history |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2009 | result = [] |
Zheng-Jie Chang | 1ace301 | 2020-02-15 04:51:05 +0800 | [diff] [blame] | 2010 | path = 'default.xml' |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2011 | parser = repo_util.ManifestParser(self.manifest_dir) |
| 2012 | for timestamp, git_rev in parser.enumerate_manifest_commits( |
Zheng-Jie Chang | d968f55 | 2020-01-16 13:31:57 +0800 | [diff] [blame] | 2013 | old_timestamp, new_timestamp, path, branch=branch): |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2014 | result.append( |
| 2015 | codechange.Spec(codechange.SPEC_FLOAT, git_rev, timestamp, path)) |
| 2016 | return result |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2017 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2018 | def collect_fixed_spec(self, old, new): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2019 | assert is_cros_full_version(old) or is_cros_snapshot_version(old) |
| 2020 | assert is_cros_full_version(new) or is_cros_snapshot_version(new) |
| 2021 | |
| 2022 | # case 1: if both are snapshot, return a list of snapshot |
| 2023 | if is_cros_snapshot_version(old) and is_cros_snapshot_version(new): |
| 2024 | return self.collect_snapshot_specs(old, new) |
| 2025 | |
| 2026 | # case 2: if both are release version |
| 2027 | # return a list of release version |
| 2028 | if is_cros_full_version(old) and is_cros_full_version(new): |
| 2029 | return self.collect_release_specs(old, new) |
| 2030 | |
| 2031 | # case 3: return a list of release version and append a snapshot |
| 2032 | # before or at the end |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 2033 | result = self.collect_release_specs( |
| 2034 | version_to_full(self.config['board'], old), |
| 2035 | version_to_full(self.config['board'], new)) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2036 | if is_cros_snapshot_version(old): |
Zheng-Jie Chang | c47af3a | 2019-11-11 17:28:58 +0800 | [diff] [blame] | 2037 | result = self.collect_snapshot_specs(old, old) + result[1:] |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2038 | elif is_cros_snapshot_version(new): |
Zheng-Jie Chang | 5cd62dd | 2019-12-09 13:21:12 +0800 | [diff] [blame] | 2039 | result += self.collect_snapshot_specs(new, new) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2040 | return result |
| 2041 | |
| 2042 | def collect_snapshot_specs(self, old, new): |
| 2043 | assert is_cros_snapshot_version(old) |
| 2044 | assert is_cros_snapshot_version(new) |
| 2045 | |
| 2046 | def guess_snapshot_version(board, snapshot_id, old, new): |
| 2047 | if old.endswith('-' + snapshot_id): |
| 2048 | return old |
| 2049 | if new.endswith('-' + snapshot_id): |
| 2050 | return new |
Zheng-Jie Chang | 5402083 | 2020-04-21 15:52:48 +0800 | [diff] [blame] | 2051 | gs_path = ('gs://chromeos-image-archive/{board}-snapshot/' |
Kuang-che Wu | f791afa | 2019-10-28 19:53:26 +0800 | [diff] [blame] | 2052 | 'R*-{snapshot_id}-*'.format( |
| 2053 | board=board, snapshot_id=snapshot_id)) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2054 | for line in gsutil_ls(gs_path, ignore_errors=True): |
| 2055 | m = re.match(r'^gs:\S+(R\d+-\d+\.\d+\.\d+-\d+)\S+', line) |
| 2056 | if m: |
| 2057 | return m.group(1) |
Zheng-Jie Chang | 026cd5d | 2019-12-04 12:13:01 +0800 | [diff] [blame] | 2058 | return None |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2059 | |
| 2060 | result = [] |
| 2061 | path = 'snapshot.xml' |
| 2062 | revisions = self.lookup_snapshot_manifest_revisions(old, new) |
Kuang-che Wu | f791afa | 2019-10-28 19:53:26 +0800 | [diff] [blame] | 2063 | for timestamp, _git_rev, snapshot_id in revisions: |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2064 | snapshot_version = guess_snapshot_version(self.config['board'], |
| 2065 | snapshot_id, old, new) |
Zheng-Jie Chang | 026cd5d | 2019-12-04 12:13:01 +0800 | [diff] [blame] | 2066 | if snapshot_version: |
| 2067 | result.append( |
| 2068 | codechange.Spec(codechange.SPEC_FIXED, snapshot_version, timestamp, |
| 2069 | path)) |
| 2070 | else: |
| 2071 | logger.warning('snapshot id %s is not found, ignore', snapshot_id) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2072 | return result |
| 2073 | |
| 2074 | def collect_release_specs(self, old, new): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2075 | assert is_cros_full_version(old) |
| 2076 | assert is_cros_full_version(new) |
| 2077 | old_milestone, old_short_version = version_split(old) |
| 2078 | new_milestone, new_short_version = version_split(new) |
| 2079 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2080 | result = [] |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2081 | for milestone in git_util.list_dir_from_revision( |
Kuang-che Wu | 0fff988 | 2020-12-14 17:37:31 +0800 | [diff] [blame] | 2082 | self.historical_manifest_git_dir, self.historical_manifest_branch_name, |
| 2083 | 'buildspecs'): |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2084 | if not milestone.isdigit(): |
| 2085 | continue |
| 2086 | if not int(old_milestone) <= int(milestone) <= int(new_milestone): |
| 2087 | continue |
| 2088 | |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 2089 | files = git_util.list_dir_from_revision( |
Kuang-che Wu | 0fff988 | 2020-12-14 17:37:31 +0800 | [diff] [blame] | 2090 | self.historical_manifest_git_dir, |
| 2091 | self.historical_manifest_branch_name, |
Kuang-che Wu | 74768d3 | 2018-09-07 12:03:24 +0800 | [diff] [blame] | 2092 | os.path.join('buildspecs', milestone)) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2093 | |
| 2094 | for fn in files: |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2095 | path = os.path.join('buildspecs', milestone, fn) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2096 | short_version, ext = os.path.splitext(fn) |
| 2097 | if ext != '.xml': |
| 2098 | continue |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2099 | if (util.is_version_lesseq(old_short_version, short_version) and |
| 2100 | util.is_version_lesseq(short_version, new_short_version) and |
| 2101 | util.is_direct_relative_version(short_version, new_short_version)): |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2102 | rev = make_cros_full_version(milestone, short_version) |
Kuang-che Wu | 0fff988 | 2020-12-14 17:37:31 +0800 | [diff] [blame] | 2103 | timestamp = git_util.get_commit_time( |
| 2104 | self.historical_manifest_git_dir, |
| 2105 | self.historical_manifest_branch_name, path) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2106 | result.append( |
| 2107 | codechange.Spec(codechange.SPEC_FIXED, rev, timestamp, path)) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2108 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2109 | def version_key_func(spec): |
| 2110 | _milestone, short_version = version_split(spec.name) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2111 | return util.version_key_func(short_version) |
| 2112 | |
| 2113 | result.sort(key=version_key_func) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2114 | assert result[0].name == old |
| 2115 | assert result[-1].name == new |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2116 | return result |
| 2117 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2118 | def get_manifest(self, rev): |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2119 | assert is_cros_full_version(rev) or is_cros_snapshot_version(rev) |
| 2120 | if is_cros_full_version(rev): |
| 2121 | milestone, short_version = version_split(rev) |
| 2122 | path = os.path.join('buildspecs', milestone, '%s.xml' % short_version) |
| 2123 | manifest = git_util.get_file_from_revision( |
Kuang-che Wu | 0fff988 | 2020-12-14 17:37:31 +0800 | [diff] [blame] | 2124 | self.historical_manifest_git_dir, |
| 2125 | self.historical_manifest_branch_name, path) |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame] | 2126 | else: |
| 2127 | revisions = self.lookup_snapshot_manifest_revisions(rev, rev) |
| 2128 | commit_id = revisions[0][1] |
| 2129 | manifest = git_util.get_file_from_revision(self.manifest_internal_dir, |
| 2130 | commit_id, 'snapshot.xml') |
Zheng-Jie Chang | 0fc704b | 2019-12-09 18:43:38 +0800 | [diff] [blame] | 2131 | return manifest |
| 2132 | |
| 2133 | def get_manifest_file(self, rev): |
| 2134 | assert is_cros_full_version(rev) or is_cros_snapshot_version(rev) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2135 | manifest_name = 'manifest_%s.xml' % rev |
| 2136 | manifest_path = os.path.join(self.manifest_dir, manifest_name) |
| 2137 | with open(manifest_path, 'w') as f: |
Zheng-Jie Chang | 0fc704b | 2019-12-09 18:43:38 +0800 | [diff] [blame] | 2138 | f.write(self.get_manifest(rev)) |
Zheng-Jie Chang | ec368b5 | 2020-03-02 16:25:25 +0800 | [diff] [blame] | 2139 | |
| 2140 | # workaround for b/150572399 |
| 2141 | # for chromeOS version < 12931.0.0, manifests are included from incorrect |
| 2142 | # folder .repo instead of.repo/manifests |
| 2143 | if is_cros_version_lesseq(rev, '12931.0.0'): |
| 2144 | repo_path = os.path.join(self.config['chromeos_root'], '.repo') |
| 2145 | manifest_patch_path = os.path.join(repo_path, manifest_name) |
| 2146 | with open(manifest_patch_path, 'w') as f: |
| 2147 | f.write(self.get_manifest(rev)) |
| 2148 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2149 | return manifest_name |
| 2150 | |
| 2151 | def parse_spec(self, spec): |
| 2152 | parser = repo_util.ManifestParser(self.manifest_dir) |
| 2153 | if spec.spec_type == codechange.SPEC_FIXED: |
Zheng-Jie Chang | 0fc704b | 2019-12-09 18:43:38 +0800 | [diff] [blame] | 2154 | manifest_name = self.get_manifest_file(spec.name) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2155 | manifest_path = os.path.join(self.manifest_dir, manifest_name) |
Kuang-che Wu | a572349 | 2019-11-25 20:59:34 +0800 | [diff] [blame] | 2156 | with open(manifest_path) as f: |
| 2157 | content = f.read() |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2158 | root = parser.parse_single_xml(content, allow_include=False) |
| 2159 | else: |
| 2160 | root = parser.parse_xml_recursive(spec.name, spec.path) |
| 2161 | |
| 2162 | spec.entries = parser.process_parsed_result(root) |
| 2163 | if spec.spec_type == codechange.SPEC_FIXED: |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 2164 | if not spec.is_static(): |
Kuang-che Wu | d1b7415 | 2020-05-20 08:46:46 +0800 | [diff] [blame] | 2165 | raise ValueError('fixed spec %r has unexpected floating entries' % |
| 2166 | spec.name) |
Zheng-Jie Chang | d968f55 | 2020-01-16 13:31:57 +0800 | [diff] [blame] | 2167 | spec.revision = root.get('revision') |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2168 | |
| 2169 | def sync_disk_state(self, rev): |
Zheng-Jie Chang | 0fc704b | 2019-12-09 18:43:38 +0800 | [diff] [blame] | 2170 | manifest_name = self.get_manifest_file(rev) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2171 | |
| 2172 | # For ChromeOS, mark_as_stable step requires 'repo init -m', which sticks |
| 2173 | # manifest. 'repo sync -m' is not enough |
| 2174 | repo_util.init( |
| 2175 | self.config['chromeos_root'], |
| 2176 | 'https://chrome-internal.googlesource.com/chromeos/manifest-internal', |
| 2177 | manifest_name=manifest_name, |
| 2178 | repo_url='https://chromium.googlesource.com/external/repo.git', |
Kuang-che Wu | d8fc957 | 2018-10-03 21:00:41 +0800 | [diff] [blame] | 2179 | reference=self.config['chromeos_mirror'], |
Zheng-Jie Chang | 85529ad | 2020-03-06 18:40:18 +0800 | [diff] [blame] | 2180 | # b/150753074: moblab is in non-default group and causes mark_as_stable |
| 2181 | # fail |
Kuang-che Wu | 084eef2 | 2020-03-11 18:29:48 +0800 | [diff] [blame] | 2182 | groups='default,moblab,platform-linux', |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 2183 | ) |
| 2184 | |
| 2185 | # Note, don't sync with current_branch=True for chromeos. One of its |
| 2186 | # build steps (inside mark_as_stable) executes "git describe" which |
| 2187 | # needs git tag information. |
| 2188 | repo_util.sync(self.config['chromeos_root']) |