Search in sources :

Example 1 with NodeRunner

use of org.glassfish.cluster.ssh.connect.NodeRunner in project Payara by payara.

the class NodeUtils method getGlassFishVersionOnNode.

/**
 * Get the version string from a glassfish installation on the node.
 * @param node
 * @return version string
 */
String getGlassFishVersionOnNode(Node node, AdminCommandContext context) throws CommandValidationException {
    if (node == null) {
        return "";
    }
    List<String> command = new ArrayList<>();
    command.add("version");
    command.add("--local");
    command.add("--terse");
    NodeRunner nr = new NodeRunner(habitat, logger);
    StringBuilder output = new StringBuilder();
    try {
        int commandStatus = nr.runAdminCommandOnNode(node, output, command, context);
        if (commandStatus != 0) {
            return "unknown version: " + output.toString();
        }
    } catch (Exception e) {
        throw new CommandValidationException(Strings.get("failed.to.run", command.toString(), node.getNodeHost()), e);
    }
    return output.toString().trim();
}
Also used : ArrayList(java.util.ArrayList) NodeRunner(org.glassfish.cluster.ssh.connect.NodeRunner) ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with NodeRunner

use of org.glassfish.cluster.ssh.connect.NodeRunner in project Payara by payara.

the class AddTruststoreEntryCommand method addToTrustStore.

/**
 * Runs the 'add-to-truststore' command on the local instance.
 * @param context The admin command context.
 * @throws CommandException If there's an issue running the command.
 */
private void addToTrustStore(AdminCommandContext context) throws CommandException {
    NodeRunner nodeRunner = new NodeRunner(serviceLocator, context.getLogger());
    Node node = nodes.getNode(servers.getServer(serverEnvironment.getInstanceName()).getNodeRef());
    // The DAS doesn't have a node-ref
    if (node == null && serverEnvironment.isDas()) {
        node = nodes.getDefaultLocalNode();
    }
    try {
        StringBuilder stringBuilder = new StringBuilder();
        nodeRunner.runAdminCommandOnNode(node, stringBuilder, createAddToStoreCommand("add-to-truststore", node, new File(filePath), alias), context);
        if (stringBuilder.toString().contains("Command add-to-truststore failed")) {
            throw new CommandException();
        }
    } catch (SSHCommandExecutionException | ProcessManagerException e) {
        throw new CommandException(e);
    }
}
Also used : ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) Node(com.sun.enterprise.config.serverbeans.Node) NodeRunner(org.glassfish.cluster.ssh.connect.NodeRunner) CommandException(org.glassfish.api.admin.CommandException) File(java.io.File) SSHCommandExecutionException(org.glassfish.api.admin.SSHCommandExecutionException)

Example 3 with NodeRunner

use of org.glassfish.cluster.ssh.connect.NodeRunner 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);
    }
}
Also used : ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) NodeRunner(org.glassfish.cluster.ssh.connect.NodeRunner) ActionReport(org.glassfish.api.ActionReport)

Example 4 with NodeRunner

use of org.glassfish.cluster.ssh.connect.NodeRunner in project Payara by payara.

the class AddKeystoreEntryCommand method addToKeyStore.

/**
 * Runs the 'add-to-keystore' command on the local instance.
 * @param context The admin command context.
 * @throws CommandException If there's an issue running the command.
 */
private void addToKeyStore(AdminCommandContext context) throws CommandException {
    NodeRunner nodeRunner = new NodeRunner(serviceLocator, context.getLogger());
    Node node = nodes.getNode(servers.getServer(serverEnvironment.getInstanceName()).getNodeRef());
    // The DAS doesn't have a node-ref
    if (node == null && serverEnvironment.isDas()) {
        node = nodes.getDefaultLocalNode();
    }
    try {
        StringBuilder stringBuilder = new StringBuilder();
        nodeRunner.runAdminCommandOnNode(node, stringBuilder, createAddToStoreCommand("add-to-keystore", node, new File(filePath), alias), context);
        if (stringBuilder.toString().contains("Command add-to-keystore failed")) {
            throw new CommandException();
        }
    } catch (SSHCommandExecutionException | ProcessManagerException e) {
        throw new CommandException(e);
    }
}
Also used : ProcessManagerException(com.sun.enterprise.universal.process.ProcessManagerException) Node(com.sun.enterprise.config.serverbeans.Node) NodeRunner(org.glassfish.cluster.ssh.connect.NodeRunner) CommandException(org.glassfish.api.admin.CommandException) File(java.io.File) SSHCommandExecutionException(org.glassfish.api.admin.SSHCommandExecutionException)

Aggregations

ProcessManagerException (com.sun.enterprise.universal.process.ProcessManagerException)4 NodeRunner (org.glassfish.cluster.ssh.connect.NodeRunner)4 Node (com.sun.enterprise.config.serverbeans.Node)2 File (java.io.File)2 CommandException (org.glassfish.api.admin.CommandException)2 SSHCommandExecutionException (org.glassfish.api.admin.SSHCommandExecutionException)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 ActionReport (org.glassfish.api.ActionReport)1