Search in sources :

Example 6 with ElasticsearchSetupException

use of com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException in project elasticsearch-maven-plugin by alexcojocaru.

the class ProcessUtil method createEnvironment.

/**
 * Create an environment by merging the current environment and the supplied one.
 * If the supplied environment is null, null is returned.
 * @param environment the environment properties to append
 * @return an execution environment
 */
public static Map<String, String> createEnvironment(Map<String, String> environment) {
    Map<String, String> result = null;
    try {
        result = EnvironmentUtils.getProcEnvironment();
    } catch (IOException ex) {
        throw new ElasticsearchSetupException("Cannot get the current process environment", ex);
    }
    // Clean up the base environment:
    // The elasticsearch start and plugin scripts print warnings if these environment variables
    // are set, so lets unset them.
    result.remove("JAVA_TOOL_OPTIONS");
    result.remove("JAVA_OPTS");
    // The following environment variables may interfere with the elasticsearch instance.
    result.remove("ES_HOME");
    result.remove("ES_JAVA_OPTS");
    result.remove("ES_PATH_CONF");
    result.remove("ES_TMPDIR");
    // Now that we have a clean environment, set the provided variables on it.
    if (environment != null) {
        result.putAll(environment);
    }
    return result;
}
Also used : ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException) IOException(java.io.IOException)

Example 7 with ElasticsearchSetupException

use of com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException 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 8 with ElasticsearchSetupException

use of com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException 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 9 with ElasticsearchSetupException

use of com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException 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)

Example 10 with ElasticsearchSetupException

use of com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException in project elasticsearch-maven-plugin by alexcojocaru.

the class BootstrapClusterStep method executeInitCommand.

private void executeInitCommand(ElasticsearchClient client, Log log, String command) {
    log.debug(String.format("Parsing command: %s", command));
    ElasticsearchCommand esCommand = parseStringCommand(command);
    if (esCommand.isSkip()) {
        return;
    }
    String url = "/" + esCommand.getRelativeUrl();
    String content = esCommand.getJson();
    try {
        switch(esCommand.getRequestMethod()) {
            case PUT:
                client.put(url, content);
                break;
            case POST:
                client.post(url, content, String.class);
                break;
            case DELETE:
                client.delete(url);
                break;
            default:
                throw new IllegalStateException(String.format("Unsupported request method: %s", esCommand.getRequestMethod()));
        }
    } catch (ElasticsearchClientException e) {
        throw new ElasticsearchSetupException(String.format("Cannot execute command %s", command), e);
    }
}
Also used : ElasticsearchCommand(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand) ElasticsearchClientException(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClientException) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException)

Aggregations

ElasticsearchSetupException (com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException)13 IOException (java.io.IOException)8 ElasticsearchCommand (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand)5 File (java.io.File)5 List (java.util.List)3 Log (org.apache.maven.plugin.logging.Log)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ElasticsearchClientException (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClientException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)2 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)2 TeeOutputStream (org.apache.commons.io.output.TeeOutputStream)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ElasticsearchClient (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClient)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Map (java.util.Map)1 CommandLine (org.apache.commons.exec.CommandLine)1