use of org.apache.geode.management.internal.cli.shell.Gfsh in project geode by apache.
the class Launcher method parseOptions.
private int parseOptions(final String... args) {
OptionSet parsedOptions;
try {
parsedOptions = this.commandLineParser.parse(args);
} catch (OptionException e) {
System.err.println(CliStrings.format(MSG_INVALID_COMMAND_OR_OPTION, CliUtil.arrayToString(args)));
return ExitShellRequest.FATAL_EXIT.getExitCode();
}
boolean launchShell = true;
boolean onlyPrintUsage = parsedOptions.has(HELP_OPTION);
if (parsedOptions.has(EXECUTE_OPTION) || onlyPrintUsage) {
launchShell = false;
}
Gfsh gfsh = null;
try {
gfsh = Gfsh.getInstance(launchShell, args, new GfshConfig());
this.startupTimeLogHelper.logStartupTime();
} catch (ClassNotFoundException cnfex) {
log(cnfex, gfsh);
} catch (IOException ioex) {
log(ioex, gfsh);
} catch (IllegalStateException isex) {
System.err.println("ERROR : " + isex.getMessage());
}
ExitShellRequest exitRequest = ExitShellRequest.NORMAL_EXIT;
if (gfsh != null) {
try {
if (launchShell) {
gfsh.start();
gfsh.waitForComplete();
exitRequest = gfsh.getExitShellRequest();
} else if (onlyPrintUsage) {
printUsage(gfsh, System.out);
} else {
@SuppressWarnings("unchecked") List<String> commandsToExecute = (List<String>) parsedOptions.valuesOf(EXECUTE_OPTION);
// Execute all of the commands in the list, one at a time.
for (int i = 0; i < commandsToExecute.size() && exitRequest == ExitShellRequest.NORMAL_EXIT; i++) {
String command = commandsToExecute.get(i);
// sanitize the output string to not show the password
System.out.println(GfshParser.LINE_SEPARATOR + "(" + (i + 1) + ") Executing - " + GfshHistory.redact(command) + GfshParser.LINE_SEPARATOR);
if (!gfsh.executeScriptLine(command) || gfsh.getLastExecutionStatus() != 0) {
exitRequest = ExitShellRequest.FATAL_EXIT;
}
}
}
} catch (InterruptedException iex) {
log(iex, gfsh);
}
}
return exitRequest.getExitCode();
}
use of org.apache.geode.management.internal.cli.shell.Gfsh in project geode by apache.
the class CommandManager method obtainHelp.
public String obtainHelp(String buffer) {
int terminalWidth = -1;
Gfsh gfsh = Gfsh.getCurrentInstance();
if (gfsh != null) {
terminalWidth = gfsh.getTerminalWidth();
}
return helper.getHelp(buffer, terminalWidth);
}
use of org.apache.geode.management.internal.cli.shell.Gfsh in project geode by apache.
the class AbstractResultData method readFileDataAndDump.
/**
* @param byteDataArray
* @throws GfJsonException
* @throws DataFormatException
* @throws IOException
*/
public static void readFileDataAndDump(GfJsonArray byteDataArray, String directory) throws GfJsonException, DataFormatException, IOException {
boolean overwriteAllExisting = false;
int length = byteDataArray.size();
// TODO - Abhishek Make this consistent -
String options = length > 1 ? "(y/N/a)" : "(y/N)";
BYTEARRAY_LOOP: for (int i = 0; i < length; i++) {
GfJsonObject object = byteDataArray.getJSONObject(i);
int fileType = object.getInt(FILE_TYPE_FIELD);
if (fileType != FILE_TYPE_BINARY && fileType != FILE_TYPE_TEXT) {
throw new IllegalArgumentException("Unsupported file type found.");
}
// build file name
byte[] fileNameBytes = null;
String fileName = null;
GfJsonArray fileNameJsonBytes = object.getJSONArray(FILE_NAME_FIELD);
if (fileNameJsonBytes != null) {
// if in gfsh
fileNameBytes = GfJsonArray.toByteArray(fileNameJsonBytes);
fileName = new String(fileNameBytes);
} else {
// if on member
fileName = (String) object.get(FILE_NAME_FIELD);
}
// build file message
byte[] fileMessageBytes = null;
String fileMessage = null;
GfJsonArray fileMessageJsonBytes = object.getJSONArray(FILE_MESSAGE);
if (fileMessageJsonBytes != null) {
// if in gfsh
fileMessageBytes = GfJsonArray.toByteArray(fileMessageJsonBytes);
fileMessage = new String(fileMessageBytes);
} else {
// if on member
fileMessage = (String) object.get(FILE_MESSAGE);
}
String fileDataString = (String) object.get(FILE_DATA_FIELD);
int fileDataLength = (int) object.get(DATA_LENGTH_FIELD);
byte[] byteArray = Base64.getDecoder().decode(fileDataString);
byte[] uncompressBytes = CliUtil.uncompressBytes(byteArray, fileDataLength).getData();
boolean isGfshVM = CliUtil.isGfshVM();
File fileToDumpData = new File(fileName);
if (!fileToDumpData.isAbsolute()) {
if (directory == null || directory.isEmpty()) {
directory = System.getProperty("user.dir", ".");
}
fileToDumpData = new File(directory, fileName);
}
File parentDirectory = fileToDumpData.getParentFile();
if (parentDirectory != null) {
parentDirectory.mkdirs();
}
if (fileToDumpData.exists()) {
String fileExistsMessage = CliStrings.format(CliStrings.ABSTRACTRESULTDATA__MSG__FILE_WITH_NAME_0_EXISTS_IN_1, new Object[] { fileName, fileToDumpData.getParent(), options });
if (isGfshVM) {
Gfsh gfsh = Gfsh.getCurrentInstance();
if (gfsh != null && !gfsh.isQuietMode() && !overwriteAllExisting) {
fileExistsMessage = fileExistsMessage + " Overwrite? " + options + " : ";
String interaction = gfsh.interact(fileExistsMessage);
if ("a".equalsIgnoreCase(interaction.trim())) {
overwriteAllExisting = true;
} else if (!"y".equalsIgnoreCase(interaction.trim())) {
// do not save file & continue
continue BYTEARRAY_LOOP;
}
}
} else {
throw new IOException(fileExistsMessage);
}
} else if (!parentDirectory.exists()) {
handleCondition(CliStrings.format(CliStrings.ABSTRACTRESULTDATA__MSG__PARENT_DIRECTORY_OF_0_DOES_NOT_EXIST, fileToDumpData.getAbsolutePath()), isGfshVM);
return;
} else if (!parentDirectory.canWrite()) {
handleCondition(CliStrings.format(CliStrings.ABSTRACTRESULTDATA__MSG__PARENT_DIRECTORY_OF_0_IS_NOT_WRITABLE, fileToDumpData.getAbsolutePath()), isGfshVM);
return;
} else if (!parentDirectory.isDirectory()) {
handleCondition(CliStrings.format(CliStrings.ABSTRACTRESULTDATA__MSG__PARENT_OF_0_IS_NOT_DIRECTORY, fileToDumpData.getAbsolutePath()), isGfshVM);
return;
}
if (fileType == FILE_TYPE_TEXT) {
FileWriter fw = new FileWriter(fileToDumpData);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(new String(uncompressBytes));
bw.flush();
fw.flush();
fw.close();
} else if (fileType == FILE_TYPE_BINARY) {
FileOutputStream fos = new FileOutputStream(fileToDumpData);
fos.write(uncompressBytes);
fos.flush();
fos.close();
}
// System.out.println("fileMessage :: "+fileMessage);
if (fileMessage != null && !fileMessage.isEmpty()) {
if (isGfshVM) {
Gfsh.println(MessageFormat.format(fileMessage, new Object[] { fileToDumpData.getAbsolutePath() }));
}
}
// System.out.println(new String(uncompressed));
}
}
use of org.apache.geode.management.internal.cli.shell.Gfsh in project geode by apache.
the class ShellCommands method exit.
@CliCommand(value = { CliStrings.EXIT, "quit" }, help = CliStrings.EXIT__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GFSH })
public ExitShellRequest exit() throws IOException {
Gfsh gfshInstance = getGfsh();
gfshInstance.stop();
ExitShellRequest exitShellRequest = gfshInstance.getExitShellRequest();
if (exitShellRequest == null) {
// shouldn't really happen, but we'll fallback to this anyway
exitShellRequest = ExitShellRequest.NORMAL_EXIT;
}
return exitShellRequest;
}
use of org.apache.geode.management.internal.cli.shell.Gfsh in project geode by apache.
the class ShellCommands method describeConnection.
@CliCommand(value = { CliStrings.DESCRIBE_CONNECTION }, help = CliStrings.DESCRIBE_CONNECTION__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GFSH, CliStrings.TOPIC_GEODE_JMX })
public Result describeConnection() {
Result result = null;
try {
TabularResultData tabularResultData = ResultBuilder.createTabularResultData();
Gfsh gfshInstance = getGfsh();
if (gfshInstance.isConnectedAndReady()) {
OperationInvoker operationInvoker = gfshInstance.getOperationInvoker();
// tabularResultData.accumulate("Monitored GemFire DS", operationInvoker.toString());
tabularResultData.accumulate("Connection Endpoints", operationInvoker.toString());
} else {
tabularResultData.accumulate("Connection Endpoints", "Not connected");
}
result = ResultBuilder.buildResult(tabularResultData);
} catch (Exception e) {
ErrorResultData errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(e.getMessage());
result = ResultBuilder.buildResult(errorResultData);
}
return result;
}
Aggregations