Search in sources :

Example 56 with CliCommand

use of org.springframework.shell.core.annotation.CliCommand in project cas by apereo.

the class ValidateRegisteredServiceCommand method validateService.

/**
 * Validate service.
 *
 * @param file      the file
 * @param directory the directory
 */
@CliCommand(value = "validate-service", help = "Validate a given JSON/YAML service definition by path or directory")
public void validateService(@CliOption(key = { "file" }, help = "Path to the JSON/YAML service definition file", specifiedDefaultValue = "", unspecifiedDefaultValue = "", optionContext = "Path to the JSON/YAML service definition") final String file, @CliOption(key = { "directory" }, help = "Path to the JSON/YAML service definitions directory", specifiedDefaultValue = "/etc/cas/services", unspecifiedDefaultValue = "/etc/cas/services", optionContext = "Path to the JSON/YAML service definitions directory") final String directory) {
    if (StringUtils.isBlank(file) && StringUtils.isBlank(directory)) {
        LOGGER.warn("Either file or directory must be specified");
        return;
    }
    if (StringUtils.isNotBlank(file)) {
        final File filePath = new File(file);
        validate(filePath);
        return;
    }
    if (StringUtils.isNotBlank(directory)) {
        final File directoryPath = new File(directory);
        if (directoryPath.isDirectory()) {
            FileUtils.listFiles(directoryPath, new String[] { "json", "yml" }, false).forEach(this::validate);
        }
        return;
    }
}
Also used : File(java.io.File) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 57 with CliCommand

use of org.springframework.shell.core.annotation.CliCommand in project ambari-shell by sequenceiq.

the class ConfigCommands method setConfig.

/**
   * Sets the desired configuration.
   */
@CliCommand(value = "configuration set", help = "Sets the desired configuration")
public String setConfig(@CliOption(key = "type", mandatory = true, help = "Type of the configuration") ConfigType configType, @CliOption(key = "url", help = "URL of the config") String url, @CliOption(key = "file", help = "File of the config") File file) throws IOException {
    Configuration configuration = new Configuration(false);
    if (file == null) {
        configuration.addResource(new URL(url));
    } else {
        configuration.addResource(new FileInputStream(file));
    }
    Map<String, String> config = new HashMap<String, String>();
    Iterator<Map.Entry<String, String>> iterator = configuration.iterator();
    while (iterator.hasNext()) {
        Map.Entry<String, String> entry = iterator.next();
        config.put(entry.getKey(), entry.getValue());
    }
    client.modifyConfiguration(configType.getName(), config);
    return "Restart is required!\n" + renderSingleMap(config, "KEY", "VALUE");
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) HashMap(java.util.HashMap) TableRenderer.renderSingleMap(com.sequenceiq.ambari.shell.support.TableRenderer.renderSingleMap) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) FileInputStream(java.io.FileInputStream) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 58 with CliCommand

use of org.springframework.shell.core.annotation.CliCommand in project ambari-shell by sequenceiq.

the class ConfigCommands method downloadConfig.

/**
   * Modify the desired configuration.
   */
@CliCommand(value = "configuration download", help = "Downloads the desired configuration")
public String downloadConfig(@CliOption(key = "type", mandatory = true, help = "Type of the configuration") ConfigType configType) throws IOException {
    String configTypeName = configType.getName();
    Map<String, String> config = client.getServiceConfigMap(configTypeName).get(configTypeName);
    Configuration configuration = new Configuration(false);
    for (String key : config.keySet()) {
        configuration.set(key, config.get(key));
    }
    File file = new File(configTypeName);
    FileWriter writer = new FileWriter(file);
    configuration.writeXml(writer);
    return "Configuration saved to: " + file.getAbsolutePath();
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) FileWriter(java.io.FileWriter) File(java.io.File) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 59 with CliCommand

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

the class DiskStoreCommands method destroyDiskStore.

@CliCommand(value = CliStrings.DESTROY_DISK_STORE, help = CliStrings.DESTROY_DISK_STORE__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_DISKSTORE })
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result destroyDiskStore(@CliOption(key = CliStrings.DESTROY_DISK_STORE__NAME, mandatory = true, help = CliStrings.DESTROY_DISK_STORE__NAME__HELP) String name, @CliOption(key = CliStrings.DESTROY_DISK_STORE__GROUP, help = CliStrings.DESTROY_DISK_STORE__GROUP__HELP, optionContext = ConverterHint.MEMBERGROUP) String[] groups) {
    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(new DestroyDiskStoreFunction(), new Object[] { name }, targetMembers);
        List<CliFunctionResult> results = CliFunctionResult.cleanResults((List<?>) rc.getResult());
        AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
        for (CliFunctionResult result : results) {
            if (result.getThrowable() != null) {
                tabularData.accumulate("Member", result.getMemberIdOrName());
                tabularData.accumulate("Result", "ERROR: " + result.getThrowable().getClass().getName() + ": " + result.getThrowable().getMessage());
                accumulatedData = true;
                tabularData.setStatus(Status.ERROR);
            } else if (result.getMessage() != null) {
                tabularData.accumulate("Member", result.getMemberIdOrName());
                tabularData.accumulate("Result", result.getMessage());
                accumulatedData = true;
                if (xmlEntity.get() == null) {
                    xmlEntity.set(result.getXmlEntity());
                }
            }
        }
        if (!accumulatedData) {
            return ResultBuilder.createInfoResult("No matching disk stores found.");
        }
        Result result = ResultBuilder.buildResult(tabularData);
        if (xmlEntity.get() != null) {
            persistClusterConfiguration(result, () -> getSharedConfiguration().deleteXmlEntity(xmlEntity.get(), groups));
        }
        return result;
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable th) {
        SystemFailure.checkFailure();
        return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.DESTROY_DISK_STORE__ERROR_WHILE_DESTROYING_REASON_0, new Object[] { th.getMessage() }));
    }
}
Also used : TabularResultData(org.apache.geode.management.internal.cli.result.TabularResultData) AtomicReference(java.util.concurrent.atomic.AtomicReference) Result(org.apache.geode.management.cli.Result) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) DestroyDiskStoreFunction(org.apache.geode.management.internal.cli.functions.DestroyDiskStoreFunction) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 60 with CliCommand

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

