blob: e455698507489886aecbc97c10f81dbfe69069c4 [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 Wu22aa9d42019-01-25 10:35:33 +080051def cmd_lock_dut(opts):
52 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):
55 logger.info('locked %s', host)
Kuang-che Wu22aa9d42019-01-25 10:35:33 +080056 else:
Kuang-che Wu220cc162019-10-31 00:29:37 +080057 raise Exception('unable to lock %s' % host)
Kuang-che Wu22aa9d42019-01-25 10:35:33 +080058
59
60def cmd_unlock_dut(opts):
61 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 Wu22aa9d42019-01-25 10:35:33 +080063 logger.info('%s unlocked', host)
64
65
66def do_allocate_dut(opts):
67 """Helper of cmd_allocate_dut.
68
69 Returns:
70 (todo, host)
71 todo: 'ready' or 'wait'
72 host: locked host name
73 """
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):
114 locked_dut = None
115 try:
116 todo, host = do_allocate_dut(opts)
117 locked_dut = host + '.cros' if host else None
118 result = {'result': todo, 'locked_dut': locked_dut}
119 print(json.dumps(result))
120 except Exception as e:
121 logger.exception('cmd_allocate_dut failed')
122 exception_name = e.__class__.__name__
123 result = {
124 'result': 'failed',
125 'exception': exception_name,
126 'text': str(e),
127 }
128 print(json.dumps(result))
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800129
130
Kuang-che Wua8c3c3e2019-08-28 18:49:28 +0800131def cmd_repair_dut(opts):
132 cros_lab_util.repair(opts.dut)
133
134
Kuang-che Wufe1e88a2019-09-10 21:52:25 +0800135@cli.fatal_error_handler
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800136def main():
137 common.init()
138 parser = argparse.ArgumentParser()
Kuang-che Wufe1e88a2019-09-10 21:52:25 +0800139 cli.patching_argparser_exit(parser)
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800140 common.add_common_arguments(parser)
141 subparsers = parser.add_subparsers(
142 dest='command', title='commands', metavar='<command>')
143
144 parser_version_info = subparsers.add_parser(
145 'version_info',
146 help='Query version info of given chromeos build',
147 description='Given chromeos `board` and `version`, '
148 'print version information of components.')
149 parser_version_info.add_argument(
150 'board', help='ChromeOS board name, like "samus".')
151 parser_version_info.add_argument(
152 'version',
153 type=cros_util.argtype_cros_version,
154 help='ChromeOS version, like "9876.0.0" or "R62-9876.0.0"')
155 parser_version_info.add_argument(
156 'name',
157 nargs='?',
158 help='Component name. If specified, output its version string. '
159 'Otherwise output all version info as dict in json format.')
160 parser_version_info.set_defaults(func=cmd_version_info)
161
162 parser_query_dut_board = subparsers.add_parser(
163 'query_dut_board', help='Query board name of given DUT')
164 parser_query_dut_board.add_argument('dut')
165 parser_query_dut_board.set_defaults(func=cmd_query_dut_board)
166
167 parser_reboot = subparsers.add_parser(
168 'reboot',
169 help='Reboot a DUT',
170 description='Reboot a DUT and verify the reboot is successful.')
171 parser_reboot.add_argument('dut')
172 parser_reboot.set_defaults(func=cmd_reboot)
173
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800174 parser_lock_dut = subparsers.add_parser(
175 'lock_dut',
176 help='Lock a DUT in the lab',
177 description='Lock a DUT in the lab. '
178 'This is simply wrapper of "atest" with additional checking.')
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800179 # "skylab lease-dut" doesn't take reason, so this is not required=True.
180 group = parser_lock_dut.add_mutually_exclusive_group()
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800181 group.add_argument('--session', help='session name; for creating lock reason')
182 group.add_argument('--reason', help='specify lock reason manually')
183 parser_lock_dut.add_argument('dut')
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800184 parser_lock_dut.add_argument(
185 '--duration',
186 type=float,
187 help='duration in seconds; will be round to minutes')
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800188 parser_lock_dut.set_defaults(func=cmd_lock_dut)
189
190 parser_unlock_dut = subparsers.add_parser(
191 'unlock_dut',
192 help='Unlock a DUT in the lab',
193 description='Unlock a DUT in the lab. '
194 'This is simply wrapper of "atest" with additional checking.')
195 parser_unlock_dut.add_argument(
196 '--session', help='session name; for checking lock reason before unlock')
197 parser_unlock_dut.add_argument('dut')
198 parser_unlock_dut.set_defaults(func=cmd_unlock_dut)
199
200 parser_allocate_dut = subparsers.add_parser(
201 'allocate_dut',
202 help='Allocate a DUT in the lab',
203 description='Allocate a DUT in the lab. It will lock a DUT in the lab '
204 'for bisecting. If no DUT is available (ready), it will lock one. The '
205 'caller (bisect-kit runner) of this command should keep note of the '
206 'locked DUT name and retry this command again later.')
207 parser_allocate_dut.add_argument(
208 '--session', required=True, help='session name')
209 parser_allocate_dut.add_argument(
210 '--pools', required=True, help='Pools to search dut, comma separated')
211 parser_allocate_dut.add_argument('--model', help='allocation criteria')
212 parser_allocate_dut.add_argument('--sku', help='allocation criteria')
213 parser_allocate_dut.add_argument(
214 '--label', '-b', help='Additional required labels, comma separated')
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +0800215 # Pubsub ack deadline is 10 minutes (b/143663659). Default 9 minutes with 1
216 # minute buffer.
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800217 parser_allocate_dut.add_argument(
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800218 '--time_limit',
219 type=int,
Kuang-che Wuc26dcdf2019-11-01 16:30:06 +0800220 default=9 * 60,
Kuang-che Wu0c9b7942019-10-30 16:55:39 +0800221 help='Time limit to attempt lease in seconds (default: %(default)s)')
222 parser_allocate_dut.add_argument(
223 '--duration',
224 type=float,
225 help='lease duration in seconds; will be round to minutes')
Kuang-che Wu22aa9d42019-01-25 10:35:33 +0800226 parser_allocate_dut.set_defaults(func=cmd_allocate_dut)
227
Kuang-che Wua8c3c3e2019-08-28 18:49:28 +0800228 parser_repair_dut = subparsers.add_parser(
229 'repair_dut',
230 help='Repair a DUT in the lab',
231 description='Repair a DUT in the lab. '
232 'This is simply wrapper of "deploy repair" with additional checking.')
233 parser_repair_dut.add_argument('dut')
234 parser_repair_dut.set_defaults(func=cmd_repair_dut)
235
Kuang-che Wu2ea804f2017-11-28 17:11:41 +0800236 opts = parser.parse_args()
237 common.config_logging(opts)
238 opts.func(opts)
239
240
241if __name__ == '__main__':
242 main()