Search in sources :

Example 21 with Gfsh

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();
}
Also used : GfshConfig(org.apache.geode.management.internal.cli.shell.GfshConfig) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) OptionException(joptsimple.OptionException) List(java.util.List) IOException(java.io.IOException) OptionSet(joptsimple.OptionSet) ExitShellRequest(org.springframework.shell.core.ExitShellRequest)

Example 22 with Gfsh

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);
}
Also used : Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh)

Example 23 with Gfsh

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));
    }
}
Also used : GfJsonArray(org.apache.geode.management.internal.cli.json.GfJsonArray) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) FileOutputStream(java.io.FileOutputStream) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) File(java.io.File)

Example 24 with Gfsh

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;
}
Also used : Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) ExitShellRequest(org.springframework.shell.core.ExitShellRequest) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 25 with Gfsh

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;
}
Also used : TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) RestHttpOperationInvoker(org.apache.geode.management.internal.web.shell.RestHttpOperationInvoker) OperationInvoker(org.apache.geode.management.internal.cli.shell.OperationInvoker) HttpOperationInvoker(org.apache.geode.management.internal.web.shell.HttpOperationInvoker) JmxOperationInvoker(org.apache.geode.management.internal.cli.shell.JmxOperationInvoker) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Result(org.apache.geode.management.cli.Result) ConnectToLocatorResult(org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Aggregations

Gfsh (org.apache.geode.management.internal.cli.shell.Gfsh)40 CommandResult (org.apache.geode.management.internal.cli.result.CommandResult)14 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)13 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)13 Test (org.junit.Test)13 IOException (java.io.IOException)11 CliMetaData (org.apache.geode.management.cli.CliMetaData)7 CliCommand (org.springframework.shell.core.annotation.CliCommand)7 TreeSet (java.util.TreeSet)6 ConnectToLocatorResult (org.apache.geode.management.internal.cli.domain.ConnectToLocatorResult)6 File (java.io.File)5 MalformedURLException (java.net.MalformedURLException)5 Result (org.apache.geode.management.cli.Result)5 AuthenticationFailedException (org.apache.geode.security.AuthenticationFailedException)5 ConverterHint (org.apache.geode.management.cli.ConverterHint)3 JmxOperationInvoker (org.apache.geode.management.internal.cli.shell.JmxOperationInvoker)3 ConnectionEndpoint (org.apache.geode.management.internal.cli.util.ConnectionEndpoint)3 HttpOperationInvoker (org.apache.geode.management.internal.web.shell.HttpOperationInvoker)3 RestHttpOperationInvoker (org.apache.geode.management.internal.web.shell.RestHttpOperationInvoker)3 ExitShellRequest (org.springframework.shell.core.ExitShellRequest)3