Search in sources :

Example 76 with CliCommand

use of org.springframework.shell.core.annotation.CliCommand in project geode by apache.

the class WanCommands method resumeGatewaySender.

@CliCommand(value = CliStrings.RESUME_GATEWAYSENDER, help = CliStrings.RESUME_GATEWAYSENDER__HELP)
@CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_WAN)
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result resumeGatewaySender(@CliOption(key = CliStrings.RESUME_GATEWAYSENDER__ID, mandatory = true, optionContext = ConverterHint.GATEWAY_SENDER_ID, help = CliStrings.RESUME_GATEWAYSENDER__ID__HELP) String senderId, @CliOption(key = CliStrings.RESUME_GATEWAYSENDER__GROUP, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.RESUME_GATEWAYSENDER__GROUP__HELP) String[] onGroup, @CliOption(key = CliStrings.RESUME_GATEWAYSENDER__MEMBER, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.RESUME_GATEWAYSENDER__MEMBER__HELP) String[] onMember) {
    Result result = null;
    if (senderId != null) {
        senderId = senderId.trim();
    }
    try {
        InternalCache cache = getCache();
        SystemManagementService service = (SystemManagementService) ManagementService.getExistingManagementService(cache);
        GatewaySenderMXBean bean = null;
        TabularResultData resultData = ResultBuilder.createTabularResultData();
        Set<DistributedMember> dsMembers = CliUtil.findMembers(onGroup, onMember);
        if (dsMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
        }
        for (DistributedMember member : dsMembers) {
            if (cache.getDistributedSystem().getDistributedMember().getId().equals(member.getId())) {
                bean = service.getLocalGatewaySenderMXBean(senderId);
            } else {
                ObjectName objectName = service.getGatewaySenderMBeanName(member, senderId);
                bean = service.getMBeanProxy(objectName, GatewaySenderMXBean.class);
            }
            if (bean != null) {
                if (bean.isRunning()) {
                    if (bean.isPaused()) {
                        bean.resume();
                        accumulateStartResult(resultData, member.getId(), CliStrings.GATEWAY_OK, CliStrings.format(CliStrings.GATEWAY_SENDER_0_IS_RESUMED_ON_MEMBER_1, new Object[] { senderId, member.getId() }));
                    } else {
                        accumulateStartResult(resultData, member.getId(), CliStrings.GATEWAY_ERROR, CliStrings.format(CliStrings.GATEWAY_SENDER_0_IS_NOT_PAUSED_ON_MEMBER_1, new Object[] { senderId, member.getId() }));
                    }
                } else {
                    accumulateStartResult(resultData, member.getId(), CliStrings.GATEWAY_ERROR, CliStrings.format(CliStrings.GATEWAY_SENDER_0_IS_NOT_RUNNING_ON_MEMBER_1, new Object[] { senderId, member.getId() }));
                }
            } else {
                accumulateStartResult(resultData, member.getId(), CliStrings.GATEWAY_ERROR, CliStrings.format(CliStrings.GATEWAY_SENDER_0_IS_NOT_AVAILABLE_ON_MEMBER_1, new Object[] { senderId, member.getId() }));
            }
        }
        result = ResultBuilder.buildResult(resultData);
    } catch (Exception e) {
        LogWrapper.getInstance().warning(CliStrings.GATEWAY_ERROR + CliUtil.stackTraceAsString(e));
        result = ResultBuilder.createGemFireErrorResult(CliStrings.GATEWAY_ERROR + e.getMessage());
    }
    return result;
}
Also used : TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) GatewaySenderMXBean(org.apache.geode.management.GatewaySenderMXBean) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) ExecutionException(java.util.concurrent.ExecutionException) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) ObjectName(javax.management.ObjectName) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 77 with CliCommand

use of org.springframework.shell.core.annotation.CliCommand in project geode by apache.

the class ShellCommands method history.

