Search in sources :

Example 6 with ProcessManagerException

use of com.sun.enterprise.universal.process.ProcessManagerException in project Payara by payara.

the class SSHLauncher method generateKeyPair.

/**
 * Invoke ssh-keygen using ProcessManager API
 */
private boolean generateKeyPair() throws IOException {
    String keygenCmd = findSSHKeygen();
    if (logger.isLoggable(Level.FINER)) {
        logger.finer("Using " + keygenCmd + " to generate key pair");
    }
    if (!setupSSHDir()) {
        throw new IOException("Failed to set proper permissions on .ssh directory");
    }
    StringBuffer k = new StringBuffer();
    List<String> cmdLine = new ArrayList<String>();
    cmdLine.add(keygenCmd);
    k.append(keygenCmd);
    cmdLine.add("-t");
    k.append(" ").append("-t");
    cmdLine.add("rsa");
    k.append(" ").append("rsa");
    cmdLine.add("-N");
    k.append(" ").append("-N");
    if (rawKeyPassPhrase != null && rawKeyPassPhrase.length() > 0) {
        cmdLine.add(rawKeyPassPhrase);
        k.append(" ").append(getPrintablePassword(rawKeyPassPhrase));
    } else {
        // special handling for empty passphrase on Windows
        if (OS.isWindows()) {
            cmdLine.add("\"\"");
            k.append(" ").append("\"\"");
        } else {
            cmdLine.add("");
            k.append(" ").append("");
        }
    }
    cmdLine.add("-f");
    k.append(" ").append("-f");
    cmdLine.add(keyFile);
    k.append(" ").append(keyFile);
    // cmdLine.add("-vvv");
    ProcessManager pm = new ProcessManager(cmdLine);
    if (logger.isLoggable(Level.FINER)) {
        logger.finer("Command = " + k);
    }
    pm.setTimeoutMsec(DEFAULT_TIMEOUT_MSEC);
    if (logger.isLoggable(Level.FINER))
        pm.setEcho(true);
    else
        pm.setEcho(false);
    int exit;
    try {
        exit = pm.execute();
    } catch (ProcessManagerException ex) {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("Error while executing ssh-keygen: " + ex.getMessage());
        }
        exit = 1;
    }
    if (exit == 0) {
        logger.info(keygenCmd + " successfully generated the identification " + keyFile);
    } else {
        if (logger.isLoggable(Level.FINER)) {
            logger.finer(pm.getStderr());
        }
        logger.info(keygenCmd + " failed");
    }
    return (exit == 0) ? true : false;
}
Also used : ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ProcessManager(com.sun.enterprise.universal.process.ProcessManager)

Example 7 with ProcessManagerException

use of com.sun.enterprise.universal.process.ProcessManagerException in project Payara by payara.

the class NucleusTestUtils method cmdDetachWithOutput.

public static NadminReturn cmdDetachWithOutput(final File cmd, final int timeout, final String... args) {
    List<String> command = new ArrayList<String>();
    command.add(cmd.toString());
    command.add("--echo");
    command.add("--detach");
    command.addAll(Arrays.asList(args));
    ProcessManager pm = new ProcessManager(command);
    // the tests may be running unattended -- don't wait forever!
    pm.setTimeoutMsec(timeout);
    pm.setEcho(false);
    pm.setEnvironment(envp);
    int exit;
    String myErr = "";
    try {
        exit = pm.execute();
    } catch (ProcessManagerTimeoutException tex) {
        myErr = "\nProcessManagerTimeoutException: command timed out after " + timeout + " ms.";
        exit = 1;
    } catch (ProcessManagerException ex) {
        exit = 1;
    }
    NadminReturn ret = new NadminReturn(exit, pm.getStdout(), pm.getStderr() + myErr, args[0]);
    write(ret.outAndErr);
    return ret;
}
Also used : ProcessManagerTimeoutException(com.sun.enterprise.universal.process.ProcessManagerTimeoutException) ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) ArrayList(java.util.ArrayList) ProcessManager(com.sun.enterprise.universal.process.ProcessManager)

Example 8 with ProcessManagerException

use of com.sun.enterprise.universal.process.ProcessManagerException in project Payara by payara.

the class NucleusTestUtils method cmdWithOutput.

public static NadminReturn cmdWithOutput(final File cmd, final int timeout, final String... args) {
    List<String> command = new ArrayList<String>();
    command.add(cmd.toString());
    command.add("--echo");
    command.addAll(Arrays.asList(args));
    ProcessManager pm = new ProcessManager(command);
    // the tests may be running unattended -- don't wait forever!
    pm.setTimeoutMsec(timeout);
    pm.setEcho(false);
    pm.setEnvironment(envp);
    int exit;
    String myErr = "";
    try {
        exit = pm.execute();
    } catch (ProcessManagerTimeoutException tex) {
        myErr = "\nProcessManagerTimeoutException: command timed out after " + timeout + " ms.";
        exit = 1;
    } catch (ProcessManagerException ex) {
        ex.printStackTrace();
        myErr = "\n" + ex.getMessage();
        exit = 1;
    }
    NadminReturn ret = new NadminReturn(exit, pm.getStdout(), pm.getStderr() + myErr, args[0]);
    write(ret.outAndErr);
    return ret;
}
Also used : ProcessManagerTimeoutException(com.sun.enterprise.universal.process.ProcessManagerTimeoutException) ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) ArrayList(java.util.ArrayList) ProcessManager(com.sun.enterprise.universal.process.ProcessManager)

