Search in sources :

Example 86 with CliCommand

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

the class StartJConsoleCommand method startJConsole.

@CliCommand(value = CliStrings.START_JCONSOLE, help = CliStrings.START_JCONSOLE__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_MANAGER, CliStrings.TOPIC_GEODE_JMX, CliStrings.TOPIC_GEODE_M_AND_M })
public Result startJConsole(@CliOption(key = CliStrings.START_JCONSOLE__INTERVAL, unspecifiedDefaultValue = "4", help = CliStrings.START_JCONSOLE__INTERVAL__HELP) final int interval, @CliOption(key = CliStrings.START_JCONSOLE__NOTILE, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.START_JCONSOLE__NOTILE__HELP) final boolean notile, @CliOption(key = CliStrings.START_JCONSOLE__PLUGINPATH, help = CliStrings.START_JCONSOLE__PLUGINPATH__HELP) final String pluginpath, @CliOption(key = CliStrings.START_JCONSOLE__VERSION, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = CliStrings.START_JCONSOLE__VERSION__HELP) final boolean version, @CliOption(key = CliStrings.START_JCONSOLE__J, optionContext = GfshParser.J_OPTION_CONTEXT, help = CliStrings.START_JCONSOLE__J__HELP) final String[] jvmArgs) {
    try {
        String[] jconsoleCommandLine = createJConsoleCommandLine(null, interval, notile, pluginpath, version, jvmArgs);
        if (isDebugging()) {
            getGfsh().printAsInfo(String.format("JConsole command-line ($1%s)", Arrays.toString(jconsoleCommandLine)));
        }
        Process jconsoleProcess = Runtime.getRuntime().exec(jconsoleCommandLine);
        StringBuilder message = new StringBuilder();
        if (version) {
            jconsoleProcess.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(jconsoleProcess.getErrorStream()));
            for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                message.append(line);
                message.append(StringUtils.LINE_SEPARATOR);
            }
            IOUtils.close(reader);
        } else {
            getGfsh().printAsInfo(CliStrings.START_JCONSOLE__RUN);
            String jconsoleProcessOutput = waitAndCaptureProcessStandardErrorStream(jconsoleProcess);
            if (StringUtils.isNotBlank(jconsoleProcessOutput)) {
                message.append(StringUtils.LINE_SEPARATOR);
                message.append(jconsoleProcessOutput);
            }
        }
        return ResultBuilder.createInfoResult(message.toString());
    } catch (GemFireException | IllegalStateException | IllegalArgumentException e) {
        return ResultBuilder.createShellClientErrorResult(e.getMessage());
    } catch (IOException e) {
        return ResultBuilder.createShellClientErrorResult(CliStrings.START_JCONSOLE__IO_EXCEPTION_MESSAGE);
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable t) {
        SystemFailure.checkFailure();
        return ResultBuilder.createShellClientErrorResult(String.format(CliStrings.START_JCONSOLE__CATCH_ALL_ERROR_MESSAGE, toString(t, false)));
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) GemFireException(org.apache.geode.GemFireException) BufferedReader(java.io.BufferedReader) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 87 with CliCommand

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

the class StartJVisualVMCommand method startJVisualVM.

@CliCommand(value = CliStrings.START_JVISUALVM, help = CliStrings.START_JVISUALVM__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_MANAGER, CliStrings.TOPIC_GEODE_JMX, CliStrings.TOPIC_GEODE_M_AND_M })
public Result startJVisualVM(@CliOption(key = CliStrings.START_JCONSOLE__J, optionContext = GfshParser.J_OPTION_CONTEXT, help = CliStrings.START_JCONSOLE__J__HELP) final String[] jvmArgs) {
    try {
        String[] jvisualvmCommandLine = createJVisualVMCommandLine(jvmArgs);
        if (isDebugging()) {
            getGfsh().printAsInfo(String.format("JVisualVM command-line (%1$s)", Arrays.toString(jvisualvmCommandLine)));
        }
        Process jvisualvmProcess = Runtime.getRuntime().exec(jvisualvmCommandLine);
        getGfsh().printAsInfo(CliStrings.START_JVISUALVM__RUN);
        String jvisualvmProcessOutput = waitAndCaptureProcessStandardErrorStream(jvisualvmProcess);
        InfoResultData infoResultData = ResultBuilder.createInfoResultData();
        if (StringUtils.isNotBlank(jvisualvmProcessOutput)) {
            infoResultData.addLine(StringUtils.LINE_SEPARATOR);
            infoResultData.addLine(jvisualvmProcessOutput);
        }
        return ResultBuilder.buildResult(infoResultData);
    } catch (GemFireException | IllegalStateException | IllegalArgumentException 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_JVISUALVM__ERROR_MESSAGE, toString(t, false)));
    }
}
Also used : GemFireException(org.apache.geode.GemFireException) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 88 with CliCommand

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

