/** * Returns a command that runs only if the specified executable exists on the path (using `which`). * if the command runs and fails that exit is preserved (but if the executable is not on the path exit code is zero). * @see #ifFileExistsElse0(String, String) for implementation discussion, using <code>{ { test -z `which executable` && true ; } || command ; } */ public static String onlyIfExecutableMissing(String executable, String command) { return alternativesGroup(format("which %s", executable), command); }
/** * Returns a command that runs only if the specified file (or link or directory) exists; * if the command runs and fails that exit is preserved (but if the file does not exist exit code is zero). * Executed as { { not-file-exists && ok ; } || command ; } for portability. * ("if [ ... ] ; then xxx ; else xxx ; fi" syntax is not quite as portable, I seem to recall (not sure, Alex Aug 2013).) */ public static String ifFileExistsElse0(String path, String command) { return alternativesGroup( chainGroup(format("test ! -e %s", path), "true"), command); } /** as {@link #ifFileExistsElse0(String, String)} but returns non-zero if the test fails (also returns non-zero if the command fails,
/** * Returns a command that runs only if the specified executable exists on the path (using `which`). * if the command runs and fails that exit is preserved (but if the executable is not on the path exit code is zero). * @see #ifFileExistsElse0(String, String) for implementation discussion, using <code>{ { test -z `which executable` && true ; } || command ; } */ public static String ifExecutableElse0(String executable, String command) { return alternativesGroup( chainGroup(format("test -z `which %s`", executable), "true"), command); }
/** requires the command to have a non-zero exit code; e.g. * <code>require("which foo", "Command foo must be found", 1)</code> */ public static String require(String command, String failureMessage, int exitCode) { return alternativesGroup(command, fail(failureMessage, exitCode)); }
/** as {@link #require(String, String, int)} but returning the original exit code */ public static String require(String command, String failureMessage) { return alternativesGroup(command, chainGroup("EXIT_CODE=$?", warn(failureMessage), "exit $EXIT_CODE")); }
public static String waitForPortFree(int port, Duration timeout, boolean failOnTimeout) { long secs = Math.max(timeout.toSeconds(), 1); // TODO How platform-dependent are the args + output format of netstat? // TODO Not using sudo as wrapping either netstat call or sudo(alternativesGroup(...)) fails; parentheses too confusing! String netstatCommand = alternativesGroup( "sudo netstat -antp --tcp", // for Centos "sudo netstat -antp TCP"); // for OS X // number could appear in an IP address or as a port; look for white space at end, and dot or colon before String grepCommand = "grep -E '(:|\\.)"+port+"($|\\s)' > /dev/null"; List<String> commands = ImmutableList.of( "for i in {1.."+secs+"}; do", " "+BashCommands.requireExecutable("netstat"), " "+alternativesGroup( chainGroup("which awk", "AWK_EXEC=awk"), chainGroup("which gawk", "AWK_EXEC=gawk"), chainGroup("which /usr/bin/awk", "AWK_EXEC=/usr/bin/awk"), chainGroup("echo \"No awk to determine if Port "+port+" still in use; aborting\"", "exit 1")), " "+netstatCommand+" | $AWK_EXEC '{print $4}' | "+grepCommand+" && result=0 || result=$?", " [ \"$result\" != 0 ] && break", " sleep 1", "done", "if test \"$result\" -eq 0; then", " "+ (failOnTimeout ? "echo \"Port "+port+" still in use (according to netstat); aborting\" && exit 1" : "echo \"Port "+port+" still in use (according to netstat); continuing\""), "fi"); return Joiner.on("\n").join(commands); }
ifExecutableElse0("apt-get", getAptRepository()), "rm -f bin", // if left over from previous incomplete/failed install (not sure why that keeps happening!) alternativesGroup(findOrInstall), alternativesGroup(linkFromHere)) .failOnNonZeroResultCode() .queue();
sudo("touch " + getPidFile()), sudo("chown postgres:postgres " + getPidFile()), alternativesGroup( chainGroup(format("test -e %s", getInstallDir() + "/bin/initdb"), sudoAsUser("postgres", getInstallDir() + "/bin/initdb -D " + getDataDir())),