/** requires the test to pass, as valid bash `test` arguments; e.g. * <code>requireTest("-f /etc/hosts", "Hosts file must exist", 1)</code> */ public static String requireTest(String test, String failureMessage, int exitCode) { return require("test "+test, failureMessage, exitCode); }
/** task which returns 0 if pid in the given file is running; * method accepts wildcards so long as they match a single file on the remote end * <p> * returns 1 if no matching file, * 1 if matching file but no matching process, * and 2 if 2+ matching files */ public static SshEffectorTaskFactory<Integer> codePidFromFileRunning(final String pidFile) { return ssh(BashCommands.chain( // this fails, but isn't an error BashCommands.requireTest("-f "+pidFile, "The PID file "+pidFile+" does not exist."), // this fails and logs an error picked up later BashCommands.requireTest("`ls "+pidFile+" | wc -w` -eq 1", "ERROR: there are multiple matching PID files"), // this fails and logs an error picked up later BashCommands.require("cat "+pidFile, "ERROR: the PID file "+pidFile+" cannot be read (permissions?)."), // finally check the process "ps -p `cat "+pidFile+"`")).summary("PID file "+pidFile+" is-running check (exit code)") .allowingNonZeroExitCode() .addCompletionListener(new Function<ProcessTaskWrapper<?>,Void>() { @Override public Void apply(ProcessTaskWrapper<?> input) { if (input.getStderr().contains("ERROR:")) throw new IllegalStateException("Invalid or inaccessible PID filespec: "+pidFile); return null; } }); }
/** as {@link #requireTest(String, String, int)} but returning the original exit code */ public static String requireTest(String test, String failureMessage) { return require("test "+test, failureMessage); }
/** * Returns commands to download the URL, saving as the given file. Will try each URL in turn until one is successful * (see `curl -f` documentation). */ public static List<String> commandsToDownloadUrlsAs(List<String> urls, String saveAs) { return Arrays.asList(INSTALL_CURL, require(simpleDownloadUrlAs(urls, saveAs), "Could not retrieve "+saveAs+". Tried: " + Joiner.on(", ").join(urls), 9)); } public static String commandToDownloadUrlsAs(List<String> urls, String saveAs) {
/** fails with nice error if the given file does not exist */ public static String requireExecutable(String command) { return require("which "+BashStringEscapes.wrapBash(command), "The required executable \""+command+"\" does not exist"); }
public static String commandToDownloadUrlsAs(List<String> urls, String saveAs) { return chain(INSTALL_CURL, require(simpleDownloadUrlAs(urls, saveAs), "Could not retrieve "+saveAs+". Tried: " + Joiner.on(", ").join(urls), 9)); } public static String commandToDownloadUrlAs(String url, String saveAs) {
/** * Returns command to download the URL, sending the output to stdout -- * suitable for redirect by appending " | tar xvf". * Will try each URL in turn until one is successful */ public static String downloadToStdout(List<String> urls) { return chain( INSTALL_CURL + " > /dev/null", require(simpleDownloadUrlAs(urls, null), "Could not retrieve file. Tried: " + Joiner.on(", ").join(urls), 9)); }
public static String commandToDownloadUrlAs(String url, String saveAs) { return chain(INSTALL_CURL, require(simpleDownloadUrlAs(Arrays.asList(url), saveAs), "Could not retrieve "+saveAs+" from " + url, 9)); }