use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.
the class ShellCommands method executeCommand.
private static InfoResultData executeCommand(Gfsh gfsh, String userCommand, boolean useConsole) throws IOException {
InfoResultData infoResultData = ResultBuilder.createInfoResultData();
String cmdToExecute = userCommand;
String cmdExecutor = "/bin/sh";
String cmdExecutorOpt = "-c";
if (SystemUtils.isWindows()) {
cmdExecutor = "cmd";
cmdExecutorOpt = "/c";
} else if (useConsole) {
cmdToExecute = cmdToExecute + " </dev/tty >/dev/tty";
}
String[] commandArray = { cmdExecutor, cmdExecutorOpt, cmdToExecute };
ProcessBuilder builder = new ProcessBuilder();
builder.command(commandArray);
builder.directory();
builder.redirectErrorStream();
Process proc = builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String lineRead = "";
while ((lineRead = input.readLine()) != null) {
infoResultData.addLine(lineRead);
}
proc.getOutputStream().close();
try {
if (proc.waitFor() != 0) {
gfsh.logWarning("The command '" + userCommand + "' did not complete successfully", null);
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e.getMessage(), e);
}
return infoResultData;
}
use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.
the class ShellCommands method jmxConnect.
private Result jmxConnect(Map<String, String> sslConfigProps, ConnectionEndpoint memberRmiHostPort, ConnectionEndpoint locatorTcpHostPort, boolean useSsl, String userName, String passwordToUse, String gfSecurityPropertiesPath, boolean retry) {
ConnectionEndpoint hostPortToConnect = null;
Gfsh gfsh = getGfsh();
try {
// locator to find the rmi host port
if (memberRmiHostPort != null) {
hostPortToConnect = memberRmiHostPort;
} else {
// Used for gfsh->locator connection & not needed for gfsh->manager connection
if (useSsl || !sslConfigProps.isEmpty()) {
sslConfigProps.put(MCAST_PORT, String.valueOf(0));
sslConfigProps.put(LOCATORS, "");
String sslInfoLogMsg = "Connecting to Locator via SSL.";
if (useSsl) {
sslInfoLogMsg = CliStrings.CONNECT__USE_SSL + " is set to true. " + sslInfoLogMsg;
}
gfsh.logToFile(sslInfoLogMsg, null);
}
Gfsh.println(CliStrings.format(CliStrings.CONNECT__MSG__CONNECTING_TO_LOCATOR_AT_0, new Object[] { locatorTcpHostPort.toString(false) }));
ConnectToLocatorResult connectToLocatorResult = connectToLocator(locatorTcpHostPort.getHost(), locatorTcpHostPort.getPort(), CONNECT_LOCATOR_TIMEOUT_MS, sslConfigProps);
hostPortToConnect = connectToLocatorResult.getMemberEndpoint();
// (jmx-manager-ssl=false)
if ((useSsl || !sslConfigProps.isEmpty()) && !connectToLocatorResult.isJmxManagerSslEnabled()) {
gfsh.logInfo(CliStrings.CONNECT__USE_SSL + " is set to true. But JMX Manager doesn't support SSL, connecting without SSL.", null);
sslConfigProps.clear();
}
}
if (!sslConfigProps.isEmpty()) {
gfsh.logToFile("Connecting to manager via SSL.", null);
}
// print out the connecting endpoint
if (!retry) {
Gfsh.println(CliStrings.format(CliStrings.CONNECT__MSG__CONNECTING_TO_MANAGER_AT_0, new Object[] { hostPortToConnect.toString(false) }));
}
InfoResultData infoResultData = ResultBuilder.createInfoResultData();
JmxOperationInvoker operationInvoker = new JmxOperationInvoker(hostPortToConnect.getHost(), hostPortToConnect.getPort(), userName, passwordToUse, sslConfigProps, gfSecurityPropertiesPath);
gfsh.setOperationInvoker(operationInvoker);
infoResultData.addLine(CliStrings.format(CliStrings.CONNECT__MSG__SUCCESS, hostPortToConnect.toString(false)));
LogWrapper.getInstance().info(CliStrings.format(CliStrings.CONNECT__MSG__SUCCESS, hostPortToConnect.toString(false)));
return ResultBuilder.buildResult(infoResultData);
} catch (Exception e) {
// all other exceptions, just logs it and returns a connection error
if (!(e instanceof SecurityException) && !(e instanceof AuthenticationFailedException)) {
return handleExcpetion(e, hostPortToConnect);
}
// connection error
if (userName != null) {
return handleExcpetion(e, hostPortToConnect);
}
// otherwise, prompt for username and password and retry the conenction
try {
userName = gfsh.readText(CliStrings.CONNECT__USERNAME + ": ");
passwordToUse = gfsh.readPassword(CliStrings.CONNECT__PASSWORD + ": ");
// avoid a stack overflow.
if (userName == null && passwordToUse == null)
return handleExcpetion(e, hostPortToConnect);
return jmxConnect(sslConfigProps, hostPortToConnect, null, useSsl, userName, passwordToUse, gfSecurityPropertiesPath, true);
} catch (IOException ioe) {
return handleExcpetion(ioe, hostPortToConnect);
}
} finally {
Gfsh.redirectInternalJavaLoggers();
}
}
use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.
the class StartVsdCommand method startVsd.
@CliCommand(value = CliStrings.START_VSD, help = CliStrings.START_VSD__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_M_AND_M, CliStrings.TOPIC_GEODE_STATISTICS })
public Result startVsd(@CliOption(key = CliStrings.START_VSD__FILE, help = CliStrings.START_VSD__FILE__HELP) final String[] statisticsArchiveFilePathnames) {
try {
String geodeHome = System.getenv("GEODE_HOME");
assertState(StringUtils.isNotBlank(geodeHome), CliStrings.GEODE_HOME_NOT_FOUND_ERROR_MESSAGE);
assertState(IOUtils.isExistingPathname(getPathToVsd()), String.format(CliStrings.START_VSD__NOT_FOUND_ERROR_MESSAGE, geodeHome));
String[] vsdCommandLine = createdVsdCommandLine(statisticsArchiveFilePathnames);
if (isDebugging()) {
getGfsh().printAsInfo(String.format("GemFire VSD command-line (%1$s)", Arrays.toString(vsdCommandLine)));
}
Process vsdProcess = Runtime.getRuntime().exec(vsdCommandLine);
getGfsh().printAsInfo(CliStrings.START_VSD__RUN);
String vsdProcessOutput = waitAndCaptureProcessStandardErrorStream(vsdProcess);
InfoResultData infoResultData = ResultBuilder.createInfoResultData();
if (StringUtils.isNotBlank(vsdProcessOutput)) {
infoResultData.addLine(StringUtils.LINE_SEPARATOR);
infoResultData.addLine(vsdProcessOutput);
}
return ResultBuilder.buildResult(infoResultData);
} catch (GemFireException | IllegalStateException | IllegalArgumentException | FileNotFoundException e) {
return ResultBuilder.createShellClientErrorResult(e.getMessage());
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable t) {
SystemFailure.checkFailure();
return ResultBuilder.createShellClientErrorResult(String.format(CliStrings.START_VSD__ERROR_MESSAGE, toString(t, false)));
}
}
use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.
the class StatusLocatorCommand method createStatusLocatorResult.
protected Result createStatusLocatorResult(final LocatorLauncher.LocatorState state) throws NumberFormatException, IOException, ClassNotFoundException {
InfoResultData infoResultData = ResultBuilder.createInfoResultData();
infoResultData.addLine(state.toString());
infoResultData.addLine(ClusterConfigurationStatusRetriever.fromLocator(state));
return ResultBuilder.buildResult(infoResultData);
}
use of org.apache.geode.management.internal.cli.result.InfoResultData in project geode by apache.
the class ConfigCommands method exportConfig.
/**
* Export the cache configuration in XML format.
*
* @param member Member for which to write the configuration
* @param group Group or groups for which to write the configuration
* @return Results of the attempt to write the configuration
*/
@CliCommand(value = { CliStrings.EXPORT_CONFIG }, help = CliStrings.EXPORT_CONFIG__HELP)
@CliMetaData(interceptor = "org.apache.geode.management.internal.cli.commands.ConfigCommands$Interceptor", relatedTopic = { CliStrings.TOPIC_GEODE_CONFIG })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public Result exportConfig(@CliOption(key = { CliStrings.EXPORT_CONFIG__MEMBER }, optionContext = ConverterHint.ALL_MEMBER_IDNAME, help = CliStrings.EXPORT_CONFIG__MEMBER__HELP) String[] member, @CliOption(key = { CliStrings.EXPORT_CONFIG__GROUP }, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.EXPORT_CONFIG__GROUP__HELP) String[] group, @CliOption(key = { CliStrings.EXPORT_CONFIG__DIR }, help = CliStrings.EXPORT_CONFIG__DIR__HELP) String dir) {
InfoResultData infoData = ResultBuilder.createInfoResultData();
Set<DistributedMember> targetMembers = CliUtil.findMembers(group, member);
if (targetMembers.isEmpty()) {
return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
}
try {
ResultCollector<?, ?> rc = CliUtil.executeFunction(this.exportConfigFunction, null, targetMembers);
List<CliFunctionResult> results = CliFunctionResult.cleanResults((List<?>) rc.getResult());
for (CliFunctionResult result : results) {
if (result.getThrowable() != null) {
infoData.addLine(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__EXCEPTION, result.getMemberIdOrName(), result.getThrowable()));
} else if (result.isSuccessful()) {
String cacheFileName = result.getMemberIdOrName() + "-cache.xml";
String propsFileName = result.getMemberIdOrName() + "-gf.properties";
String[] fileContent = (String[]) result.getSerializables();
infoData.addAsFile(cacheFileName, fileContent[0], "Downloading Cache XML file: {0}", false);
infoData.addAsFile(propsFileName, fileContent[1], "Downloading properties file: {0}", false);
}
}
return ResultBuilder.buildResult(infoData);
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable th) {
SystemFailure.checkFailure();
th.printStackTrace(System.err);
return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__EXCEPTION, th.getClass().getName() + ": " + th.getMessage()));
}
}
Aggregations