the class StartPulseCommand method startPulse.

@CliCommand(value = CliStrings.START_PULSE, help = CliStrings.START_PULSE__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_MANAGER, CliStrings.TOPIC_GEODE_JMX, CliStrings.TOPIC_GEODE_M_AND_M })
public Result startPulse(@CliOption(key = CliStrings.START_PULSE__URL, unspecifiedDefaultValue = "http://localhost:7070/pulse", help = CliStrings.START_PULSE__URL__HELP) final String url) {
    try {
        if (StringUtils.isNotBlank(url)) {
            browse(URI.create(url));
            return ResultBuilder.createInfoResult(CliStrings.START_PULSE__RUN);
        } else {
            if (isConnectedAndReady()) {
                OperationInvoker operationInvoker = getGfsh().getOperationInvoker();
                ObjectName managerObjectName = (ObjectName) operationInvoker.getAttribute(ManagementConstants.OBJECTNAME__DISTRIBUTEDSYSTEM_MXBEAN, "ManagerObjectName");
                String pulseURL = (String) operationInvoker.getAttribute(managerObjectName.toString(), "PulseURL");
                if (StringUtils.isNotBlank(pulseURL)) {
                    browse(URI.create(pulseURL));
                    return ResultBuilder.createInfoResult(CliStrings.START_PULSE__RUN + " with URL: " + pulseURL);
                } else {
                    String pulseMessage = (String) operationInvoker.getAttribute(managerObjectName.toString(), "StatusMessage");
                    return (StringUtils.isNotBlank(pulseMessage) ? ResultBuilder.createGemFireErrorResult(pulseMessage) : ResultBuilder.createGemFireErrorResult(CliStrings.START_PULSE__URL__NOTFOUND));
                }
            } else {
                return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.GFSH_MUST_BE_CONNECTED_FOR_LAUNCHING_0, "GemFire Pulse"));
            }
        }
    } catch (Exception 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_PULSE__ERROR, toString(t, false)));
    }
}
Also used : OperationInvoker(org.apache.geode.management.internal.cli.shell.OperationInvoker) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 89 with CliCommand

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

the class StatusLocatorCommand method statusLocator.

