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