Search in sources :

Example 1 with InstanceConfiguration

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);
    }
}
Also used : Monitor(com.github.alexcojocaru.mojo.elasticsearch.v2.client.Monitor) ElasticsearchClient(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClient)

Example 2 with InstanceConfiguration

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);
}
Also used : TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) Log(org.apache.maven.plugin.logging.Log) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 3 with InstanceConfiguration

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);
        }
    }
}
Also used : ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException) IOException(java.io.IOException) File(java.io.File)

Example 4 with InstanceConfiguration

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");
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) Log(org.apache.maven.plugin.logging.Log) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException) IOException(java.io.IOException) File(java.io.File)

Example 5 with InstanceConfiguration

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);
}
Also used : TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) Log(org.apache.maven.plugin.logging.Log) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Aggregations

File (java.io.File)5 IOException (java.io.IOException)5 ElasticsearchSetupException (com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException)4 Log (org.apache.maven.plugin.logging.Log)4 ClusterConfiguration (com.github.alexcojocaru.mojo.elasticsearch.v2.ClusterConfiguration)2 InstanceConfiguration (com.github.alexcojocaru.mojo.elasticsearch.v2.InstanceConfiguration)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CommandLine (org.apache.commons.exec.CommandLine)2 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)2 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)2 TeeOutputStream (org.apache.commons.io.output.TeeOutputStream)2 Test (org.junit.Test)2 PluginConfiguration (com.github.alexcojocaru.mojo.elasticsearch.v2.PluginConfiguration)1 ElasticsearchClient (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClient)1 Monitor (com.github.alexcojocaru.mojo.elasticsearch.v2.client.Monitor)1 ArtifactException (com.github.alexcojocaru.mojo.elasticsearch.v2.configuration.ArtifactException)1 PostStartClusterSequence (com.github.alexcojocaru.mojo.elasticsearch.v2.step.PostStartClusterSequence)1 PostStartInstanceSequence (com.github.alexcojocaru.mojo.elasticsearch.v2.step.PostStartInstanceSequence)1