@CliCommand(value = CliStrings.HISTORY, help = CliStrings.HISTORY__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GFSH })
public Result history(@CliOption(key = { CliStrings.HISTORY__FILE }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.HISTORY__FILE__HELP) String saveHistoryTo, @CliOption(key = { CliStrings.HISTORY__CLEAR }, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.HISTORY__CLEAR__HELP) Boolean clearHistory) {
    // process clear history
    if (clearHistory) {
        return executeClearHistory();
    } else {
        // Process file option
        Gfsh gfsh = Gfsh.getCurrentInstance();
        ErrorResultData errorResultData = null;
        StringBuilder contents = new StringBuilder();
        Writer output = null;
        int historySize = gfsh.getHistorySize();
        String historySizeString = String.valueOf(historySize);
        int historySizeWordLength = historySizeString.length();
        GfshHistory gfshHistory = gfsh.getGfshHistory();
        Iterator<?> it = gfshHistory.entries();
        boolean flagForLineNumbers = !(saveHistoryTo != null && saveHistoryTo.length() > 0);
        long lineNumber = 0;
        while (it.hasNext()) {
            String line = it.next().toString();
            if (line.isEmpty() == false) {
                if (flagForLineNumbers) {
                    lineNumber++;
                    contents.append(String.format("%" + historySizeWordLength + "s  ", lineNumber));
                }
                contents.append(line);
                contents.append(GfshParser.LINE_SEPARATOR);
            }
        }
        try {
            // write to a user file
            if (saveHistoryTo != null && saveHistoryTo.length() > 0) {
                File saveHistoryToFile = new File(saveHistoryTo);
                output = new BufferedWriter(new FileWriter(saveHistoryToFile));
                if (!saveHistoryToFile.exists()) {
                    errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.HISTORY__MSG__FILE_DOES_NOT_EXISTS);
                    return ResultBuilder.buildResult(errorResultData);
                }
                if (!saveHistoryToFile.isFile()) {
                    errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.HISTORY__MSG__FILE_SHOULD_NOT_BE_DIRECTORY);
                    return ResultBuilder.buildResult(errorResultData);
                }
                if (!saveHistoryToFile.canWrite()) {
                    errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine(CliStrings.HISTORY__MSG__FILE_CANNOT_BE_WRITTEN);
                    return ResultBuilder.buildResult(errorResultData);
                }
                output.write(contents.toString());
            }
        } catch (IOException ex) {
            return ResultBuilder.createInfoResult("File error " + ex.getMessage() + " for file " + saveHistoryTo);
        } finally {
            try {
                if (output != null) {
                    output.close();
                }
            } catch (IOException e) {
                errorResultData = ResultBuilder.createErrorResultData().setErrorCode(ResultBuilder.ERRORCODE_DEFAULT).addLine("exception in closing file");
                return ResultBuilder.buildResult(errorResultData);
            }
        }
        if (saveHistoryTo != null && saveHistoryTo.length() > 0) {
            // since written to file no need to display the content
            return ResultBuilder.createInfoResult("Wrote successfully to file " + saveHistoryTo);
        } else {
            return ResultBuilder.createInfoResult(contents.toString());
        }
    }
}
Also used : GfshHistory(org.apache.geode.management.internal.cli.shell.jline.GfshHistory) FileWriter(java.io.FileWriter) IOException(java.io.IOException) ConnectionEndpoint(org.apache.geode.management.internal.cli.util.ConnectionEndpoint) ConverterHint(org.apache.geode.management.cli.ConverterHint) BufferedWriter(java.io.BufferedWriter) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) File(java.io.File) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 78 with CliCommand

use of org.springframework.shell.core.annotation.CliCommand in project geode by apache.

the class ShellCommands method connect.

@CliCommand(value = { CliStrings.CONNECT }, help = CliStrings.CONNECT__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GFSH, CliStrings.TOPIC_GEODE_JMX, CliStrings.TOPIC_GEODE_MANAGER })
public Result connect(@CliOption(key = { CliStrings.CONNECT__LOCATOR }, unspecifiedDefaultValue = ConnectionEndpointConverter.DEFAULT_LOCATOR_ENDPOINTS, optionContext = ConnectionEndpoint.LOCATOR_OPTION_CONTEXT, help = CliStrings.CONNECT__LOCATOR__HELP) ConnectionEndpoint locatorTcpHostPort, @CliOption(key = { CliStrings.CONNECT__JMX_MANAGER }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConnectionEndpoint.JMXMANAGER_OPTION_CONTEXT, help = CliStrings.CONNECT__JMX_MANAGER__HELP) ConnectionEndpoint memberRmiHostPort, @CliOption(key = { CliStrings.CONNECT__USE_HTTP }, mandatory = false, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.CONNECT__USE_HTTP__HELP) boolean useHttp, @CliOption(key = { CliStrings.CONNECT__URL }, mandatory = false, unspecifiedDefaultValue = CliStrings.CONNECT__DEFAULT_BASE_URL, help = CliStrings.CONNECT__URL__HELP) String url, @CliOption(key = { CliStrings.CONNECT__USERNAME }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONNECT__USERNAME__HELP) String userName, @CliOption(key = { CliStrings.CONNECT__PASSWORD }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONNECT__PASSWORD__HELP) String password, @CliOption(key = { CliStrings.CONNECT__KEY_STORE }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONNECT__KEY_STORE__HELP) String keystore, @CliOption(key = { CliStrings.CONNECT__KEY_STORE_PASSWORD }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONNECT__KEY_STORE_PASSWORD__HELP) String keystorePassword, @CliOption(key = { CliStrings.CONNECT__TRUST_STORE }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONNECT__TRUST_STORE__HELP) String truststore, @CliOption(key = { CliStrings.CONNECT__TRUST_STORE_PASSWORD }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONNECT__TRUST_STORE_PASSWORD__HELP) String truststorePassword, @CliOption(key = { CliStrings.CONNECT__SSL_CIPHERS }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONNECT__SSL_CIPHERS__HELP) String sslCiphers, @CliOption(key = { CliStrings.CONNECT__SSL_PROTOCOLS }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONNECT__SSL_PROTOCOLS__HELP) String sslProtocols, @CliOption(key = CliStrings.CONNECT__SECURITY_PROPERTIES, optionContext = ConverterHint.FILE_PATH, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONNECT__SECURITY_PROPERTIES__HELP) final String gfSecurityPropertiesPath, @CliOption(key = { CliStrings.CONNECT__USE_SSL }, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.CONNECT__USE_SSL__HELP) final boolean useSsl) {
    Result result;
    String passwordToUse = decrypt(password);
    String keystoreToUse = keystore;
    String keystorePasswordToUse = keystorePassword;
    String truststoreToUse = truststore;
    String truststorePasswordToUse = truststorePassword;
    String sslCiphersToUse = sslCiphers;
    String sslProtocolsToUse = sslProtocols;
    Gfsh gfsh = getGfsh();
    if (gfsh != null && gfsh.isConnectedAndReady()) {
        return ResultBuilder.createInfoResult("Already connected to: " + getGfsh().getOperationInvoker().toString());
    }
    Map<String, String> sslConfigProps = null;
    try {
        if (userName != null && userName.length() > 0) {
            if (passwordToUse == null || passwordToUse.length() == 0) {
                passwordToUse = gfsh.readPassword(CliStrings.CONNECT__PASSWORD + ": ");
            }
            if (passwordToUse == null || passwordToUse.length() == 0) {
                return ResultBuilder.createConnectionErrorResult(CliStrings.CONNECT__MSG__JMX_PASSWORD_MUST_BE_SPECIFIED);
            }
        }
        sslConfigProps = this.readSSLConfiguration(useSsl, keystoreToUse, keystorePasswordToUse, truststoreToUse, truststorePasswordToUse, sslCiphersToUse, sslProtocolsToUse, gfSecurityPropertiesPath);
    } catch (IOException e) {
        return handleExcpetion(e, null);
    }
    if (useHttp) {
        result = httpConnect(sslConfigProps, useSsl, url, userName, passwordToUse);
    } else {
        result = jmxConnect(sslConfigProps, memberRmiHostPort, locatorTcpHostPort, useSsl, userName, passwordToUse, gfSecurityPropertiesPath, false);
    }
    return result;
}
Also used : Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) 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)

