Kuang-che Wu | 875c89a | 2020-01-08 14:30:55 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 3 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
Kuang-che Wu | 68db08a | 2018-03-30 11:50:34 +0800 | [diff] [blame] | 6 | """Helper script to manipulate chromeos DUT or query info.""" |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | import argparse |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 9 | import asyncio |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 10 | import json |
| 11 | import logging |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 12 | import os |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 13 | import random |
| 14 | import time |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 15 | |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 16 | from bisect_kit import cli |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 17 | from bisect_kit import common |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 18 | from bisect_kit import configure |
Kuang-che Wu | c45cfa4 | 2019-01-15 00:15:01 +0800 | [diff] [blame] | 19 | from bisect_kit import cros_lab_util |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 20 | from bisect_kit import cros_util |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 21 | from bisect_kit import errors |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 22 | |
Zheng-Jie Chang | 17f36c8 | 2020-06-16 05:21:59 +0800 | [diff] [blame] | 23 | DEFAULT_DUT_POOL = 'DUT_POOL_QUOTA' |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 24 | logger = logging.getLogger(__name__) |
| 25 | |
Kuang-che Wu | 2572342 | 2020-09-24 22:20:26 +0800 | [diff] [blame] | 26 | models_to_avoid = { |
| 27 | # model: reason |
| 28 | 'kasumi': 'b/160458394 stateful partition is too small', |
| 29 | 'mimrock': 'b/160458394 stateful partition is too small', |
| 30 | 'vorticon': 'b/160458394 stateful partition is too small', |
| 31 | } |
| 32 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 33 | |
| 34 | def cmd_version_info(opts): |
| 35 | info = cros_util.version_info(opts.board, opts.version) |
| 36 | if opts.name: |
| 37 | if opts.name not in info: |
| 38 | logger.error('unknown name=%s', opts.name) |
| 39 | print(info[opts.name]) |
| 40 | else: |
| 41 | print(json.dumps(info, sort_keys=True, indent=4)) |
| 42 | |
| 43 | |
| 44 | def cmd_query_dut_board(opts): |
| 45 | assert cros_util.is_dut(opts.dut) |
| 46 | print(cros_util.query_dut_board(opts.dut)) |
| 47 | |
| 48 | |
| 49 | def cmd_reboot(opts): |
Kuang-che Wu | 2534bab | 2020-10-23 17:37:16 +0800 | [diff] [blame] | 50 | if not cros_util.is_dut(opts.dut): |
| 51 | if opts.force: |
| 52 | logger.warning('%s is not a Chrome OS device?', opts.dut) |
| 53 | else: |
| 54 | raise errors.ArgumentError( |
| 55 | 'dut', 'not a Chrome OS device (--force to continue)') |
| 56 | |
Kuang-che Wu | 2ac9a92 | 2020-09-03 16:50:12 +0800 | [diff] [blame] | 57 | cros_util.reboot( |
| 58 | opts.dut, force_reboot_callback=cros_lab_util.reboot_via_servo) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 59 | |
| 60 | |
Kuang-che Wu | c45cfa4 | 2019-01-15 00:15:01 +0800 | [diff] [blame] | 61 | def _get_label_by_prefix(info, prefix): |
| 62 | for label in info['Labels']: |
| 63 | if label.startswith(prefix + ':'): |
| 64 | return label |
| 65 | return None |
| 66 | |
| 67 | |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 68 | def cmd_lease_dut(opts): |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 69 | if opts.duration is not None and opts.duration < 60: |
| 70 | raise errors.ArgumentError('--duration', 'must be at least 60 seconds') |
Kuang-che Wu | 7c1fa58 | 2021-01-14 17:57:49 +0800 | [diff] [blame] | 71 | reason = opts.reason or cros_lab_util.make_lease_reason(opts.session) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 72 | host = cros_lab_util.dut_host_name(opts.dut) |
Kuang-che Wu | 220cc16 | 2019-10-31 00:29:37 +0800 | [diff] [blame] | 73 | logger.info('trying to lease %s', host) |
Kuang-che Wu | 7c1fa58 | 2021-01-14 17:57:49 +0800 | [diff] [blame] | 74 | if cros_lab_util.skylab_lease_dut(host, reason, opts.duration): |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 75 | logger.info('leased %s', host) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 76 | else: |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 77 | raise Exception('unable to lease %s' % host) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 78 | |
| 79 | |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 80 | def cmd_release_dut(opts): |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 81 | host = cros_lab_util.dut_host_name(opts.dut) |
Kuang-che Wu | 220cc16 | 2019-10-31 00:29:37 +0800 | [diff] [blame] | 82 | cros_lab_util.skylab_release_dut(host) |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 83 | logger.info('%s released', host) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 84 | |
| 85 | |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 86 | def verify_dimensions_by_lab(dimensions): |
| 87 | result = [] |
| 88 | bots_dimensions = cros_lab_util.swarming_bots_dimensions() |
| 89 | for dimension in dimensions: |
| 90 | key, value = dimension.split(':', 1) |
| 91 | if value in bots_dimensions.get(key, []): |
| 92 | result.append(dimension) |
| 93 | else: |
| 94 | logger.warning('dimension=%s is unknown in the lab, typo? ignored', |
| 95 | dimension) |
| 96 | return result |
| 97 | |
| 98 | |
Kuang-che Wu | eac89bd | 2020-10-23 16:07:15 +0800 | [diff] [blame] | 99 | def select_available_bots_randomly(dimensions, |
| 100 | variants, |
| 101 | num=1, |
| 102 | is_busy=None, |
| 103 | filter_func=None): |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 104 | bots = [] |
| 105 | for variant in variants: |
| 106 | # There might be thousand bots available, set 'limit' to reduce swarming |
| 107 | # API cost. This is not uniform random, but should be good enough. |
| 108 | bots += cros_lab_util.swarming_bots_list( |
| 109 | dimensions + [variant], is_busy=is_busy, limit=10) |
Kuang-che Wu | 2572342 | 2020-09-24 22:20:26 +0800 | [diff] [blame] | 110 | |
Kuang-che Wu | eac89bd | 2020-10-23 16:07:15 +0800 | [diff] [blame] | 111 | if filter_func: |
| 112 | bots = list(filter(filter_func, bots)) |
| 113 | return random.sample(bots, min(num, len(bots))) |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 114 | |
| 115 | |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 116 | def filter_dimensions_by_board(boards_with_prebuilt, dimensions): |
| 117 | result = [] |
| 118 | for dimension in dimensions: |
| 119 | bots = cros_lab_util.swarming_bots_list([dimension], is_busy=None, limit=1) |
| 120 | if not bots: |
| 121 | continue |
| 122 | board = bots[0]['dimensions']['label-board'][0] |
| 123 | if board not in boards_with_prebuilt: |
| 124 | logger.warning( |
| 125 | 'dimension=%s (board=%s) does not have corresponding ' |
| 126 | 'prebuilt image, ignore', dimension, board) |
| 127 | continue |
| 128 | result.append(dimension) |
| 129 | return result |
| 130 | |
| 131 | |
Kuang-che Wu | eac89bd | 2020-10-23 16:07:15 +0800 | [diff] [blame] | 132 | def is_acceptable_bot(boards_with_prebuilt, bot): |
| 133 | model = bot['dimensions']['label-model'][0] |
| 134 | if model in models_to_avoid: |
| 135 | logger.warning('model=%s is bad (reason:%s), ignore', model, |
| 136 | models_to_avoid[model]) |
| 137 | return False |
| 138 | |
Kuang-che Wu | 79ea119 | 2020-10-27 15:16:05 +0800 | [diff] [blame] | 139 | if boards_with_prebuilt is not None: |
| 140 | # Sometimes swarming database has inconsistent records. For example, |
| 141 | # label-model=kefka + label-board=strago are incorrect (should be |
| 142 | # label-board=kefka). It is probably human errors (strago is kefka's |
| 143 | # reference board). |
| 144 | board = bot['dimensions']['label-board'][0] |
| 145 | if board not in boards_with_prebuilt: |
| 146 | logger.warning('%s has unexpected board=%s, ignore', |
| 147 | bot['dimensions']['dut_name'][0], board) |
| 148 | return False |
Kuang-che Wu | eac89bd | 2020-10-23 16:07:15 +0800 | [diff] [blame] | 149 | |
| 150 | return True |
Kuang-che Wu | 0461977 | 2020-10-22 18:57:07 +0800 | [diff] [blame] | 151 | |
| 152 | |
Kuang-che Wu | 7c1fa58 | 2021-01-14 17:57:49 +0800 | [diff] [blame] | 153 | async def lease_dut_parallelly(duration, bots, reason, timeout=None): |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 154 | tasks = [] |
| 155 | hosts = [] |
| 156 | for bot in bots: |
| 157 | host = bot['dimensions']['dut_name'][0] |
| 158 | hosts.append(host) |
| 159 | tasks.append( |
Kuang-che Wu | 7c1fa58 | 2021-01-14 17:57:49 +0800 | [diff] [blame] | 160 | asyncio.create_task( |
| 161 | cros_lab_util.async_lease(host, reason, duration=duration))) |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 162 | |
| 163 | try: |
| 164 | logger.info('trying to lease %d DUTs: %s', len(hosts), hosts) |
| 165 | for coro in asyncio.as_completed(tasks, timeout=timeout): |
| 166 | host = await coro |
| 167 | if host: |
| 168 | logger.info('leased %s', host) |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 169 | # Unfinished lease tasks will be cancelled when asyncio.run is |
| 170 | # finishing. |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 171 | return host |
| 172 | return None |
| 173 | except asyncio.TimeoutError: |
| 174 | return None |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 175 | |
| 176 | |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 177 | def normalize_board_name(chromeos_root, board): |
| 178 | """Normalize BOARD name. |
| 179 | |
| 180 | Here, we want to find the actual device board. Suffixes like -kernelnext will |
| 181 | be removed. So we can use that name to query DUTs inside the lab. |
| 182 | |
| 183 | Args: |
| 184 | chromeos_root: chromeos source root |
| 185 | board: BOARD name |
| 186 | |
| 187 | Returns: |
| 188 | normalized BOARD name |
| 189 | """ |
| 190 | overlays = cros_util.parse_chromeos_overlays(chromeos_root) |
| 191 | boards_info = cros_util.resolve_basic_boards(overlays) |
| 192 | return boards_info[board] |
| 193 | |
| 194 | |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 195 | def do_allocate_dut(opts): |
| 196 | """Helper of cmd_allocate_dut. |
| 197 | |
| 198 | Returns: |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 199 | (todo, host, board_to_build) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 200 | todo: 'ready' or 'wait' |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 201 | host: leased host name |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 202 | board_to_build: board name for building image |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 203 | """ |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 204 | if not opts.dut_name and not opts.pool: |
| 205 | raise errors.ArgumentError('--pool', |
| 206 | 'need to be specified if not --dut_name') |
Kuang-che Wu | 7e8abe6 | 2020-07-02 09:42:27 +0800 | [diff] [blame] | 207 | if opts.version_hint: |
| 208 | for v in opts.version_hint.split(','): |
| 209 | if cros_util.is_cros_version(v) or cros_util.is_cros_snapshot_version(v): |
| 210 | continue |
| 211 | raise errors.ArgumentError( |
| 212 | '--version_hint', |
| 213 | 'should be Chrome OS version numbers, separated by comma') |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 214 | if opts.duration is not None and opts.duration < 60: |
| 215 | raise errors.ArgumentError('--duration', 'must be at least 60 seconds') |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 216 | |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 217 | t0 = time.time() |
Zheng-Jie Chang | 17f36c8 | 2020-06-16 05:21:59 +0800 | [diff] [blame] | 218 | dimensions = ['dut_state:ready'] |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 219 | if not opts.dut_name: |
Zheng-Jie Chang | 17f36c8 | 2020-06-16 05:21:59 +0800 | [diff] [blame] | 220 | dimensions.append('label-pool:' + opts.pool) |
| 221 | |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 222 | variants = [] |
| 223 | if opts.board: |
| 224 | for board in opts.board.split(','): |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 225 | variants.append('label-board:' + |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 226 | normalize_board_name(opts.chromeos_root, board)) |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 227 | if opts.model: |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 228 | for model in opts.model.split(','): |
Kuang-che Wu | 2572342 | 2020-09-24 22:20:26 +0800 | [diff] [blame] | 229 | if model in models_to_avoid: |
| 230 | logger.warning('model=%s is bad (reason:%s), ignore', model, |
| 231 | models_to_avoid[model]) |
| 232 | continue |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 233 | variants.append('label-model:' + model) |
Kuang-che Wu | 2572342 | 2020-09-24 22:20:26 +0800 | [diff] [blame] | 234 | if not variants: |
| 235 | raise errors.ArgumentError('--model', |
| 236 | 'all specified models are not supported') |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 237 | if opts.sku: |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 238 | for sku in opts.sku.split(','): |
| 239 | variants.append('label-hwid_sku:' + cros_lab_util.normalize_sku_name(sku)) |
| 240 | if opts.dut_name: |
| 241 | for dut_name in opts.dut_name.split(','): |
| 242 | variants.append('dut_name:' + dut_name) |
| 243 | |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 244 | variants = verify_dimensions_by_lab(variants) |
| 245 | variants = sorted(set(variants)) # dedup |
| 246 | if not variants: |
| 247 | raise errors.NoDutAvailable( |
| 248 | 'Invalid constraints: %s;%s;%s;%s' % |
| 249 | (opts.board, opts.model, opts.sku, opts.dut_name)) |
| 250 | |
| 251 | # Filter variants by prebuilt images. |
Kuang-che Wu | 79ea119 | 2020-10-27 15:16:05 +0800 | [diff] [blame] | 252 | boards_with_prebuilt = None |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 253 | if opts.version_hint: |
| 254 | if not opts.builder_hint: |
| 255 | opts.builder_hint = opts.board |
| 256 | if not opts.builder_hint: |
| 257 | raise errors.ArgumentError('--builder_hint', |
| 258 | 'must be specified along with --version_hint') |
| 259 | boards_with_prebuilt = [] |
| 260 | versions = opts.version_hint.split(',') |
| 261 | for builder in opts.builder_hint.split(','): |
| 262 | if not all(cros_util.has_test_image(builder, v) for v in versions): |
| 263 | logger.warning( |
| 264 | 'builder=%s does not have prebuilt test image for %s, ignore', |
| 265 | builder, opts.version_hint) |
| 266 | continue |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 267 | boards_with_prebuilt.append( |
| 268 | normalize_board_name(opts.chromeos_root, builder)) |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 269 | logger.info('boards with prebuilt: %s', boards_with_prebuilt) |
| 270 | if not boards_with_prebuilt: |
| 271 | raise errors.ArgumentError( |
| 272 | '--version_hint', |
| 273 | 'given builders have no prebuilt for %s' % opts.version_hint) |
| 274 | variants = filter_dimensions_by_board(boards_with_prebuilt, variants) |
| 275 | if not variants: |
| 276 | raise errors.NoDutAvailable( |
| 277 | 'Devices with specified constraints have no prebuilt. ' |
| 278 | 'Wrong version number?') |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 279 | |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 280 | while True: |
Kuang-che Wu | eac89bd | 2020-10-23 16:07:15 +0800 | [diff] [blame] | 281 | filter_func = lambda bot: is_acceptable_bot(boards_with_prebuilt, bot) |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 282 | # Query every time because each iteration takes a few minutes |
| 283 | bots = select_available_bots_randomly( |
Kuang-che Wu | eac89bd | 2020-10-23 16:07:15 +0800 | [diff] [blame] | 284 | dimensions, |
| 285 | variants, |
| 286 | num=opts.parallel, |
| 287 | is_busy=False, |
| 288 | filter_func=filter_func) |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 289 | if not bots: |
| 290 | bots = select_available_bots_randomly( |
Kuang-che Wu | eac89bd | 2020-10-23 16:07:15 +0800 | [diff] [blame] | 291 | dimensions, |
| 292 | variants, |
| 293 | num=opts.parallel, |
| 294 | is_busy=True, |
| 295 | filter_func=filter_func) |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 296 | if not bots: |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 297 | raise errors.NoDutAvailable( |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 298 | 'no bots satisfy constraints; all are in maintenance state?') |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 299 | |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 300 | remaining_time = opts.time_limit - (time.time() - t0) |
| 301 | if remaining_time <= 0: |
| 302 | break |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 303 | timeout = min(120, remaining_time) |
Kuang-che Wu | 7c1fa58 | 2021-01-14 17:57:49 +0800 | [diff] [blame] | 304 | reason = cros_lab_util.make_lease_reason(opts.session) |
| 305 | host = asyncio.run( |
| 306 | lease_dut_parallelly(opts.duration, bots, reason, timeout)) |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 307 | if host: |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 308 | # Resolve what board we should build during bisection. |
| 309 | board_to_build = None |
| 310 | bots = cros_lab_util.swarming_bots_list(['dut_name:' + host]) |
| 311 | host_board = bots[0]['dimensions']['label-board'][0] |
| 312 | if opts.builder_hint: |
| 313 | for builder in opts.builder_hint.split(','): |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 314 | if normalize_board_name(opts.chromeos_root, builder) == host_board: |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 315 | board_to_build = builder |
| 316 | break |
Kuang-che Wu | 0461977 | 2020-10-22 18:57:07 +0800 | [diff] [blame] | 317 | else: |
| 318 | raise errors.DutLeaseException('DUT with unexpected board:%s' % |
| 319 | host_board) |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 320 | else: |
| 321 | board_to_build = host_board |
| 322 | |
| 323 | return 'ready', host, board_to_build |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 324 | time.sleep(1) |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 325 | |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 326 | logger.warning('unable to lease DUT in time limit') |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 327 | return 'wait', None, None |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 328 | |
| 329 | |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 330 | def cmd_allocate_dut(opts): |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 331 | leased_dut = None |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 332 | try: |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 333 | todo, host, board = do_allocate_dut(opts) |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 334 | leased_dut = cros_lab_util.dut_name_to_address(host) if host else None |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 335 | result = {'result': todo, 'leased_dut': leased_dut, 'board': board} |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 336 | print(json.dumps(result)) |
| 337 | except Exception as e: |
| 338 | logger.exception('cmd_allocate_dut failed') |
| 339 | exception_name = e.__class__.__name__ |
| 340 | result = { |
| 341 | 'result': 'failed', |
| 342 | 'exception': exception_name, |
| 343 | 'text': str(e), |
| 344 | } |
| 345 | print(json.dumps(result)) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 346 | |
| 347 | |
Kuang-che Wu | a8c3c3e | 2019-08-28 18:49:28 +0800 | [diff] [blame] | 348 | def cmd_repair_dut(opts): |
| 349 | cros_lab_util.repair(opts.dut) |
| 350 | |
| 351 | |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 352 | @cli.fatal_error_handler |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 353 | def main(): |
| 354 | common.init() |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 355 | parents = [common.common_argument_parser, common.session_optional_parser] |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 356 | parser = argparse.ArgumentParser() |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 357 | cli.patching_argparser_exit(parser) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 358 | subparsers = parser.add_subparsers( |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 359 | dest='command', title='commands', metavar='<command>', required=True) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 360 | |
| 361 | parser_version_info = subparsers.add_parser( |
| 362 | 'version_info', |
| 363 | help='Query version info of given chromeos build', |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 364 | parents=parents, |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 365 | description='Given chromeos `board` and `version`, ' |
| 366 | 'print version information of components.') |
| 367 | parser_version_info.add_argument( |
| 368 | 'board', help='ChromeOS board name, like "samus".') |
| 369 | parser_version_info.add_argument( |
| 370 | 'version', |
| 371 | type=cros_util.argtype_cros_version, |
| 372 | help='ChromeOS version, like "9876.0.0" or "R62-9876.0.0"') |
| 373 | parser_version_info.add_argument( |
| 374 | 'name', |
| 375 | nargs='?', |
| 376 | help='Component name. If specified, output its version string. ' |
| 377 | 'Otherwise output all version info as dict in json format.') |
| 378 | parser_version_info.set_defaults(func=cmd_version_info) |
| 379 | |
| 380 | parser_query_dut_board = subparsers.add_parser( |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 381 | 'query_dut_board', help='Query board name of given DUT', parents=parents) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 382 | parser_query_dut_board.add_argument('dut') |
| 383 | parser_query_dut_board.set_defaults(func=cmd_query_dut_board) |
| 384 | |
| 385 | parser_reboot = subparsers.add_parser( |
| 386 | 'reboot', |
| 387 | help='Reboot a DUT', |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 388 | parents=parents, |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 389 | description='Reboot a DUT and verify the reboot is successful.') |
Kuang-che Wu | 2534bab | 2020-10-23 17:37:16 +0800 | [diff] [blame] | 390 | parser_reboot.add_argument('--force', action='store_true') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 391 | parser_reboot.add_argument('dut') |
| 392 | parser_reboot.set_defaults(func=cmd_reboot) |
| 393 | |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 394 | parser_lease_dut = subparsers.add_parser( |
| 395 | 'lease_dut', |
| 396 | help='Lease a DUT in the lab', |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 397 | parents=[common.common_argument_parser], |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 398 | description='Lease a DUT in the lab. ' |
| 399 | 'This is implemented by `skylab lease-dut` with additional checking.') |
Kuang-che Wu | 7c1fa58 | 2021-01-14 17:57:49 +0800 | [diff] [blame] | 400 | group = parser_lease_dut.add_mutually_exclusive_group(required=True) |
| 401 | group.add_argument('--session', help='session name') |
| 402 | group.add_argument('--reason', help='specify lease reason manually') |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 403 | parser_lease_dut.add_argument('dut') |
| 404 | parser_lease_dut.add_argument( |
| 405 | '--duration', |
| 406 | type=float, |
| 407 | help='duration in seconds; will be round to minutes') |
| 408 | parser_lease_dut.set_defaults(func=cmd_lease_dut) |
| 409 | |
| 410 | parser_release_dut = subparsers.add_parser( |
| 411 | 'release_dut', |
| 412 | help='Release a DUT in the lab', |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 413 | parents=parents, |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 414 | description='Release a DUT in the lab. ' |
| 415 | 'This is implemented by `skylab release-dut` with additional checking.') |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 416 | parser_release_dut.add_argument('dut') |
| 417 | parser_release_dut.set_defaults(func=cmd_release_dut) |
| 418 | |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 419 | parser_allocate_dut = subparsers.add_parser( |
| 420 | 'allocate_dut', |
| 421 | help='Allocate a DUT in the lab', |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 422 | parents=[common.common_argument_parser], |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 423 | description='Allocate a DUT in the lab. It will lease a DUT in the lab ' |
| 424 | 'for bisecting. The caller (bisect-kit runner) of this command should ' |
| 425 | 'retry this command again later if no DUT available now.') |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 426 | parser_allocate_dut.add_argument( |
| 427 | '--session', required=True, help='session name') |
| 428 | parser_allocate_dut.add_argument( |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 429 | '--pool', |
| 430 | help='Pool to search DUT (default: %(default)s)', |
| 431 | default=DEFAULT_DUT_POOL) |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 432 | group = parser_allocate_dut.add_mutually_exclusive_group(required=True) |
| 433 | group.add_argument('--board', help='allocation criteria; comma separated') |
| 434 | group.add_argument('--model', help='allocation criteria; comma separated') |
| 435 | group.add_argument('--sku', help='allocation criteria; comma separated') |
| 436 | group.add_argument('--dut_name', help='allocation criteria; comma separated') |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 437 | parser_allocate_dut.add_argument( |
Kuang-che Wu | 1e56ce2 | 2020-06-29 11:21:51 +0800 | [diff] [blame] | 438 | '--version_hint', help='chromeos version; comma separated') |
Kuang-che Wu | b529d2d | 2020-09-10 12:26:56 +0800 | [diff] [blame] | 439 | parser_allocate_dut.add_argument( |
| 440 | '--builder_hint', help='chromeos builder; comma separated') |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 441 | # Pubsub ack deadline is 10 minutes (b/143663659). Default 9 minutes with 1 |
| 442 | # minute buffer. |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 443 | parser_allocate_dut.add_argument( |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 444 | '--time_limit', |
| 445 | type=int, |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 446 | default=9 * 60, |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 447 | help='Time limit to attempt lease in seconds (default: %(default)s)') |
| 448 | parser_allocate_dut.add_argument( |
| 449 | '--duration', |
| 450 | type=float, |
| 451 | help='lease duration in seconds; will be round to minutes') |
Kuang-che Wu | 5157dee | 2020-07-18 01:13:41 +0800 | [diff] [blame] | 452 | parser_allocate_dut.add_argument( |
| 453 | '--parallel', |
| 454 | type=int, |
| 455 | default=1, |
| 456 | help='Submit multiple lease tasks to speed up (default: %(default)d)') |
Kuang-che Wu | 9d14c16 | 2020-11-03 19:35:18 +0800 | [diff] [blame] | 457 | parser_allocate_dut.add_argument( |
| 458 | '--chromeos_root', |
| 459 | type=cli.argtype_dir_path, |
| 460 | default=configure.get('DEFAULT_CHROMEOS_ROOT', |
| 461 | os.path.expanduser('~/chromiumos')), |
| 462 | help='Chrome OS source tree, for overlay data (default: %(default)s)') |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 463 | parser_allocate_dut.set_defaults(func=cmd_allocate_dut) |
| 464 | |
Kuang-che Wu | a8c3c3e | 2019-08-28 18:49:28 +0800 | [diff] [blame] | 465 | parser_repair_dut = subparsers.add_parser( |
| 466 | 'repair_dut', |
| 467 | help='Repair a DUT in the lab', |
Kuang-che Wu | d2d6e41 | 2021-01-28 16:26:41 +0800 | [diff] [blame] | 468 | parents=parents, |
Kuang-che Wu | a8c3c3e | 2019-08-28 18:49:28 +0800 | [diff] [blame] | 469 | description='Repair a DUT in the lab. ' |
| 470 | 'This is simply wrapper of "deploy repair" with additional checking.') |
| 471 | parser_repair_dut.add_argument('dut') |
| 472 | parser_repair_dut.set_defaults(func=cmd_repair_dut) |
| 473 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 474 | opts = parser.parse_args() |
| 475 | common.config_logging(opts) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 476 | opts.func(opts) |
| 477 | |
| 478 | |
| 479 | if __name__ == '__main__': |
| 480 | main() |