protected CommandLine getShutdownScriptCommand(String basePath) { String pid = ProcessUtil.getElasticsearchPid(basePath); return ProcessUtil.buildKillCommandLine(pid); }
/** * 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 * @return the output (as separate lines) */ public static List<String> executeScript(InstanceConfiguration config, CommandLine command) { return executeScript(config, command, null, null, false); }
@Override public void run() { FilesystemUtil.setScriptPermission(config, "elasticsearch"); ProcessUtil.executeScript(config, getStartScriptCommand(), null, new ForkedElasticsearchProcessDestroyer(config)); }
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); CommandLine removeCmd = ProcessUtil.buildCommandLine("bin/elasticsearch-plugin") .addArgument("remove") .addArgument(pluginName); ProcessUtil.executeScript(config, removeCmd);
try pid = ProcessUtil.getElasticsearchPid(config.getBaseDir()); log.debug("Read PID '" + pid + "' from pid file"); try isAlive = ProcessUtil.isWindowsProcessAlive(config, pid); log.debug("Process is still running: " + isAlive); config.getId())); CommandLine command = ProcessUtil.buildKillCommandLine(pid); for (int retry = 0; retry < 3; ++retry) ProcessUtil.executeScript(config, command, true); ProcessUtil.cleanupPid(config.getBaseDir()); "... the Elasticsearch process [%d] has stopped.", config.getId())); ProcessUtil.cleanupPid(config.getBaseDir());
private String getArtifactId(String flavour, String version) { if (VersionUtil.isBetween_5_0_0_and_6_2_x(version)) // no flavour for ES under 6.3.0 { return "elasticsearch"; } else if (StringUtils.isEmpty(flavour)) { return "elasticsearch-oss"; } else if ("default".equals(flavour)) { return "elasticsearch"; } else { return String.format("elasticsearch-%s", flavour); } }
@Override public void execute(ClusterConfiguration config) { String version = config.getVersion(); if (StringUtils.isBlank(version)) { throw new ElasticsearchSetupException(String.format( "Please provide a valid Elasticsearch version.")); } if (VersionUtil.isUnder_5_0_0(version)) { throw new ElasticsearchSetupException(String.format( "elasticsearch-maven-plugin supports only versions 5+ of Elasticsearch. You configured: %s.", version)); } }
@Test public void testIsEqualOrGreater_6_4_0() { assertTrue(VersionUtil.isEqualOrGreater_6_4_0("6.4.0")); assertTrue(VersionUtil.isEqualOrGreater_6_4_0("6.4.1")); assertTrue(VersionUtil.isEqualOrGreater_6_4_0("6.5.0")); assertTrue(VersionUtil.isEqualOrGreater_6_4_0("6.11.3")); assertTrue(VersionUtil.isEqualOrGreater_6_4_0("7.0.0")); assertTrue(VersionUtil.isEqualOrGreater_6_4_0("11.2.1")); assertFalse(VersionUtil.isEqualOrGreater_6_4_0("6.3.1")); assertFalse(VersionUtil.isEqualOrGreater_6_4_0("6.3.0")); assertFalse(VersionUtil.isEqualOrGreater_6_4_0("6.0.0")); assertFalse(VersionUtil.isEqualOrGreater_6_4_0("5.1.1")); assertFalse(VersionUtil.isEqualOrGreater_6_4_0("4.11.1")); }
@Override public void execute() throws MojoExecutionException, MojoFailureException { if (skip) { getLog().info("Skipping plugin execution"); return; } ClusterConfiguration clusterConfig = buildClusterConfiguration(); for (InstanceConfiguration config : clusterConfig.getInstanceConfigurationList()) { try { getLog().info(String.format("Stopping Elasticsearch [%s]", config)); String baseDir = config.getBaseDir(); ProcessUtil.executeScript(config, getShutdownScriptCommand(baseDir)); getLog().info(String.format("Elasticsearch [%d] stopped", config.getId())); ProcessUtil.cleanupPid(baseDir); } catch (Exception e) { getLog().error("Exception while stopping Elasticsearch", e); } } }
protected File getUnpackDirectory() { File tempDir = FilesystemUtil.getTempDirectory(); File upackDirectory = new File(tempDir, UUID.randomUUID().toString()); upackDirectory.mkdirs(); return upackDirectory; }
protected CommandLine getStartScriptCommand() CommandLine cmd = ProcessUtil.buildCommandLine("bin/elasticsearch");
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("install") .addArgument("--batch") .addArgument(plugin.getUri()); ProcessUtil.executeScript(config, cmd, environment, null);
@Test public void testIsBetween_5_0_0_and_6_x_x() { assertTrue(VersionUtil.isBetween_5_0_0_and_6_2_x("5.0.0")); assertTrue(VersionUtil.isBetween_5_0_0_and_6_2_x("5.2.1")); assertTrue(VersionUtil.isBetween_5_0_0_and_6_2_x("6.0.0")); assertTrue(VersionUtil.isBetween_5_0_0_and_6_2_x("6.1.0")); assertTrue(VersionUtil.isBetween_5_0_0_and_6_2_x("6.2.0")); assertTrue(VersionUtil.isBetween_5_0_0_and_6_2_x("6.2.19")); assertFalse(VersionUtil.isBetween_5_0_0_and_6_2_x("6.3.0")); assertFalse(VersionUtil.isBetween_5_0_0_and_6_2_x("6.4.9")); assertFalse(VersionUtil.isBetween_5_0_0_and_6_2_x("7.0.0")); assertFalse(VersionUtil.isBetween_5_0_0_and_6_2_x("7.1.0")); }
@Test public void testIsUnder_5_0_0() { assertTrue(VersionUtil.isUnder_5_0_0("0.9.7")); assertTrue(VersionUtil.isUnder_5_0_0("4.2")); assertTrue(VersionUtil.isUnder_5_0_0("4.2.1")); assertFalse(VersionUtil.isUnder_5_0_0("5.0.0")); assertFalse(VersionUtil.isUnder_5_0_0("5.2.9")); assertFalse(VersionUtil.isUnder_5_0_0("6.3.0")); }
/** * 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 * @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) { return executeScript(config, command, environment, processDestroyer, false); }
/** * Download the artifact from the download repository. * @param artifactReference * @param config * @return the downloaded file * @throws IOException */ private File downloadArtifact( ElasticsearchArtifact artifactReference, ClusterConfiguration config) throws IOException { String filename = String.format( ELASTICSEARCH_FILENAME, artifactReference.getArtifactId(), artifactReference.getVersion()); File tempFile = new File(FilesystemUtil.getTempDirectory(), filename); tempFile.deleteOnExit(); FileUtils.deleteQuietly(tempFile); URL downloadUrl = new URL( StringUtils.isBlank(config.getDownloadUrl()) ? String.format(ELASTICSEARCH_DOWNLOAD_URL, filename) : config.getDownloadUrl()); config.getLog().debug("Downloading " + downloadUrl + " to " + tempFile); FileUtils.copyURLToFile(downloadUrl, tempFile); return tempFile; }
@Override public void execute(ClusterConfiguration config) { String flavour = config.getFlavour(); String version = config.getVersion(); config.getLog().debug( "Checking flavour '" + flavour + "' against version '" + version + "'"); if (StringUtils.isNotBlank(flavour) && VersionUtil.isBetween_5_0_0_and_6_2_x(version)) { throw new ElasticsearchSetupException(String.format( "The flavour property is not supported for Elasticsearch [5.0.0 - 6.3.0).")); } }
/** * 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 disableLogging - whether to disable the logging of the command or not * @return the output (as separate lines) */ public static List<String> executeScript( InstanceConfiguration config, CommandLine command, boolean disableLogging) { return executeScript(config, command, null, null, disableLogging); }
/** * Check if the process with the given PID is running or not. * This method only handles processes running on Windows. * @param config - the instance config * @param pid the process ID * @return true if the process is running, false otherwise */ public static boolean isWindowsProcessAlive(InstanceConfiguration config, String pid) { CommandLine command = new CommandLine("tasklist") .addArgument("/FI") .addArgument("PID eq " + pid, true); List<String> output = executeScript(config, command, true); String keyword = String.format(" %s ", pid); boolean isRunning = output.stream().anyMatch(s -> s.contains(keyword)); return isRunning; }
/** * Set the 755 permissions on the given script. * @param config - the instance config * @param scriptName - the name of the script (located in the bin directory) to make executable */ public static void setScriptPermission(InstanceConfiguration config, String scriptName) { if (SystemUtils.IS_OS_WINDOWS) { // we do not have file permissions on windows return; } CommandLine command = new CommandLine("chmod") .addArgument("755") .addArgument(String.format("bin/%s", scriptName)); ProcessUtil.executeScript(config, command); command = new CommandLine("sed") .addArguments("-i''") .addArgument("-e") .addArgument("1s:.*:#!/usr/bin/env bash:", false) .addArgument(String.format("bin/%s", scriptName)); ProcessUtil.executeScript(config, command); } }