use of com.github.alexcojocaru.mojo.elasticsearch.v2.InstanceConfiguration in project elasticsearch-maven-plugin by alexcojocaru.
the class WaitToStartInstanceStep method execute.
@Override
public void execute(InstanceConfiguration config) {
int timeout = config.getStartupTimeout();
try (ElasticsearchClient client = new ElasticsearchClient.Builder().withInstanceConfiguration(config).withHostname("localhost").build()) {
Monitor monitor = new Monitor(client, config.getClusterConfiguration().getLog());
monitor.waitToStartInstance(config.getBaseDir(), config.getClusterConfiguration().getClusterName(), timeout);
}
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.InstanceConfiguration in project elasticsearch-maven-plugin by alexcojocaru.
the class ProcessUtil method executeScript.
/**
* Run the given command as a process within the supplied instance config context
* and wait until it finalizes. An ElasticsearchSetupException is thrown if the exit code
* is not 0.
* @param config - the instance config
* @param command - the command to execute
* @param commandInput - the optional input string to send to the given command
* @param environment - a map of environment variables; can be null
* @param processDestroyer - a destroyer handler for the spawned process; can be null
* @param disableLogging - whether to disable the logging of the command or not
* @return the output (not trimmed of whitespaces) of the given command, as separate lines
*/
public static List<String> executeScript(InstanceConfiguration config, CommandLine command, Optional<String> commandInput, Map<String, String> environment, ProcessDestroyer processDestroyer, boolean disableLogging) {
Log log = config.getClusterConfiguration().getLog();
int instanceId = config.getId();
File baseDir = new File(config.getBaseDir());
Map<String, String> completeEnvironment = createEnvironment(environment);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(baseDir);
// allows null
executor.setProcessDestroyer(processDestroyer);
ByteArrayInputStream inputStream = new ByteArrayInputStream(commandInput.map(String::getBytes).orElse(new byte[] {}));
// set up a tap on the output stream, to collect to output and return it from this method
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
executor.setStreamHandler(new PumpStreamHandler(disableLogging ? outputStream : new TeeOutputStream(System.out, outputStream), disableLogging ? errorStream : new TeeOutputStream(System.err, errorStream), inputStream));
try {
log.debug(String.format("Using environment: %s", completeEnvironment));
String commandMessage = String.format("Elasticsearch[%d]: Executing command '%s' with input '%s' in directory '%s'", instanceId, command.toString(), commandInput.orElse(""), baseDir);
if (disableLogging) {
log.debug(commandMessage);
} else {
log.info(commandMessage);
}
int exitCode = executor.execute(command, completeEnvironment);
if (exitCode != 0) {
throw new ElasticsearchSetupException(String.format("Elasticsearch [%d]: Command '%s' in directory '%s' finished with exit code %d; see above for details", instanceId, command, baseDir, exitCode));
}
String resultMessage = String.format("Elasticsearch[%d]: The process finished with exit code %d", instanceId, exitCode);
if (disableLogging) {
log.debug(resultMessage);
} else {
log.info(resultMessage);
}
} catch (IOException e) {
List<String> output = readBuffer(outputStream);
List<String> error = readBuffer(errorStream);
String lineSeparator = System.getProperty("line.separator");
StringBuilder message = new StringBuilder();
message.append("Elasticsearch [");
message.append(instanceId);
message.append("]: Cannot execute command '");
message.append(command);
message.append("' in directory '");
message.append(baseDir);
message.append("'");
message.append(lineSeparator);
message.append("Output:");
message.append(lineSeparator);
message.append(StringUtils.join(output, lineSeparator));
message.append(lineSeparator);
message.append("Error:");
message.append(lineSeparator);
message.append(StringUtils.join(error, lineSeparator));
throw new ElasticsearchSetupException(message.toString(), e);
}
return readBuffer(outputStream);
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.InstanceConfiguration in project elasticsearch-maven-plugin by alexcojocaru.
the class RemoveExistingDataStep method execute.
@Override
public void execute(InstanceConfiguration config) {
if (Boolean.FALSE.equals(config.getClusterConfiguration().isKeepExistingData())) {
File baseDir = new File(config.getBaseDir());
try {
File dataDir = FilesystemUtil.getDataDirectory(baseDir);
File logsDir = FilesystemUtil.getLogsDirectory(baseDir);
FileUtils.deleteDirectory(dataDir);
FileUtils.deleteDirectory(logsDir);
} catch (IOException e) {
throw new ElasticsearchSetupException("Failed to delete the existing Elasticsearch data from " + baseDir.getAbsolutePath(), e);
}
}
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.InstanceConfiguration in project elasticsearch-maven-plugin by alexcojocaru.
the class RemovePluginsStep method execute.
@Override
public void execute(InstanceConfiguration config) {
Log log = config.getClusterConfiguration().getLog();
File pluginsDir = new File(config.getBaseDir(), "plugins");
try {
log.debug(String.format("Checking if the plugins directory with path: '%s' exists", pluginsDir.getCanonicalPath()));
} catch (IOException e) {
throw new ElasticsearchSetupException("Cannot check if the plugins directory exists", e);
}
if (pluginsDir.exists()) {
log.debug("The plugins directory exists; removing all installed plugins");
if (VersionUtil.isEqualOrGreater_6_4_0(config.getClusterConfiguration().getVersion())) {
FilesystemUtil.setScriptPermission(config, "elasticsearch-cli");
}
FilesystemUtil.setScriptPermission(config, "elasticsearch-plugin");
CommandLine cmd = ProcessUtil.buildCommandLine("bin/elasticsearch-plugin").addArgument("list");
List<String> output = ProcessUtil.executeScript(config, cmd, config.getEnvironmentVariables());
// remove empty entries and trim
List<String> pluginNames = output.stream().map(String::trim).filter(StringUtils::isNotEmpty).collect(Collectors.toCollection(ArrayList::new));
for (String pluginName : pluginNames) {
log.info(String.format("Removing plugin '%s'", pluginName));
CommandLine removeCmd = ProcessUtil.buildCommandLine("bin/elasticsearch-plugin").addArgument("remove").addArgument(pluginName);
ProcessUtil.executeScript(config, removeCmd, config.getEnvironmentVariables());
}
} else {
log.debug("The plugins directory does not exist");
}
}
use of com.github.alexcojocaru.mojo.elasticsearch.v2.InstanceConfiguration in project elasticsearch-maven-plugin by alexcojocaru.
the class ProcessUtil method executeScript.
/**
* Run the given command as a process within the supplied instance config context
* and wait until it finalizes. An ElasticsearchSetupException is thrown if the exit code
* is not 0.
* @param config - the instance config
* @param command - the command to execute
* @param environment - a map of environment variables; can be null
* @param processDestroyer - a destroyer handler for the spawned process; can be null
* @param disableLogging - whether to disable the logging of the command or not
* @return the output (not trimmed of whitespaces) of the given command, as separate lines
*/
public static List<String> executeScript(InstanceConfiguration config, CommandLine command, Map<String, String> environment, ProcessDestroyer processDestroyer, boolean disableLogging) {
Log log = config.getClusterConfiguration().getLog();
int instanceId = config.getId();
File baseDir = new File(config.getBaseDir());
Map<String, String> completeEnvironment = createEnvironment(environment);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(baseDir);
// allows null
executor.setProcessDestroyer(processDestroyer);
// set up a tap on the output stream, to collect to output and return it from this method
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
executor.setStreamHandler(new PumpStreamHandler(disableLogging ? outputStream : new TeeOutputStream(System.out, outputStream), disableLogging ? errorStream : new TeeOutputStream(System.err, errorStream)));
try {
log.debug(String.format("Using environment: %s", completeEnvironment));
String commandMessage = String.format("Elasticsearch[%d]: Executing command '%s' in directory '%s'", instanceId, command.toString(), baseDir);
if (disableLogging) {
log.debug(commandMessage);
} else {
log.info(commandMessage);
}
int exitCode = executor.execute(command, completeEnvironment);
if (exitCode != 0) {
throw new ElasticsearchSetupException(String.format("Elasticsearch [%d]: Command '%s' in directory '%s' finished with exit code %d; see above for details", instanceId, command, baseDir, exitCode));
}
String resultMessage = String.format("Elasticsearch[%d]: The process finished with exit code %d", instanceId, exitCode);
if (disableLogging) {
log.debug(resultMessage);
} else {
log.info(resultMessage);
}
} catch (IOException e) {
List<String> output = readBuffer(outputStream);
List<String> error = readBuffer(errorStream);
String lineSeparator = System.getProperty("line.separator");
StringBuilder message = new StringBuilder();
message.append("Elasticsearch [");
message.append(instanceId);
message.append("]: Cannot execute command '");
message.append(command);
message.append("' in directory '");
message.append(baseDir);
message.append("'");
message.append(lineSeparator);
message.append("Output:");
message.append(lineSeparator);
message.append(StringUtils.join(output, lineSeparator));
message.append(lineSeparator);
message.append("Error:");
message.append(lineSeparator);
message.append(StringUtils.join(error, lineSeparator));
throw new ElasticsearchSetupException(message.toString(), e);
}
return readBuffer(outputStream);
}
Aggregations