Search in sources :

Example 46 with CliCommand

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

the class ConfigCommands method describeConfig.

@CliCommand(value = { CliStrings.DESCRIBE_CONFIG }, help = CliStrings.DESCRIBE_CONFIG__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_CONFIG })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public Result describeConfig(@CliOption(key = CliStrings.DESCRIBE_CONFIG__MEMBER, optionContext = ConverterHint.ALL_MEMBER_IDNAME, help = CliStrings.DESCRIBE_CONFIG__MEMBER__HELP, mandatory = true) String memberNameOrId, @CliOption(key = CliStrings.DESCRIBE_CONFIG__HIDE__DEFAULTS, help = CliStrings.DESCRIBE_CONFIG__HIDE__DEFAULTS__HELP, unspecifiedDefaultValue = "true", specifiedDefaultValue = "true") boolean hideDefaults) {
    Result result = null;
    try {
        DistributedMember targetMember = null;
        if (memberNameOrId != null && !memberNameOrId.isEmpty()) {
            targetMember = CliUtil.getDistributedMemberByNameOrId(memberNameOrId);
        }
        if (targetMember != null) {
            ResultCollector<?, ?> rc = CliUtil.executeFunction(getMemberConfigFunction, new Boolean(hideDefaults), targetMember);
            ArrayList<?> output = (ArrayList<?>) rc.getResult();
            Object obj = output.get(0);
            if (obj != null && obj instanceof MemberConfigurationInfo) {
                MemberConfigurationInfo memberConfigInfo = (MemberConfigurationInfo) obj;
                CompositeResultData crd = ResultBuilder.createCompositeResultData();
                crd.setHeader(CliStrings.format(CliStrings.DESCRIBE_CONFIG__HEADER__TEXT, memberNameOrId));
                List<String> jvmArgsList = memberConfigInfo.getJvmInputArguments();
                TabularResultData jvmInputArgs = crd.addSection().addSection().addTable();
                for (String jvmArg : jvmArgsList) {
                    jvmInputArgs.accumulate("JVM command line arguments", jvmArg);
                }
                addSection(crd, memberConfigInfo.getGfePropsSetUsingApi(), "GemFire properties defined using the API");
                addSection(crd, memberConfigInfo.getGfePropsRuntime(), "GemFire properties defined at the runtime");
                addSection(crd, memberConfigInfo.getGfePropsSetFromFile(), "GemFire properties defined with the property file");
                addSection(crd, memberConfigInfo.getGfePropsSetWithDefaults(), "GemFire properties using default values");
                addSection(crd, memberConfigInfo.getCacheAttributes(), "Cache attributes");
                List<Map<String, String>> cacheServerAttributesList = memberConfigInfo.getCacheServerAttributes();
                if (cacheServerAttributesList != null && !cacheServerAttributesList.isEmpty()) {
                    SectionResultData cacheServerSection = crd.addSection();
                    cacheServerSection.setHeader("Cache-server attributes");
                    for (Map<String, String> cacheServerAttributes : cacheServerAttributesList) {
                        addSubSection(cacheServerSection, cacheServerAttributes, "");
                    }
                }
                result = ResultBuilder.buildResult(crd);
            }
        } else {
            ErrorResultData erd = ResultBuilder.createErrorResultData();
            erd.addLine(CliStrings.format(CliStrings.DESCRIBE_CONFIG__MEMBER__NOT__FOUND, new Object[] { memberNameOrId }));
            result = ResultBuilder.buildResult(erd);
        }
    } catch (FunctionInvocationTargetException e) {
        result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COULD_NOT_EXECUTE_COMMAND_TRY_AGAIN, CliStrings.DESCRIBE_CONFIG));
    } catch (Exception e) {
        ErrorResultData erd = ResultBuilder.createErrorResultData();
        erd.addLine(e.getMessage());
        result = ResultBuilder.buildResult(erd);
    }
    return result;
}
Also used : CompositeResultData(org.apache.geode.management.internal.cli.result.CompositeResultData) TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) ArrayList(java.util.ArrayList) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) IOException(java.io.IOException) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) MemberConfigurationInfo(org.apache.geode.management.internal.cli.domain.MemberConfigurationInfo) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) SectionResultData(org.apache.geode.management.internal.cli.result.CompositeResultData.SectionResultData) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) HashMap(java.util.HashMap) Map(java.util.Map) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 47 with CliCommand

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

the class CreateAlterDestroyRegionCommands method destroyRegion.

@CliCommand(value = { CliStrings.DESTROY_REGION }, help = CliStrings.DESTROY_REGION__HELP)
@CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_REGION)
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result destroyRegion(@CliOption(key = CliStrings.DESTROY_REGION__REGION, optionContext = ConverterHint.REGION_PATH, mandatory = true, help = CliStrings.DESTROY_REGION__REGION__HELP) String regionPath) {
    if (regionPath == null) {
        return ResultBuilder.createInfoResult(CliStrings.DESTROY_REGION__MSG__SPECIFY_REGIONPATH_TO_DESTROY);
    }
    if (StringUtils.isBlank(regionPath) || regionPath.equals(Region.SEPARATOR)) {
        return ResultBuilder.createInfoResult(CliStrings.format(CliStrings.DESTROY_REGION__MSG__REGIONPATH_0_NOT_VALID, new Object[] { regionPath }));
    }
    Result result;
    AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
    try {
        InternalCache cache = getCache();
        ManagementService managementService = ManagementService.getExistingManagementService(cache);
        String regionPathToUse = regionPath;
        if (!regionPathToUse.startsWith(Region.SEPARATOR)) {
            regionPathToUse = Region.SEPARATOR + regionPathToUse;
        }
        Set<DistributedMember> regionMembersList = findMembersForRegion(cache, managementService, regionPathToUse);
        if (regionMembersList.size() == 0) {
            return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.DESTROY_REGION__MSG__COULDNOT_FIND_REGIONPATH_0_IN_GEODE, new Object[] { regionPath, "jmx-manager-update-rate milliseconds" }));
        }
        CliFunctionResult destroyRegionResult;
        ResultCollector<?, ?> resultCollector = CliUtil.executeFunction(RegionDestroyFunction.INSTANCE, regionPath, regionMembersList);
        List<CliFunctionResult> resultsList = (List<CliFunctionResult>) resultCollector.getResult();
        String message = CliStrings.format(CliStrings.DESTROY_REGION__MSG__REGION_0_1_DESTROYED, new Object[] { regionPath, "" });
        // Only if there is an error is this set to false
        boolean isRegionDestroyed = true;
        for (CliFunctionResult aResultsList : resultsList) {
            destroyRegionResult = aResultsList;
            if (destroyRegionResult.isSuccessful()) {
                xmlEntity.set(destroyRegionResult.getXmlEntity());
            } else if (destroyRegionResult.getThrowable() != null) {
                Throwable t = destroyRegionResult.getThrowable();
                LogWrapper.getInstance().info(t.getMessage(), t);
                message = CliStrings.format(CliStrings.DESTROY_REGION__MSG__ERROR_OCCURRED_WHILE_DESTROYING_0_REASON_1, new Object[] { regionPath, t.getMessage() });
                isRegionDestroyed = false;
            } else {
                message = CliStrings.format(CliStrings.DESTROY_REGION__MSG__UNKNOWN_RESULT_WHILE_DESTROYING_REGION_0_REASON_1, new Object[] { regionPath, destroyRegionResult.getMessage() });
                isRegionDestroyed = false;
            }
        }
        if (isRegionDestroyed) {
            result = ResultBuilder.createInfoResult(message);
        } else {
            result = ResultBuilder.createUserErrorResult(message);
        }
    } catch (IllegalStateException e) {
        result = ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.DESTROY_REGION__MSG__ERROR_WHILE_DESTROYING_REGION_0_REASON_1, new Object[] { regionPath, e.getMessage() }));
    } catch (Exception e) {
        result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.DESTROY_REGION__MSG__ERROR_WHILE_DESTROYING_REGION_0_REASON_1, new Object[] { regionPath, e.getMessage() }));
    }
    if (xmlEntity.get() != null) {
        persistClusterConfiguration(result, () -> getSharedConfiguration().deleteXmlEntity(xmlEntity.get(), null));
    }
    return result;
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) CommandResultException(org.apache.geode.management.internal.cli.result.CommandResultException) MalformedObjectNameException(javax.management.MalformedObjectNameException) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) FetchRegionAttributesFunctionResult(org.apache.geode.management.internal.cli.functions.FetchRegionAttributesFunction.FetchRegionAttributesFunctionResult) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) ManagementService(org.apache.geode.management.ManagementService) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) List(java.util.List) ArrayList(java.util.ArrayList) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 48 with CliCommand

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

