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 27 28DEFAULT_CONFIG_FILE = "acloud.config" 29 30# Commands 31CMD_SWAP_KERNEL = "swap_kernel" 32 33 34def _ParseArgs(args): 35 """Parse args. 36 37 Args: 38 args: argument list passed from main. 39 40 Returns: 41 Parsed args. 42 """ 43 parser = argparse.ArgumentParser() 44 subparsers = parser.add_subparsers() 45 46 swap_kernel_parser = subparsers.add_parser(CMD_SWAP_KERNEL) 47 swap_kernel_parser.required = False 48 swap_kernel_parser.set_defaults(which=CMD_SWAP_KERNEL) 49 swap_kernel_parser.add_argument( 50 "--instance_name", 51 type=str, 52 dest="instance_name", 53 required=True, 54 help="The names of the instances that will have their kernels swapped, " 55 "separated by spaces, e.g. --instance_names instance-1 instance-2") 56 swap_kernel_parser.add_argument( 57 "--local_kernel_image", 58 type=str, 59 dest="local_kernel_image", 60 required=True, 61 help="Path to a local disk image to use, e.g /tmp/bzImage") 62 acloud_common.AddCommonArguments(swap_kernel_parser) 63 64 return parser.parse_args(args) 65 66 67def main(argv): 68 """Main entry. 69 70 Args: 71 argv: list of system arguments. 72 73 Returns: 74 0 if success. Non-zero otherwise. 75 """ 76 args = _ParseArgs(argv) 77 config_mgr = config.AcloudConfigManager(args.config_file) 78 cfg = config_mgr.Load() 79 cfg.OverrideWithArgs(args) 80 81 k_swapper = kernel_swapper.KernelSwapper(cfg, args.instance_name) 82 report = k_swapper.SwapKernel(args.local_kernel_image) 83 84 report.Dump(args.report_file) 85 if report.errors: 86 msg = "\n".join(report.errors) 87 sys.stderr.write("Encountered the following errors:\n%s\n" % msg) 88 return 1 89 return 0 90