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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations