Search in sources :

Example 1 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ExportImportClusterConfigurationCommands method importSharedConfig.

@CliCommand(value = { CliStrings.IMPORT_SHARED_CONFIG }, help = CliStrings.IMPORT_SHARED_CONFIG__HELP)
@CliMetaData(interceptor = "org.apache.geode.management.internal.cli.commands.ExportImportClusterConfigurationCommands$ImportInterceptor", relatedTopic = { CliStrings.TOPIC_GEODE_CONFIG })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.MANAGE)
@SuppressWarnings("unchecked")
public Result importSharedConfig(@CliOption(key = { CliStrings.IMPORT_SHARED_CONFIG__ZIP }, mandatory = true, help = CliStrings.IMPORT_SHARED_CONFIG__ZIP__HELP) String zip) {
    InternalLocator locator = InternalLocator.getLocator();
    if (!locator.isSharedConfigurationRunning()) {
        ErrorResultData errorData = ResultBuilder.createErrorResultData();
        errorData.addLine(CliStrings.SHARED_CONFIGURATION_NOT_STARTED);
        return ResultBuilder.buildResult(errorData);
    }
    InternalCache cache = getCache();
    Set<DistributedMember> servers = CliUtil.getAllNormalMembers(cache);
    Set<String> regionsWithData = servers.stream().map(this::getRegionNamesOnServer).flatMap(Collection::stream).collect(toSet());
    if (!regionsWithData.isEmpty()) {
        return ResultBuilder.createGemFireErrorResult("Cannot import cluster configuration with existing regions: " + regionsWithData.stream().collect(joining(",")));
    }
    byte[][] shellBytesData = CommandExecutionContext.getBytesFromShell();
    String zipFileName = CliUtil.bytesToNames(shellBytesData)[0];
    byte[] zipBytes = CliUtil.bytesToData(shellBytesData)[0];
    Result result;
    InfoResultData infoData = ResultBuilder.createInfoResultData();
    File zipFile = new File(zipFileName);
    try {
        ClusterConfigurationService sc = locator.getSharedConfiguration();
        // backup the old config
        for (Configuration config : sc.getEntireConfiguration().values()) {
            sc.writeConfigToFile(config);
        }
        sc.renameExistingSharedConfigDirectory();
        FileUtils.writeByteArrayToFile(zipFile, zipBytes);
        ZipUtils.unzip(zipFileName, sc.getSharedConfigurationDirPath());
        // load it from the disk
        sc.loadSharedConfigurationFromDisk();
        infoData.addLine(CliStrings.IMPORT_SHARED_CONFIG__SUCCESS__MSG);
    } catch (Exception e) {
        ErrorResultData errorData = ResultBuilder.createErrorResultData();
        errorData.addLine("Import failed");
        logSevere(e);
        result = ResultBuilder.buildResult(errorData);
        // if import is unsuccessful, don't need to bounce the server.
        return result;
    } finally {
        FileUtils.deleteQuietly(zipFile);
    }
    // Bounce the cache of each member
    Set<CliFunctionResult> functionResults = servers.stream().map(this::reCreateCache).collect(toSet());
    for (CliFunctionResult functionResult : functionResults) {
        if (functionResult.isSuccessful()) {
            infoData.addLine("Successfully applied the imported cluster configuration on " + functionResult.getMemberIdOrName());
        } else {
            infoData.addLine("Failed to apply the imported cluster configuration on " + functionResult.getMemberIdOrName() + " due to " + functionResult.getMessage());
        }
    }
    result = ResultBuilder.buildResult(infoData);
    return result;
}
Also used : InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) InternalCache(org.apache.geode.internal.cache.InternalCache) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) FileResult(org.apache.geode.management.internal.cli.result.FileResult) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) InternalLocator(org.apache.geode.distributed.internal.InternalLocator) ClusterConfigurationService(org.apache.geode.distributed.internal.ClusterConfigurationService) DistributedMember(org.apache.geode.distributed.DistributedMember) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) File(java.io.File) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 2 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ExportImportClusterConfigurationCommands method exportSharedConfig.

