diff --git a/README.md b/README.md index 7fa64cc..d52df5f 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ dockerCompose.isRequiredBy(test) // hooks 'dependsOn composeUp' and 'finalizedBy dockerCompose { // useComposeFiles = ['docker-compose.yml', 'docker-compose.prod.yml'] // like 'docker-compose -f ' + // captureContainersOutput = true // prinnts output of all containers to Gradle output - very useful for debugging // stopContainers = false // useful for debugging // removeContainers = false // removeImages = "None" // Other accepted values are: "All" and "Local" diff --git a/src/main/groovy/com/avast/gradle/dockercompose/ComposeExtension.groovy b/src/main/groovy/com/avast/gradle/dockercompose/ComposeExtension.groovy index e1c7891..65f0797 100644 --- a/src/main/groovy/com/avast/gradle/dockercompose/ComposeExtension.groovy +++ b/src/main/groovy/com/avast/gradle/dockercompose/ComposeExtension.groovy @@ -17,6 +17,7 @@ class ComposeExtension { boolean buildBeforeUp = true boolean waitForTcpPorts = true + boolean captureContainersOutput = false Duration waitAfterTcpProbeFailure = Duration.ofSeconds(1) Duration waitForTcpPortsTimeout = Duration.ofMinutes(15) Duration waitForTcpPortsDisconnectionProbeTimeout = Duration.ofMillis(1000) diff --git a/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy b/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy index 7f2bab5..d6fbb44 100644 --- a/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy +++ b/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeUp.groovy @@ -10,6 +10,7 @@ import org.gradle.process.ExecSpec import org.yaml.snakeyaml.Yaml import java.time.Instant +import java.util.concurrent.Executors class ComposeUp extends DefaultTask { @@ -40,6 +41,9 @@ class ComposeUp extends DefaultTask { e.commandLine extension.composeCommand('up', '-d') } try { + if (extension.captureContainersOutput) { + captureContainersOutput() + } servicesInfos = loadServicesInfo().collectEntries { [(it.name): (it)] } waitForHealthyContainers(servicesInfos.values()) if (extension.waitForTcpPorts) { @@ -52,6 +56,34 @@ class ComposeUp extends DefaultTask { } } + protected void captureContainersOutput() { + def t = Executors.defaultThreadFactory().newThread(new Runnable() { + @Override + void run() { + project.exec { ExecSpec e -> + e.commandLine extension.composeCommand('logs', '-f', '--no-color') + e.standardOutput = new OutputStream() { + def buffer = new ArrayList() + @Override + void write(int b) throws IOException { + if (b == 10 || b == 13) { + if (buffer.size() > 0) { + def toPrint = buffer.collect { it as byte }.toArray() as byte[] + logger.lifecycle(new String(toPrint)) + buffer.clear() + } + } else { + buffer.add(b as Byte) + } + } + } + } + } + }) + t.daemon = true + t.start() + } + protected Iterable loadServicesInfo() { getServiceNames().collect { createServiceInfo(it) } }