Example 79 with CliCommand

use of org.springframework.shell.core.annotation.CliCommand in project geode by apache.

the class ShellCommands method echo.

@CliCommand(value = { CliStrings.ECHO }, help = CliStrings.ECHO__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GFSH })
public Result echo(@CliOption(key = { CliStrings.ECHO__STR, "" }, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, specifiedDefaultValue = "", mandatory = true, help = CliStrings.ECHO__STR__HELP) String stringToEcho) {
    Result result = null;
    if (stringToEcho.equals("$*")) {
        Gfsh gfshInstance = getGfsh();
        Map<String, String> envMap = gfshInstance.getEnv();
        Set<Entry<String, String>> setEnvMap = envMap.entrySet();
        TabularResultData resultData = buildResultForEcho(setEnvMap);
        result = ResultBuilder.buildResult(resultData);
    } else {
        result = ResultBuilder.createInfoResult(stringToEcho);
    }
    return result;
}
Also used : Entry(java.util.Map.Entry) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) 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)

Example 80 with CliCommand

use of org.springframework.shell.core.annotation.CliCommand in project geode by apache.

the class ShellCommands method executeScript.

@CliCommand(value = { CliStrings.RUN }, help = CliStrings.RUN__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GFSH })
public Result executeScript(@CliOption(key = CliStrings.RUN__FILE, optionContext = ConverterHint.FILE, mandatory = true, help = CliStrings.RUN__FILE__HELP) File file, @CliOption(key = { CliStrings.RUN__QUIET }, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.RUN__QUIET__HELP) boolean quiet, @CliOption(key = { CliStrings.RUN__CONTINUEONERROR }, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.RUN__CONTINUEONERROR__HELP) boolean continueOnError) {
    Result result = null;
    Gfsh gfsh = Gfsh.getCurrentInstance();
    try {
        result = gfsh.executeScript(file, quiet, continueOnError);
    } catch (IllegalArgumentException e) {
        result = ResultBuilder.createShellClientErrorResult(e.getMessage());
    }
    return result;
}
Also used : Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) 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

CliCommand (org.springframework.shell.core.annotation.CliCommand)112 CliMetaData (org.apache.geode.management.cli.CliMetaData)94 Result (org.apache.geode.management.cli.Result)66 DistributedMember (org.apache.geode.distributed.DistributedMember)60 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)58 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)42 InternalCache (org.apache.geode.internal.cache.InternalCache)37 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)35 CommandResultException (org.apache.geode.management.internal.cli.result.CommandResultException)31 ArrayList (java.util.ArrayList)29 List (java.util.List)25 ConverterHint (org.apache.geode.management.cli.ConverterHint)24 IOException (java.io.IOException)20 File (java.io.File)17 InfoResultData (org.apache.geode.management.internal.cli.result.InfoResultData)17 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)16 XmlEntity (org.apache.geode.management.internal.configuration.domain.XmlEntity)16 ExecutionException (java.util.concurrent.ExecutionException)15 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)15 HashSet (java.util.HashSet)14