Brooklyn

brooklyn.util.ssh
[Java] Class BashCommands

java.lang.Object
  brooklyn.util.ssh.BashCommands

public class BashCommands

Field Summary
static java.lang.String INSTALL_CURL

static java.lang.String INSTALL_SYSSTAT

static java.lang.String INSTALL_TAR

static java.lang.String INSTALL_UNZIP

static java.lang.String INSTALL_WGET

static java.lang.String INSTALL_ZIP

 
Method Summary
static java.lang.String alternatives(java.util.Collection commands)

As alternativesGroup(Collection)

static java.lang.String alternatives(java.lang.String... commands)

As alternatives(Collection), but explicitly using ( ) grouping characters to ensure exits are caught.

static java.lang.String alternativesGroup(java.util.Collection commands)

As alternativesSubshell(Collection)

static java.lang.String alternativesGroup(java.lang.String... commands)

returns the pattern formatted with the given arg if the arg is not null, otherwise returns null

static java.lang.String alternativesSubshell(java.util.Collection commands)

static java.lang.String alternativesSubshell(java.lang.String... commands)

static java.lang.String chain(java.util.Collection commands)

As chain(Collection), but explicitly using { } grouping characters to ensure exits are propagated.

static java.lang.String chain(java.lang.String... commands)

static java.lang.String chainGroup(java.util.Collection commands)

static java.lang.String chainGroup(java.lang.String... commands)

As chainSubshell(Collection)

static java.lang.String chainSubshell(java.util.Collection commands)

static java.lang.String chainSubshell(java.lang.String... commands)

As alternatives(Collection)

static java.lang.String commandToDownloadUrlAs(java.lang.String url, java.lang.String saveAs)

static java.lang.String commandToDownloadUrlsAs(java.util.List urls, java.lang.String saveAs)

as downloadToStdout(List) but varargs for convenience

static java.util.List commandsToDownloadUrlsAs(java.util.List urls, java.lang.String saveAs)

static java.lang.String dontRequireTtyForSudo()

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).

static java.lang.String downloadToStdout(java.util.List urls)

static java.lang.String downloadToStdout(java.lang.String... urls)

static java.lang.String executeCommandThenAsUserTeeOutputToFile(java.lang.String commandWhoseOutputToWrite, java.lang.String user, java.lang.String file)

executes a command, then as user tees the output to the given file.

static java.lang.String fail(java.lang.String message, int code)

as requireTest(String, String, int) but returning the original exit code

static java.lang.String formatIfNotNull(java.lang.String pattern, java.lang.Object arg)

static java.lang.String ifExecutableElse0(java.lang.String executable, java.lang.String command)

Returns a command that runs only if the specified executable exists on the path (using `which`).

static java.lang.String ifExecutableElse1(java.lang.String executable, java.lang.String command)

static java.lang.String ifFileExistsElse0(java.lang.String path, java.lang.String command)

