Skip to content
This repository has been archived by the owner on Oct 30, 2020. It is now read-only.

Commit

Permalink
add await feature (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimbjorklund authored and David Kane committed Dec 9, 2018
1 parent bc8eb40 commit fc8f168
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.dkanejs.maven.plugins</groupId>
<artifactId>docker-compose-maven-plugin</artifactId>
<version>2.2.0</version>
<version>2.3.0-SNAPSHOT</version>

<packaging>maven-plugin</packaging>

Expand Down Expand Up @@ -107,7 +107,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.0.0</version>
<version>3.1.0</version>
<configuration>
<debug>false</debug>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
Expand All @@ -127,6 +127,7 @@
<pomInclude>up-down-host/pom.xml</pomInclude>
<pomInclude>up-down-multiple-compose-files/pom.xml</pomInclude>
<pomInclude>up-down-multiple-compose-files-ignoring-single/pom.xml</pomInclude>
<pomInclude>up-await/pom.xml</pomInclude>
</pomIncludes>
<postBuildHookScript>verify</postBuildHookScript>
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
Expand Down
6 changes: 6 additions & 0 deletions src/it/up-await/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3.2'
services:
test:
image: busybox
container_name: mpdc-it-up
command: echo -e Docker Compose has run successfully
58 changes: 58 additions & 0 deletions src/it/up-await/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.dkanejs.maven.plugins.it</groupId>
<artifactId>await</artifactId>
<version>1.0</version>

<description>Verify "docker-compose with awaitCmd</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>process-test-classes</id>
<phase>process-test-classes</phase>
<configuration>
<target>
<chmod file="target/test-classes/await.sh" perm="755"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>up</id>
<phase>verify</phase>
<goals>
<goal>up</goal>
</goals>
<configuration>
<composeFile>${project.basedir}/docker-compose.yml</composeFile>
<detachedMode>false</detachedMode>
<awaitCmd>${project.build.testOutputDirectory}/await.sh</awaitCmd>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3 changes: 3 additions & 0 deletions src/it/up-await/src/test/resources/await.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
echo "Waiting..."
exit 0
13 changes: 13 additions & 0 deletions src/it/up-await/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.nio.file.Paths

String buildLog = new File("${basedir}/build.log").getText("UTF-8")

String composeFile = Paths.get("${basedir}/docker-compose.yml").toString()

assert buildLog.contains("Running: docker-compose -f $composeFile up --no-color" as CharSequence)
assert buildLog.contains("Docker Compose has run successfully")
assert buildLog.contains("Waiting...")

def cleanUpProcess = new ProcessBuilder("docker", "system", "prune", "-a", "-f").start().waitFor()

assert cleanUpProcess == 0
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ abstract class AbstractDockerComposeMojo extends AbstractMojo {
@Parameter(property = "dockerCompose.envFile")
private String envFile;

/**
* Cmd to run and wait for exit status 0
*/
@Parameter(property = "dockerCompose.awaitCmd")
String awaitCmd;

/**
* Arguments to awaitCmd (comma separated)
*/
@Parameter(property = "dockerCompose.awaitCmdArgs")
String awaitCmdArgs;

/**
* Timeout for await (seconds)
*/
@Parameter(property = "dockerCompose.awaitTimeout", defaultValue = "30")
int awaitTimeout;

void execute(List<String> args) throws MojoExecutionException {

ProcessBuilder pb = buildProcess(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.maven.plugins.annotations.Mojo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -36,5 +37,45 @@ public void execute() throws MojoExecutionException {
args.add("--no-color");

super.execute(args);

if (awaitCmd != null) {
await(buildCmd(awaitCmd, awaitCmdArgs));
}
}

private List<String> buildCmd(String cmd, String args) {
String[] cmdArgs = null;
if (args != null) {
cmdArgs = args.split(",");
}
List<String> answer = new ArrayList<>();
answer.add(cmd);
if (cmdArgs != null) {
answer.addAll(Arrays.asList(cmdArgs));
}
return answer;
}

private void await(List<String> awaitCmd) {
Integer exitCode = null;
ProcessBuilder pb = new ProcessBuilder(awaitCmd).inheritIO();
long start = System.currentTimeMillis();
try {
boolean stillWaiting;
do {
Process process = null;
if (exitCode != null) {
Thread.sleep(1000);
}
exitCode = pb.start().waitFor();
stillWaiting = (System.currentTimeMillis() - start) < (awaitTimeout * 1000);
} while ((exitCode != 0) && stillWaiting);

if (exitCode != 0) {
throw new RuntimeException("await failed after " + awaitTimeout + " seconds");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit fc8f168

Please sign in to comment.