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. |
| 6 | """Switcher for ChromeOS prebuilt""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | import argparse |
| 10 | import logging |
| 11 | import os |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 12 | import sys |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 13 | |
| 14 | from bisect_kit import cli |
| 15 | from bisect_kit import common |
| 16 | from bisect_kit import configure |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 17 | from bisect_kit import cros_lab_util |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 18 | from bisect_kit import cros_util |
| 19 | |
| 20 | logger = logging.getLogger(__name__) |
| 21 | |
| 22 | |
| 23 | def create_argument_parser(): |
| 24 | parser = argparse.ArgumentParser(description=__doc__) |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 25 | cli.patching_argparser_exit(parser) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 26 | common.add_common_arguments(parser) |
| 27 | parser.add_argument( |
Kuang-che Wu | 0ebbf7c | 2019-08-28 18:19:19 +0800 | [diff] [blame] | 28 | '--dut', |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 29 | type=cli.argtype_notempty, |
| 30 | metavar='DUT', |
| 31 | default=configure.get('DUT', '')) |
| 32 | parser.add_argument( |
| 33 | 'version', |
| 34 | nargs='?', |
| 35 | type=cros_util.argtype_cros_version, |
| 36 | metavar='CROS_VERSION', |
| 37 | default=configure.get('CROS_VERSION', ''), |
| 38 | help='ChromeOS version number, short (10162.0.0) or full (R64-10162.0.0)') |
| 39 | parser.add_argument( |
| 40 | '--board', |
| 41 | metavar='BOARD', |
| 42 | default=configure.get('BOARD', ''), |
| 43 | help='ChromeOS board name') |
| 44 | parser.add_argument( |
| 45 | '--clobber-stateful', |
Kuang-che Wu | 86fbde5 | 2019-01-18 15:41:00 +0800 | [diff] [blame] | 46 | '--clobber_stateful', |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 47 | action='store_true', |
| 48 | help='Clobber stateful partition when performing update') |
| 49 | parser.add_argument( |
| 50 | '--no-disable-rootfs-verification', |
Kuang-che Wu | 86fbde5 | 2019-01-18 15:41:00 +0800 | [diff] [blame] | 51 | '--no_disable_rootfs_verification', |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 52 | dest='disable_rootfs_verification', |
| 53 | action='store_false', |
| 54 | help="Don't disable rootfs verification after update is completed") |
| 55 | parser.add_argument( |
| 56 | '--default_chromeos_root', |
| 57 | type=cli.argtype_dir_path, |
| 58 | default=configure.get('DEFAULT_CHROMEOS_ROOT', |
Kuang-che Wu | 927231f | 2018-07-24 14:21:56 +0800 | [diff] [blame] | 59 | os.path.expanduser('~/chromiumos')), |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 60 | help='Default chromeos tree to run "cros flash" (default: %(default)s)') |
| 61 | |
| 62 | return parser |
| 63 | |
| 64 | |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 65 | def switch(opts): |
| 66 | # TODO(kcwu): clear cache of cros flash |
Zheng-Jie Chang | 127c330 | 2019-09-10 17:17:04 +0800 | [diff] [blame^] | 67 | if cros_util.is_cros_snapshot_version(opts.version): |
| 68 | image_path = cros_util.prepare_snapshot_image(opts.default_chromeos_root, |
| 69 | opts.board, opts.version) |
| 70 | else: |
| 71 | image_path = cros_util.prepare_prebuilt_image(opts.default_chromeos_root, |
| 72 | opts.board, opts.version) |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 73 | |
| 74 | if cros_util.cros_flash_with_retry( |
| 75 | opts.default_chromeos_root, |
| 76 | opts.dut, |
| 77 | opts.board, |
| 78 | image_path, |
| 79 | version=opts.version, |
| 80 | clobber_stateful=opts.clobber_stateful, |
| 81 | disable_rootfs_verification=opts.disable_rootfs_verification, |
| 82 | repair_callback=cros_lab_util.repair): |
| 83 | return 0 |
| 84 | return 1 |
| 85 | |
| 86 | |
Kuang-che Wu | fe1e88a | 2019-09-10 21:52:25 +0800 | [diff] [blame] | 87 | @cli.fatal_error_handler |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 88 | def main(args=None): |
| 89 | common.init() |
| 90 | parser = create_argument_parser() |
| 91 | opts = parser.parse_args(args) |
| 92 | common.config_logging(opts) |
| 93 | |
Kuang-che Wu | 4427814 | 2019-03-04 11:33:57 +0800 | [diff] [blame] | 94 | if not cros_util.is_good_dut(opts.dut): |
| 95 | logger.error('%r is not a good DUT', opts.dut) |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 96 | if not cros_lab_util.repair(opts.dut): |
Kuang-che Wu | 0476d1f | 2019-03-04 19:27:01 +0800 | [diff] [blame] | 97 | sys.exit(cli.EXIT_CODE_FATAL) |
Kuang-che Wu | 414d67f | 2019-05-28 11:28:57 +0800 | [diff] [blame] | 98 | if not opts.board: |
| 99 | opts.board = cros_util.query_dut_board(opts.dut) |
| 100 | |
| 101 | try: |
| 102 | returncode = switch(opts) |
| 103 | except Exception: |
| 104 | logger.exception('switch failed') |
| 105 | returncode = 1 |
| 106 | |
| 107 | # No matter switching succeeded or not, DUT must be in good state. |
| 108 | # switch() already tried repairing if possible, no repair here. |
| 109 | if not cros_util.is_good_dut(opts.dut): |
| 110 | logger.fatal('%r is not a good DUT', opts.dut) |
| 111 | returncode = cli.EXIT_CODE_FATAL |
| 112 | logger.info('done') |
| 113 | sys.exit(returncode) |
Kuang-che Wu | 2ea804f | 2017-11-28 17:11:41 +0800 | [diff] [blame] | 114 | |
| 115 | |
| 116 | if __name__ == '__main__': |
| 117 | main() |