Search in sources :

Example 76 with CliMetaData

use of org.apache.geode.management.cli.CliMetaData 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 77 with CliMetaData

use of org.apache.geode.management.cli.CliMetaData 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 78 with CliMetaData

use of org.apache.geode.management.cli.CliMetaData in project geode by apache.

the class Helper method addCommand.

public void addCommand(CliCommand command, Method commandMethod) {
    // put all the command synonyms in the command map
    Arrays.stream(command.value()).forEach(cmd -> {
        commands.put(cmd, commandMethod);
    });
    // resolve the hint message for each method
    CliMetaData cliMetaData = commandMethod.getDeclaredAnnotation(CliMetaData.class);
    if (cliMetaData == null)
        return;
    String[] related = cliMetaData.relatedTopic();
    // for hint message, we only need to show the first synonym
    String commandString = command.value()[0];
    if (related == null) {
        return;
    }
    Arrays.stream(related).forEach(topic -> {
        Topic foundTopic = topics.get(topic);
        if (foundTopic == null) {
            throw new IllegalArgumentException("No such topic found in the initial map: " + topic);
        }
        foundTopic.addRelatedCommand(commandString, command.help());
    });
}
Also used : CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 79 with CliMetaData

use of org.apache.geode.management.cli.CliMetaData 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)

Example 80 with CliMetaData

use of org.apache.geode.management.cli.CliMetaData in project geode by apache.

the class IndexCommands method createDefinedIndexes.

@CliCommand(value = CliStrings.CREATE_DEFINED_INDEXES, help = CliStrings.CREATE_DEFINED__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_REGION, CliStrings.TOPIC_GEODE_DATA })
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public // TODO : Add optionContext for indexName
Result createDefinedIndexes(@CliOption(key = CliStrings.CREATE_DEFINED_INDEXES__MEMBER, mandatory = false, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.CREATE_DEFINED_INDEXES__MEMBER__HELP) final String[] memberNameOrID, @CliOption(key = CliStrings.CREATE_DEFINED_INDEXES__GROUP, mandatory = false, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.CREATE_DEFINED_INDEXES__GROUP__HELP) final String[] group) {
    Result result = null;
    AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
    if (indexDefinitions.isEmpty()) {
        final InfoResultData infoResult = ResultBuilder.createInfoResultData();
        infoResult.addLine(CliStrings.DEFINE_INDEX__FAILURE__MSG);
        return ResultBuilder.buildResult(infoResult);
    }
    try {
        final Set<DistributedMember> targetMembers = CliUtil.findMembers(group, memberNameOrID);
        if (targetMembers.isEmpty()) {
            return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
        }
        final Cache cache = CacheFactory.getAnyInstance();
        final ResultCollector<?, ?> rc = CliUtil.executeFunction(createDefinedIndexesFunction, indexDefinitions, 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_DEFINED_INDEXES__SUCCESS__MSG);
            int num = 0;
            for (final String memberId : successfulMembers) {
                ++num;
                infoResult.addLine(CliStrings.format(CliStrings.CREATE_DEFINED_INDEXES__NUMBER__AND__MEMBER, num, memberId));
            }
            result = ResultBuilder.buildResult(infoResult);
        } else {
            // Group members by the exception thrown.
            final ErrorResultData erd = ResultBuilder.createErrorResultData();
            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_DEFINED_INDEXES__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) 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) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Aggregations

CliMetaData (org.apache.geode.management.cli.CliMetaData)94 CliCommand (org.springframework.shell.core.annotation.CliCommand)93 Result (org.apache.geode.management.cli.Result)65 DistributedMember (org.apache.geode.distributed.DistributedMember)58 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)56 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)40 InternalCache (org.apache.geode.internal.cache.InternalCache)37 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)33 CommandResultException (org.apache.geode.management.internal.cli.result.CommandResultException)31 ArrayList (java.util.ArrayList)28 List (java.util.List)24 ConverterHint (org.apache.geode.management.cli.ConverterHint)22 IOException (java.io.IOException)20 InfoResultData (org.apache.geode.management.internal.cli.result.InfoResultData)17 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)16 ExecutionException (java.util.concurrent.ExecutionException)15 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)15 XmlEntity (org.apache.geode.management.internal.configuration.domain.XmlEntity)15 HashSet (java.util.HashSet)14 ObjectName (javax.management.ObjectName)14