blob: 9bed2bb21d6c77b3623c83a5277b83b12b815d4f [file] [log] [blame]
Kuang-che Wu875c89a2020-01-08 14:30:55 +08001#!/usr/bin/env python3
Kuang-che Wu6e4beca2018-06-27 17:45:02 +08002# -*- coding: utf-8 -*-
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08003# 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 Wu68db08a2018-03-30 11:50:34 +08006"""Helper script to manipulate chromeos DUT or query info."""
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08007from __future__ import print_function
8import argparse
9import json
10import logging
Kuang-che Wu0c9b7942019-10-30 16:55:39 +080011import random
12import time
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080013
Kuang-che Wufe1e88a2019-09-10 21:52:25 +080014from bisect_kit import cli
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080015from bisect_kit import common
Kuang-che Wuc45cfa42019-01-15 00:15:01 +080016from bisect_kit import cros_lab_util
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080017from bisect_kit import cros_util
Kuang-che Wu22aa9d42019-01-25 10:35:33 +080018from bisect_kit import errors
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +080019from bisect_kit import util
Kuang-che Wu2ea804f2017-11-28 17:11:41 +080020
21logger = logging.getLogger(__name__)
22
23
24def 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
34def cmd_query_dut_board(opts):
35 assert cros_util.is_dut(opts.dut)
36 print(cros_util.query_dut_board(opts.dut))
37
38
39def cmd_reboot(opts):
40 assert cros_util.is_dut(opts.dut)
41 cros_util.reboot(opts.dut)
42
43
Kuang-che Wuc45cfa42019-01-15 00:15:01 +080044def _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 Wuca456462019-11-04 17:32:55 +080051def cmd_lease_dut(opts):
Kuang-che Wu22aa9d42019-01-25 10:35:33 +080052 host = cros_lab_util.dut_host_name(opts.dut)
Kuang-che Wu220cc162019-10-31 00:29:37 +080053 logger.info('trying to lease %s', host)
54 if cros_lab_util.skylab_lease_dut(host, opts.duration):
Kuang-che Wuca456462019-11-04 17:32:55 +080055 logger.info('leased %s', host)
Kuang-che Wu22aa9d42019-01-25 10:35:33 +080056 else:
Kuang-che Wuca456462019-11-04 17:32:55 +080057 raise Exception('unable to lease %s' % host)
Kuang-che Wu22aa9d42019-01-25 10:35:33 +080058
59
Kuang-che Wuca456462019-11-04 17:32:55 +080060def cmd_release_dut(opts):
Kuang-che Wu22aa9d42019-01-25 10:35:33 +080061 host = cros_lab_util.dut_host_name(opts.dut)
Kuang-che Wu220cc162019-10-31 00:29:37 +080062 cros_lab_util.skylab_release_dut(host)
Kuang-che Wuca456462019-11-04 17:32:55 +080063 logger.info('%s released', host)
Kuang-che Wu22aa9d42019-01-25 10:35:33 +080064
65
66def do_allocate_dut(opts):
67 """Helper of cmd_allocate_dut.
68
69 Returns:
70 (todo, host)
71 todo: 'ready' or 'wait'
Kuang-che Wuca456462019-11-04 17:32:55 +080072 host: leased host name
Kuang-che Wu22aa9d42019-01-25 10:35:33 +080073 """
74 if not opts.model and not opts.sku:
75 raise errors.ArgumentError('--model or --sku', 'need to be specified')
76
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +080077 t0 = time.time()
Kuang-che Wu0c9b7942019-10-30 16:55:39 +080078 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 Wuc26dcdf2019-11-01 16:30:06 +080085 while True:
Kuang-che Wu9501f342019-11-15 17:15:21 +080086 # Query every time because each iteration takes 10+ minutes
Kuang-che Wu0c9b7942019-10-30 16:55:39 +080087 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 Wuc26dcdf2019-11-01 16:30:06 +080097 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 Wuceaa5ec2019-11-05 14:21:33 +0800104 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 Wuc26dcdf2019-11-01 16:30:06 +0800110 except util.TimeoutExpired:
111 break
112 time.sleep(1)
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800113
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +0800114 logger.warning('unable to lease DUT in time limit')
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800115 return 'wait', None
116
117
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800118def cmd_allocate_dut(opts):
Kuang-che Wuca456462019-11-04 17:32:55 +0800119 leased_dut = None
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800120 try:
121 todo, host = do_allocate_dut(opts)
Kuang-che Wuca456462019-11-04 17:32:55 +0800122 leased_dut = host + '.cros' if host else None
Kuang-che Wu611939f2020-04-14 19:12:50 +0800123 result = {'result': todo, 'leased_dut': leased_dut}
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800124 print(json.dumps(result))
125 except Exception as e:
126 logger.exception('cmd_allocate_dut failed')
127 exception_name = e.__class__.__name__
128 result = {
129 'result': 'failed',
130 'exception': exception_name,
131 'text': str(e),
132 }
133 print(json.dumps(result))
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800134
135
Kuang-che Wua8c3c3e2019-08-28 18:49:28 +0800136def cmd_repair_dut(opts):
137 cros_lab_util.repair(opts.dut)
138
139
Kuang-che Wufe1e88a2019-09-10 21:52:25 +0800140@cli.fatal_error_handler
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800141def main():
142 common.init()
143 parser = argparse.ArgumentParser()
Kuang-che Wufe1e88a2019-09-10 21:52:25 +0800144 cli.patching_argparser_exit(parser)
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800145 common.add_common_arguments(parser)
146 subparsers = parser.add_subparsers(
147 dest='command', title='commands', metavar='<command>')
148
149 parser_version_info = subparsers.add_parser(
150 'version_info',
151 help='Query version info of given chromeos build',
152 description='Given chromeos `board` and `version`, '
153 'print version information of components.')
154 parser_version_info.add_argument(
155 'board', help='ChromeOS board name, like "samus".')
156 parser_version_info.add_argument(
157 'version',
158 type=cros_util.argtype_cros_version,
159 help='ChromeOS version, like "9876.0.0" or "R62-9876.0.0"')
160 parser_version_info.add_argument(
161 'name',
162 nargs='?',
163 help='Component name. If specified, output its version string. '
164 'Otherwise output all version info as dict in json format.')
165 parser_version_info.set_defaults(func=cmd_version_info)
166
167 parser_query_dut_board = subparsers.add_parser(
168 'query_dut_board', help='Query board name of given DUT')
169 parser_query_dut_board.add_argument('dut')
170 parser_query_dut_board.set_defaults(func=cmd_query_dut_board)
171
172 parser_reboot = subparsers.add_parser(
173 'reboot',
174 help='Reboot a DUT',
175 description='Reboot a DUT and verify the reboot is successful.')
176 parser_reboot.add_argument('dut')
177 parser_reboot.set_defaults(func=cmd_reboot)
178
Kuang-che Wuca456462019-11-04 17:32:55 +0800179 parser_lease_dut = subparsers.add_parser(
180 'lease_dut',
181 help='Lease a DUT in the lab',
182 description='Lease a DUT in the lab. '
183 'This is implemented by `skylab lease-dut` with additional checking.')
184 # "skylab lease-dut" doesn't take reason, so this is not required=True.
185 parser_lease_dut.add_argument('--session', help='session name')
186 parser_lease_dut.add_argument('dut')
187 parser_lease_dut.add_argument(
188 '--duration',
189 type=float,
190 help='duration in seconds; will be round to minutes')
191 parser_lease_dut.set_defaults(func=cmd_lease_dut)
192
193 parser_release_dut = subparsers.add_parser(
194 'release_dut',
195 help='Release a DUT in the lab',
196 description='Release a DUT in the lab. '
197 'This is implemented by `skylab release-dut` with additional checking.')
198 parser_release_dut.add_argument('--session', help='session name')
199 parser_release_dut.add_argument('dut')
200 parser_release_dut.set_defaults(func=cmd_release_dut)
201
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800202 parser_allocate_dut = subparsers.add_parser(
203 'allocate_dut',
204 help='Allocate a DUT in the lab',
Kuang-che Wuca456462019-11-04 17:32:55 +0800205 description='Allocate a DUT in the lab. It will lease a DUT in the lab '
206 'for bisecting. The caller (bisect-kit runner) of this command should '
207 'retry this command again later if no DUT available now.')
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800208 parser_allocate_dut.add_argument(
209 '--session', required=True, help='session name')
210 parser_allocate_dut.add_argument(
211 '--pools', required=True, help='Pools to search dut, comma separated')
212 parser_allocate_dut.add_argument('--model', help='allocation criteria')
213 parser_allocate_dut.add_argument('--sku', help='allocation criteria')
214 parser_allocate_dut.add_argument(
215 '--label', '-b', help='Additional required labels, comma separated')
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +0800216 # Pubsub ack deadline is 10 minutes (b/143663659). Default 9 minutes with 1
217 # minute buffer.
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800218 parser_allocate_dut.add_argument(
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800219 '--time_limit',
220 type=int,
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +0800221 default=9 * 60,
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800222 help='Time limit to attempt lease in seconds (default: %(default)s)')
223 parser_allocate_dut.add_argument(
224 '--duration',
225 type=float,
226 help='lease duration in seconds; will be round to minutes')
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800227 parser_allocate_dut.set_defaults(func=cmd_allocate_dut)
228
Kuang-che Wua8c3c3e2019-08-28 18:49:28 +0800229 parser_repair_dut = subparsers.add_parser(
230 'repair_dut',
231 help='Repair a DUT in the lab',
232 description='Repair a DUT in the lab. '
233 'This is simply wrapper of "deploy repair" with additional checking.')
234 parser_repair_dut.add_argument('dut')
235 parser_repair_dut.set_defaults(func=cmd_repair_dut)
236
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800237 opts = parser.parse_args()
238 common.config_logging(opts)
Kuang-che Wud3a4e842019-12-11 12:15:23 +0800239
240 # It's optional by default since python3.
241 if not opts.command:
242 parser.error('command is missing')
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800243 opts.func(opts)
244
245
246if __name__ == '__main__':
247 main()