the class DataCommands method query.

@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION })
@MultiStepCommand
@CliCommand(value = { CliStrings.QUERY }, help = CliStrings.QUERY__HELP)
public Object query(@CliOption(key = CliStrings.QUERY__QUERY, help = CliStrings.QUERY__QUERY__HELP, mandatory = true) final String query, @CliOption(key = CliStrings.QUERY__STEPNAME, help = "Step name", unspecifiedDefaultValue = CliStrings.QUERY__STEPNAME__DEFAULTVALUE) String stepName, @CliOption(key = CliStrings.QUERY__INTERACTIVE, help = CliStrings.QUERY__INTERACTIVE__HELP, unspecifiedDefaultValue = "true") final boolean interactive) {
    if (!CliUtil.isGfshVM() && stepName.equals(CliStrings.QUERY__STEPNAME__DEFAULTVALUE)) {
        return ResultBuilder.createInfoResult(CliStrings.QUERY__MSG__NOT_SUPPORTED_ON_MEMBERS);
    }
    Object[] arguments = new Object[] { query, stepName, interactive };
    CLIStep exec = new DataCommandFunction.SelectExecStep(arguments);
    CLIStep display = new DataCommandFunction.SelectDisplayStep(arguments);
    CLIStep move = new DataCommandFunction.SelectMoveStep(arguments);
    CLIStep quit = new DataCommandFunction.SelectQuitStep(arguments);
    CLIStep[] steps = { exec, display, move, quit };
    return CLIMultiStepHelper.chooseStep(steps, stepName);
}
Also used : CLIStep(org.apache.geode.management.internal.cli.multistep.CLIStep) MultiStepCommand(org.apache.geode.management.internal.cli.multistep.MultiStepCommand) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 49 with CliCommand

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

the class DataCommands method importData.

@CliCommand(value = CliStrings.IMPORT_DATA, help = CliStrings.IMPORT_DATA__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION })
public Result importData(@CliOption(key = CliStrings.IMPORT_DATA__REGION, optionContext = ConverterHint.REGION_PATH, mandatory = true, help = CliStrings.IMPORT_DATA__REGION__HELP) String regionName, @CliOption(key = CliStrings.IMPORT_DATA__FILE, mandatory = true, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.IMPORT_DATA__FILE__HELP) String filePath, @CliOption(key = CliStrings.IMPORT_DATA__MEMBER, mandatory = true, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.IMPORT_DATA__MEMBER__HELP) String memberNameOrId, @CliOption(key = CliStrings.IMPORT_DATA__INVOKE_CALLBACKS, unspecifiedDefaultValue = "false", help = CliStrings.IMPORT_DATA__INVOKE_CALLBACKS__HELP) boolean invokeCallbacks) {
    this.securityService.authorizeRegionWrite(regionName);
    Result result;
    try {
        final DistributedMember targetMember = CliUtil.getDistributedMemberByNameOrId(memberNameOrId);
        if (!filePath.endsWith(CliStrings.GEODE_DATA_FILE_EXTENSION)) {
            return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.INVALID_FILE_EXTENSION, CliStrings.GEODE_DATA_FILE_EXTENSION));
        }
        if (targetMember != null) {
            final Object[] args = { regionName, filePath, invokeCallbacks };
            ResultCollector<?, ?> rc = CliUtil.executeFunction(importDataFunction, args, targetMember);
            List<Object> results = (List<Object>) rc.getResult();
            if (results != null) {
                Object resultObj = results.get(0);
                if (resultObj instanceof String) {
                    result = ResultBuilder.createInfoResult((String) resultObj);
                } else if (resultObj instanceof Exception) {
                    result = ResultBuilder.createGemFireErrorResult(((Exception) resultObj).getMessage());
                } else {
                    result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMMAND_FAILURE_MESSAGE, CliStrings.IMPORT_DATA));
                }
            } else {
                result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMMAND_FAILURE_MESSAGE, CliStrings.IMPORT_DATA));
            }
        } else {
            result = ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.IMPORT_DATA__MEMBER__NOT__FOUND, memberNameOrId));
        }
    } catch (CacheClosedException e) {
        result = ResultBuilder.createGemFireErrorResult(e.getMessage());
    } catch (FunctionInvocationTargetException e) {
        result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COMMAND_FAILURE_MESSAGE, CliStrings.IMPORT_DATA));
    }
    return result;
}
Also used : DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) List(java.util.List) ArrayList(java.util.ArrayList) CacheClosedException(org.apache.geode.cache.CacheClosedException) TimeoutException(java.util.concurrent.TimeoutException) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) CacheClosedException(org.apache.geode.cache.CacheClosedException) Result(org.apache.geode.management.cli.Result) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 50 with CliCommand

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