@CliCommand(value = CliStrings.STATUS_LOCATOR, help = CliStrings.STATUS_LOCATOR__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_LOCATOR, CliStrings.TOPIC_GEODE_LIFECYCLE })
public Result statusLocator(@CliOption(key = CliStrings.STATUS_LOCATOR__MEMBER, optionContext = ConverterHint.LOCATOR_MEMBER_IDNAME, help = CliStrings.STATUS_LOCATOR__MEMBER__HELP) final String member, @CliOption(key = CliStrings.STATUS_LOCATOR__HOST, help = CliStrings.STATUS_LOCATOR__HOST__HELP) final String locatorHost, @CliOption(key = CliStrings.STATUS_LOCATOR__PORT, help = CliStrings.STATUS_LOCATOR__PORT__HELP) final Integer locatorPort, @CliOption(key = CliStrings.STATUS_LOCATOR__PID, help = CliStrings.STATUS_LOCATOR__PID__HELP) final Integer pid, @CliOption(key = CliStrings.STATUS_LOCATOR__DIR, help = CliStrings.STATUS_LOCATOR__DIR__HELP) final String workingDirectory) {
    try {
        if (StringUtils.isNotBlank(member)) {
            if (isConnectedAndReady()) {
                final MemberMXBean locatorProxy = getMemberMXBean(member);
                if (locatorProxy != null) {
                    LocatorLauncher.LocatorState state = LocatorLauncher.LocatorState.fromJson(locatorProxy.status());
                    return createStatusLocatorResult(state);
                } else {
                    return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.STATUS_LOCATOR__NO_LOCATOR_FOUND_FOR_MEMBER_ERROR_MESSAGE, member));
                }
            } else {
                return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.STATUS_SERVICE__GFSH_NOT_CONNECTED_ERROR_MESSAGE, LOCATOR_TERM_NAME));
            }
        } else {
            final LocatorLauncher locatorLauncher = new LocatorLauncher.Builder().setCommand(LocatorLauncher.Command.STATUS).setBindAddress(locatorHost).setDebug(isDebugging()).setPid(pid).setPort(locatorPort).setWorkingDirectory(workingDirectory).build();
            final LocatorLauncher.LocatorState state = locatorLauncher.status();
            return createStatusLocatorResult(state);
        }
    } catch (IllegalArgumentException | IllegalStateException e) {
        return ResultBuilder.createUserErrorResult(e.getMessage());
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable t) {
        SystemFailure.checkFailure();
        return ResultBuilder.createShellClientErrorResult(String.format(CliStrings.STATUS_LOCATOR__GENERAL_ERROR_MESSAGE, getLocatorId(locatorHost, locatorPort), StringUtils.defaultIfBlank(workingDirectory, SystemUtils.CURRENT_DIRECTORY), toString(t, getGfsh().getDebug())));
    }
}
Also used : LocatorLauncher(org.apache.geode.distributed.LocatorLauncher) MemberMXBean(org.apache.geode.management.MemberMXBean) MXBeanProvider.getMemberMXBean(org.apache.geode.management.internal.cli.shell.MXBeanProvider.getMemberMXBean) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 90 with CliCommand

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

the class IndexCommands method createIndex.

