use of org.apache.geode.management.cli.CliMetaData in project geode by apache.
the class RegionCommands method listRegion.
@CliCommand(value = { CliStrings.LIST_REGION }, help = CliStrings.LIST_REGION__HELP)
@CliMetaData(shellOnly = false, relatedTopic = CliStrings.TOPIC_GEODE_REGION)
@ResourceOperation(resource = Resource.DATA, operation = Operation.READ)
public Result listRegion(@CliOption(key = { CliStrings.LIST_REGION__GROUP }, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.LIST_REGION__GROUP__HELP) String[] group, @CliOption(key = { CliStrings.LIST_REGION__MEMBER }, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.LIST_REGION__MEMBER__HELP) String[] memberNameOrId) {
Result result = null;
try {
Set<RegionInformation> regionInfoSet = new LinkedHashSet<RegionInformation>();
ResultCollector<?, ?> rc = null;
Set<DistributedMember> targetMembers = CliUtil.findMembers(group, memberNameOrId);
if (targetMembers.isEmpty()) {
return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
}
TabularResultData resultData = ResultBuilder.createTabularResultData();
rc = CliUtil.executeFunction(getRegionsFunction, null, targetMembers);
ArrayList<?> resultList = (ArrayList<?>) rc.getResult();
if (resultList != null) {
Iterator<?> iters = resultList.iterator();
while (iters.hasNext()) {
Object resultObj = iters.next();
if (resultObj != null) {
if (resultObj instanceof Object[]) {
Object[] resultObjectArray = (Object[]) resultObj;
for (Object regionInfo : resultObjectArray) {
if (regionInfo instanceof RegionInformation) {
regionInfoSet.add((RegionInformation) regionInfo);
}
}
}
}
}
Set<String> regionNames = new TreeSet<String>();
for (RegionInformation regionInfo : regionInfoSet) {
regionNames.add(regionInfo.getName());
Set<String> subRegionNames = regionInfo.getSubRegionNames();
for (String subRegionName : subRegionNames) {
regionNames.add(subRegionName);
}
}
for (String regionName : regionNames) {
resultData.accumulate("List of regions", regionName);
}
if (!regionNames.isEmpty()) {
result = ResultBuilder.buildResult(resultData);
} else {
result = ResultBuilder.createInfoResult(CliStrings.LIST_REGION__MSG__NOT_FOUND);
}
}
} catch (FunctionInvocationTargetException e) {
result = ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.COULD_NOT_EXECUTE_COMMAND_TRY_AGAIN, CliStrings.LIST_REGION));
} catch (Exception e) {
result = ResultBuilder.createGemFireErrorResult(CliStrings.LIST_REGION__MSG__ERROR + " : " + e.getMessage());
}
return result;
}
use of org.apache.geode.management.cli.CliMetaData in project geode by apache.
the class FunctionCommands method listFunction.
@CliCommand(value = CliStrings.LIST_FUNCTION, help = CliStrings.LIST_FUNCTION__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_FUNCTION })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public Result listFunction(@CliOption(key = CliStrings.LIST_FUNCTION__MATCHES, help = CliStrings.LIST_FUNCTION__MATCHES__HELP) String matches, @CliOption(key = CliStrings.LIST_FUNCTION__GROUP, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.LIST_FUNCTION__GROUP__HELP) String[] groups, @CliOption(key = CliStrings.LIST_FUNCTION__MEMBER, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.LIST_FUNCTION__MEMBER__HELP) String[] members) {
TabularResultData tabularData = ResultBuilder.createTabularResultData();
boolean accumulatedData = false;
InternalCache cache = getCache();
Set<DistributedMember> targetMembers = CliUtil.findMembers(groups, members);
if (targetMembers.isEmpty()) {
return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
}
try {
ResultCollector<?, ?> rc = CliUtil.executeFunction(this.listFunctionFunction, new Object[] { matches }, targetMembers);
List<CliFunctionResult> results = CliFunctionResult.cleanResults((List<?>) rc.getResult());
for (CliFunctionResult result : results) {
if (result.getThrowable() != null) {
tabularData.accumulate("Member", result.getMemberIdOrName());
tabularData.accumulate("Function", "<ERROR: " + result.getThrowable().getMessage() + ">");
accumulatedData = true;
tabularData.setStatus(Status.ERROR);
} else if (result.isSuccessful()) {
String[] strings = (String[]) result.getSerializables();
Arrays.sort(strings);
for (int i = 0; i < strings.length; i++) {
tabularData.accumulate("Member", result.getMemberIdOrName());
tabularData.accumulate("Function", strings[i]);
accumulatedData = true;
}
}
}
if (!accumulatedData) {
return ResultBuilder.createInfoResult(CliStrings.LIST_FUNCTION__NO_FUNCTIONS_FOUND_ERROR_MESSAGE);
}
return ResultBuilder.buildResult(tabularData);
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable th) {
SystemFailure.checkFailure();
return ResultBuilder.createGemFireErrorResult("Exception while attempting to list functions: " + th.getMessage());
}
}
use of org.apache.geode.management.cli.CliMetaData in project geode by apache.
the class DeployCommands method deploy.
/**
* Deploy one or more JAR files to members of a group or all members.
*
* @param groups Group(s) to deploy the JAR to or null for all members
* @param jar JAR file to deploy
* @param dir Directory of JAR files to deploy
* @return The result of the attempt to deploy
*/
@CliCommand(value = { CliStrings.DEPLOY }, help = CliStrings.DEPLOY__HELP)
@CliMetaData(interceptor = "org.apache.geode.management.internal.cli.commands.DeployCommands$Interceptor", relatedTopic = { CliStrings.TOPIC_GEODE_CONFIG })
public Result deploy(@CliOption(key = { CliStrings.DEPLOY__GROUP }, help = CliStrings.DEPLOY__GROUP__HELP, optionContext = ConverterHint.MEMBERGROUP) String[] groups, @CliOption(key = { CliStrings.DEPLOY__JAR }, help = CliStrings.DEPLOY__JAR__HELP) String jar, @CliOption(key = { CliStrings.DEPLOY__DIR }, help = CliStrings.DEPLOY__DIR__HELP) String dir) {
try {
// since deploy function can potentially do a lot of damage to security, this action should
// require these following privileges
SecurityService securityService = SecurityService.getSecurityService();
securityService.authorizeClusterManage();
securityService.authorizeClusterWrite();
securityService.authorizeDataManage();
securityService.authorizeDataWrite();
TabularResultData tabularData = ResultBuilder.createTabularResultData();
byte[][] shellBytesData = CommandExecutionContext.getBytesFromShell();
String[] jarNames = CliUtil.bytesToNames(shellBytesData);
byte[][] jarBytes = CliUtil.bytesToData(shellBytesData);
Set<DistributedMember> targetMembers;
targetMembers = CliUtil.findMembers(groups, null);
if (targetMembers.size() > 0) {
// this deploys the jars to all the matching servers
ResultCollector<?, ?> resultCollector = CliUtil.executeFunction(this.deployFunction, new Object[] { jarNames, jarBytes }, targetMembers);
List<CliFunctionResult> results = CliFunctionResult.cleanResults((List<?>) resultCollector.getResult());
for (CliFunctionResult result : results) {
if (result.getThrowable() != null) {
tabularData.accumulate("Member", result.getMemberIdOrName());
tabularData.accumulate("Deployed JAR", "");
tabularData.accumulate("Deployed JAR Location", "ERROR: " + result.getThrowable().getClass().getName() + ": " + result.getThrowable().getMessage());
tabularData.setStatus(Status.ERROR);
} else {
String[] strings = (String[]) result.getSerializables();
for (int i = 0; i < strings.length; i += 2) {
tabularData.accumulate("Member", result.getMemberIdOrName());
tabularData.accumulate("Deployed JAR", strings[i]);
tabularData.accumulate("Deployed JAR Location", strings[i + 1]);
}
}
}
}
Result result = ResultBuilder.buildResult(tabularData);
persistClusterConfiguration(result, () -> getSharedConfiguration().addJarsToThisLocator(jarNames, jarBytes, groups));
return result;
} catch (NotAuthorizedException e) {
// for NotAuthorizedException, will catch this later in the code
throw e;
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable t) {
SystemFailure.checkFailure();
return ResultBuilder.createGemFireErrorResult(String.format("Exception while attempting to deploy: (%1$s)", toString(t, isDebugging())));
}
}
use of org.apache.geode.management.cli.CliMetaData in project geode by apache.
the class DeployCommands method undeploy.
/**
* Undeploy one or more JAR files from members of a group or all members.
*
* @param groups Group(s) to undeploy the JAR from or null for all members
* @param jars JAR(s) to undeploy (separated by comma)
* @return The result of the attempt to undeploy
*/
@CliCommand(value = { CliStrings.UNDEPLOY }, help = CliStrings.UNDEPLOY__HELP)
@CliMetaData(relatedTopic = { CliStrings.TOPIC_GEODE_CONFIG })
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result undeploy(@CliOption(key = { CliStrings.UNDEPLOY__GROUP }, help = CliStrings.UNDEPLOY__GROUP__HELP, optionContext = ConverterHint.MEMBERGROUP) String[] groups, @CliOption(key = { CliStrings.UNDEPLOY__JAR }, help = CliStrings.UNDEPLOY__JAR__HELP, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE) String jars) {
try {
TabularResultData tabularData = ResultBuilder.createTabularResultData();
boolean accumulatedData = false;
Set<DistributedMember> targetMembers = CliUtil.findMembers(groups, null);
if (targetMembers.isEmpty()) {
return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
}
ResultCollector<?, ?> rc = CliUtil.executeFunction(this.undeployFunction, new Object[] { jars }, targetMembers);
List<CliFunctionResult> results = CliFunctionResult.cleanResults((List<?>) rc.getResult());
for (CliFunctionResult result : results) {
if (result.getThrowable() != null) {
tabularData.accumulate("Member", result.getMemberIdOrName());
tabularData.accumulate("Un-Deployed JAR", "");
tabularData.accumulate("Un-Deployed JAR Location", "ERROR: " + result.getThrowable().getClass().getName() + ": " + result.getThrowable().getMessage());
accumulatedData = true;
tabularData.setStatus(Status.ERROR);
} else {
String[] strings = (String[]) result.getSerializables();
for (int i = 0; i < strings.length; i += 2) {
tabularData.accumulate("Member", result.getMemberIdOrName());
tabularData.accumulate("Un-Deployed JAR", strings[i]);
tabularData.accumulate("Un-Deployed From JAR Location", strings[i + 1]);
accumulatedData = true;
}
}
}
if (!accumulatedData) {
return ResultBuilder.createInfoResult(CliStrings.UNDEPLOY__NO_JARS_FOUND_MESSAGE);
}
Result result = ResultBuilder.buildResult(tabularData);
if (tabularData.getStatus().equals(Status.OK)) {
persistClusterConfiguration(result, () -> getSharedConfiguration().removeJars(jars == null ? null : jars.split(","), groups));
}
return result;
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable th) {
SystemFailure.checkFailure();
return ResultBuilder.createGemFireErrorResult("Exception while attempting to un-deploy: " + th.getClass().getName() + ": " + th.getMessage());
}
}
use of org.apache.geode.management.cli.CliMetaData in project geode by apache.
the class CreateAlterDestroyRegionCommands method alterRegion.
@CliCommand(value = CliStrings.ALTER_REGION, help = CliStrings.ALTER_REGION__HELP)
@CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_REGION)
public Result alterRegion(@CliOption(key = CliStrings.ALTER_REGION__REGION, mandatory = true, help = CliStrings.ALTER_REGION__REGION__HELP) String regionPath, @CliOption(key = CliStrings.ALTER_REGION__GROUP, optionContext = ConverterHint.MEMBERGROUP, help = CliStrings.ALTER_REGION__GROUP__HELP) String[] groups, @CliOption(key = CliStrings.ALTER_REGION__ENTRYEXPIRATIONIDLETIME, specifiedDefaultValue = "-1", help = CliStrings.ALTER_REGION__ENTRYEXPIRATIONIDLETIME__HELP) Integer entryExpirationIdleTime, @CliOption(key = CliStrings.ALTER_REGION__ENTRYEXPIRATIONIDLETIMEACTION, specifiedDefaultValue = "INVALIDATE", help = CliStrings.ALTER_REGION__ENTRYEXPIRATIONIDLETIMEACTION__HELP) String entryExpirationIdleTimeAction, @CliOption(key = CliStrings.ALTER_REGION__ENTRYEXPIRATIONTIMETOLIVE, specifiedDefaultValue = "-1", help = CliStrings.ALTER_REGION__ENTRYEXPIRATIONTIMETOLIVE__HELP) Integer entryExpirationTTL, @CliOption(key = CliStrings.ALTER_REGION__ENTRYEXPIRATIONTTLACTION, specifiedDefaultValue = "INVALIDATE", help = CliStrings.ALTER_REGION__ENTRYEXPIRATIONTTLACTION__HELP) String entryExpirationTTLAction, @CliOption(key = CliStrings.ALTER_REGION__REGIONEXPIRATIONIDLETIME, specifiedDefaultValue = "-1", help = CliStrings.ALTER_REGION__REGIONEXPIRATIONIDLETIME__HELP) Integer regionExpirationIdleTime, @CliOption(key = CliStrings.ALTER_REGION__REGIONEXPIRATIONIDLETIMEACTION, specifiedDefaultValue = "INVALIDATE", help = CliStrings.ALTER_REGION__REGIONEXPIRATIONIDLETIMEACTION__HELP) String regionExpirationIdleTimeAction, @CliOption(key = CliStrings.ALTER_REGION__REGIONEXPIRATIONTTL, specifiedDefaultValue = "-1", help = CliStrings.ALTER_REGION__REGIONEXPIRATIONTTL__HELP) Integer regionExpirationTTL, @CliOption(key = CliStrings.ALTER_REGION__REGIONEXPIRATIONTTLACTION, specifiedDefaultValue = "INVALIDATE", help = CliStrings.ALTER_REGION__REGIONEXPIRATIONTTLACTION__HELP) String regionExpirationTTLAction, @CliOption(key = CliStrings.ALTER_REGION__CACHELISTENER, specifiedDefaultValue = "", help = CliStrings.ALTER_REGION__CACHELISTENER__HELP) String[] cacheListeners, @CliOption(key = CliStrings.ALTER_REGION__CACHELOADER, specifiedDefaultValue = "null", help = CliStrings.ALTER_REGION__CACHELOADER__HELP) String cacheLoader, @CliOption(key = CliStrings.ALTER_REGION__CACHEWRITER, specifiedDefaultValue = "null", help = CliStrings.ALTER_REGION__CACHEWRITER__HELP) String cacheWriter, @CliOption(key = CliStrings.ALTER_REGION__ASYNCEVENTQUEUEID, specifiedDefaultValue = "", help = CliStrings.ALTER_REGION__ASYNCEVENTQUEUEID__HELP) String[] asyncEventQueueIds, @CliOption(key = CliStrings.ALTER_REGION__GATEWAYSENDERID, specifiedDefaultValue = "", help = CliStrings.ALTER_REGION__GATEWAYSENDERID__HELP) String[] gatewaySenderIds, @CliOption(key = CliStrings.ALTER_REGION__CLONINGENABLED, specifiedDefaultValue = "false", help = CliStrings.ALTER_REGION__CLONINGENABLED__HELP) Boolean cloningEnabled, @CliOption(key = CliStrings.ALTER_REGION__EVICTIONMAX, specifiedDefaultValue = "0", help = CliStrings.ALTER_REGION__EVICTIONMAX__HELP) Integer evictionMax) {
Result result;
AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
this.securityService.authorizeRegionManage(regionPath);
try {
InternalCache cache = getCache();
if (groups != null) {
validateGroups(cache, groups);
}
RegionFunctionArgs.ExpirationAttrs entryIdle = null;
if (entryExpirationIdleTime != null || entryExpirationIdleTimeAction != null) {
if (entryExpirationIdleTime != null && entryExpirationIdleTime == -1) {
entryExpirationIdleTime = ExpirationAttributes.DEFAULT.getTimeout();
}
if (CliMetaData.ANNOTATION_DEFAULT_VALUE.equals(entryExpirationIdleTimeAction)) {
entryExpirationIdleTimeAction = ExpirationAttributes.DEFAULT.getAction().toString();
}
entryIdle = new RegionFunctionArgs.ExpirationAttrs(RegionFunctionArgs.ExpirationAttrs.ExpirationFor.ENTRY_IDLE, entryExpirationIdleTime, entryExpirationIdleTimeAction);
}
RegionFunctionArgs.ExpirationAttrs entryTTL = null;
if (entryExpirationTTL != null || entryExpirationTTLAction != null) {
if (entryExpirationTTL != null && entryExpirationTTL == -1) {
entryExpirationTTL = ExpirationAttributes.DEFAULT.getTimeout();
}
if (CliMetaData.ANNOTATION_DEFAULT_VALUE.equals(entryExpirationTTLAction)) {
entryExpirationTTLAction = ExpirationAttributes.DEFAULT.getAction().toString();
}
entryTTL = new RegionFunctionArgs.ExpirationAttrs(RegionFunctionArgs.ExpirationAttrs.ExpirationFor.ENTRY_TTL, entryExpirationTTL, entryExpirationTTLAction);
}
RegionFunctionArgs.ExpirationAttrs regionIdle = null;
if (regionExpirationIdleTime != null || regionExpirationIdleTimeAction != null) {
if (regionExpirationIdleTime != null && regionExpirationIdleTime == -1) {
regionExpirationIdleTime = ExpirationAttributes.DEFAULT.getTimeout();
}
if (CliMetaData.ANNOTATION_DEFAULT_VALUE.equals(regionExpirationIdleTimeAction)) {
regionExpirationIdleTimeAction = ExpirationAttributes.DEFAULT.getAction().toString();
}
regionIdle = new RegionFunctionArgs.ExpirationAttrs(RegionFunctionArgs.ExpirationAttrs.ExpirationFor.REGION_IDLE, regionExpirationIdleTime, regionExpirationIdleTimeAction);
}
RegionFunctionArgs.ExpirationAttrs regionTTL = null;
if (regionExpirationTTL != null || regionExpirationTTLAction != null) {
if (regionExpirationTTL != null && regionExpirationTTL == -1) {
regionExpirationTTL = ExpirationAttributes.DEFAULT.getTimeout();
}
if (CliMetaData.ANNOTATION_DEFAULT_VALUE.equals(regionExpirationTTLAction)) {
regionExpirationTTLAction = ExpirationAttributes.DEFAULT.getAction().toString();
}
regionTTL = new RegionFunctionArgs.ExpirationAttrs(RegionFunctionArgs.ExpirationAttrs.ExpirationFor.REGION_TTL, regionExpirationTTL, regionExpirationTTLAction);
}
cacheLoader = convertDefaultValue(cacheLoader, StringUtils.EMPTY);
cacheWriter = convertDefaultValue(cacheWriter, StringUtils.EMPTY);
RegionFunctionArgs regionFunctionArgs;
regionFunctionArgs = new RegionFunctionArgs(regionPath, null, null, false, null, null, null, entryIdle, entryTTL, regionIdle, regionTTL, null, null, null, null, cacheListeners, cacheLoader, cacheWriter, asyncEventQueueIds, gatewaySenderIds, null, cloningEnabled, null, null, null, null, null, null, null, null, evictionMax, null, null, null, null);
Set<String> cacheListenersSet = regionFunctionArgs.getCacheListeners();
if (cacheListenersSet != null && !cacheListenersSet.isEmpty()) {
for (String cacheListener : cacheListenersSet) {
if (!isClassNameValid(cacheListener)) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.ALTER_REGION__MSG__SPECIFY_VALID_CLASSNAME_FOR_CACHELISTENER_0_IS_INVALID, new Object[] { cacheListener }));
}
}
}
if (cacheLoader != null && !isClassNameValid(cacheLoader)) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.ALTER_REGION__MSG__SPECIFY_VALID_CLASSNAME_FOR_CACHELOADER_0_IS_INVALID, new Object[] { cacheLoader }));
}
if (cacheWriter != null && !isClassNameValid(cacheWriter)) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.ALTER_REGION__MSG__SPECIFY_VALID_CLASSNAME_FOR_CACHEWRITER_0_IS_INVALID, new Object[] { cacheWriter }));
}
if (evictionMax != null && evictionMax < 0) {
throw new IllegalArgumentException(CliStrings.format(CliStrings.ALTER_REGION__MSG__SPECIFY_POSITIVE_INT_FOR_EVICTIONMAX_0_IS_NOT_VALID, new Object[] { evictionMax }));
}
Set<DistributedMember> targetMembers = CliUtil.findMembers(groups, null);
if (targetMembers.isEmpty()) {
return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
}
ResultCollector<?, ?> resultCollector = CliUtil.executeFunction(new RegionAlterFunction(), regionFunctionArgs, targetMembers);
List<CliFunctionResult> regionAlterResults = (List<CliFunctionResult>) resultCollector.getResult();
TabularResultData tabularResultData = ResultBuilder.createTabularResultData();
final String errorPrefix = "ERROR: ";
for (CliFunctionResult regionAlterResult : regionAlterResults) {
boolean success = regionAlterResult.isSuccessful();
tabularResultData.accumulate("Member", regionAlterResult.getMemberIdOrName());
if (success) {
tabularResultData.accumulate("Status", regionAlterResult.getMessage());
xmlEntity.set(regionAlterResult.getXmlEntity());
} else {
tabularResultData.accumulate("Status", errorPrefix + regionAlterResult.getMessage());
tabularResultData.setStatus(Status.ERROR);
}
}
result = ResultBuilder.buildResult(tabularResultData);
} catch (IllegalArgumentException | IllegalStateException e) {
LogWrapper.getInstance().info(e.getMessage());
result = ResultBuilder.createUserErrorResult(e.getMessage());
} catch (RuntimeException e) {
LogWrapper.getInstance().info(e.getMessage(), e);
result = ResultBuilder.createGemFireErrorResult(e.getMessage());
}
if (xmlEntity.get() != null) {
persistClusterConfiguration(result, () -> getSharedConfiguration().addXmlEntity(xmlEntity.get(), groups));
}
return result;
}
Aggregations