the class ConfigCommands method exportConfig.

/**
   * Export the cache configuration in XML format.
   *
   * @param member Member for which to write the configuration
   * @param group Group or groups for which to write the configuration
   * @return Results of the attempt to write the configuration
   */
@CliCommand(value = { CliStrings.EXPORT_CONFIG }, help = CliStrings.EXPORT_CONFIG__HELP)
@CliMetaData(interceptor = "org.apache.geode.management.internal.cli.commands.ConfigCommands$Interceptor", relatedTopic = { CliStrings.TOPIC_GEODE_CONFIG })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public Result exportConfig(@CliOption(key = { CliStrings.EXPORT_CONFIG__MEMBER }, optionContext = ConverterHint.ALL_MEMBER_IDNAME, help = CliStrings.EXPORT_CONFIG__MEMBER__HELP) String[] member, @CliOption(key = { CliStrings.EXPORT_CONFIG__GROUP }, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.EXPORT_CONFIG__GROUP__HELP) String[] group, @CliOption(key = { CliStrings.EXPORT_CONFIG__DIR }, help = CliStrings.EXPORT_CONFIG__DIR__HELP) String dir) {
    InfoResultData infoData = ResultBuilder.createInfoResultData();
    Set<DistributedMember> targetMembers = CliUtil.findMembers(group, member);
    if (targetMembers.isEmpty()) {
        return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
    }
    try {
        ResultCollector<?, ?> rc = CliUtil.executeFunction(this.exportConfigFunction, null, targetMembers);
        List<CliFunctionResult> results = CliFunctionResult.cleanResults((List<?>) rc.getResult());
        for (CliFunctionResult result : results) {
            if (result.getThrowable() != null) {
                infoData.addLine(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__EXCEPTION, result.getMemberIdOrName(), result.getThrowable()));
            } else if (result.isSuccessful()) {
                String cacheFileName = result.getMemberIdOrName() + "-cache.xml";
                String propsFileName = result.getMemberIdOrName() + "-gf.properties";
                String[] fileContent = (String[]) result.getSerializables();
                infoData.addAsFile(cacheFileName, fileContent[0], "Downloading Cache XML file: {0}", false);
                infoData.addAsFile(propsFileName, fileContent[1], "Downloading properties file: {0}", false);
            }
        }
        return ResultBuilder.buildResult(infoData);
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable th) {
        SystemFailure.checkFailure();
        th.printStackTrace(System.err);
        return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.EXPORT_CONFIG__MSG__EXCEPTION, th.getClass().getName() + ": " + th.getMessage()));
    }
}
Also used : CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) DistributedMember(org.apache.geode.distributed.DistributedMember) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

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