Search in sources :

Example 1 with ArgumentsParser

use of org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser in project ovirt-engine by oVirt.

the class Main method main.

public static void main(String... args) throws IOException {
    int exitStatus = 1;
    List<String> cmdArgs = new ArrayList<>(Arrays.asList(args));
    substitutions.put("@PROGRAM_NAME@", PROGRAM_NAME);
    try {
        Properties props = new Properties();
        try (InputStream in = Main.class.getResourceAsStream("arguments.properties");
            Reader reader = new InputStreamReader(in)) {
            props.load(reader);
        }
        ArgumentsParser parser = new ArgumentsParser(props, "core");
        parser.getSubstitutions().putAll(substitutions);
        parser.parse(cmdArgs);
        Map<String, Object> argMap = parser.getParsedArgs();
        if ((Boolean) argMap.get("help")) {
            System.out.format("Usage: %s", parser.getUsage());
            throw new ExitException("Help", 0);
        } else if ((Boolean) argMap.get("version")) {
            System.out.format("%s-%s (%s)%n", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_DISPLAY_NAME);
            throw new ExitException("Version", 0);
        }
        if (!parser.getErrors().isEmpty()) {
            for (Throwable t : parser.getErrors()) {
                System.err.format("FATAL: %s%n", t.getMessage());
            }
            throw new ExitException("Parsing error", 1);
        }
        if (cmdArgs.size() < 1) {
            System.err.println("Action not provided");
            throw new ExitException("Action not provided", 1);
        }
        Action action;
        try {
            action = Action.valueOf(cmdArgs.get(0).toUpperCase().replace("-", "_"));
        } catch (IllegalArgumentException e) {
            System.err.printf("Invalid action '%s'%n", cmdArgs.get(0));
            throw new ExitException("Invalid action", 1);
        }
        action.execute(props, cmdArgs);
        exitStatus = 0;
    } catch (ExitException e) {
        exitStatus = e.getExitCode();
    } catch (Exception e) {
        System.err.format("FATAL: %s%n", e.getMessage());
        e.printStackTrace(System.err);
    }
    System.exit(exitStatus);
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Properties(java.util.Properties) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) ArgumentsParser(org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser)

Example 2 with ArgumentsParser

use of org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser in project ovirt-engine by oVirt.

the class AAAServiceImpl method parseArguments.

