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