def main(): """main read function""" # Add a file handler to the global LOGGER log_file = get_config_value('LOG_FILE') log_level = get_config_value('LOG_LEVEL').upper() create_file_handler(LOGGER, log_file, log_level) if len(sys.argv) != 2: LOGGER.error('%s received %s arguments, expected 2', basename(__file__), len(sys.argv)) sys.exit(1) try: read_injected_files(sys.argv[1]) except RuntimeError as runtime_exception: LOGGER.exception(runtime_exception) sys.exit(1) sys.exit(0)
def create_log_handler(): """create log handler for the global LOGGER based on LOG_FILE and LOG_LEVEL""" log_file = config.get_config_value('LOG_FILE') log_level = config.get_config_value('LOG_LEVEL').upper() create_file_handler(LOGGER, log_file, log_level)
def main(): """main command handler""" parser = argparse.ArgumentParser( description='Prepare a cloud image from a virtual disk') parser.add_argument('-a', '--artifacts-dir', required=True, help='Absolute path to the artifacts directory') parser.add_argument('-c', '--check-name', action="store_true", help='Check cloud image name') parser.add_argument('-i', '--input', required=True, help='Absolute path to the input virtual disk') parser.add_argument('-p', '--platform', required=True, help='The cloud type (i.e. aws, gce, azure, alibaba)') parser.add_argument( '-s', '--seed-image-name', default='', help='Use supplied autogenerated seed cloud image name') parser.add_argument('-u', '--user-image-name', default='', help='Use user-supplied cloud image name') args = parser.parse_args() # Check either seed or user cloud image name was provided if (args.seed_image_name == '' and args.user_image_name == '') or \ (args.seed_image_name != '' and args.user_image_name != ''): raise Exception( 'You must provide either --seed-image-name or --user-image-name') # Add a file handler to the global LOGGER log_file = get_config_value('LOG_FILE') log_level = get_config_value('LOG_LEVEL').upper() create_file_handler(LOGGER, log_file, log_level) if args.check_name: # Check name if args.user_image_name == '': raise Exception( '--check-name can only be used with --user-image-name') ImageController.check_valid_name(args.platform, args.user_image_name) else: result = False try: # Prepare image image_controller = ImageController(args.artifacts_dir, args.platform, args.input) image_controller.prepare(args.seed_image_name, args.user_image_name) # If execution came so far, all is well. result = True except RuntimeError as runtime_exce: LOGGER.exception(runtime_exce) finally: # Clean-up image controller and other internal constructs it created. image_controller.clean_up() if result is True: LOGGER.info("SUCCESS: Image generation completed.") else: LOGGER.warning( "FAILURE: Check the log file '%s' and fix the problem " "before re-running.", log_file)