public class BashCommands
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
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 |
Constructor and Description |
---|
BashCommands() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
addSbinPathCommand() |
static java.lang.String |
alternatives(java.util.Collection<java.lang.String> commands)
Returns a sequence of chained commands that runs until one of them succeeds (i.e.
|
static java.lang.String |
alternatives(java.lang.String... commands)
|
static java.lang.String |
alternativesGroup(java.util.Collection<java.lang.String> commands)
As
alternatives(Collection) , but explicitly using { } grouping characters
to ensure exits are propagated. |
static java.lang.String |
alternativesGroup(java.lang.String... commands)
|
static java.lang.String |
alternativesSubshell(java.util.Collection<java.lang.String> commands)
As
alternatives(Collection) , but explicitly using ( ) grouping characters
to ensure exits are caught. |
static java.lang.String |
alternativesSubshell(java.lang.String... commands)
|
static java.lang.String |
appendToEtcHosts(java.lang.String ip,
java.lang.String... hostnames) |
static java.lang.String |
chain(java.util.Collection<java.lang.String> commands)
Returns a sequence of chained commands that runs until one of them fails (i.e.
|
static java.lang.String |
chain(java.lang.String... commands)
Convenience for
chain(Collection) |
static java.lang.String |
chainGroup(java.util.Collection<java.lang.String> commands)
As
chain(Collection) , but explicitly using { } grouping characters
to ensure exits are propagated. |
static java.lang.String |
chainGroup(java.lang.String... commands)
|
static java.lang.String |
chainSubshell(java.util.Collection<java.lang.String> commands)
As
chain(Collection) , but explicitly using ( ) grouping characters
to ensure exits are caught. |
static java.lang.String |
chainSubshell(java.lang.String... commands)
|
static java.util.List<java.lang.String> |
commandsToDownloadUrlsAs(java.util.List<java.lang.String> urls,
java.lang.String saveAs)
Returns commands to download the URL, saving as the given file.
|
static java.lang.String |
commandToDownloadUrlAs(java.lang.String url,
java.lang.String saveAs) |
static java.lang.String |
commandToDownloadUrlsAs(java.util.List<java.lang.String> 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<java.lang.String> urls)
Returns command to download the URL, sending the output to stdout --
suitable for redirect by appending " | tar xvf".
|
static java.lang.String |
downloadToStdout(java.lang.String... urls)
as
downloadToStdout(List) but varargs for convenience |
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)
returns a command which logs a message to stdout and stderr then exits with the given error code
|
static java.lang.String |
formatIfNotNull(java.lang.String pattern,
java.lang.Object arg)
returns the pattern formatted with the given arg if the arg is not null, otherwise returns null
|
static java.lang.String |
generateKeyInDotSshIdRsaIfNotThere()
generates ~/.ssh/id_rsa if that file does not exist
|
static com.google.common.collect.ImmutableList<java.lang.String> |
ifExecutableElse(java.lang.String command,
java.util.List<java.lang.String> ifTrue,
java.util.List<java.lang.String> otherwise) |
static java.lang.String |
ifExecutableElse(java.lang.String command,
java.lang.String ifTrue,
java.lang.String otherwise) |
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 |
ifNotExecutable(java.lang.String command,
java.lang.String statement)
Returns a command which
executes
statement only if command is NOT found in $PATH |
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 |
installJava(int version)
Install a particular Java runtime, fails if not possible.
|
static java.lang.String |
installJava6() |
static java.lang.String |
installJava6IfPossible() |
static java.lang.String |
installJava6OrFail() |
static java.lang.String |
installJava7() |
static java.lang.String |
installJava7IfPossible() |
static java.lang.String |
installJava7Or6OrFail() |
static java.lang.String |
installJava7OrFail() |
static java.lang.String |
installJava8() |
static java.lang.String |
installJava8IfPossible() |
static java.lang.String |
installJavaLatestOrFail() |
static java.lang.String |
installJavaLatestOrWarn() |
static java.lang.String |
installPackage(java.util.Map<?,?> flags,
java.lang.String packageDefaultName)
Returns a command for installing the given package.
|
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 |
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)
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)
cats the given text to the given command, using bash << multi-line input syntax
|
static java.lang.String |
prependToEtcHosts(java.lang.String ip,
java.lang.String... hostnames) |
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)
as
require(String, String, int) but returning the original exit code |
static java.lang.String |
require(java.lang.String command,
java.lang.String failureMessage,
int exitCode)
requires the command to have a non-zero exit code; e.g.
|
static java.lang.String |
requireExecutable(java.lang.String command)
fails with nice error if the given file does not exist
|
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)
as
requireTest(String, String, int) but returning the original exit code |
static java.lang.String |
requireTest(java.lang.String test,
java.lang.String failureMessage,
int exitCode)
requires the test to pass, as valid bash `test` arguments; e.g.
|
static java.util.List<java.lang.String> |
setHostname(java.lang.String newHostname)
Sets the hostname, splitting the given hostname if it contains a dot to include the unqualified and fully qualified names.
|
static java.util.List<java.lang.String> |
setHostname(java.lang.String hostPart,
java.lang.String domainPart)
Sets the hostname to
hostPart + "." + domainPart , or if domainPart is null/empty then {code hostPart}. |
static java.lang.String |
simpleDownloadUrlAs(java.util.List<java.lang.String> urls,
java.lang.String saveAs)
Same as
downloadUrlAs(List, String) , except does not install curl, and does not exit on failure,
and if saveAs is null it downloads it so stdout. |
static java.lang.String |
simpleDownloadUrlAs(java.util.List<java.lang.String> urls,
java.lang.String user,
java.lang.String password,
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)
Deprecated.
since 0.7.0 semantics of this are fiddly, e.g. whether user gets their environment
|
static java.lang.String |
unzip(java.lang.String file,
java.lang.String targetDir) |
static java.lang.String |
waitForFileContents(java.lang.String file,
java.lang.String desiredContent,
Duration timeout,
boolean failOnTimeout) |
static java.lang.String |
waitForPortFree(int port,
Duration timeout,
boolean failOnTimeout) |
static java.lang.String |
warn(java.lang.String message) |
public static final java.lang.String INSTALL_TAR
public static final java.lang.String INSTALL_CURL
public static final java.lang.String INSTALL_WGET
public static final java.lang.String INSTALL_ZIP
public static final java.lang.String INSTALL_UNZIP
public static final java.lang.String INSTALL_SYSSTAT
public static java.lang.String installExecutable(java.util.Map<?,?> flags, java.lang.String executable)
installPackage(java.lang.String)
and accepts the same flags e.g. for apt, yum, rpm.public static java.lang.String installExecutable(java.lang.String executable)
public static java.lang.String quiet(java.lang.String command)
public static java.lang.String ok(java.lang.String command)
public static java.lang.String sudo(java.lang.String command)
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).@Beta public static java.lang.String sudoAsUser(java.lang.String user, java.lang.String command)
public static java.lang.String addSbinPathCommand()
public static java.lang.String executeCommandThenAsUserTeeOutputToFile(java.lang.String commandWhoseOutputToWrite, java.lang.String user, java.lang.String file)
public static java.lang.String dontRequireTtyForSudo()
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.)
public static java.lang.String generateKeyInDotSshIdRsaIfNotThere()
public static java.lang.String ifFileExistsElse0(java.lang.String path, java.lang.String command)
public static java.lang.String ifFileExistsElse1(java.lang.String path, java.lang.String command)
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)public static java.lang.String ifExecutableElse0(java.lang.String executable, java.lang.String command)
public static java.lang.String ifExecutableElse1(java.lang.String executable, java.lang.String command)
ifExecutableElse0(String, String)
but returns 1 if the test fails (also returns non-zero if the command fails)public static java.lang.String ifNotExecutable(java.lang.String command, java.lang.String statement)
statement
only if command
is NOT found in $PATH
command
- statement
- public static java.lang.String onlyIfExecutableMissing(java.lang.String executable, java.lang.String command)
public static java.lang.String ifExecutableElse(java.lang.String command, java.lang.String ifTrue, java.lang.String otherwise)
public static com.google.common.collect.ImmutableList<java.lang.String> ifExecutableElse(java.lang.String command, java.util.List<java.lang.String> ifTrue, java.util.List<java.lang.String> otherwise)
public static java.lang.String chain(java.util.Collection<java.lang.String> commands)
chainGroup(Collection)
or chainSubshell(Collection)
to be clear.)public static java.lang.String chain(java.lang.String... commands)
chain(Collection)
public static java.lang.String chainGroup(java.util.Collection<java.lang.String> commands)
chain(Collection)
, but explicitly using { } grouping characters
to ensure exits are propagated.public static java.lang.String chainGroup(java.lang.String... commands)
public static java.lang.String chainSubshell(java.util.Collection<java.lang.String> commands)
chain(Collection)
, but explicitly using ( ) grouping characters
to ensure exits are caught.public static java.lang.String chainSubshell(java.lang.String... commands)
public static java.lang.String alternatives(java.util.Collection<java.lang.String> commands)
alternativesGroup(Collection)
or alternativesSubshell(Collection)
to be clear.)public static java.lang.String alternatives(java.lang.String... commands)
public static java.lang.String alternativesGroup(java.util.Collection<java.lang.String> commands)
alternatives(Collection)
, but explicitly using { } grouping characters
to ensure exits are propagated.public static java.lang.String alternativesGroup(java.lang.String... commands)
public static java.lang.String alternativesSubshell(java.util.Collection<java.lang.String> commands)
alternatives(Collection)
, but explicitly using ( ) grouping characters
to ensure exits are caught.public static java.lang.String alternativesSubshell(java.lang.String... commands)
public static java.lang.String formatIfNotNull(java.lang.String pattern, java.lang.Object arg)
public static java.lang.String installPackage(java.lang.String packageDefaultName)
public static java.lang.String installPackage(java.util.Map<?,?> flags, java.lang.String packageDefaultName)
Warns, but does not fail or return non-zero if it ultimately fails.
Flags can contain common overrides for apt
, yum
, port
and brew
as the package names can be different for each of those. Setting the default package name to
null will use only the overridden package manager values. The onlyifmissing
flag
adds a check for an executable, and only attempts to install packages if it is not found.
installPackage(ImmutableMap.of("yum", "openssl-devel", "apt", "openssl libssl-dev zlib1g-dev"), "libssl-devel"); installPackage(ImmutableMap.of("apt", "libaio1"), null); installPackage(ImmutableMap.of("onlyifmissing", "curl"), "curl");
public static java.lang.String installPackageOrFail(java.util.Map<?,?> flags, java.lang.String packageDefaultName)
public static java.lang.String installPackageOr(java.util.Map<?,?> flags, java.lang.String packageDefaultName, java.lang.String optionalCommandToRunIfNone)
public static java.lang.String warn(java.lang.String message)
public static java.lang.String fail(java.lang.String message, int code)
public static java.lang.String require(java.lang.String command, java.lang.String failureMessage, int exitCode)
require("which foo", "Command foo must be found", 1)
public static java.lang.String require(java.lang.String command, java.lang.String failureMessage)
require(String, String, int)
but returning the original exit codepublic static java.lang.String requireTest(java.lang.String test, java.lang.String failureMessage, int exitCode)
requireTest("-f /etc/hosts", "Hosts file must exist", 1)
public static java.lang.String requireTest(java.lang.String test, java.lang.String failureMessage)
requireTest(String, String, int)
but returning the original exit codepublic static java.lang.String requireFile(java.lang.String file)
public static java.lang.String requireExecutable(java.lang.String command)
public static java.lang.String waitForFileContents(java.lang.String file, java.lang.String desiredContent, Duration timeout, boolean failOnTimeout)
public static java.lang.String waitForPortFree(int port, Duration timeout, boolean failOnTimeout)
public static java.lang.String unzip(java.lang.String file, java.lang.String targetDir)
public static java.util.List<java.lang.String> commandsToDownloadUrlsAs(java.util.List<java.lang.String> urls, java.lang.String saveAs)
public static java.lang.String commandToDownloadUrlsAs(java.util.List<java.lang.String> urls, java.lang.String saveAs)
public static java.lang.String commandToDownloadUrlAs(java.lang.String url, java.lang.String saveAs)
public static java.lang.String downloadToStdout(java.util.List<java.lang.String> urls)
public static java.lang.String downloadToStdout(java.lang.String... urls)
downloadToStdout(List)
but varargs for conveniencepublic static java.lang.String simpleDownloadUrlAs(java.util.List<java.lang.String> urls, java.lang.String saveAs)
downloadUrlAs(List, String)
, except does not install curl, and does not exit on failure,
and if saveAs is null it downloads it so stdout.public static java.lang.String simpleDownloadUrlAs(java.util.List<java.lang.String> urls, java.lang.String user, java.lang.String password, java.lang.String saveAs)
public static java.lang.String installJava(int version)
Note Java 8 is not yet supported on SUSE
installJava6OrFail()
,
installJava7Or6OrFail()
,
installJavaLatestOrFail()
public static java.lang.String installJava6()
public static java.lang.String installJava7()
public static java.lang.String installJava8()
public static java.lang.String installJava6IfPossible()
public static java.lang.String installJava7IfPossible()
public static java.lang.String installJava8IfPossible()
public static java.lang.String installJava6OrFail()
public static java.lang.String installJava7OrFail()
public static java.lang.String installJava7Or6OrFail()
public static java.lang.String installJavaLatestOrFail()
public static java.lang.String installJavaLatestOrWarn()
public static java.lang.String pipeTextTo(java.lang.String text, java.lang.String command)
public static java.lang.String prependToEtcHosts(java.lang.String ip, java.lang.String... hostnames)
public static java.lang.String appendToEtcHosts(java.lang.String ip, java.lang.String... hostnames)
@Beta public static java.util.List<java.lang.String> setHostname(java.lang.String newHostname)
#setHostname(String, String)}
@Beta public static java.util.List<java.lang.String> setHostname(java.lang.String hostPart, java.lang.String domainPart)
hostPart + "." + domainPart
, or if domainPart is null/empty then {code hostPart}.hostPart
- domainPart
-