Skip to content

Commit

Permalink
minor tweaks of logging
Browse files Browse the repository at this point in the history
  • Loading branch information
augi committed Jul 30, 2018
1 parent c368960 commit 330ba48
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
64 changes: 34 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Simplifies usage of [Docker Compose](https://www.docker.com/docker-compose) for

`composeBuild` task builds the services of the application.

`composePush` task pushes images for services to their respective `registry/repository`
`composePush` task pushes images for services to their respective `registry/repository`.

`composeLogs` task stores logs from all containers to files in `containerLogToDir` directory.

## Quick start
```gradle
Expand Down Expand Up @@ -71,36 +73,38 @@ apply plugin: 'docker-compose'
dockerCompose.isRequiredBy(test) // hooks 'dependsOn composeUp' and 'finalizedBy composeDown', and exposes environment variables and system properties (if possible)
dockerCompose {
// useComposeFiles = ['docker-compose.yml', 'docker-compose.prod.yml'] // like 'docker-compose -f <file>'; default is empty
// startedServices = ['web'] // list of services to execute when calling 'docker-compose up' or 'docker-compose pull' (when not specified, all services are executed)
// scale = [${serviceName1}: 5, ${serviceName2}: 2] // Pass docker compose --scale option like 'docker-compose up --scale serviceName1=5 --scale serviceName2=2'
// forceRecreate = false // pass '--force-recreate' when calling 'docker-compose up' when set to 'true`
// buildBeforeUp = true // performs 'docker-compose build' before calling the 'up' command; default is true
// buildBeforePull = true // performs 'docker-compose build' before calling the 'pull' command; default is true
// ignorePullFailure = false // when set to true, pass '--ignore-pull-failure' to 'docker-compose pull'
// ignorePushFailure = false // when set to true, pass '--ignore-push-failure' to 'docker-compose push'
// pushServices = [] // which services should be pushed, if not defined then upon `composePush` task all defined services in compose file will be pushed (default behaviour)
// buildAdditionalArgs = ['--force-rm']
// pullAdditionalArgs = ['--ignore-pull-failures']
// upAdditionalArgs = ['--no-deps']
// downAdditionalArgs = ['--some-switch']
// waitForTcpPorts = true // turns off the waiting for exposed TCP ports opening
// captureContainersOutput = false // if true, prints output of all containers to Gradle output - very useful for debugging; default is false
// captureContainersOutputToFile = '/path/to/logFile' // sends output of all containers to a log file
// stopContainers = true // doesn't call `docker-compose down` - see below the paragraph about reconnecting
// removeContainers = true
// removeImages = "None" // Other accepted values are: "All" and "Local"
// removeVolumes = true
// removeOrphans = false // removes containers for services not defined in the Compose file
useComposeFiles = ['docker-compose.yml', 'docker-compose.prod.yml'] // like 'docker-compose -f <file>'; default is empty
startedServices = ['web'] // list of services to execute when calling 'docker-compose up' or 'docker-compose pull' (when not specified, all services are executed)
scale = [${serviceName1}: 5, ${serviceName2}: 2] // Pass docker compose --scale option like 'docker-compose up --scale serviceName1=5 --scale serviceName2=2'
forceRecreate = false // pass '--force-recreate' when calling 'docker-compose up' when set to 'true`
buildBeforeUp = true // performs 'docker-compose build' before calling the 'up' command; default is true
buildBeforePull = true // performs 'docker-compose build' before calling the 'pull' command; default is true
ignorePullFailure = false // when set to true, pass '--ignore-pull-failure' to 'docker-compose pull'
ignorePushFailure = false // when set to true, pass '--ignore-push-failure' to 'docker-compose push'
pushServices = [] // which services should be pushed, if not defined then upon `composePush` task all defined services in compose file will be pushed (default behaviour)
buildAdditionalArgs = ['--force-rm']
pullAdditionalArgs = ['--ignore-pull-failures']
upAdditionalArgs = ['--no-deps']
downAdditionalArgs = ['--some-switch']
waitForTcpPorts = true // turns off the waiting for exposed TCP ports opening
captureContainersOutput = false // if true, prints output of all containers to Gradle output - very useful for debugging; default is false
captureContainersOutputToFile = '/path/to/logFile' // sends output of all containers to a log file
composeLogToFile = project.file('build/my-logs.txt') // redirect output of composeUp and composeDown tasks to this file; default is null (ouput is not redirected)
containerLogToDir = project.file('build/logs') // directory where composeLogs task stores output of the containers; default: build/containers-logs
stopContainers = true // doesn't call `docker-compose down` - see below the paragraph about reconnecting
removeContainers = true
removeImages = "None" // Other accepted values are: "All" and "Local"
removeVolumes = true
removeOrphans = false // removes containers for services not defined in the Compose file
// projectName = 'my-project' // allow to set custom docker-compose project name (defaults to a stable name derived from absolute path of the project), set to null to Docker Compose default (directory name)
// executable = '/path/to/docker-compose' // allow to set the path of the docker-compose executable (useful if not present in PATH)
// dockerExecutable = '/path/to/docker' // allow to set the path of the docker executable (useful if not present in PATH)
// dockerComposeWorkingDirectory = '/path/where/docker-compose/is/invoked/from'
// dockerComposeStopTimeout = java.time.Duration.ofSeconds(20) // time before docker-compose sends SIGTERM to the running containers after the composeDown task has been started
// environment.put 'BACKEND_ADDRESS', '192.168.1.100' // Pass environment variable to 'docker-compose' for substitution in compose file
projectName = 'my-project' // allow to set custom docker-compose project name (defaults to a stable name derived from absolute path of the project), set to null to Docker Compose default (directory name)
executable = '/path/to/docker-compose' // allow to set the path of the docker-compose executable (useful if not present in PATH)
dockerExecutable = '/path/to/docker' // allow to set the path of the docker executable (useful if not present in PATH)
dockerComposeWorkingDirectory = '/path/where/docker-compose/is/invoked/from'
dockerComposeStopTimeout = java.time.Duration.ofSeconds(20) // time before docker-compose sends SIGTERM to the running containers after the composeDown task has been started
environment.put 'BACKEND_ADDRESS', '192.168.1.100' // Pass environment variable to 'docker-compose' for substitution in compose file
}
test.doFirst {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ class ComposeSettings {
boolean buildBeforeUp = true
boolean buildBeforePull = true
boolean waitForTcpPorts = true
boolean captureContainersOutput = false
File captureContainersOutputToFile = null
Duration waitAfterTcpProbeFailure = Duration.ofSeconds(1)
Duration waitForTcpPortsTimeout = Duration.ofMinutes(15)
Duration waitForTcpPortsDisconnectionProbeTimeout = Duration.ofMillis(1000)
Duration waitAfterHealthyStateProbeFailure = Duration.ofSeconds(5)
Duration waitForHealthyStateTimeout = Duration.ofMinutes(15)
List<String> useComposeFiles = []

boolean captureContainersOutput = false
File captureContainersOutputToFile = null
File composeLogToFile = null
File containerLogToDir

List<String> startedServices = []
Map<String, Integer> scale = [:]
boolean removeOrphans = false
Expand Down Expand Up @@ -71,9 +74,6 @@ 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 @@ -99,6 +99,8 @@ class ComposeSettings {
def fullPathMd5 = MessageDigest.getInstance("MD5").digest(project.projectDir.absolutePath.toString().getBytes(StandardCharsets.UTF_8)).encodeHex().toString()
this.projectName = fullPathMd5 + '_' + project.name + '_' + name

this.containerLogToDir = project.buildDir.toPath().resolve('containers-logs').toFile()

if (OperatingSystem.current().isMacOsX()) {
// Default installation is inaccessible from path, so set sensible
// defaults for this platform.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ class ComposeLogs extends DefaultTask {

ComposeLogs() {
group = 'docker'
description = 'Records log output from services in containers of docker-compose project'
description = 'Stores 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")
settings.containerLogToDir.mkdirs()
def logStream = new FileOutputStream("${settings.containerLogToDir.absolutePath}/${service}.log")
String[] args = ['logs', '-t', service]
settings.composeExecutor.executeWithCustomOutputWithExitValue(logStream, args)
}
Expand Down

0 comments on commit 330ba48

Please sign in to comment.