Skip to content

Commit

Permalink
composeLogs task added
Browse files Browse the repository at this point in the history
  • Loading branch information
dougecko authored and augi committed Jul 30, 2018
1 parent a9191c0 commit c368960
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 3 deletions.
Empty file added gradle.properties
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class ComposeExecutor {
this.logger = settings.project.logger
}

void executeWithCustomOutputWithExitValue(OutputStream os, String... args) {
executeWithCustomOutput(os, false, args)
}

void executeWithCustomOutputNoExitValue(OutputStream os, String... args) {
executeWithCustomOutput(os, true, args)
}

private void executeWithCustomOutput(OutputStream os, Boolean ignoreExitValue, String... args) {
def ex = this.settings
project.exec { ExecSpec e ->
Expand All @@ -33,7 +41,10 @@ class ComposeExecutor {
}
finalArgs.addAll(args)
e.commandLine finalArgs
e.standardOutput = os
if( null != os ) {
e.standardOutput = os
e.errorOutput = os
}
e.ignoreExitValue = ignoreExitValue
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.avast.gradle.dockercompose
import com.avast.gradle.dockercompose.tasks.ComposeBuild
import com.avast.gradle.dockercompose.tasks.ComposeDown
import com.avast.gradle.dockercompose.tasks.ComposeDownForced
import com.avast.gradle.dockercompose.tasks.ComposeLogs
import com.avast.gradle.dockercompose.tasks.ComposePull
import com.avast.gradle.dockercompose.tasks.ComposePush
import com.avast.gradle.dockercompose.tasks.ComposeUp
Expand All @@ -24,6 +25,7 @@ class ComposeSettings {
final ComposeDownForced downForcedTask
final ComposeBuild buildTask
final ComposePull pullTask
final ComposeLogs logsTask
final ComposePush pushTask
final Project project
final DockerExecutor dockerExecutor
Expand Down Expand Up @@ -69,6 +71,9 @@ class ComposeSettings {
String dockerComposeWorkingDirectory = null
Duration dockerComposeStopTimeout = Duration.ofSeconds(10)

File composeLogToFile = null
String containerLogToDir = null

ComposeSettings(Project project, String name = '') {
this.project = project

Expand All @@ -82,6 +87,8 @@ class ComposeSettings {
downTask.settings = this
downForcedTask = project.tasks.create(name ? "${name}ComposeDownForced" : 'composeDownForced', ComposeDownForced)
downForcedTask.settings = this
logsTask = project.tasks.create(name ? "${name}ComposeLogs" : 'composeLogs', ComposeLogs)
logsTask.settings = this
pushTask = project.tasks.create(name ? "${name}ComposePush" : 'composePush', ComposePush)
pushTask.settings = this

Expand Down Expand Up @@ -193,6 +200,14 @@ class ComposeSettings {
captureContainersOutputToFile = file
}

void setComposeLogToFile(CharSequence path) {
composeLogToFile = project.file(path)
}

void setComposeLogToFile(File file) {
composeLogToFile = file
}

boolean removeOrphans() {
composeExecutor.version >= VersionNumber.parse('1.7.0') && this.removeOrphans
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ class ComposeDownForced extends DefaultTask {
}
args += settings.downAdditionalArgs
}
settings.composeExecutor.execute(args)
def composeLog = null
if(settings.composeLogToFile) {
logger.debug "Logging docker-compose down to: ${settings.composeLogToFile}"
settings.composeLogToFile.parentFile.mkdirs()
composeLog = new FileOutputStream(settings.composeLogToFile)
}
settings.composeExecutor.executeWithCustomOutputWithExitValue(composeLog, args)
} else {
if (!settings.startedServices.empty) {
settings.composeExecutor.execute(*['rm', '-f', *settings.startedServices])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.avast.gradle.dockercompose.tasks

import com.avast.gradle.dockercompose.ComposeSettings
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

class ComposeLogs extends DefaultTask {

ComposeSettings settings

ComposeLogs() {
group = 'docker'
description = 'Records log output from services in containers of docker-compose project'
}

@TaskAction
void logs() {

if( !settings.containerLogToDir ) {
println 'Not recording container logs: containerLogToDir not specified.'
return
}

settings.composeExecutor.serviceNames.each { service ->
println "Extracting container log from service '${service}'"
new File(settings.containerLogToDir).mkdirs()
def logStream = new FileOutputStream("${settings.containerLogToDir}/${service}.log")
String[] args = ['logs', '-t', service]
settings.composeExecutor.executeWithCustomOutputWithExitValue(logStream, args)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ class ComposeUp extends DefaultTask {
}
args += settings.startedServices
try {
settings.composeExecutor.execute(args)
def composeLog = null
if(settings.composeLogToFile) {
logger.debug "Logging docker-compose up to: ${settings.composeLogToFile}"
settings.composeLogToFile.parentFile.mkdirs()
composeLog = new FileOutputStream(settings.composeLogToFile)
}
settings.composeExecutor.executeWithCustomOutputWithExitValue(composeLog, args)
startCapturing()
def servicesToLoad = settings.startedServices ?: settings.composeExecutor.getServiceNames()
servicesInfos = loadServicesInfo(servicesToLoad).collectEntries { [(it.name): (it)] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.avast.gradle.dockercompose
import com.avast.gradle.dockercompose.tasks.ComposeBuild
import com.avast.gradle.dockercompose.tasks.ComposeDown
import com.avast.gradle.dockercompose.tasks.ComposeDownForced
import com.avast.gradle.dockercompose.tasks.ComposeLogs
import com.avast.gradle.dockercompose.tasks.ComposePull
import com.avast.gradle.dockercompose.tasks.ComposePush
import com.avast.gradle.dockercompose.tasks.ComposeUp
Expand All @@ -26,6 +27,7 @@ class DockerComposePluginTest extends Specification {
project.tasks.composePull instanceof ComposePull
project.tasks.composePush instanceof ComposePush
project.tasks.composeBuild instanceof ComposeBuild
project.tasks.composeLogs instanceof ComposeLogs
project.extensions.findByName('dockerCompose') instanceof ComposeExtension
}

Expand All @@ -45,6 +47,7 @@ class DockerComposePluginTest extends Specification {
project.tasks.nestedComposePull instanceof ComposePull
project.tasks.composePush instanceof ComposePush
project.tasks.nestedComposeBuild instanceof ComposeBuild
project.tasks.nestedComposeLogs instanceof ComposeLogs
ComposeUp up = project.tasks.nestedComposeUp
up.settings.useComposeFiles == ['test.yml']
}
Expand Down Expand Up @@ -126,6 +129,7 @@ class DockerComposePluginTest extends Specification {
project.tasks.integrationTestComposePull instanceof ComposePull
project.tasks.integrationTestComposePush instanceof ComposePush
project.tasks.integrationTestComposeBuild instanceof ComposeBuild
project.tasks.integrationTestComposeLogs instanceof ComposeLogs
ComposeUp up = project.tasks.integrationTestComposeUp
up.settings.useComposeFiles == ['test.yml']
task.dependsOn.contains(project.tasks.integrationTestComposeUp)
Expand Down

0 comments on commit c368960

Please sign in to comment.