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