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 |
| 9 | import json |
| 10 | import logging |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 11 | import random |
| 12 | import time |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 13 | |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 14 | from bisect_kit import cli |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 15 | from bisect_kit import common |
Kuang-che Wu | c45cfa4 | 2019-01-15 00:15:01 +0800 | [diff] [blame] | 16 | from bisect_kit import cros_lab_util |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 17 | from bisect_kit import cros_util |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 18 | from bisect_kit import errors |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 19 | from bisect_kit import util |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 20 | |
Zheng-Jie Chang | 17f36c8 | 2020-06-16 05:21:59 +0800 | [diff] [blame^] | 21 | DEFAULT_DUT_POOL = 'DUT_POOL_QUOTA' |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 22 | logger = logging.getLogger(__name__) |
| 23 | |
| 24 | |
| 25 | def cmd_version_info(opts): |
| 26 | info = cros_util.version_info(opts.board, opts.version) |
| 27 | if opts.name: |
| 28 | if opts.name not in info: |
| 29 | logger.error('unknown name=%s', opts.name) |
| 30 | print(info[opts.name]) |
| 31 | else: |
| 32 | print(json.dumps(info, sort_keys=True, indent=4)) |
| 33 | |
| 34 | |
| 35 | def cmd_query_dut_board(opts): |
| 36 | assert cros_util.is_dut(opts.dut) |
| 37 | print(cros_util.query_dut_board(opts.dut)) |
| 38 | |
| 39 | |
| 40 | def cmd_reboot(opts): |
| 41 | assert cros_util.is_dut(opts.dut) |
| 42 | cros_util.reboot(opts.dut) |
| 43 | |
| 44 | |
Kuang-che Wu | c45cfa4 | 2019-01-15 00:15:01 +0800 | [diff] [blame] | 45 | def _get_label_by_prefix(info, prefix): |
| 46 | for label in info['Labels']: |
| 47 | if label.startswith(prefix + ':'): |
| 48 | return label |
| 49 | return None |
| 50 | |
| 51 | |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 52 | def cmd_lease_dut(opts): |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 53 | host = cros_lab_util.dut_host_name(opts.dut) |
Kuang-che Wu | 220cc16 | 2019-10-31 00:29:37 +0800 | [diff] [blame] | 54 | logger.info('trying to lease %s', host) |
| 55 | if cros_lab_util.skylab_lease_dut(host, opts.duration): |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 56 | logger.info('leased %s', host) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 57 | else: |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 58 | raise Exception('unable to lease %s' % host) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 59 | |
| 60 | |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 61 | def cmd_release_dut(opts): |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 62 | host = cros_lab_util.dut_host_name(opts.dut) |
Kuang-che Wu | 220cc16 | 2019-10-31 00:29:37 +0800 | [diff] [blame] | 63 | cros_lab_util.skylab_release_dut(host) |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 64 | logger.info('%s released', host) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 65 | |
| 66 | |
| 67 | def do_allocate_dut(opts): |
| 68 | """Helper of cmd_allocate_dut. |
| 69 | |
| 70 | Returns: |
| 71 | (todo, host) |
| 72 | todo: 'ready' or 'wait' |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 73 | host: leased host name |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 74 | """ |
Zheng-Jie Chang | 17f36c8 | 2020-06-16 05:21:59 +0800 | [diff] [blame^] | 75 | if not opts.model and not opts.sku and not opts.dut_name: |
| 76 | raise errors.ArgumentError('--model or --sku or --dut_name', |
| 77 | 'need to be specified') |
| 78 | if opts.dut_name and (opts.model or opts.sku): |
| 79 | raise errors.ArgumentError( |
| 80 | '--model or --sku should be empty if dut_name is specified') |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 81 | |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 82 | t0 = time.time() |
Zheng-Jie Chang | 17f36c8 | 2020-06-16 05:21:59 +0800 | [diff] [blame^] | 83 | dimensions = ['dut_state:ready'] |
| 84 | if opts.dut_name: |
| 85 | dimensions.append('dut_name:' + opts.dut_name) |
| 86 | else: |
| 87 | dimensions.append('label-pool:' + opts.pool) |
| 88 | |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 89 | if opts.model: |
| 90 | dimensions.append('label-model:' + opts.model) |
| 91 | if opts.sku: |
| 92 | dimensions.append('label-hwid_sku:' + |
| 93 | cros_lab_util.normalize_sku_name(opts.sku)) |
| 94 | |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 95 | while True: |
Kuang-che Wu | 9501f34 | 2019-11-15 17:15:21 +0800 | [diff] [blame] | 96 | # Query every time because each iteration takes 10+ minutes |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 97 | bots = cros_lab_util.swarming_bots_list(dimensions, is_busy=False) |
| 98 | if not bots: |
| 99 | bots = cros_lab_util.swarming_bots_list(dimensions) |
| 100 | if not bots: |
| 101 | raise errors.NoDutAvailable( |
| 102 | 'no bots satisfy constraints; incorrect model/sku? %s' % dimensions) |
| 103 | |
| 104 | bot = random.choice(bots) |
| 105 | host = bot['dimensions']['dut_name'][0] |
| 106 | logger.info('trying to lease %s', host) |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 107 | remaining_time = opts.time_limit - (time.time() - t0) |
| 108 | if remaining_time <= 0: |
| 109 | break |
| 110 | try: |
| 111 | if cros_lab_util.skylab_lease_dut( |
| 112 | host, opts.duration, timeout=remaining_time): |
| 113 | logger.info('leased %s (bot_id=%s)', host, bot['bot_id']) |
Kuang-che Wu | ceaa5ec | 2019-11-05 14:21:33 +0800 | [diff] [blame] | 114 | dut = host + '.cros' |
| 115 | if cros_util.is_good_dut(dut): |
| 116 | return 'ready', host |
| 117 | logger.warning('the leased DUT is broken; ' |
| 118 | 'return it and lease another one later') |
| 119 | cros_lab_util.skylab_release_dut(host) |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 120 | except util.TimeoutExpired: |
| 121 | break |
| 122 | time.sleep(1) |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 123 | |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 124 | logger.warning('unable to lease DUT in time limit') |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 125 | return 'wait', None |
| 126 | |
| 127 | |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 128 | def cmd_allocate_dut(opts): |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 129 | leased_dut = None |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 130 | try: |
| 131 | todo, host = do_allocate_dut(opts) |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 132 | leased_dut = host + '.cros' if host else None |
Kuang-che Wu | 611939f | 2020-04-14 19:12:50 +0800 | [diff] [blame] | 133 | result = {'result': todo, 'leased_dut': leased_dut} |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 134 | print(json.dumps(result)) |
| 135 | except Exception as e: |
| 136 | logger.exception('cmd_allocate_dut failed') |
| 137 | exception_name = e.__class__.__name__ |
| 138 | result = { |
| 139 | 'result': 'failed', |
| 140 | 'exception': exception_name, |
| 141 | 'text': str(e), |
| 142 | } |
| 143 | print(json.dumps(result)) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 144 | |
| 145 | |
Kuang-che Wu | a8c3c3e | 2019-08-28 18:49:28 +0800 | [diff] [blame] | 146 | def cmd_repair_dut(opts): |
| 147 | cros_lab_util.repair(opts.dut) |
| 148 | |
| 149 | |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 150 | @cli.fatal_error_handler |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 151 | def main(): |
| 152 | common.init() |
| 153 | parser = argparse.ArgumentParser() |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 154 | cli.patching_argparser_exit(parser) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 155 | common.add_common_arguments(parser) |
| 156 | subparsers = parser.add_subparsers( |
| 157 | dest='command', title='commands', metavar='<command>') |
| 158 | |
| 159 | parser_version_info = subparsers.add_parser( |
| 160 | 'version_info', |
| 161 | help='Query version info of given chromeos build', |
| 162 | description='Given chromeos `board` and `version`, ' |
| 163 | 'print version information of components.') |
| 164 | parser_version_info.add_argument( |
| 165 | 'board', help='ChromeOS board name, like "samus".') |
| 166 | parser_version_info.add_argument( |
| 167 | 'version', |
| 168 | type=cros_util.argtype_cros_version, |
| 169 | help='ChromeOS version, like "9876.0.0" or "R62-9876.0.0"') |
| 170 | parser_version_info.add_argument( |
| 171 | 'name', |
| 172 | nargs='?', |
| 173 | help='Component name. If specified, output its version string. ' |
| 174 | 'Otherwise output all version info as dict in json format.') |
| 175 | parser_version_info.set_defaults(func=cmd_version_info) |
| 176 | |
| 177 | parser_query_dut_board = subparsers.add_parser( |
| 178 | 'query_dut_board', help='Query board name of given DUT') |
| 179 | parser_query_dut_board.add_argument('dut') |
| 180 | parser_query_dut_board.set_defaults(func=cmd_query_dut_board) |
| 181 | |
| 182 | parser_reboot = subparsers.add_parser( |
| 183 | 'reboot', |
| 184 | help='Reboot a DUT', |
| 185 | description='Reboot a DUT and verify the reboot is successful.') |
| 186 | parser_reboot.add_argument('dut') |
| 187 | parser_reboot.set_defaults(func=cmd_reboot) |
| 188 | |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 189 | parser_lease_dut = subparsers.add_parser( |
| 190 | 'lease_dut', |
| 191 | help='Lease a DUT in the lab', |
| 192 | description='Lease a DUT in the lab. ' |
| 193 | 'This is implemented by `skylab lease-dut` with additional checking.') |
| 194 | # "skylab lease-dut" doesn't take reason, so this is not required=True. |
| 195 | parser_lease_dut.add_argument('--session', help='session name') |
| 196 | parser_lease_dut.add_argument('dut') |
| 197 | parser_lease_dut.add_argument( |
| 198 | '--duration', |
| 199 | type=float, |
| 200 | help='duration in seconds; will be round to minutes') |
| 201 | parser_lease_dut.set_defaults(func=cmd_lease_dut) |
| 202 | |
| 203 | parser_release_dut = subparsers.add_parser( |
| 204 | 'release_dut', |
| 205 | help='Release a DUT in the lab', |
| 206 | description='Release a DUT in the lab. ' |
| 207 | 'This is implemented by `skylab release-dut` with additional checking.') |
| 208 | parser_release_dut.add_argument('--session', help='session name') |
| 209 | parser_release_dut.add_argument('dut') |
| 210 | parser_release_dut.set_defaults(func=cmd_release_dut) |
| 211 | |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 212 | parser_allocate_dut = subparsers.add_parser( |
| 213 | 'allocate_dut', |
| 214 | help='Allocate a DUT in the lab', |
Kuang-che Wu | ca45646 | 2019-11-04 17:32:55 +0800 | [diff] [blame] | 215 | description='Allocate a DUT in the lab. It will lease a DUT in the lab ' |
| 216 | 'for bisecting. The caller (bisect-kit runner) of this command should ' |
| 217 | 'retry this command again later if no DUT available now.') |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 218 | parser_allocate_dut.add_argument( |
| 219 | '--session', required=True, help='session name') |
| 220 | parser_allocate_dut.add_argument( |
Zheng-Jie Chang | 17f36c8 | 2020-06-16 05:21:59 +0800 | [diff] [blame^] | 221 | '--pool', help='Pool to search dut', default=DEFAULT_DUT_POOL) |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 222 | parser_allocate_dut.add_argument('--model', help='allocation criteria') |
| 223 | parser_allocate_dut.add_argument('--sku', help='allocation criteria') |
Zheng-Jie Chang | 17f36c8 | 2020-06-16 05:21:59 +0800 | [diff] [blame^] | 224 | parser_allocate_dut.add_argument('--dut_name', help='allocation criteria') |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 225 | parser_allocate_dut.add_argument( |
| 226 | '--label', '-b', help='Additional required labels, comma separated') |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 227 | # Pubsub ack deadline is 10 minutes (b/143663659). Default 9 minutes with 1 |
| 228 | # minute buffer. |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 229 | parser_allocate_dut.add_argument( |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 230 | '--time_limit', |
| 231 | type=int, |
Kuang-che Wu | c26dcdf | 2019-11-01 16:30:06 +0800 | [diff] [blame] | 232 | default=9 * 60, |
Kuang-che Wu | 0c9b794 | 2019-10-30 16:55:39 +0800 | [diff] [blame] | 233 | help='Time limit to attempt lease in seconds (default: %(default)s)') |
| 234 | parser_allocate_dut.add_argument( |
| 235 | '--duration', |
| 236 | type=float, |
| 237 | help='lease duration in seconds; will be round to minutes') |
Kuang-che Wu | 22aa9d4 | 2019-01-25 10:35:33 +0800 | [diff] [blame] | 238 | parser_allocate_dut.set_defaults(func=cmd_allocate_dut) |
| 239 | |
Kuang-che Wu | a8c3c3e | 2019-08-28 18:49:28 +0800 | [diff] [blame] | 240 | parser_repair_dut = subparsers.add_parser( |
| 241 | 'repair_dut', |
| 242 | help='Repair a DUT in the lab', |
| 243 | description='Repair a DUT in the lab. ' |
| 244 | 'This is simply wrapper of "deploy repair" with additional checking.') |
| 245 | parser_repair_dut.add_argument('dut') |
| 246 | parser_repair_dut.set_defaults(func=cmd_repair_dut) |
| 247 | |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 248 | opts = parser.parse_args() |
| 249 | common.config_logging(opts) |
Kuang-che Wu | d3a4e84 | 2019-12-11 12:15:23 +0800 | [diff] [blame] | 250 | |
| 251 | # It's optional by default since python3. |
| 252 | if not opts.command: |
| 253 | parser.error('command is missing') |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 254 | opts.func(opts) |
| 255 | |
| 256 | |
| 257 | if __name__ == '__main__': |
| 258 | main() |