public static void extractTar(File tarFile, File targetDirectory, Duration timeLimit) throws CommandFailedException { Preconditions.checkNotNull(tarFile, "tarFile is null"); Preconditions.checkNotNull(targetDirectory, "targetDirectory is null"); Preconditions.checkArgument(targetDirectory.isDirectory(), "targetDirectory is not a directory: " + targetDirectory.getAbsolutePath()); new Command("tar", "zxf", tarFile.getAbsolutePath()) .setDirectory(targetDirectory) .setTimeLimit(timeLimit) .execute(executor); }
@Override public SlotLifecycleState status(Deployment deployment) { try { int exitCode = createCommand("status", deployment, launcherTimeout) .setSuccessfulExitCodes(0, 1, 2, 3) .execute(executor); if (exitCode == 0) { return RUNNING; } else { return STOPPED; } } catch (CommandFailedException e) { return UNKNOWN; } }
public Command addArgs(String... args) { Preconditions.checkNotNull(args, "args is null"); return addArgs(ImmutableList.copyOf(args)); }
private Command createCommand(String commandName, Deployment deployment, Duration timeLimit) { File launcherScript = new File(new File(deployment.getDeploymentDir(), "bin"), "launcher"); Command command = new Command(launcherScript.getAbsolutePath(), commandName) .setDirectory(deployment.getDataDir()) .setTimeLimit(timeLimit) .addEnvironment("HOME", deployment.getDataDir().getAbsolutePath()); return command; }
throws CommandFailedException, InterruptedException ProcessBuilder processBuilder = new ProcessBuilder(command.getCommand()); processBuilder.directory(command.getDirectory()); processBuilder.redirectErrorStream(true); processBuilder.environment().putAll(command.getEnvironment()); if (!command.getSuccessfulExitCodes().contains(exitCode)) { String out = outputProcessor.getOutput(); throw new CommandFailedException(command, exitCode, out);
@Override public SlotLifecycleState start(Deployment deployment) { updateNodeConfig(deployment); Command command = createCommand("start", deployment, launcherTimeout); try { command.execute(executor); return RUNNING; } catch (CommandFailedException e) { log.error("ENVIRONMENT:\n %s", Joiner.on("\n ").withKeyValueSeparator("=").join(command.getEnvironment())); throw new RuntimeException("start failed: " + e.getMessage()); } }
public Command setDirectory(File directory) { Preconditions.checkNotNull(directory, "directory is null"); return new Command(command, successfulExitCodes, directory, environment, timeLimit); }
@Override public SlotLifecycleState stop(Deployment deployment) { updateNodeConfig(deployment); try { createCommand("stop", deployment, stopTimeout).execute(executor); return STOPPED; } catch (CommandFailedException e) { throw new RuntimeException("stop failed: " + e.getMessage()); } }
public Command setDirectory(String directory) { Preconditions.checkNotNull(directory, "directory is null"); return setDirectory(new File(directory)); }
public Command setSuccessfulExitCodes(int... successfulExitCodes) { Preconditions.checkNotNull(successfulExitCodes, "successfulExitCodes is null"); return setSuccessfulExitCodes(ImmutableSet.copyOf(Ints.asList(successfulExitCodes))); }
public Command setTimeLimit(double value, TimeUnit timeUnit) { return setTimeLimit(new Duration(value, timeUnit)); }
public CommandFailedException(Command command, int exitCode, String output) { super(String.format("%s exited with %s\n%s", command.getCommand(), exitCode, output)); this.command = command; this.exitCode = exitCode; this.output = output; }
public Command setTimeLimit(Duration timeLimit) { Preconditions.checkNotNull(timeLimit, "timeLimit is null"); return new Command(command, successfulExitCodes, directory, environment, timeLimit); }
@Override public SlotLifecycleState restart(Deployment deployment) { updateNodeConfig(deployment); try { Command command = createCommand("restart", deployment, stopTimeout); command.execute(executor); return RUNNING; } catch (CommandFailedException e) { throw new RuntimeException("restart failed: " + e.getMessage()); } }
public CommandFailedException(Command command, String message, Throwable cause) { super(String.format("%s %s%s", command.getCommand(), message, cause == null ? "" : ": " + cause.getMessage()), cause); this.command = command; exitCode = null; output = null; }
public static void createTar(File sourceDirectory, File tarFile) throws CommandFailedException { Preconditions.checkNotNull(sourceDirectory, "sourceDirectory is null"); Preconditions.checkArgument(sourceDirectory.isDirectory(), "sourceDirectory is not a directory: " + sourceDirectory.getAbsolutePath()); Preconditions.checkNotNull(tarFile, "tarFile is null"); tarFile.getParentFile().mkdirs(); new Command("tar", "zcf", tarFile.getAbsolutePath(), sourceDirectory.getName()) .setDirectory(sourceDirectory.getParent()) .setTimeLimit(5, TimeUnit.MINUTES) .execute(executor); }
public Command setSuccessfulExitCodes(Set<Integer> successfulExitCodes) { Preconditions.checkNotNull(successfulExitCodes, "successfulExitCodes is null"); Preconditions.checkArgument(!successfulExitCodes.isEmpty(), "successfulExitCodes is empty"); return new Command(command, successfulExitCodes, directory, environment, timeLimit); }
public static void createSymbolicLink(File source, File target) throws CommandFailedException { Preconditions.checkNotNull(source, "source is null"); Preconditions.checkArgument(source.exists(), "source does not exist: " + source.getAbsolutePath()); Preconditions.checkNotNull(target, "source is null"); Preconditions.checkArgument(!target.exists(), "target already exists: " + target.getAbsolutePath()); target.getParentFile().mkdirs(); new Command("ln", "-s", source.getAbsolutePath(), target.getAbsolutePath()) .setDirectory(target.getParent()) .setTimeLimit(5, TimeUnit.MINUTES) .execute(executor); }
public Command addEnvironment(Map<String, String> environment) { Preconditions.checkNotNull(environment, "environment is null"); ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String>builder().putAll(this.environment).putAll(environment); return new Command(command, successfulExitCodes, directory, builder.build(), timeLimit); }
public Command addArgs(Iterable<String> args) { Preconditions.checkNotNull(args, "args is null"); ImmutableList.Builder<String> command = ImmutableList.<String>builder().addAll(this.command).addAll(args); return new Command(command.build(), successfulExitCodes, directory, environment, timeLimit); }