/** * 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); }
/** * 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); }
/** * 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); }
/** * 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; }
@Override public void run() { FilesystemUtil.setScriptPermission(config, "elasticsearch"); ProcessUtil.executeScript(config, getStartScriptCommand(), null, new ForkedElasticsearchProcessDestroyer(config)); }
.addArgument("list"); List<String> output = ProcessUtil.executeScript(config, cmd); .addArgument(pluginName); ProcessUtil.executeScript(config, removeCmd);
/** * 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); } }
@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); } } }
ProcessUtil.executeScript(config, command, true);
.addArgument(plugin.getUri()); ProcessUtil.executeScript(config, cmd, environment, null);