1#!/usr/bin/env python 2# 3# Copyright 2016 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16"""Acloud Kernel Utility. 17 18This CLI implements additional functionality to acloud CLI. 19""" 20import argparse 21import sys 22 23from acloud.public import acloud_common 24from acloud.public import config 25from acloud.public.acloud_kernel import kernel_swapper 26 27DEFAULT_CONFIG_FILE = "acloud.config" 28 29# Commands 30CMD_SWAP_KERNEL = "swap_kernel" 31 32 33def _ParseArgs(args): 34 """Parse args. 35 36 Args: 37 args: argument list passed from main. 38 39 Returns: 40 Parsed args. 41 """ 42 parser = argparse.ArgumentParser() 43 subparsers = parser.add_subparsers() 44 45 swap_kernel_parser = subparsers.add_parser(CMD_SWAP_KERNEL) 46 swap_kernel_parser.required = False 47 swap_kernel_parser.set_defaults(which=CMD_SWAP_KERNEL) 48 swap_kernel_parser.add_argument( 49 "--instance_name", 50 type=str, 51 dest="instance_name", 52 required=True, 53 help="The names of the instances that will have their kernels swapped, " 54 "separated by spaces, e.g. --instance_names instance-1 instance-2") 55 swap_kernel_parser.add_argument( 56 "--local_kernel_image", 57 type=str, 58 dest="local_kernel_image", 59 required=True, 60 help="Path to a local disk image to use, e.g /tmp/bzImage") 61 acloud_common.AddCommonArguments(swap_kernel_parser) 62 63 return parser.parse_args(args) 64 65 66def main(argv): 67 """Main entry. 68 69 Args: 70 argv: list of system arguments. 71 72 Returns: 73 0 if success. Non-zero otherwise. 74 """ 75 args = _ParseArgs(argv) 76 config_mgr = config.AcloudConfigManager(args.config_file) 77 cfg = config_mgr.Load() 78 cfg.OverrideWithArgs(args) 79 80 k_swapper = kernel_swapper.KernelSwapper(cfg, args.instance_name) 81 report = k_swapper.SwapKernel(args.local_kernel_image) 82 83 report.Dump(args.report_file) 84 if report.errors: 85 msg = "\n".join(report.errors) 86 sys.stderr.write("Encountered the following errors:\n%s\n" % msg) 87 return 1 88 return 0 89