|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | METHOD | DETAIL: FIELD | METHOD |
interface Shell
Contains shell related methods. Accessible in any script under the variable shell
.
Method Summary | |
---|---|
java.lang.Object
|
ant(groovy.lang.Closure closure)
Exporting ant access to the shell to run any |
java.lang.String
|
cat(java.lang.Object location)
Returns the content of the location as a |
java.lang.Object
|
chmod(java.lang.Object file, java.lang.Object perm)
Changes the permission of the |
Resource
|
chmodPlusX(java.lang.Object file)
Shortcut/More efficient implementation of the more generic chmod(Object, Object) call |
Resource
|
chmodRecursive(java.lang.Object dir, java.lang.Object perm)
Changes the permission of the |
Resource
|
cp(java.lang.Object from, java.lang.Object to)
Copy from to to... |
Resource
|
createTempDir()
Create a temporary directory |
Resource
|
createTempDir(java.lang.Object args)
Create a temporary directory |
java.lang.Object
|
demultiplexExecStream(java.io.InputStream execStream, java.io.OutputStream stdout, java.io.OutputStream stderr)
Demultiplexes the exec stream as generated by exec(Map) when |
java.lang.Object
|
deserializeFromFile(java.lang.Object file)
Reads the content of the file and deserializes it (java serialization). |
Resource
|
eachChildRecurse(java.lang.Object dir, java.lang.Object closure)
Every child resource of this resource (recursively) is being passed to the closure. |
java.lang.String
|
exec(java.lang.Object executable, java.lang.Object executableArgs)
Executes a shell command... the command will be delegated straight to shell and the output of the shell command is returned. |
java.lang.String
|
exec(java.lang.Object executable, java.lang.Object... executableArgs)
Executes a shell command... the command will be delegated straight to shell and the output of the shell command is returned. |
java.lang.String
|
exec(java.lang.String command)
Executes a shell command... the command will be delegated straight to shell and the output of the shell command is returned. |
java.lang.String
|
exec(java.util.Collection command)
Executes a shell command... the command will be delegated straight to shell and the output of the shell command is returned. |
java.lang.Object
|
exec(java.util.Map args)
More generic form of the exec call which allows you to configure what you provide and what you expect. |
void
|
fail(java.lang.Object message)
Calling this method will force a script failure (will throw an exception) |
Resource
|
fetch(java.lang.Object location)
Fetches the file pointed to by the location. |
Resource
|
fetch(java.lang.Object location, java.lang.Object destination)
Fetches the file pointed to by the location. |
java.lang.Object
|
findAll(java.lang.Object dir, java.lang.Object closure)
Every child resource of this resource (recursively) is being passed to the closure. |
java.util.Map
|
getEnv()
@return all the environment properties exposed by the glu agent (most are coming from its
configuration, others are computed (like |
java.util.Collection
|
getMimeTypes(java.lang.Object file)
Try to guess the mime types of a given file |
Resource
|
getRoot()
the root of the file system used by this shell. |
Resource
|
getTmpRoot()
the tmp root of the file system used by this shell. |
java.util.Collection
|
grep(java.lang.Object location, java.lang.Object pattern)
Similarly to the unix grep command, checks the location one line at a time and returns all the lines which matches the pattern. |
java.lang.Object
|
grep(java.lang.Object location, java.lang.Object pattern, java.lang.Object options)
Similarly to the unix grep command, checks the location one line at a time and returns all the lines which matches the pattern |
Resource
|
gunzip(java.lang.Object file)
Gunzips the provided file in a temporary location |
Resource
|
gunzip(java.lang.Object file, java.lang.Object toFile)
Gunzips the provided file in the provided location |
Resource
|
gzip(java.lang.Object file)
Compresses the provided file (in the same folder) |
Resource
|
gzip(java.lang.Object file, java.lang.Object toFile)
Compresses the provided file as |
java.util.Map
|
gzipFileOrFolder(java.lang.Object fileOrFolder, boolean recurse)
Compresses each file in a folder |
java.util.Map
|
httpHead(java.lang.Object location)
Issue a 'HEAD' request. |
java.util.Map
|
httpPost(java.lang.Object location, java.util.Map parameters)
Issue a 'POST' request. |
boolean
|
listening(java.lang.Object server, java.lang.Object port)
@return |
java.lang.Object
|
ls(java.lang.Object dir, groovy.lang.Closure closure)
@param dir starting point for listing ({@see #toResource(Object)} for possible values) |
java.lang.Object
|
ls(java.lang.Object dir)
list all the files under the provided directory (or root if not provided) (not recursive) |
java.lang.Object
|
ls()
list all the files under root only (not recursive) |
java.lang.Object
|
ls(groovy.lang.Closure closure)
Same as the other |
Resource
|
mkdirs(java.lang.Object dir)
Create the directory as well as its parents if they don't exist |
Resource
|
mv(java.lang.Object from, java.lang.Object to)
Move from to to... |
java.lang.Object
|
noException(groovy.lang.Closure closure)
Runs the closure in a protected block that will not throw an exception but will return
|
java.lang.String
|
readContent(java.lang.Object file)
reads the content from the file and return it as a |
java.lang.String
|
replaceTokens(java.lang.String input, java.util.Map tokens)
Replaces the tokens provided in the map in the input. |
Resource
|
replaceTokens(java.lang.Object from, java.lang.Object to, java.util.Map tokens)
Processes |
void
|
rm(java.lang.Object file)
Delete the file |
void
|
rmEmptyDirs(java.lang.Object dir)
Remove all empty directories (that are children (recursively) of the provided directory). |
void
|
rmdirs(java.lang.Object dir)
Delete the directory (recursive) |
java.lang.Object
|
safeOverwrite(java.lang.Object file, groovy.lang.Closure closure)
This convenient call takes a file you want to (over)write to and a closure. |
Resource
|
saveContent(java.lang.Object file, java.lang.String content)
creates a file and populate its content with the provided ( |
Resource
|
saveContent(java.lang.Object file, java.lang.String content, java.util.Map tokens)
Processes the content to the token replacement method. |
Resource
|
serializeToFile(java.lang.Object file, java.lang.Object serializable)
Serializes (java serialization!) |
java.io.InputStream
|
tail(java.lang.Object file, long maxLine)
Tail the file |
java.io.InputStream
|
tail(java.lang.Object args)
Tail the file |
Resource
|
tempFile(java.lang.Object args)
Creates a temp file: |
Resource
|
tempFile()
Creates a temp file with all default values |
Resource
|
toResource(java.lang.Object file)
Returns a resource relative to this filesystem. |
Resource
|
untar(java.lang.Object file)
Untars the provided file in a temporary location. |
Resource
|
untar(java.lang.Object file, java.lang.Object toDir)
Untars the provided file in the provided location. |
Resource
|
untarAndDecrypt(java.lang.Object file, java.lang.Object toDir, java.lang.Object encryptionKeys)
untar + decrypt using the encryption keys (encrytion keys are automatically provided by glu
and are available in any glu script with |
Resource
|
unzip(java.lang.Object file)
Unzips the provided file in a temporary location |
Resource
|
unzip(java.lang.Object file, java.lang.Object toDir)
Unzips the provided file in the provided location |
boolean
|
waitFor(java.lang.Object args, groovy.lang.Closure condition)
Waits for the condition to be |
boolean
|
waitFor(groovy.lang.Closure condition)
Shortcut when no args |
java.lang.Object
|
withInputStream(java.lang.Object file, java.lang.Object closure)
Safe pattern to read from a file: you provide the |
java.lang.Object
|
withMBeanServerConnection(java.lang.Object pid, groovy.lang.Closure closure)
Invokes the closure with an |
java.lang.Object
|
withObjectInputStream(java.lang.Object file, java.lang.Object closure)
Safe pattern to read from a file: you provide the |
java.lang.Object
|
withObjectOutputStream(java.lang.Object file, java.lang.Object closure)
Safe pattern to write to a file: you provide the |
java.lang.Object
|
withOutputStream(java.lang.Object file, java.lang.Object closure)
Safe pattern to write to a file: you provide the |
java.lang.Object
|
withReader(java.lang.Object file, groovy.lang.Closure closure)
Same as |
java.lang.Object
|
withWriter(java.lang.Object file, groovy.lang.Closure closure)
Same as |
Method Detail |
---|
java.lang.Object ant(groovy.lang.Closure closure)
ant
command.
java.lang.String cat(java.lang.Object location)
String
or
null
if the location is not reachable
location
- the location you want to fetch (usually remote)Strint
or null
java.lang.Object chmod(java.lang.Object file, java.lang.Object perm)
file
file
- ({@see #toResource(Object)} for possible values)perm
- expressed in unix fashion (ex: +x)
Resource chmodPlusX(java.lang.Object file)
file
- ({@see #toResource(Object)} for possible values)Resource
Resource chmodRecursive(java.lang.Object dir, java.lang.Object perm)
dir
and recursively
dir
- ({@see #toResource(Object)} for possible values)perm
- expressed in unix fashion (ex: +x)
Resource cp(java.lang.Object from, java.lang.Object to)
from
- ({@see #toResource(Object)} for possible values)to
- ({@see #toResource(Object)} for possible values)to
Resource createTempDir()
Resource createTempDir(java.lang.Object args)
java.lang.Object demultiplexExecStream(java.io.InputStream execStream, java.io.OutputStream stdout, java.io.OutputStream stderr)
args.res
is
stream
. The following is equivalent:
OutputStream myStdout = ...
OutputStream myStderr = ...
exec(command: xxx, stdout: myStdout, stderr: myStderr, res: exitValue)
is 100% equivalent to:
demultiplexExecStream(exec(command: xxx, res: stream), myStdout, myStderr)args.failOnError
was set to true
execStream
- the stream as generated by exec(Map)stdout
- the stream to write the output (optional, can be null
)stderr
- the stream to write the error (optional, can be null
)
java.lang.Object deserializeFromFile(java.lang.Object file)
file
- ({@see #toResource(Object)} for possible values)
Resource eachChildRecurse(java.lang.Object dir, java.lang.Object closure)
dir
- where to start the recursion ({@see #toResource(Object)} for possible values)closure
- will be called back for all children (recursively) as a Resource
and can do whatever it wants with it (return value will be ignored)
java.lang.String exec(java.lang.Object executable, java.lang.Object executableArgs)
executable
- ({@see #toResource(Object)} for possible values)executableArgs
- either a String
or a Collection
java.lang.String exec(java.lang.Object executable, java.lang.Object... executableArgs)
java.lang.String exec(java.lang.String command)
command
- the full command (including the arguments) as a String
java.lang.String exec(java.util.Collection command)
command
- the full command (including the arguments) as a Collection
java.lang.Object exec(java.util.Map args)
args.stdout
or
args.stderr
it will be executed in a separate thread (in order to avoid
blocking indefinitely).
- Note that if you request stdout
, stderr
or
all
for the result of this call, stdout
and stderr
are
converted to a String
using (by default) the "UTF-8" charset and all lines
are terminated with "\n" and the last "\n" is removed. Use stdoutBytes
or
stderrBytes
if you wish to get the bytes directly
- Note that if you request stream
, then the call return immediately
(it is non blocking in this case) and you get a single InputStream
which multiplexes stdout/stderr and exit value (see MultiplexedInputStream
for
details). In this case you should make sure to read the entire stream and properly close it
as shown in the following code example:
InputStream stream = shell.exec(command: 'xxx', res: 'stream')
try
{
// read stream
}
finally
{
stream.close()
}
- Note that if you request stdoutStream
or stderrStream
, then the
call return immediately (it is non blocking in this case) and you get the stream you requested.
In this case you should make sure to read the entire stream and properly close it as
shown in the following code example:
InputStream stream = shell.exec(command: 'xxx', res: 'stdoutStream')
try
{
// read stream
}
finally
{
stream.close()
}
args.command
- the command to execute. It will be delegated to the shell so it should
be native to the OS on which the agent runs (either a String
or a Collection
) (required)args.pwd
- the directory from which the command will be run (optional, will
default to the "current" directory)args.env
- a map (String
, String
) containing environment
variables to be passed to sub-process. If the value is null
,
it will remove it from the inherited environment variables. (optional)args.inheritEnv
- a boolean to determine if the environment variables in effect in the
agent will be passed down to the sub-process (optional, default to
true
)args.stdin
- any input that can "reasonably" be converted into an
InputStream
) to provide to the command line execution
(optional, default to no stdin)args.stdout
- Closure
(if you want to handle it yourself),
OutputStream
(where stdout will be written to),
(optional: depends on args.res)args.stderr
- Closure
(if you want to handle it yourself),
OutputStream
(where stdout will be written to),
(optional: depends on args.res)args.redirectStderr
- boolean
to redirect stderr into stdout
(optional, default to false
). Note that this can also
be accomplished with the command itself with something like "2>&1"args.failOnError
- do you want the command to fail (with an exception) when there is
an error (default to true
)args.res
- what do you want the call to return
stdout
, stdoutBytes
, stdoutStream
stderr
, stderrBytes
, stderrStream
all
, allBytes
(a map with 3 parameters, exitValue, stdout, stderr)
exitValue
,
stream
(default to stdout
)
void fail(java.lang.Object message)
Resource fetch(java.lang.Object location)
String
or URI
and must
contain a scheme. Example of locations: http://locahost:8080/file.txt'
,
file:/tmp/file.txt
, ivy:/org.linkedin/util-core/1.0.0
.
location
- the location you want to fetch (usually remote)Resource
)
Resource fetch(java.lang.Object location, java.lang.Object destination)
File
,
a String
or URI
and must contain a scheme. Example of locations:
http://locahost:8080/file.txt'
, file:/tmp/file.txt
,
ivy:/org.linkedin/util-core/1.0.0
. The difference with the other fetch method
is that it fetches the file in the provided destination rather than in the tmp space.
location
- the location you want to fetch (usually remote)destination
- ({@see #toResource(Object)} for possible values)Resource
) (if
destination
is a file then returns destination
as a
Resource
otherwise it will be a Resource
inside the
destination
folder.
java.lang.Object findAll(java.lang.Object dir, java.lang.Object closure)
true
then it will be part of the result.
dir
- where to start the recursion ({@see #toResource(Object)} for possible values)closure
- will be called back for all children (recursively) as a Resource
and should return true
or false
true
java.util.Map getEnv()
glu.agent.pid
which is the
pid of the agent).
java.util.Collection getMimeTypes(java.lang.Object file)
file
- ({@see #toResource(Object)} for possible values)
Resource getRoot()
Resource getTmpRoot()
java.util.Collection grep(java.lang.Object location, java.lang.Object pattern)
location
- (see fetch(Object) for possible values)pattern
- regular expression (see java pattern)
java.lang.Object grep(java.lang.Object location, java.lang.Object pattern, java.lang.Object options)
location
- (see fetch(Object) for possible values)pattern
- regular expression (see java pattern)options
- options to the command:
out
: an object to output the lines which match (default to [])
count
: returns the count only (does not use out)
maxCount
: stop reading after maxCount
matchesoptions
Resource gunzip(java.lang.Object file)
file
- ({@see #toResource(Object)} for possible values)Resource
)
Resource gunzip(java.lang.Object file, java.lang.Object toFile)
file
- ({@see #toResource(Object)} for possible values)toDir
- ({@see #toResource(Object)} for possible values)Resource
)
Resource gzip(java.lang.Object file)
file
- ({@see #toResource(Object)} for possible values)Resource
)
Resource gzip(java.lang.Object file, java.lang.Object toFile)
toFile
file
- ({@see #toResource(Object)} for possible values)toFile
- ({@see #toResource(Object)} for possible values)Resource
)
java.util.Map gzipFileOrFolder(java.lang.Object fileOrFolder, boolean recurse)
fileOrFolder
- a file (behaves like gzip(Object)) or a folder
({@see #toResource(Object)} for possible values)recurse
- if true
then recursively process all folders
java.util.Map httpHead(java.lang.Object location)
java.util.Map httpPost(java.lang.Object location, java.util.Map parameters)
application/x-www-form-urlencoded
content type.
parameters
- the parameters of the post as map of key value pairs (value can be a single
value or a collection of values)
boolean listening(java.lang.Object server, java.lang.Object port)
true
if there is a socket open on the server/port combination
java.lang.Object ls(java.lang.Object dir, groovy.lang.Closure closure)
dir
- starting point for listing ({@see #toResource(Object)} for possible values)closure
- the closure (dsl) containing include(name: '') and exclude(name: '') values
java.lang.Object ls(java.lang.Object dir)
dir
- starting point for listing ({@see #toResource(Object)} for possible values)
java.lang.Object ls()
java.lang.Object ls(groovy.lang.Closure closure)
ls
, but starts at root
closure
- the closure (dsl) containing include(name: '') and exclude(name: '') values
Resource mkdirs(java.lang.Object dir)
dir
- directory to create ({@see #toResource(Object)} for possible values)
Resource mv(java.lang.Object from, java.lang.Object to)
from
- ({@see #toResource(Object)} for possible values)to
- ({@see #toResource(Object)} for possible values)to
java.lang.Object noException(groovy.lang.Closure closure)
null
in the case one happens
null
(and log the exception as a debug message)
java.lang.String readContent(java.lang.Object file)
String
file
- ({@see #toResource(Object)} for possible values)
java.lang.String replaceTokens(java.lang.String input, java.util.Map tokens)
input = "abcd @myToken@" assert "abcd foo" == replaceTokens(input, [myToken: 'foo'])
Resource replaceTokens(java.lang.Object from, java.lang.Object to, java.util.Map tokens)
from
through the replacement token mechanism and writes the result to
to
from
- ({@see #toResource(Object)} for possible values)to
- ({@see #toResource(Object)} for possible values)}tokens
- a map of tokento
as a Resource
void rm(java.lang.Object file)
file
- ({@see #toResource(Object)} for possible values)
void rmEmptyDirs(java.lang.Object dir)
dir
- ({@see #toResource(Object)} for possible values)
void rmdirs(java.lang.Object dir)
dir
- ({@see #toResource(Object)} for possible values)
java.lang.Object safeOverwrite(java.lang.Object file, groovy.lang.Closure closure)
file
- the final file where you want your output to be ({@see #toResource(Object)} for
possible values)closure
- takes a Resource
as a parameter that you should use
Resource saveContent(java.lang.Object file, java.lang.String content)
String
) content
file
- ({@see #toResource(Object)} for possible values)content
- the content to store in the file
Resource saveContent(java.lang.Object file, java.lang.String content, java.util.Map tokens)
Resource serializeToFile(java.lang.Object file, java.lang.Object serializable)
serializable
provided and store in the
file
file
- ({@see #toResource(Object)} for possible values)serializable
- the object that needs to be serialized
java.io.InputStream tail(java.lang.Object file, long maxLine)
java.io.InputStream tail(java.lang.Object args)
Resource tempFile(java.lang.Object args)
args.destdir
- where the file should be created (optional)args.prefix
- a prefix for the file (optional)args.suffix
- a suffix for the file (optional)args.deleteonexit
- if the temp file should be deleted on exit (default toargs.createParents
- if the parent directories should be created (default to
true
)
Resource tempFile()
Resource toResource(java.lang.Object file)
file
- can be of type, java.io.File
,
org.linkedin.util.io.resource.Resource
, java.net.URI
,
java.lang.String
(which can be converted into a URI)
Resource untar(java.lang.Object file)
tar -zxf
)
file
- ({@see #toResource(Object)} for possible values)Resource
)
Resource untar(java.lang.Object file, java.lang.Object toDir)
tar -zxf
)
file
- ({@see #toResource(Object)} for possible values)Resource
)
Resource untarAndDecrypt(java.lang.Object file, java.lang.Object toDir, java.lang.Object encryptionKeys)
args.encriptionKeys
)
file
- ({@see #toResource(Object)} for possible values)toDir
- ({@see #toResource(Object)} for possible values)Resource
)
Resource unzip(java.lang.Object file)
file
- ({@see #toResource(Object)} for possible values)Resource
)
Resource unzip(java.lang.Object file, java.lang.Object toDir)
file
- ({@see #toResource(Object)} for possible values)toDir
- ({@see #toResource(Object)} for possible values)Resource
)
boolean waitFor(java.lang.Object args, groovy.lang.Closure condition)
true
no longer than the timeout. true
is returned if the condition was satisfied, false
otherwise (if you specify
noException)
args.timeout
- how long max to wait (any value convertible to a Timespan
)args.heartbeat
- how long to wait between calling the condition (any value convertible to
a Timespan
)args.noException
- to get false
instead of an exceptioncondition
- the closure should return true
if the condition is satisfied,
false
otherwisetrue
if condition was satisfied within the timeout, false
otherwise (if args.noException is provided
otherwise an exception will
be raised)
boolean waitFor(groovy.lang.Closure condition)
java.lang.Object withInputStream(java.lang.Object file, java.lang.Object closure)
file
and the
closure
gets called back with an java.io.InputStream object as an
argument so you don't have to worry about closing it.
file
- ({@see #toResource(Object)} for possible values)closure
- the callback (takes an java.io.InputStream as argument)
java.lang.Object withMBeanServerConnection(java.lang.Object pid, groovy.lang.Closure closure)
MBeanServerConnection
to the jmx control running
on the vm started with the provided pid. The closure will be invoked with null
if cannot determine the process.
pid
- the pid of the process you want to get an mbean connectionclosure
- will be called back with a MBeanServerConnection
(which will be
null
if cannot connect)
java.lang.Object withObjectInputStream(java.lang.Object file, java.lang.Object closure)
file
and the
closure
gets called back with an java.io.ObjectInputStream object as an
argument so you don't have to worry about closing it.
file
- ({@see #toResource(Object)} for possible values)closure
- the callback (takes an java.io.ObjectInputStream as argument)
java.lang.Object withObjectOutputStream(java.lang.Object file, java.lang.Object closure)
file
and the
closure
gets called back with an java.io.ObjectOutputStream object as an
argument so you don't have to worry about closing it. Note that the implementation use
safeOverwrite(Object, Closure) under the cover making it safe to overwrite previous
content.
file
- ({@see #toResource(Object)} for possible values)closure
- the callback (takes an java.io.ObjectOutputStream as argument)
java.lang.Object withOutputStream(java.lang.Object file, java.lang.Object closure)
file
and the
closure
gets called back with an java.io.OutputStream object as an
argument so you don't have to worry about closing it. Note that the implementation use
safeOverwrite(Object, Closure) under the cover making it safe to overwrite previous
content.
file
- ({@see #toResource(Object)} for possible values)closure
- the callback (takes an java.io.OutputStream as argument)
java.lang.Object withReader(java.lang.Object file, groovy.lang.Closure closure)
withInputStream
but wraps in a reader using a configured charset (defaults
to UTF-8).
file
- ({@see #toResource(Object)} for possible values)
java.lang.Object withWriter(java.lang.Object file, groovy.lang.Closure closure)
withOutputStream
but wraps in a writer using a configured charset (defaults
to UTF-8).
file
- ({@see #toResource(Object)} for possible values)
Groovy Documentation