@Override
public void parseArguments(List<String> args) throws Exception {
    args.remove(0);
    Properties props = new Properties();
    try (InputStream in = AAAServiceImpl.class.getResourceAsStream("arguments.properties");
        Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
        props.load(reader);
    }
    Map<String, String> substitutions = context.get(ContextKeys.CLI_PARSER_SUBSTITUTIONS);
    ArgumentsParser parser = new ArgumentsParser(props, "module");
    parser.getSubstitutions().putAll(substitutions);
    parser.parse(args);
    argModuleMap = parser.getParsedArgs();
    if ((Boolean) argModuleMap.get("help")) {
        System.out.format("Usage: %s", parser.getUsage());
        throw new ExitException("Help", 0);
    }
    if (!parser.getErrors().isEmpty()) {
        for (Throwable t : parser.getErrors()) {
            log.error(t.getMessage());
        }
        throw new ExitException("Parsing error", 1);
    }
    if (args.size() < 1) {
        log.error("Action not provided");
        throw new ExitException("Action not provided", 1);
    }
    try {
        action = Action.valueOf(args.get(0).toUpperCase().replace("-", "_"));
    } catch (IllegalArgumentException e) {
        log.error("Invalid action '{}'", args.get(0));
        throw new ExitException("Invalid action", 1);
    }
    argMap = action.parse(substitutions, props, args);
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Properties(java.util.Properties) ArgumentsParser(org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser) ExitException(org.ovirt.engine.exttool.core.ExitException)

Example 3 with ArgumentsParser

use of org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser in project ovirt-engine by oVirt.

the class ExtensionsToolExecutor method main.

public static void main(String... args) {
    int exitStatus = 1;
    Path tempJAASConf = null;
    List<String> cmdArgs = new ArrayList<>(Arrays.asList(args));
    try {
        tempJAASConf = createTemporaryJAASconfiguration();
        final Map<String, String> contextSubstitutions = new HashMap<>();
        contextSubstitutions.put("@ENGINE_ETC@", ENGINE_ETC);
        contextSubstitutions.put("@PROGRAM_NAME@", PROGRAM_NAME);
        context.put(ModuleService.ContextKeys.CLI_PARSER_SUBSTITUTIONS, contextSubstitutions);
        setupLogger();
        ArgumentsParser parser;
        Map<String, ModuleService> moduleServices = loadModules(ModuleService.class);
        context.put(ModuleService.ContextKeys.MODULES, moduleServices);
        final Map<String, String> substitutions = new HashMap<>(contextSubstitutions);
        substitutions.put("@MODULE_LIST@", getModules(moduleServices));
        try (InputStream stream = ExtensionsToolExecutor.class.getResourceAsStream("arguments.properties")) {
            parser = new ArgumentsParser(stream, "core");
            parser.getSubstitutions().putAll(substitutions);
        }
        parser.parse(cmdArgs);
        Map<String, Object> argMap = parser.getParsedArgs();
        setupLogger(argMap);
        log.debug("Version: {}-{} ({})", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_DISPLAY_NAME);
        if ((Boolean) argMap.get("help")) {
            System.out.format("Usage: %s", parser.getUsage());
            throw new ExitException("Help", 0);
        } else if ((Boolean) argMap.get("version")) {
            System.out.format("%s-%s (%s)%n", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_DISPLAY_NAME);
            throw new ExitException("Version", 0);
        }
        if (!parser.getErrors().isEmpty()) {
            for (Throwable t : parser.getErrors()) {
                log.error(t.getMessage());
                log.debug(t.getMessage(), t);
            }
            throw new ExitException("Parsing error", 1);
        }
        if (cmdArgs.size() < 1) {
            log.error("Please provide module.");
            throw new ExitException("Module not provided", 1);
        }
        String module = cmdArgs.get(0);
        ModuleService moduleService = moduleServices.get(module);
        if (moduleService == null) {
            log.error("No such '{}' module exists.", module);
            throw new ExitException(1);
        }
        moduleService.parseArguments(cmdArgs);
        log.info("========================================================================");
        log.info("============================ Initialization ============================");
        log.info("========================================================================");
        ExtensionsManager extensionsManager = new ExtensionsManager();
        extensionsManager.getGlobalContext().put(Base.GlobalContextKeys.APPLICATION_NAME, Base.ApplicationNames.OVIRT_ENGINE_EXTENSIONS_TOOL);
        context.put(ModuleService.ContextKeys.EXTENSION_MANAGER, extensionsManager);
        loadExtensions(extensionsManager, moduleService, argMap);
        log.info("========================================================================");
        log.info("============================== Execution ===============================");
        log.info("========================================================================");
        moduleService.run();
        exitStatus = 0;
    } catch (ExitException e) {
        log.debug(e.getMessage(), e);
        exitStatus = e.getExitCode();
    } catch (Throwable t) {
        log.error(t.getMessage() != null ? t.getMessage() : t.getClass().getName());
        log.debug("Exception:", t);
    } finally {
        if (tempJAASConf != null) {
            try {
                Files.delete(tempJAASConf);
            } catch (IOException ex) {
                log.warn("Failed to delete temporary file '{}'", tempJAASConf.toString());
            }
        }
    }
    log.debug("Exiting with status '{}'", exitStatus);
    System.exit(exitStatus);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExtensionsManager(org.ovirt.engine.core.extensions.mgr.ExtensionsManager) ArgumentsParser(org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser)

Example 4 with ArgumentsParser

use of org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser in project ovirt-engine by oVirt.

the class InfoServiceImpl method parseArguments.

@Override
public void parseArguments(List<String> args) throws Exception {
    args.remove(0);
    Properties props = new Properties();
    try (InputStream in = InfoServiceImpl.class.getResourceAsStream("info.properties");
        Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
        props.load(reader);
    }
    Map<String, String> substitutions = context.get(ContextKeys.CLI_PARSER_SUBSTITUTIONS);
    ArgumentsParser parser = new ArgumentsParser(props, "module");
    parser.getSubstitutions().putAll(substitutions);
    parser.parse(args);
    argModuleMap = parser.getParsedArgs();
    if ((Boolean) argModuleMap.get("help")) {
        System.out.format("Usage: %s", parser.getUsage());
        throw new ExitException("Help", 0);
    }
    if (!parser.getErrors().isEmpty()) {
        for (Throwable t : parser.getErrors()) {
            log.error(t.getMessage());
        }
        throw new ExitException("Parsing error", 1);
    }
    if (args.size() < 1) {
        log.error("Action not provided");
        throw new ExitException("Action not provided", 1);
    }
    try {
        action = Action.valueOf(args.get(0).toUpperCase().replace("-", "_"));
    } catch (IllegalArgumentException e) {
        log.error("Invalid action '{}'", args.get(0));
        throw new ExitException("Invalid action", 1);
    }
    argMap = action.parse(substitutions, props, args);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Properties(java.util.Properties) ArgumentsParser(org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser)

Example 5 with ArgumentsParser

use of org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser in project ovirt-engine by oVirt.

the class SsoRegistrationToolExecutor method main.

public static void main(String... args) {
    int exitStatus = 1;
    List<String> cmdArgs = new ArrayList<>(Arrays.asList(args));
    try {
        final Map<String, String> contextSubstitutions = new HashMap<>();
        contextSubstitutions.put("@ENGINE_ETC@", ENGINE_ETC);
        contextSubstitutions.put("@PROGRAM_NAME@", PROGRAM_NAME);
        setupLogger();
        ArgumentsParser parser;
        final Map<String, String> substitutions = new HashMap<>(contextSubstitutions);
        try (InputStream stream = SsoRegistrationToolExecutor.class.getResourceAsStream("arguments.properties")) {
            parser = new ArgumentsParser(stream, "core");
            parser.getSubstitutions().putAll(substitutions);
        }
        parser.parse(cmdArgs);
        Map<String, Object> argMap = parser.getParsedArgs();
        setupLogger(argMap);
        log.debug("Version: {}-{} ({})", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_DISPLAY_NAME);
        if ((Boolean) argMap.get("help")) {
            System.out.format("Usage: %s", parser.getUsage());
            throw new ExitException("Help", 0);
        } else if ((Boolean) argMap.get("version")) {
            System.out.format("%s-%s (%s)%n", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_DISPLAY_NAME);
            throw new ExitException("Version", 0);
        }
        if (!parser.getErrors().isEmpty()) {
            for (Throwable t : parser.getErrors()) {
                log.error(t.getMessage());
                log.debug(t.getMessage(), t);
            }
            throw new ExitException("Parsing error", 1);
        }
        DBUtils dbUtils = new DBUtils();
        log.info("=========================================================================");
        log.info("================== oVirt Sso Client Registration Tool ===================");
        log.info("=========================================================================");
        String clientId = getUserInput("Client Id: ");
        String certificateFile = getUserInput("Client CA Certificate File Location: ");
        while (!new File(certificateFile).exists()) {
            System.out.format("%s is not a valid certificate, please enter path to an existing certificate.%n", certificateFile);
            certificateFile = getUserInput("Enter Client CA Certificate File Location: ");
        }
        String callbackPrefix = getUserInput("Callback Prefix URL: ");
        while (!callbackPrefix.startsWith("http") && !callbackPrefix.startsWith("https")) {
            System.out.format("%s is not a valid URL, please enter a proper URL.%n", callbackPrefix);
            callbackPrefix = getUserInput("Enter Callback Prefix URL: ");
        }
        String clientSecret = generateClientSecret();
        String encodedClientSecret = encode(argMap, clientSecret);
        dbUtils.unregisterClient(clientId);
        dbUtils.registerClient(clientId, encodedClientSecret, certificateFile, callbackPrefix);
        String tmpFile = createTmpSsoClientConfFile(clientId, clientSecret, certificateFile, callbackPrefix);
        System.out.println("Client registration completed successfully");
        System.out.format("Client secret has been written to file %s%n", tmpFile);
        log.info("========================================================================");
        log.info("========================= Execution Completed ==========================");
        log.info("========================================================================");
        exitStatus = 0;
    } catch (ExitException e) {
        log.debug(e.getMessage(), e);
        exitStatus = e.getExitCode();
    } catch (Throwable t) {
        t.printStackTrace();
        log.error(t.getMessage() != null ? t.getMessage() : t.getClass().getName());
        log.debug("Exception:", t);
    }
    log.debug("Exiting with status '{}'", exitStatus);
    System.exit(exitStatus);
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) DBUtils(org.ovirt.engine.ssoreg.db.DBUtils) ArgumentsParser(org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser) File(java.io.File)

Aggregations

InputStream (java.io.InputStream)6 ArgumentsParser (org.ovirt.engine.core.uutils.cli.parser.ArgumentsParser)6 InputStreamReader (java.io.InputStreamReader)4 Reader (java.io.Reader)4 Properties (java.util.Properties)4 ArrayList (java.util.ArrayList)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ExitException (org.ovirt.engine.exttool.core.ExitException)2 File (java.io.File)1 Path (java.nio.file.Path)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ExtensionsManager (org.ovirt.engine.core.extensions.mgr.ExtensionsManager)1 DBUtils (org.ovirt.engine.ssoreg.db.DBUtils)1