@CliCommand(value = { CliStrings.EXPORT_SHARED_CONFIG }, help = CliStrings.EXPORT_SHARED_CONFIG__HELP)
@CliMetaData(interceptor = "org.apache.geode.management.internal.cli.commands.ExportImportClusterConfigurationCommands$ExportInterceptor", relatedTopic = { CliStrings.TOPIC_GEODE_CONFIG })
@ResourceOperation(resource = Resource.CLUSTER, operation = Operation.READ)
public Result exportSharedConfig(@CliOption(key = { CliStrings.EXPORT_SHARED_CONFIG__FILE }, mandatory = true, help = CliStrings.EXPORT_SHARED_CONFIG__FILE__HELP) String zipFileName) {
    InternalLocator locator = InternalLocator.getLocator();
    if (!locator.isSharedConfigurationRunning()) {
        return ResultBuilder.createGemFireErrorResult(CliStrings.SHARED_CONFIGURATION_NOT_STARTED);
    }
    Path tempDir;
    try {
        tempDir = Files.createTempDirectory("clusterConfig");
    } catch (IOException e) {
        logSevere(e);
        ErrorResultData errorData = ResultBuilder.createErrorResultData().addLine("Unable to create temp directory");
        return ResultBuilder.buildResult(errorData);
    }
    File zipFile = tempDir.resolve("exportedCC.zip").toFile();
    ClusterConfigurationService sc = locator.getSharedConfiguration();
    Result result;
    try {
        for (Configuration config : sc.getEntireConfiguration().values()) {
            sc.writeConfigToFile(config);
        }
        ZipUtils.zipDirectory(sc.getSharedConfigurationDirPath(), zipFile.getCanonicalPath());
        InfoResultData infoData = ResultBuilder.createInfoResultData();
        byte[] byteData = FileUtils.readFileToByteArray(zipFile);
        infoData.addAsFile(zipFileName, byteData, InfoResultData.FILE_TYPE_BINARY, CliStrings.EXPORT_SHARED_CONFIG__DOWNLOAD__MSG, false);
        result = ResultBuilder.buildResult(infoData);
    } catch (Exception e) {
        ErrorResultData errorData = ResultBuilder.createErrorResultData();
        errorData.addLine("Export failed");
        logSevere(e);
        result = ResultBuilder.buildResult(errorData);
    } finally {
        zipFile.delete();
    }
    return result;
}
Also used : Path(java.nio.file.Path) InternalLocator(org.apache.geode.distributed.internal.InternalLocator) ClusterConfigurationService(org.apache.geode.distributed.internal.ClusterConfigurationService) Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) InfoResultData(org.apache.geode.management.internal.cli.result.InfoResultData) IOException(java.io.IOException) ErrorResultData(org.apache.geode.management.internal.cli.result.ErrorResultData) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) Result(org.apache.geode.management.cli.Result) FileResult(org.apache.geode.management.internal.cli.result.FileResult) CliFunctionResult(org.apache.geode.management.internal.cli.functions.CliFunctionResult) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData) ResourceOperation(org.apache.geode.management.internal.security.ResourceOperation)

Example 3 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ClusterConfigurationService method addJarsToThisLocator.

/**
   * Add jar information into the shared configuration and save the jars in the file system used
   * when deploying jars
   * 
   * @return true on success
   */