Example 9 with ProcessManagerException

use of com.sun.enterprise.universal.process.ProcessManagerException in project Payara by payara.

the class SetupLocalDcom method executeCommand.

@Override
protected int executeCommand() throws CommandException, CommandValidationException {
    try {
        List<String> cmds = new ArrayList<String>();
        cmds.add(CPP_APP.getAbsolutePath());
        if (verbose)
            cmds.add("--verbose");
        ProcessManager pm = new ProcessManager(cmds);
        pm.execute();
        int ret = pm.getExitValue();
        if (verbose || ret != 0)
            logger.info(pm.getStdout() + pm.getStderr());
        return ret;
    } catch (ProcessManagerException ex) {
        throw new CommandException(ex);
    }
}
Also used : ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) ProcessManager(com.sun.enterprise.universal.process.ProcessManager)

Example 10 with ProcessManagerException

use of com.sun.enterprise.universal.process.ProcessManagerException in project Payara by payara.

the class NodeUtils method runAdminCommandOnNode.

/**
 * Run on admin command on a node and handle setting the message in the
 * ActionReport on an error. Note that on success no message is set in
 * the action report
 *
 * @param node  The node to run the command on. Can be local or remote
 * @param command  asadmin command to run. The list must contain all
 *                  parameters to asadmin, but not "asadmin" itself.
 * @param context   The command context. The ActionReport in this
 *                  context will be updated on an error to contain an
 *                  appropriate error message.
 * @param firstErrorMessage The first message to use if an error is
 *                          encountered. Usually something like
 *                          "Could not start instance".
 * @param humanCommand  The command the user should run on the node if
 *                      we failed to run the passed command.
 * @param output        Output from the run command.
 * @param waitForReaderThreads True: wait for the command IO to complete.
 *                      False: don't wait for IO to complete, just for
 *                      process to end.
 *                      Currently this only applies to locally run commands
 *                      and should only be set to false by start-instance
 *                      (see bug 12777).
 */
void runAdminCommandOnNode(Node node, List<String> command, AdminCommandContext context, String firstErrorMessage, String humanCommand, StringBuilder output, boolean waitForReaderThreads) {
    ActionReport report = context.getActionReport();
    boolean failure = true;
    String msg1 = firstErrorMessage;
    String msg2 = "";
    String msg3 = "";
    String nodeHost = node.getNodeHost();
    String nodeName = node.getName();
    String installDir = node.getInstallDir();
    if (output == null) {
        output = new StringBuilder();
    }
    if (StringUtils.ok(humanCommand)) {
        msg3 = Strings.get("node.remote.tocomplete", nodeHost, installDir, humanCommand);
    }
    NodeRunner nr = new NodeRunner(habitat, logger);
    try {
        int status = nr.runAdminCommandOnNode(node, output, waitForReaderThreads, command, context);
        if (status != 0) {
            // Command ran, but didn't succeed. Log full information
            msg2 = Strings.get("node.command.failed", nodeName, nodeHost, output.toString().trim(), nr.getLastCommandRun());
            logger.warning(StringUtils.cat(": ", msg1, msg2, msg3));
            // Don't expose command name to user in case it is a hidden command
            msg2 = Strings.get("node.command.failed.short", nodeName, nodeHost, output.toString().trim());
        } else {
            failure = false;
            logger.info(output.toString().trim());
        }
    } catch (SSHCommandExecutionException ec) {
        msg2 = Strings.get("node.ssh.bad.connect", nodeName, nodeHost, ec.getMessage());
        // Log some extra info
        String msg = Strings.get("node.command.failed.ssh.details", nodeName, nodeHost, ec.getCommandRun(), ec.getMessage(), ec.getSSHSettings());
        logger.warning(StringUtils.cat(": ", msg1, msg, msg3));
    } catch (ProcessManagerException ex) {
        msg2 = Strings.get("node.command.failed.local.details", ex.getMessage(), nr.getLastCommandRun());
        logger.warning(StringUtils.cat(": ", msg1, msg2, msg3));
        // User message doesn't have command that was run
        msg2 = Strings.get("node.command.failed.local.exception", ex.getMessage());
    } catch (UnsupportedOperationException e) {
        msg2 = Strings.get("node.not.ssh", nodeName, nodeHost);
        logger.warning(StringUtils.cat(": ", msg1, msg2, msg3));
    } catch (IllegalArgumentException e) {
        msg2 = e.getMessage();
        logger.warning(StringUtils.cat(": ", msg1, msg2, msg3));
    }
    if (failure) {
        report.setMessage(StringUtils.cat(NL + NL, msg1, msg2, msg3));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
    } else {
        report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
    }
    return;
}
Also used : ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) NodeRunner(org.glassfish.cluster.ssh.connect.NodeRunner) ActionReport(org.glassfish.api.ActionReport)

Aggregations

ProcessManagerException (com.sun.enterprise.universal.process.ProcessManagerException)10 ProcessManager (com.sun.enterprise.universal.process.ProcessManager)9 ArrayList (java.util.ArrayList)4 ProcessManagerTimeoutException (com.sun.enterprise.universal.process.ProcessManagerTimeoutException)2 File (java.io.File)1 IOException (java.io.IOException)1 ActionReport (org.glassfish.api.ActionReport)1 NodeRunner (org.glassfish.cluster.ssh.connect.NodeRunner)1