the class PDXCommands method configurePDX.

@CliCommand(value = CliStrings.CONFIGURE_PDX, help = CliStrings.CONFIGURE_PDX__HELP)
@CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_REGION)
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result configurePDX(@CliOption(key = CliStrings.CONFIGURE_PDX__READ__SERIALIZED, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONFIGURE_PDX__READ__SERIALIZED__HELP) Boolean readSerialized, @CliOption(key = CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS__HELP) Boolean ignoreUnreadFields, @CliOption(key = CliStrings.CONFIGURE_PDX__DISKSTORE, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, specifiedDefaultValue = "", help = CliStrings.CONFIGURE_PDX__DISKSTORE__HELP) String diskStore, @CliOption(key = CliStrings.CONFIGURE_PDX__AUTO__SERIALIZER__CLASSES, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, specifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONFIGURE_PDX__AUTO__SERIALIZER__CLASSES__HELP) String[] patterns, @CliOption(key = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, specifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES__HELP) String[] portablePatterns) {
    Result result = null;
    try {
        InfoResultData ird = ResultBuilder.createInfoResultData();
        CacheCreation cache = new CacheCreation(true);
        if ((portablePatterns != null && portablePatterns.length > 0) && (patterns != null && patterns.length > 0)) {
            return ResultBuilder.createUserErrorResult(CliStrings.CONFIGURE_PDX__ERROR__MESSAGE);
        }
        if (!CliUtil.getAllNormalMembers(CliUtil.getCacheIfExists()).isEmpty()) {
            ird.addLine(CliStrings.CONFIGURE_PDX__NORMAL__MEMBERS__WARNING);
        }
        // Set persistent and the disk-store
        if (diskStore != null) {
            cache.setPdxPersistent(true);
            ird.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = " + cache.getPdxPersistent());
            if (!diskStore.equals("")) {
                cache.setPdxDiskStore(diskStore);
                ird.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + cache.getPdxDiskStore());
            } else {
                ird.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + "DEFAULT");
            }
        } else {
            cache.setPdxPersistent(CacheConfig.DEFAULT_PDX_PERSISTENT);
            ird.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = " + cache.getPdxPersistent());
        }
        // Set read-serialized
        if (readSerialized != null) {
            cache.setPdxReadSerialized(readSerialized);
        } else {
            cache.setPdxReadSerialized(CacheConfig.DEFAULT_PDX_READ_SERIALIZED);
        }
        ird.addLine(CliStrings.CONFIGURE_PDX__READ__SERIALIZED + " = " + cache.getPdxReadSerialized());
        // Set ingoreUnreadFields
        if (ignoreUnreadFields != null) {
            cache.setPdxIgnoreUnreadFields(ignoreUnreadFields);
        } else {
            cache.setPdxIgnoreUnreadFields(CacheConfig.DEFAULT_PDX_IGNORE_UNREAD_FIELDS);
        }
        ird.addLine(CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS + " = " + cache.getPdxIgnoreUnreadFields());
        if (portablePatterns != null) {
            ReflectionBasedAutoSerializer autoSerializer = new ReflectionBasedAutoSerializer(portablePatterns);
            cache.setPdxSerializer(autoSerializer);
            ird.addLine("PDX Serializer " + cache.getPdxSerializer().getClass().getName());
            ird.addLine("Portable classes " + Arrays.toString(portablePatterns));
        }
        if (patterns != null) {
            ReflectionBasedAutoSerializer nonPortableAutoSerializer = new ReflectionBasedAutoSerializer(true, patterns);
            cache.setPdxSerializer(nonPortableAutoSerializer);
            ird.addLine("PDX Serializer : " + cache.getPdxSerializer().getClass().getName());
            ird.addLine("Non portable classes :" + Arrays.toString(patterns));
        }
        final StringWriter stringWriter = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(stringWriter);
        CacheXmlGenerator.generate(cache, printWriter, true, false, false);
        printWriter.close();
        String xmlDefinition = stringWriter.toString();
        // TODO jbarrett - shouldn't this use the same loadXmlDefinition that other constructors use?
        XmlEntity xmlEntity = XmlEntity.builder().withType(CacheXml.PDX).withConfig(xmlDefinition).build();
        result = ResultBuilder.buildResult(ird);
        persistClusterConfiguration(result, () -> getSharedConfiguration().addXmlEntity(xmlEntity, null));
    } catch (Exception e) {
        return ResultBuilder.createGemFireErrorResult(e.getMessage());
    }
    return result;
}
Also used : XmlEntity(org.apache.geode.management.internal.configuration.domain.XmlEntity) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) StringWriter(java.io.StringWriter) ReflectionBasedAutoSerializer(org.apache.geode.pdx.ReflectionBasedAutoSerializer) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) Result(org.apache.geode.management.cli.Result) PrintWriter(java.io.PrintWriter) 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