public static KnifeTaskFactory<Boolean> isKnifeInstalled() { return new KnifeTaskFactory<Boolean>("knife install check") .knifeAddParameters("node list") .notThrowingOnCommonKnifeErrors() .returningIsExitCodeZero(); }
/** Inserts the knife command at the current place in the list. * Can be run multiple times. The knife command added at the end of the list * if this is not invoked (and it is the only command if nothing is {@link #add(String...)}ed. */ public KnifeTaskFactory<RET> addKnifeCommandToScript() { add(KNIFE_PLACEHOLDER); return self(); }
@Nullable protected String knifeSetupCommands() { if (knifeSetupCommands!=null) return knifeSetupCommands; String knifeSetupCommandsFromConfig = entityConfig(ChefConfig.KNIFE_SETUP_COMMANDS); if (knifeSetupCommandsFromConfig!=null) return knifeSetupCommandsFromConfig; // if not supplied will use global config return null; }
/** 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, " "); }
@Override public List<String> getCommands() { MutableList<String> result = new MutableList<String>(); String setupCommands = knifeSetupCommands(); if (setupCommands != null && Strings.isNonBlank(setupCommands)) result.add(setupCommands); int numKnifes = 0; for (String c: super.getCommands()) { if (c==KNIFE_PLACEHOLDER) result.add(buildKnifeCommand(numKnifes++)); else result.add(c); } if (numKnifes==0) result.add(buildKnifeCommand(numKnifes++)); return result.toImmutable(); }
protected <T> T entityConfig(ConfigKey<T> key) { Entity entity = entity(); if (entity!=null) return entity.getConfig(key); return null; }
protected String buildKnifeCommand(int knifeCommandIndex) { String result = super.buildKnifeCommand(knifeCommandIndex); if (runTwice == Boolean.TRUE) result = BashCommands.alternatives(result, result); return result; }
public KnifeConvergeTaskFactory<RET> knifeAddParameters(String word1, String ...words) { super.knifeAddParameters(word1, words); return self(); }
public Void apply(@Nullable ProcessTaskWrapper<?> input) { if (input.getExitCode()!=0 && input.getStderr().indexOf("WARNING: No knife configuration file found")>=0) { String myConfig = knifeConfigFileOption(); if (Strings.isEmpty(myConfig)) throw new IllegalStateException("Config file for Chef knife must be specified in "+ChefConfig.KNIFE_CONFIG_FILE+" (or valid knife default set up)"); else throw new IllegalStateException("Error reading config file for Chef knife ("+myConfig+") -- does it exist?"); } return null; } };
@Override public List<Function<ProcessTaskWrapper<?>, Void>> getCompletionListeners() { MutableList<Function<ProcessTaskWrapper<?>, Void>> result = MutableList.copyOf(super.getCompletionListeners()); if (throwOnCommonKnifeErrors != Boolean.FALSE) insertKnifeCompletionListenerIntoCompletionListenersList(result); return result.toImmutable(); }
protected String knifeExecutable() { if (knifeExecutable!=null) return knifeExecutable; String knifeExecFromConfig = entityConfig(ChefConfig.KNIFE_EXECUTABLE); if (knifeExecFromConfig!=null) return BashStringEscapes.wrapBash(knifeExecFromConfig); // assume on the path, if executable not set return "knife"; }
@Nullable protected String knifeConfigFileOption() { if (knifeConfigFile!=null) return "-c "+knifeConfigFile; String knifeConfigFileFromConfig = entityConfig(ChefConfig.KNIFE_CONFIG_FILE); if (knifeConfigFileFromConfig!=null) return "-c "+BashStringEscapes.wrapBash(knifeConfigFileFromConfig); // if not supplied will use global config return null; }