@CliCommand(value = CliStrings.CREATE_INDEX, help = CliStrings.CREATE_INDEX__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
public // TODO : Add optionContext for indexName
Result createIndex(@CliOption(key = CliStrings.CREATE_INDEX__NAME, mandatory = true, help = CliStrings.CREATE_INDEX__NAME__HELP) final String indexName, @CliOption(key = CliStrings.CREATE_INDEX__EXPRESSION, mandatory = true, help = CliStrings.CREATE_INDEX__EXPRESSION__HELP) final String indexedExpression, @CliOption(key = CliStrings.CREATE_INDEX__REGION, mandatory = true, optionContext = ConverterHint.REGION_PATH, help = CliStrings.CREATE_INDEX__REGION__HELP) String regionPath, @CliOption(key = CliStrings.CREATE_INDEX__MEMBER, mandatory = false, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.CREATE_INDEX__MEMBER__HELP) final String[] memberNameOrID, @CliOption(key = CliStrings.CREATE_INDEX__TYPE, mandatory = false, unspecifiedDefaultValue = "range", optionContext = ConverterHint.INDEX_TYPE, help = CliStrings.CREATE_INDEX__TYPE__HELP) final String indexType, @CliOption(key = CliStrings.CREATE_INDEX__GROUP, mandatory = false, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.CREATE_INDEX__GROUP__HELP) final String[] group) {
    Result result = null;
    AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
    this.securityService.authorizeRegionManage(regionPath);
    try {
        final Cache cache = CacheFactory.getAnyInstance();
        int idxType = IndexInfo.RANGE_INDEX;
        // Index type check
        if ("range".equalsIgnoreCase(indexType)) {
            idxType = IndexInfo.RANGE_INDEX;
        } else if ("hash".equalsIgnoreCase(indexType)) {
            idxType = IndexInfo.HASH_INDEX;
        } else if ("key".equalsIgnoreCase(indexType)) {
            idxType = IndexInfo.KEY_INDEX;
        } else {
            return ResultBuilder.createUserErrorResult(CliStrings.CREATE_INDEX__INVALID__INDEX__TYPE__MESSAGE);
        }
        if (indexName == null || indexName.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.CREATE_INDEX__INVALID__INDEX__NAME);
        }
        if (indexedExpression == null || indexedExpression.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.CREATE_INDEX__INVALID__EXPRESSION);
        }
        if (StringUtils.isBlank(regionPath) || regionPath.equals(Region.SEPARATOR)) {
            return ResultBuilder.createUserErrorResult(CliStrings.CREATE_INDEX__INVALID__REGIONPATH);
        }
        if (!regionPath.startsWith(Region.SEPARATOR)) {
            regionPath = Region.SEPARATOR + regionPath;
        }
        IndexInfo indexInfo = new IndexInfo(indexName, indexedExpression, regionPath, idxType);
        final Set<DistributedMember> targetMembers = CliUtil.findMembers(group, memberNameOrID);
        if (targetMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
        }
        final ResultCollector<?, ?> rc = CliUtil.executeFunction(createIndexFunction, indexInfo, targetMembers);
        final List<Object> funcResults = (List<Object>) rc.getResult();
        final Set<String> successfulMembers = new TreeSet<String>();
        final Map<String, Set<String>> indexOpFailMap = new HashMap<String, Set<String>>();
        for (final Object funcResult : funcResults) {
            if (funcResult instanceof CliFunctionResult) {
                final CliFunctionResult cliFunctionResult = (CliFunctionResult) funcResult;
                if (cliFunctionResult.isSuccessful()) {
                    successfulMembers.add(cliFunctionResult.getMemberIdOrName());
                    if (xmlEntity.get() == null) {
                        xmlEntity.set(cliFunctionResult.getXmlEntity());
                    }
                } else {
                    final String exceptionMessage = cliFunctionResult.getMessage();
                    Set<String> failedMembers = indexOpFailMap.get(exceptionMessage);
                    if (failedMembers == null) {
                        failedMembers = new TreeSet<String>();
                    }
                    failedMembers.add(cliFunctionResult.getMemberIdOrName());
                    indexOpFailMap.put(exceptionMessage, failedMembers);
                }
            }
        }
        if (!successfulMembers.isEmpty()) {
            final InfoResultData infoResult = ResultBuilder.createInfoResultData();
            infoResult.addLine(CliStrings.CREATE_INDEX__SUCCESS__MSG);
            infoResult.addLine(CliStrings.format(CliStrings.CREATE_INDEX__NAME__MSG, indexName));
            infoResult.addLine(CliStrings.format(CliStrings.CREATE_INDEX__EXPRESSION__MSG, indexedExpression));
            infoResult.addLine(CliStrings.format(CliStrings.CREATE_INDEX__REGIONPATH__MSG, regionPath));
            infoResult.addLine(CliStrings.CREATE_INDEX__MEMBER__MSG);
            int num = 0;
            for (final String memberId : successfulMembers) {
                ++num;
                infoResult.addLine(CliStrings.format(CliStrings.CREATE_INDEX__NUMBER__AND__MEMBER, num, memberId));
            }
            result = ResultBuilder.buildResult(infoResult);
        } else {
            // Group members by the exception thrown.
            final ErrorResultData erd = ResultBuilder.createErrorResultData();
            erd.addLine(CliStrings.format(CliStrings.CREATE_INDEX__FAILURE__MSG, indexName));
            final Set<String> exceptionMessages = indexOpFailMap.keySet();
            for (final String exceptionMessage : exceptionMessages) {
                erd.addLine(exceptionMessage);
                erd.addLine(CliStrings.CREATE_INDEX__EXCEPTION__OCCURRED__ON);
                final Set<String> memberIds = indexOpFailMap.get(exceptionMessage);
                int num = 0;
                for (final String memberId : memberIds) {
                    ++num;
                    erd.addLine(CliStrings.format(CliStrings.CREATE_INDEX__NUMBER__AND__MEMBER, num, memberId));
                }
            }
            result = ResultBuilder.buildResult(erd);
        }
    } catch (Exception e) {
        result = ResultBuilder.createGemFireErrorResult(e.getMessage());
    }
    if (xmlEntity.get() != null) {
        persistClusterConfiguration(result, () -> getSharedConfiguration().addXmlEntity(xmlEntity.get(), group));
    }
    return result;
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) IndexInfo(org.apache.geode.management.internal.cli.domain.IndexInfo) ConverterHint(org.apache.geode.management.cli.ConverterHint) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) TreeSet(java.util.TreeSet) DistributedMember(org.apache.geode.distributed.DistributedMember) ArrayList(java.util.ArrayList) List(java.util.List) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) InternalCache(org.apache.geode.internal.cache.InternalCache) Cache(org.apache.geode.cache.Cache) 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