blob: f51cfe599adeafa40cc6cbfb93efd7a7677c465b [file] [log] [blame]
Kuang-che Wu2ea804f2017-11-28 17:11:41 +08001#!/usr/bin/env python2
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 Wu0c9b7942019-10-30 16:55:39 +080086 # Query every time because each iteration takes 10+ miuntes
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 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'])
104 return 'ready', host
105 except util.TimeoutExpired:
106 break
107 time.sleep(1)
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800108
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +0800109 logger.warning('unable to lease DUT in time limit')
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800110 return 'wait', None
111
112
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800113def cmd_allocate_dut(opts):
Kuang-che Wuca456462019-11-04 17:32:55 +0800114 leased_dut = None
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800115 try:
116 todo, host = do_allocate_dut(opts)
Kuang-che Wuca456462019-11-04 17:32:55 +0800117 leased_dut = host + '.cros' if host else None
118 # TODO(kcwu): remove "locked_dut" after bkr updated
119 result = {
120 'result': todo,
121 'locked_dut': leased_dut,
122 'leased_dut': leased_dut
123 }
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
202 # TODO(kcwu): remove `lock_dut` and `unlock_dut` after bkr updated
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800203 parser_lock_dut = subparsers.add_parser(
204 'lock_dut',
205 help='Lock a DUT in the lab',
206 description='Lock a DUT in the lab. '
207 'This is simply wrapper of "atest" with additional checking.')
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800208 # "skylab lease-dut" doesn't take reason, so this is not required=True.
209 group = parser_lock_dut.add_mutually_exclusive_group()
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800210 group.add_argument('--session', help='session name; for creating lock reason')
211 group.add_argument('--reason', help='specify lock reason manually')
212 parser_lock_dut.add_argument('dut')
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800213 parser_lock_dut.add_argument(
214 '--duration',
215 type=float,
216 help='duration in seconds; will be round to minutes')
Kuang-che Wuca456462019-11-04 17:32:55 +0800217 parser_lock_dut.set_defaults(func=cmd_lease_dut)
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800218
219 parser_unlock_dut = subparsers.add_parser(
220 'unlock_dut',
221 help='Unlock a DUT in the lab',
222 description='Unlock a DUT in the lab. '
223 'This is simply wrapper of "atest" with additional checking.')
224 parser_unlock_dut.add_argument(
225 '--session', help='session name; for checking lock reason before unlock')
226 parser_unlock_dut.add_argument('dut')
Kuang-che Wuca456462019-11-04 17:32:55 +0800227 parser_unlock_dut.set_defaults(func=cmd_release_dut)
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800228
229 parser_allocate_dut = subparsers.add_parser(
230 'allocate_dut',
231 help='Allocate a DUT in the lab',
Kuang-che Wuca456462019-11-04 17:32:55 +0800232 description='Allocate a DUT in the lab. It will lease a DUT in the lab '
233 'for bisecting. The caller (bisect-kit runner) of this command should '
234 'retry this command again later if no DUT available now.')
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800235 parser_allocate_dut.add_argument(
236 '--session', required=True, help='session name')
237 parser_allocate_dut.add_argument(
238 '--pools', required=True, help='Pools to search dut, comma separated')
239 parser_allocate_dut.add_argument('--model', help='allocation criteria')
240 parser_allocate_dut.add_argument('--sku', help='allocation criteria')
241 parser_allocate_dut.add_argument(
242 '--label', '-b', help='Additional required labels, comma separated')
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +0800243 # Pubsub ack deadline is 10 minutes (b/143663659). Default 9 minutes with 1
244 # minute buffer.
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800245 parser_allocate_dut.add_argument(
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800246 '--time_limit',
247 type=int,
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +0800248 default=9 * 60,
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800249 help='Time limit to attempt lease in seconds (default: %(default)s)')
250 parser_allocate_dut.add_argument(
251 '--duration',
252 type=float,
253 help='lease duration in seconds; will be round to minutes')
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800254 parser_allocate_dut.set_defaults(func=cmd_allocate_dut)
255
Kuang-che Wua8c3c3e2019-08-28 18:49:28 +0800256 parser_repair_dut = subparsers.add_parser(
257 'repair_dut',
258 help='Repair a DUT in the lab',
259 description='Repair a DUT in the lab. '
260 'This is simply wrapper of "deploy repair" with additional checking.')
261 parser_repair_dut.add_argument('dut')
262 parser_repair_dut.set_defaults(func=cmd_repair_dut)
263
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800264 opts = parser.parse_args()
265 common.config_logging(opts)
266 opts.func(opts)
267
268
269if __name__ == '__main__':
270 main()