Search in sources :

Example 1 with SSHCommandExecutionException

use of org.glassfish.api.admin.SSHCommandExecutionException in project Payara by payara.

the class NodeRunnerDcom method runAdminCommandOnRemoteNode.

/*
     * return 0 is success, otherwise failure
     */
public final int runAdminCommandOnRemoteNode(Node thisNode, StringBuilder output, List<String> args, List<String> stdinLines) throws SSHCommandExecutionException, IllegalArgumentException, UnsupportedOperationException {
    String humanreadable = null;
    try {
        this.node = thisNode;
        dcomInfo = new DcomInfo(node);
        List<String> fullcommand = new ArrayList<String>();
        WindowsRemoteAsadmin asadmin = dcomInfo.getAsadmin();
        if (stdinLines != null && !stdinLines.isEmpty())
            setupAuthTokenFile(fullcommand, stdinLines);
        fullcommand.addAll(args);
        humanreadable = dcomInfo.getNadminPath() + " " + commandListToString(fullcommand);
        // This is where the rubber meets the road...
        String out = asadmin.run(fullcommand);
        output.append(out);
        logger.info(Strings.get("remote.command.summary", humanreadable, out));
        return determineStatus(args);
    } catch (WindowsException ex) {
        throw new SSHCommandExecutionException(Strings.get("remote.command.error", ex.getMessage(), humanreadable), ex);
    } finally {
        teardownAuthTokenFile();
    }
}
Also used : WindowsRemoteAsadmin(com.sun.enterprise.util.cluster.windows.process.WindowsRemoteAsadmin) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) DcomInfo(org.glassfish.cluster.ssh.util.DcomInfo) SSHCommandExecutionException(org.glassfish.api.admin.SSHCommandExecutionException)

Example 2 with SSHCommandExecutionException

use of org.glassfish.api.admin.SSHCommandExecutionException 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 SSHCommandExecutionException

use of org.glassfish.api.admin.SSHCommandExecutionException in project Payara by payara.

the class NodeRunnerSsh method runAdminCommandOnRemoteNode.

public int runAdminCommandOnRemoteNode(Node node, StringBuilder output, List<String> args, List<String> stdinLines) throws SSHCommandExecutionException, IllegalArgumentException, UnsupportedOperationException {
    args.add(0, AsadminInput.CLI_INPUT_OPTION);
    // specified to read from System.in
    args.add(1, AsadminInput.SYSTEM_IN_INDICATOR);
    if (!isSshNode(node)) {
        throw new UnsupportedOperationException("Node is not of type SSH");
    }
    String installDir = node.getInstallDirUnixStyle() + "/" + SystemPropertyConstants.getComponentName();
    if (!StringUtils.ok(installDir)) {
        throw new IllegalArgumentException("Node does not have an installDir");
    }
    List<String> fullcommand = new ArrayList<String>();
    // We can just use "nadmin" even on Windows since the SSHD provider
    // will locate the command (.exe or .bat) for us
    fullcommand.add(installDir + "/lib/nadmin");
    fullcommand.addAll(args);
    try {
        lastCommandRun = commandListToString(fullcommand);
        trace("Running command on " + node.getNodeHost() + ": " + lastCommandRun);
        sshL = habitat.getService(SSHLauncher.class);
        sshL.init(node, logger);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        commandStatus = sshL.runCommand(fullcommand, outStream, stdinLines);
        output.append(outStream.toString());
        return commandStatus;
    } catch (IOException ex) {
        String m1 = " Command execution failed. " + ex.getMessage();
        String m2 = "";
        Throwable e2 = ex.getCause();
        if (e2 != null) {
            m2 = e2.getMessage();
        }
        logger.severe("Command execution failed for " + lastCommandRun);
        SSHCommandExecutionException cee = new SSHCommandExecutionException(StringUtils.cat(":", m1, m2));
        cee.setSSHSettings(sshL.toString());
        cee.setCommandRun(lastCommandRun);
        throw cee;
    } catch (java.lang.InterruptedException ei) {
        ei.printStackTrace();
        String m1 = ei.getMessage();
        String m2 = "";
        Throwable e2 = ei.getCause();
        if (e2 != null) {
            m2 = e2.getMessage();
        }
        logger.severe("Command interrupted " + lastCommandRun);
        SSHCommandExecutionException cee = new SSHCommandExecutionException(StringUtils.cat(":", m1, m2));
        cee.setSSHSettings(sshL.toString());
        cee.setCommandRun(lastCommandRun);
        throw cee;
    }
}
Also used : SSHLauncher(org.glassfish.cluster.ssh.launcher.SSHLauncher) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SSHCommandExecutionException(org.glassfish.api.admin.SSHCommandExecutionException)

Example 4 with SSHCommandExecutionException

use of org.glassfish.api.admin.SSHCommandExecutionException 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

SSHCommandExecutionException (org.glassfish.api.admin.SSHCommandExecutionException)4 Node (com.sun.enterprise.config.serverbeans.Node)2 ProcessManagerException (com.sun.enterprise.universal.process.ProcessManagerException)2 File (java.io.File)2 CommandException (org.glassfish.api.admin.CommandException)2 NodeRunner (org.glassfish.cluster.ssh.connect.NodeRunner)2 WindowsException (com.sun.enterprise.util.cluster.windows.process.WindowsException)1 WindowsRemoteAsadmin (com.sun.enterprise.util.cluster.windows.process.WindowsRemoteAsadmin)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 SSHLauncher (org.glassfish.cluster.ssh.launcher.SSHLauncher)1 DcomInfo (org.glassfish.cluster.ssh.util.DcomInfo)1