as ifFileExistsElse0(String, String) but returns non-zero if the test fails (also returns non-zero if the command fails, so you can't tell the difference :( -- we need if ; then ; else ; fi semantics for that I think, but not sure how portable that is)

static java.lang.String ifFileExistsElse1(java.lang.String path, java.lang.String command)

as ifExecutableElse0(String, String) but returns 1 if the test fails (also returns non-zero if the command fails)

static java.lang.String installExecutable(java.util.Map flags, java.lang.String executable)

Returns a string for checking whether the given executable is available, and installing it if necessary.

static java.lang.String installExecutable(java.lang.String executable)

static java.lang.String installJava6IfPossible()

cats the given text to the given command, using bash << multi-line input syntax

static java.lang.String installJava6OrFail()

static java.lang.String installJava7Or6OrFail()

static java.lang.String installJava7OrFail()

static java.lang.String installPackage(java.lang.String packageDefaultName)

static java.lang.String installPackage(java.util.Map flags, java.lang.String packageDefaultName)

static java.lang.String installPackageOr(java.util.Map flags, java.lang.String packageDefaultName, java.lang.String optionalCommandToRunIfNone)

static java.lang.String installPackageOrFail(java.util.Map flags, java.lang.String packageDefaultName)

static java.lang.String ok(java.lang.String command)

Returns a command that always exits successfully

static java.lang.String onlyIfExecutableMissing(java.lang.String executable, java.lang.String command)

Convenience for chain(Collection)

static java.lang.String pipeTextTo(java.lang.String text, java.lang.String command)

static java.lang.String quiet(java.lang.String command)

Returns a command with all output redirected to /dev/null

static java.lang.String require(java.lang.String command, java.lang.String failureMessage, int exitCode)

fails with nice error if the given file does not exist

static java.lang.String require(java.lang.String command, java.lang.String failureMessage)

fails with nice error if the given file does not exist

static java.lang.String requireExecutable(java.lang.String command)

static java.lang.String requireFile(java.lang.String file)

static java.lang.String requireTest(java.lang.String test, java.lang.String failureMessage, int exitCode)

static java.lang.String requireTest(java.lang.String test, java.lang.String failureMessage)

static java.lang.String simpleDownloadUrlAs(java.util.List urls, java.lang.String saveAs)

static java.lang.String sudo(java.lang.String command)

Returns a command for safely running as root, using sudo.

static java.lang.String sudoAsUser(java.lang.String user, java.lang.String command)

sudo to a given user and run the indicated command;

static java.lang.String warn(java.lang.String message)

returns a command which logs a message to stdout and stderr then exits with the given error code

 
Methods inherited from class java.lang.Object
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Field Detail

INSTALL_CURL

public static final java.lang.String INSTALL_CURL


INSTALL_SYSSTAT

public static final java.lang.String INSTALL_SYSSTAT


INSTALL_TAR

public static final java.lang.String INSTALL_TAR


INSTALL_UNZIP

public static final java.lang.String INSTALL_UNZIP


INSTALL_WGET

public static final java.lang.String INSTALL_WGET


INSTALL_ZIP

public static final java.lang.String INSTALL_ZIP


 
Method Detail

alternatives

public static java.lang.String alternatives(java.util.Collection commands)
As alternativesGroup(Collection)


alternatives

public static java.lang.String alternatives(java.lang.String... commands)
As alternatives(Collection), but explicitly using ( ) grouping characters to ensure exits are caught.


alternativesGroup

public static java.lang.String alternativesGroup(java.util.Collection commands)
As alternativesSubshell(Collection)


alternativesGroup

public static java.lang.String alternativesGroup(java.lang.String... commands)
returns the pattern formatted with the given arg if the arg is not null, otherwise returns null


alternativesSubshell

public static java.lang.String alternativesSubshell(java.util.Collection commands)


alternativesSubshell

public static java.lang.String alternativesSubshell(java.lang.String... commands)


chain

public static java.lang.String chain(java.util.Collection commands)
As chain(Collection), but explicitly using { } grouping characters to ensure exits are propagated.


chain

public static java.lang.String chain(java.lang.String... commands)


chainGroup

public static java.lang.String chainGroup(java.util.Collection commands)


chainGroup

public static java.lang.String chainGroup(java.lang.String... commands)
As chainSubshell(Collection)


chainSubshell

public static java.lang.String chainSubshell(java.util.Collection commands)


chainSubshell

public static java.lang.String chainSubshell(java.lang.String... commands)
As alternatives(Collection)


commandToDownloadUrlAs

public static java.lang.String commandToDownloadUrlAs(java.lang.String url, java.lang.String saveAs)


commandToDownloadUrlsAs

public static java.lang.String commandToDownloadUrlsAs(java.util.List urls, java.lang.String saveAs)
as downloadToStdout(List) but varargs for convenience


commandsToDownloadUrlsAs

public static java.util.List commandsToDownloadUrlsAs(java.util.List urls, java.lang.String saveAs)


dontRequireTtyForSudo

public static java.lang.String dontRequireTtyForSudo()
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).)


downloadToStdout

public static java.lang.String downloadToStdout(java.util.List urls)


downloadToStdout

public static java.lang.String downloadToStdout(java.lang.String... urls)


executeCommandThenAsUserTeeOutputToFile

public static java.lang.String executeCommandThenAsUserTeeOutputToFile(java.lang.String commandWhoseOutputToWrite, java.lang.String user, java.lang.String file)
executes a command, then as user tees the output to the given file. useful e.g. for appending to a file which is only writable by root or a priveleged user.


fail

public static java.lang.String fail(java.lang.String message, int code)
as requireTest(String, String, int) but returning the original exit code


formatIfNotNull

public static java.lang.String formatIfNotNull(java.lang.String pattern, java.lang.Object arg)


ifExecutableElse0

public static java.lang.String ifExecutableElse0(java.lang.String executable, java.lang.String command)
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 Also:
for implementation discussion, using { { test -z `which executable` && true ; } || command ; }


