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