Search in sources :

Example 1 with ElasticsearchClient

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

the class BootstrapClusterStep method execute.

@Override
public void execute(ClusterConfiguration config) {
    if (StringUtils.isBlank(config.getPathInitScript())) {
        // nothing to do; return
        return;
    }
    String filePath = config.getPathInitScript();
    validateFile(filePath);
    // we'll run all commands against the first node in the cluster
    ElasticsearchClient client = new ElasticsearchClient.Builder().withInstanceConfiguration(config.getInstanceConfigurationList().get(0)).withHostname("localhost").build();
    Stream<String> stream = null;
    try {
        stream = Files.lines(Paths.get(filePath));
        stream.forEach(command -> executeInitCommand(client, config.getLog(), command));
    } catch (IOException e) {
        throw new ElasticsearchSetupException("Cannot read the init script file", e);
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
Also used : ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException) IOException(java.io.IOException) ElasticsearchClient(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClient)

Example 2 with ElasticsearchClient

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

the class BootstrapClusterStep method executeInitCommand.

protected void executeInitCommand(ElasticsearchClient client, Log log, ElasticsearchCommand command) {
    String url = "/" + command.getRelativeUrl();
    String content = command.getJson();
    try {
        switch(command.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", command.getRequestMethod()));
        }
    } catch (ElasticsearchClientException e) {
        throw new ElasticsearchSetupException(String.format("Cannot execute command %s", command), e);
    }
}
Also used : ElasticsearchClientException(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClientException) ElasticsearchSetupException(com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException)

Example 3 with ElasticsearchClient

use of com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClient 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 4 with ElasticsearchClient

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

Example 5 with ElasticsearchClient

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

the class WaitToStartClusterStep method execute.

@Override
public void execute(ClusterConfiguration config) {
    try (ElasticsearchClient client = new ElasticsearchClient.Builder().withInstanceConfiguration(config.getInstanceConfigurationList().get(0)).withHostname("localhost").build()) {
        Monitor monitor = new Monitor(client, config.getLog());
        monitor.waitToStartCluster(config.getClusterName(), config.getInstanceConfigurationList().size(), config.getStartupTimeout());
    }
}
Also used : Monitor(com.github.alexcojocaru.mojo.elasticsearch.v2.client.Monitor) ElasticsearchClient(com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClient)

Aggregations

ElasticsearchSetupException (com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException)5 ElasticsearchClient (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClient)3 ElasticsearchCommand (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchCommand)3 IOException (java.io.IOException)3 ElasticsearchClientException (com.github.alexcojocaru.mojo.elasticsearch.v2.client.ElasticsearchClientException)2 Monitor (com.github.alexcojocaru.mojo.elasticsearch.v2.client.Monitor)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 List (java.util.List)1 Map (java.util.Map)1