Skip to content

Commit

Permalink
Issue #2 Adding option to log command output to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
tmullender committed Oct 30, 2014
1 parent dcfe558 commit f2af8f7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ Binds by default to the [lifecycle phase](http://maven.apache.org/ref/current/ma
background|Integer|Run asynchronously, failing after this number of seconds | No
connection|String|Connection type to use | No
executable|String|The executable to use for this execution, __defaults__ to _ansible_ | __Yes__
failOnAnsibleError|boolean|If true, the build will fail when the ansible command returns an error(a non zero exit status), __defaults__ to _false_ | No
forks|Integer|The number of parallel processes to use | No
hosts|String|Pattern for matching hosts to run the module against, __defaults__ to _localhost_ | __Yes__
inventory|File|The inventory host file | No
limit|String|Limit selected hosts to an additional pattern | No
logDirectory|File|If present the plugin will log the output of the execution to files in this directory | No
moduleArgs|String|Module arguments | No
moduleName|String|Module name to execute, __defaults__ to _ping_ | __Yes__
modulePath|File|The path to the ansible module library | No
Expand All @@ -130,9 +132,11 @@ Binds by default to the [lifecycle phase](http://maven.apache.org/ref/current/ma
connection|String|Connection type to use | No
executable|String|The executable to use for this execution, __defaults__ to _ansible-playbook_ | __Yes__
extraVars|String|Additional variables as key=value or YAML/JSON | No
failOnAnsibleError|boolean|If true, the build will fail when the ansible command returns an error(a non zero exit status), __defaults__ to _false_ | No
forks|Integer|The number of parallel processes to use | No
inventory|File|The inventory host file | No
limit|String|Limit selected hosts to an additional pattern | No
logDirectory|File|If present the plugin will log the output of the execution to files in this directory | No
modulePath|File|The path to the ansible module library | No
playbook|File|The playbook to run, __defaults__ to _playbook.yml_ | __Yes__
pollInterval|Integer|The poll interval if using background | No
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
* Created by tmullender on 22/10/14.
*/
public abstract class AbstractAnsibleMojo extends AbstractMojo {

private static final String NEW_LINE_SEPARATOR = System.getProperty("line.separator");;
private static final String STDERR_LOG = "stderr.log";
private static final String STDOUT_LOG = "stdout.log";

/**
* Connection type to use
*/
Expand Down Expand Up @@ -83,6 +88,12 @@ public abstract class AbstractAnsibleMojo extends AbstractMojo {
@Parameter( defaultValue = "false", property = "ansible.failOnAnsibleError" )
private boolean failOnAnsibleError;

/**
* If present the plugin will log the output of the execution to files in this directory
*/
@Parameter( property = "ansible.logDirectory" )
private File logDirectory;

/**
* Constructs a command from the configured parameters and executes it, logging output at debug
*
Expand Down Expand Up @@ -133,14 +144,34 @@ private int execute(final List<String> command) throws IOException, InterruptedE

private void logStream(final InputStream inputStream, final boolean error) throws IOException {
final BufferedReader output = new BufferedReader(new InputStreamReader(inputStream));
Writer fileWriter = null;
try {
fileWriter = createFileWriter(error);
logStream(output, fileWriter, error);
} finally {
fileWriter.close();
}
}

private void logStream(final BufferedReader input, final Writer output, final boolean error) throws IOException {
String line;
while((line = output.readLine()) != null){
while((line = input.readLine()) != null){
if (error) {
getLog().warn(line);
} else {
getLog().debug(line);
}
output.write(line);
output.write(NEW_LINE_SEPARATOR);
}
}

private Writer createFileWriter(final boolean error) throws IOException {
if (logDirectory == null){
return new NoopWriter();
}
final File output = new File(logDirectory, error ? STDERR_LOG : STDOUT_LOG);
return new FileWriter(output, true);
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/co/escapeideas/maven/ansible/NoopWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package co.escapeideas.maven.ansible;

import java.io.IOException;
import java.io.Writer;

/**
* Created by tmullender on 30/10/14.
*/
public class NoopWriter extends Writer {
@Override
public void write(final char[] cbuf, final int off, final int len) throws IOException {

}

@Override
public void write(final String str) throws IOException {

}

@Override
public void flush() throws IOException {

}

@Override
public void close() throws IOException {

}
}

0 comments on commit f2af8f7

Please sign in to comment.