ifExecutableElse1

public static java.lang.String ifExecutableElse1(java.lang.String executable, java.lang.String command)


ifFileExistsElse0

public static java.lang.String ifFileExistsElse0(java.lang.String path, java.lang.String command)
as ifFileExistsElse0(String, String) but returns non-zero if the test fails (also returns non-zero if the command fails, so you can't tell the difference :( -- we need if ; then ; else ; fi semantics for that I think, but not sure how portable that is)


ifFileExistsElse1

public static java.lang.String ifFileExistsElse1(java.lang.String path, java.lang.String command)
as ifExecutableElse0(String, String) but returns 1 if the test fails (also returns non-zero if the command fails)


installExecutable

public static java.lang.String installExecutable(java.util.Map flags, java.lang.String executable)
Returns a string for checking whether the given executable is available, and installing it if necessary.

Uses installPackage and accepts the same flags e.g. for apt, yum, rpm.


installExecutable

public static java.lang.String installExecutable(java.lang.String executable)


installJava6IfPossible

public static java.lang.String installJava6IfPossible()
cats the given text to the given command, using bash << multi-line input syntax


installJava6OrFail

public static java.lang.String installJava6OrFail()


installJava7Or6OrFail

public static java.lang.String installJava7Or6OrFail()


installJava7OrFail

public static java.lang.String installJava7OrFail()


installPackage

public static java.lang.String installPackage(java.lang.String packageDefaultName)


installPackage

public static java.lang.String installPackage(java.util.Map flags, java.lang.String packageDefaultName)


installPackageOr

public static java.lang.String installPackageOr(java.util.Map flags, java.lang.String packageDefaultName, java.lang.String optionalCommandToRunIfNone)


installPackageOrFail

public static java.lang.String installPackageOrFail(java.util.Map flags, java.lang.String packageDefaultName)


ok

public static java.lang.String ok(java.lang.String command)
Returns a command that always exits successfully


onlyIfExecutableMissing

public static java.lang.String onlyIfExecutableMissing(java.lang.String executable, java.lang.String command)
Convenience for chain(Collection)


pipeTextTo

public static java.lang.String pipeTextTo(java.lang.String text, java.lang.String command)


quiet

public static java.lang.String quiet(java.lang.String command)
Returns a command with all output redirected to /dev/null


require

public static java.lang.String require(java.lang.String command, java.lang.String failureMessage, int exitCode)
fails with nice error if the given file does not exist


require

public static java.lang.String require(java.lang.String command, java.lang.String failureMessage)
fails with nice error if the given file does not exist


requireExecutable

public static java.lang.String requireExecutable(java.lang.String command)


requireFile

public static java.lang.String requireFile(java.lang.String file)


requireTest

public static java.lang.String requireTest(java.lang.String test, java.lang.String failureMessage, int exitCode)


requireTest

public static java.lang.String requireTest(java.lang.String test, java.lang.String failureMessage)


simpleDownloadUrlAs

public static java.lang.String simpleDownloadUrlAs(java.util.List urls, java.lang.String saveAs)


sudo

public static java.lang.String sudo(java.lang.String command)
Returns a command for safely running as root, using sudo.

Ensuring non-blocking if password not set by using -n which means to exit if password required (this is unsupported in Ubuntu 8 but all modern OS's seem okay with this!), and (perhaps unnecessarily ?) -S which reads from stdin (routed to /dev/null, it was claimed here previously, though I'm not sure?).

Also specify -E to pass the parent environment in.

If already root, simply runs the command, wrapped in brackets in case it is backgrounded.

The command is not quoted or escaped in any ways. If you are doing privileged redirect you may need to pass e.g. "bash -c 'echo hi > file'".

If null is supplied, it is returned (sometimes used to indicate no command desired).


sudoAsUser

@Betareturn format("{ sudo -E -n -u %s -s -- %s ; }", user, command);
public static java.lang.String sudoAsUser(java.lang.String user, java.lang.String command)
sudo to a given user and run the indicated command;
deprecated:
since 0.7.0 semantics of this are fiddly, e.g. whether user gets their environment


warn

public static java.lang.String warn(java.lang.String message)
returns a command which logs a message to stdout and stderr then exits with the given error code


 

Brooklyn Multi-Cloud Application Management Platform
brooklyncentral.github.com. Apache License. © 2012.