/** As {@link #alternatives(Collection)}, but explicitly using ( ) grouping characters * to ensure exits are caught. */ public static String alternativesSubshell(Collection<String> commands) { // the spaces are not required, but it might be possible that a (( expr )) is interpreted differently // (won't hurt to have the spaces in any case!) return "( " + Strings.join(commands, " || ") + " )"; }
/** Convenience for {@link #chain(Collection)} */ public static String chain(String ...commands) { return "( " + Strings.join(commands, " && ") + " )"; }
/** * Returns a sequence of chained commands that runs until one of them fails (i.e. joined by '&&') * This currently runs as a subshell (so exits are swallowed) but behaviour may be changed imminently. * (Use {@link #chainGroup(Collection)} or {@link #chainSubshell(Collection)} to be clear.) */ public static String chain(Collection<String> commands) { return "( " + Strings.join(commands, " && ") + " )"; }
/** As {@link #chain(Collection)}, but explicitly using { } grouping characters * to ensure exits are propagated. */ public static String chainGroup(Collection<String> commands) { // spaces required around curly braces return "{ " + Strings.join(commands, " && ") + " ; }"; }
/** As {@link #chainGroup(Collection)} */ public static String chainGroup(String ...commands) { return "{ " + Strings.join(commands, " && ") + " ; }"; }
/** As {@link #alternativesGroup(Collection)} */ public static String alternativesGroup(String ...commands) { return "{ " + Strings.join(commands, " || ") + " ; }"; }
public String getSummary() { if (summary!=null) return summary; return Strings.join(commands, " ; "); }
/** As {@link #chain(Collection)}, but explicitly using ( ) grouping characters * to ensure exits are caught. */ public static String chainSubshell(Collection<String> commands) { // the spaces are not required, but it might be possible that a (( expr )) is interpreted differently // (won't hurt to have the spaces in any case!) return "( " + Strings.join(commands, " && ") + " )"; }
/** * Returns a sequence of chained commands that runs until one of them succeeds (i.e. joined by '||'). * This currently runs as a subshell (so exits are swallowed) but behaviour may be changed imminently. * (Use {@link #alternativesGroup(Collection)} or {@link #alternativesSubshell(Collection)} to be clear.) */ public static String alternatives(Collection<String> commands) { return "( " + Strings.join(commands, " || ") + " )"; }
/** As {@link #alternatives(Collection)}, but explicitly using { } grouping characters * to ensure exits are propagated. */ public static String alternativesGroup(Collection<String> commands) { // spaces required around curly braces return "{ " + Strings.join(commands, " || ") + " ; }"; }
/** As {@link #alternativesSubshell(Collection)} */ public static String alternativesSubshell(String ...commands) { return "( " + Strings.join(commands, " || ") + " )"; }
/** * Returns a sequence of alternative commands that runs until one of the commands succeeds; * or if none succeed, it will run the failure command. * @deprecated since 0.6.0; this method just treats the failure command as another alternative so hardly seems worth it; * prefer {@link #alternatives(Collection)} */ @Deprecated public static String alternatives(Collection<String> commands, String failureCommand) { return format("(%s || %s)", Strings.join(commands, " || "), failureCommand); }
public String apply(Entity input) { return Strings.join(Preconditions.checkNotNull(input.getConfig(ChefConfig.CHEF_RUN_LIST), "%s must be supplied for %s", ChefConfig.CHEF_RUN_LIST, input), ","); } }, true);
public String apply(@Nullable ProcessTaskWrapper<?> input) { if (logger!=null) logger.info(input+" COMMANDS:\n"+Strings.join(input.getCommands(),"\n")); if (logger!=null) logger.info(input+" STDOUT:\n"+input.getStdout()); if (logger!=null) logger.info(input+" STDERR:\n"+input.getStderr()); if (requireZero && input.getExitCode()!=0) throw new IllegalStateException("non-zero exit code in "+input.getSummary()+": see log for more details!"); return input.getStdout(); } };
/** executes a set of commands by sending them as a single process to `bash -c` * (single command argument of all the commands, joined with separator) * <p> * consequence of this is that you should not normally need to escape things oddly in your commands, * type them just as you would into a bash shell (if you find exceptions please note them here!) */ public static int execProcesses(List<String> cmds, Map<String,?> env, OutputStream out, OutputStream err, String separator, boolean asLoginShell, Object contextForLogging) { MutableList<String> commands = new MutableList<String>().append("bash"); if (asLoginShell) commands.append("-l"); commands.append("-c", Strings.join(cmds, Preconditions.checkNotNull(separator, "separator"))); return execSingleProcess(commands, env, out, err, contextForLogging); }
protected RuntimeException logWithDetailsAndThrow(String message, Throwable optionalCause) { log.warn(message+" (throwing)"); Streams.logStreamTail(log, "STDERR of problem in "+Tasks.current(), stderr, 1024); Streams.logStreamTail(log, "STDOUT of problem in "+Tasks.current(), stdout, 1024); Streams.logStreamTail(log, "STDIN of problem in "+Tasks.current(), Streams.byteArrayOfString(Strings.join(getLines(),"\n")), 4096); if (optionalCause!=null) throw new IllegalStateException(message, optionalCause); throw new IllegalStateException(message); }
/** returns a "groupId:artifactId:version:(classifier:)packaging" string * which maven refers to as the co-ordinate */ public String getCoordinate() { return Strings.join(MutableList.<String>of().append(groupId, artifactId, packaging). appendIfNotNull(classifier).append(version), ":"); }
/** creates the TaskBuilder which can be further customized; typically invoked by the initial {@link #newTask()} */ public TaskBuilder<Object> constructCustomizedTaskBuilder() { TaskBuilder<Object> tb = TaskBuilder.builder().dynamic(false).name("ssh: "+getSummary()); tb.tag(BrooklynTasks.tagForStream(BrooklynTasks.STREAM_STDIN, Streams.byteArrayOfString(Strings.join(commands, "\n")))); return tb; }
protected void startWithKnifeAsync() { DynamicTasks.queue( ChefServerTasks.knifeConvergeTask() .knifeRunList(Strings.join(Preconditions.checkNotNull(entity().getConfig(ChefConfig.CHEF_RUN_LIST), "%s must be supplied for %s", ChefConfig.CHEF_RUN_LIST, entity()), ",")) .knifeAddAttributes(entity().getConfig(CHEF_LAUNCH_ATTRIBUTES)) .knifeRunTwice(entity().getConfig(CHEF_RUN_CONVERGE_TWICE)) ); }
/** creates the command for running knife. * in some cases knife may be added multiple times, * and in that case the parameter here tells which time it is being added, * on a single run. */ protected String buildKnifeCommand(int knifeCommandIndex) { MutableList<String> words = new MutableList<String>(); words.add(knifeExecutable()); words.addAll(initialKnifeParameters()); words.addAll(knifeParameters()); String x = knifeConfigFileOption(); if (Strings.isNonBlank(x)) words.add(knifeConfigFileOption()); return Strings.join(words, " "); }