public boolean addJarsToThisLocator(String[] jarNames, byte[][] jarBytes, String[] groups) {
    lockSharedConfiguration();
    boolean success = true;
    try {
        if (groups == null) {
            groups = new String[] { ClusterConfigurationService.CLUSTER_CONFIG };
        }
        Region<String, Configuration> configRegion = getConfigurationRegion();
        for (String group : groups) {
            Configuration configuration = configRegion.get(group);
            if (configuration == null) {
                configuration = new Configuration(group);
                createConfigDirIfNecessary(group);
            }
            String groupDir = FilenameUtils.concat(this.configDirPath, group);
            for (int i = 0; i < jarNames.length; i++) {
                String filePath = FilenameUtils.concat(groupDir, jarNames[i]);
                try {
                    File jarFile = new File(filePath);
                    FileUtils.writeByteArrayToFile(jarFile, jarBytes[i]);
                } catch (IOException e) {
                    logger.info(e);
                }
            }
            // update the record after writing the jars to the file system, since the listener
            // will need the jars on file to upload to other locators. Need to update the jars
            // using a new copy of the Configuration so that the change listener will pick up the jar
            // name changes.
            Configuration configurationCopy = new Configuration(configuration);
            configurationCopy.addJarNames(jarNames);
            configRegion.put(group, configurationCopy);
        }
    } catch (Exception e) {
        success = false;
        logger.info(e.getMessage(), e);
    } finally {
        unlockSharedConfiguration();
    }
    return success;
}
Also used : Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) IOException(java.io.IOException) File(java.io.File) TimeoutException(org.apache.geode.cache.TimeoutException) CancelException(org.apache.geode.CancelException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) IOException(java.io.IOException) LeaseExpiredException(org.apache.geode.distributed.LeaseExpiredException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 4 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ClusterConfigurationService method readConfiguration.

/**
   * Reads the configuration information from the shared configuration directory and returns a
   * {@link Configuration} object
   * 
   * @return {@link Configuration}
   */
private Configuration readConfiguration(File groupConfigDir) throws SAXException, ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException, IOException {
    Configuration configuration = new Configuration(groupConfigDir.getName());
    File cacheXmlFull = new File(groupConfigDir, configuration.getCacheXmlFileName());
    File propertiesFull = new File(groupConfigDir, configuration.getPropertiesFileName());
    configuration.setCacheXmlFile(cacheXmlFull);
    configuration.setPropertiesFile(propertiesFull);
    Set<String> jarFileNames = Arrays.stream(groupConfigDir.list()).filter((String filename) -> filename.endsWith(".jar")).collect(Collectors.toSet());
    configuration.addJarNames(jarFileNames);
    return configuration;
}
Also used : Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) File(java.io.File)

Example 5 with Configuration

use of org.apache.geode.management.internal.configuration.domain.Configuration in project geode by apache.

the class ClusterConfigurationService method createConfigurationResponse.

/**
   * Creates a ConfigurationResponse based on the configRequest, configuration response contains the
   * requested shared configuration This method locks the ClusterConfigurationService
   */
public ConfigurationResponse createConfigurationResponse(final ConfigurationRequest configRequest) throws LeaseExpiredException, IOException {
    ConfigurationResponse configResponse = new ConfigurationResponse();
    for (int i = 0; i < configRequest.getNumAttempts(); i++) {
        boolean isLocked = this.sharedConfigLockingService.lock(SHARED_CONFIG_LOCK_NAME, 5000, 5000);
        try {
            if (isLocked) {
                Set<String> groups = configRequest.getGroups();
                groups.add(ClusterConfigurationService.CLUSTER_CONFIG);
                logger.info("Building up configuration response with following configurations: {}", groups);
                for (String group : groups) {
                    Configuration configuration = getConfiguration(group);
                    configResponse.addConfiguration(configuration);
                }
                Map<String, byte[]> jarNamesToJarBytes = getAllJarsFromThisLocator(groups);
                String[] jarNames = jarNamesToJarBytes.keySet().stream().toArray(String[]::new);
                byte[][] jarBytes = jarNamesToJarBytes.values().toArray(new byte[jarNames.length][]);
                configResponse.addJarsToBeDeployed(jarNames, jarBytes);
                configResponse.setFailedToGetSharedConfig(false);
                return configResponse;
            }
        } finally {
            this.sharedConfigLockingService.unlock(SHARED_CONFIG_LOCK_NAME);
        }
    }
    configResponse.setFailedToGetSharedConfig(true);
    return configResponse;
}
Also used : ConfigurationResponse(org.apache.geode.management.internal.configuration.messages.ConfigurationResponse) Configuration(org.apache.geode.management.internal.configuration.domain.Configuration)

Aggregations

Configuration (org.apache.geode.management.internal.configuration.domain.Configuration)25 IOException (java.io.IOException)14 File (java.io.File)11 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 TransformerException (javax.xml.transform.TransformerException)8 SAXException (org.xml.sax.SAXException)8 CancelException (org.apache.geode.CancelException)7 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)7 TimeoutException (org.apache.geode.cache.TimeoutException)7 LeaseExpiredException (org.apache.geode.distributed.LeaseExpiredException)7 Properties (java.util.Properties)5 ClusterConfigurationService (org.apache.geode.distributed.internal.ClusterConfigurationService)5 InternalLocator (org.apache.geode.distributed.internal.InternalLocator)4 ConfigurationResponse (org.apache.geode.management.internal.configuration.messages.ConfigurationResponse)4 Document (org.w3c.dom.Document)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2