All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.alexcojocaru.mojo.elasticsearch.v2.step.RemovePluginsStep Maven / Gradle / Ivy

Go to download

A Maven plugin to run a single node Elasticsearch cluster during the integration test phase of a build

There is a newer version: 6.28
Show newest version
package com.github.alexcojocaru.mojo.elasticsearch.v2.step;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.logging.Log;

import com.github.alexcojocaru.mojo.elasticsearch.v2.ElasticsearchSetupException;
import com.github.alexcojocaru.mojo.elasticsearch.v2.InstanceConfiguration;
import com.github.alexcojocaru.mojo.elasticsearch.v2.util.FilesystemUtil;
import com.github.alexcojocaru.mojo.elasticsearch.v2.util.ProcessUtil;

/**
 * Remove the plugins directory from the current Elasticsearch instance, if any.
 * That prevents the plugin installation from failing when the ES directory is not cleaned up.
 * (https://github.com/alexcojocaru/elasticsearch-maven-plugin/issues/41).
 *
 * @author Alex Cojocaru
 */
public class RemovePluginsStep
        implements InstanceStep
{
    @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");

            FilesystemUtil.setScriptPermission(config, "elasticsearch-plugin");

            CommandLine cmd = ProcessUtil.buildCommandLine("bin/elasticsearch-plugin")
                    .addArgument("list");
            
            List output = ProcessUtil.executeScript(config, cmd);
            // remove empty entries and trim
            List 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);
            }
        }
        else
        {
            log.debug("The plugins directory does not exist");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy