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_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, java.lang.String failureCommand)

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

Returns a sequence of chained commands that runs until one of them succeeds (i.e. joined by '||').

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

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

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

As alternativesGroup(Collection)

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

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

As alternativesSubshell(Collection)

static java.lang.String chain(java.util.Collection 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.

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

Convenience for chain(Collection)

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

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

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

As chainGroup(Collection)

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

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

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

As chainSubshell(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)

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

static java.lang.String dontRequireTtyForSudo()

some machines require a tty for sudo; brooklyn by default does not use a tty (so that it can get separate error+stdout streams); you can enable a tty as an option to every ssh command, or you can do it once and modify the machine so that a tty is not subsequently required.

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

as downloadToStdout(List) but varargs for convenience

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

Same as downloadUrlAs(java.util.List, java.lang.String), except does not install curl, and does not exit on failure, and if saveAs is null it downloads it so stdout.

static java.util.List downloadUrlAs(java.lang.String url, java.lang.String entityVersionPath, java.lang.String pathlessFilenameToSaveAs)

static java.util.List downloadUrlAs(java.util.Map flags, java.lang.String url, java.lang.String entityVersionPath, java.lang.String pathlessFilenameToSaveAs)

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

@deprecated since 0.6.0 use commandsToDownloadUrlsAs(List, String) (because method uses a list), or commandToDownloadUrlsAs(List, String) for the single-string variant

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 exists(java.lang.String executable, java.lang.String... commands)

Returns a command that runs only if the specified executable is in the path.

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

requires the command to have a non-zero exit code; e.g.

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

Returns a command that runs only if the specified file exists

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)

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

static java.lang.String ifFileExistsElse0(java.lang.String path, java.lang.String 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).

static java.lang.String ifFileExistsElse1(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 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 installJava6()

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 installPackage(java.util.Map flags, java.lang.String packageDefaultName)

static java.lang.String installPackage(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 missing(java.lang.String executable, java.lang.String command)

Returns a command that runs only if the specified executable is NOT in the path

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

Returns a command that always exits successfully

static java.lang.String on(java.lang.String osName, java.lang.String command)

Returns a command that runs only if the operating system is as specified; Checks /etc/issue for the specified name

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

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

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

requires the test to pass, as valid bash `test` arguments; e.g.

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

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

fails with nice error if the given file does not exist

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

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

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

fails with nice error if the given file does not exist

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(), java.lang.Object#wait(long), java.lang.Object#wait(long, int), 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_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, java.lang.String failureCommand)


alternatives

public static java.lang.String alternatives(java.util.Collection 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 alternativesGroup(Collection) or alternativesSubshell(Collection) to be clear.)


alternatives

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


alternativesGroup

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


alternativesGroup

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


alternativesSubshell

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


alternativesSubshell

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


chain

public static java.lang.String chain(java.util.Collection 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 chainGroup(Collection) or chainSubshell(Collection) to be clear.)


chain

public static java.lang.String chain(java.lang.String... commands)
Convenience for chain(Collection)


chainGroup

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


chainGroup

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


chainSubshell

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


chainSubshell

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


commandsToDownloadUrlsAs

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


dontRequireTtyForSudo

public static java.lang.String dontRequireTtyForSudo()
some machines require a tty for sudo; brooklyn by default does not use a tty (so that it can get separate error+stdout streams); you can enable a tty as an option to every ssh command, or you can do it once and modify the machine so that a tty is not subsequently required.

this command must be run with allocatePTY set as a flag to ssh. see SshTasks.dontRequireTtyForSudo which sets that up.

(having a tty for sudo seems like another case of imaginary security which is just irritating. like water restrictions at airport security.)


downloadToStdout

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


downloadToStdout

public static java.lang.String downloadToStdout(java.lang.String... urls)
Same as downloadUrlAs(java.util.List, java.lang.String), except does not install curl, and does not exit on failure, and if saveAs is null it downloads it so stdout.


downloadUrlAs

* currently using {@code curl}.
public static java.util.List downloadUrlAs(java.lang.String url, java.lang.String entityVersionPath, java.lang.String pathlessFilenameToSaveAs)


downloadUrlAs

} else {
public static java.util.List downloadUrlAs(java.util.Map flags, java.lang.String url, java.lang.String entityVersionPath, java.lang.String pathlessFilenameToSaveAs)


downloadUrlAs

*/
public static java.util.List downloadUrlAs(java.util.List urls, java.lang.String saveAs)
deprecated:
since 0.6.0 use commandsToDownloadUrlsAs(List, String) (because method uses a list), or commandToDownloadUrlsAs(List, String) for the single-string variant


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.


exists

public static java.lang.String exists(java.lang.String executable, java.lang.String... commands)
Returns a command that runs only if the specified executable is in the path. If command is null, no command runs (and the script component this creates will return true if the executable).
deprecated:
since 0.6.0 use ifExecutableElse0(String, String)


fail

public static java.lang.String fail(java.lang.String message, int code)
requires the command to have a non-zero exit code; e.g. require("which foo", "Command foo must be found", 1)


file

public static java.lang.String file(java.lang.String path, java.lang.String command)
Returns a command that runs only if the specified file exists
deprecated:
since 0.6.0 use ifFileExistsElse0(String, String) or ifFileExistsElse1(String, String)


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)
as ifExecutableElse0(String, String) but returns 1 if the test fails (also returns non-zero if the command fails)


ifFileExistsElse0

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


ifFileExistsElse1

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


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)


installJava6

return installPackageOrFail(MutableMap.of("apt", "openjdk-6-jdk","yum", "java-1.6.0-openjdk-devel"), null);
public static java.lang.String installJava6()


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


installPackage

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


installPackage

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


missing

public static java.lang.String missing(java.lang.String executable, java.lang.String command)
Returns a command that runs only if the specified executable is NOT in the path
deprecated:
since 0.6.0 use onlyIfExecutableMissing(String, String)


ok

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


on

public static java.lang.String on(java.lang.String osName, java.lang.String command)
Returns a command that runs only if the operating system is as specified; Checks /etc/issue for the specified name
deprecated:
since 0.6.0 very non-portable


onlyIfExecutableMissing

public static java.lang.String onlyIfExecutableMissing(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 ; }


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)
as require(String, String, int) but returning the original exit code


require

public static java.lang.String require(java.lang.String command, java.lang.String failureMessage)
requires the test to pass, as valid bash `test` arguments; e.g. requireTest("-f /etc/hosts", "Hosts file must exist", 1)


requireExecutable

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


requireFile

public static java.lang.String requireFile(java.lang.String file)
fails with nice error if the given file does not exist


requireTest

public static java.lang.String requireTest(java.lang.String test, java.lang.String failureMessage, int exitCode)
as requireTest(String, String, int) but returning the original exit code


requireTest

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


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

public static java.lang.String sudoAsUser(java.lang.String user, java.lang.String command)
sudo to a given user and run the indicated command


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.