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();
}
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);
}
}
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);
}
}
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);
}
}
Aggregations