Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/main/java/com/github/dockerjava/client/DockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,28 @@ public class DockerClient {
private AuthConfig authConfig;

public DockerClient() throws DockerException {
this(Config.createConfig());
this(10000, true);
}
public DockerClient(Integer readTimeout, boolean enableLoggingFilter) throws DockerException {
this(Config.createConfig(), readTimeout, enableLoggingFilter);
}

public DockerClient(String serverUrl) throws DockerException {
this(configWithServerUrl(serverUrl));
this(serverUrl, 10000, true);
}

public DockerClient(String serverUrl, Integer readTimeout, boolean enableLoggingFilter) throws DockerException {
this(configWithServerUrl(serverUrl), readTimeout, enableLoggingFilter);
}

private static Config configWithServerUrl(String serverUrl)
throws DockerException {
final Config c = Config.createConfig();
c.url = URI.create(serverUrl);
return c;
}

private DockerClient(Config config) {
public DockerClient(Config config, Integer readTimeout, boolean enableLoggingFilter) {
ClientConfig clientConfig = new DefaultClientConfig();

SchemeRegistry schemeRegistry = new SchemeRegistry();
Expand All @@ -101,14 +108,19 @@ private DockerClient(Config config) {
client = new ApacheHttpClient4(new ApacheHttpClient4Handler(httpClient,
null, false), clientConfig);

client.setReadTimeout(10000);
// 1 hour
client.setReadTimeout(readTimeout);

client.addFilter(new JsonClientFilter());
client.addFilter(new SelectiveLoggingFilter());

if (enableLoggingFilter)
client.addFilter(new SelectiveLoggingFilter());

baseResource = client.resource(config.url + "/v" + config.version);
}



public void setCredentials(String username, String password, String email) {
if (username == null) {
throw new IllegalArgumentException("username is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -35,7 +37,9 @@ public class BuildImgCmd extends AbstrDockerCmd<BuildImgCmd, ClientResponse> {

private static final Logger LOGGER = LoggerFactory.getLogger(BuildImgCmd.class);

private static final Pattern ADD_PATTERN = Pattern.compile("^ADD\\s+(.*)\\s+(.*)$");
private static final Pattern ADD_OR_COPY_PATTERN = Pattern.compile("^(ADD|COPY)\\s+(.*)\\s+(.*)$");

private static final Pattern ENV_PATTERN = Pattern.compile("^ENV\\s+(.*)\\s+(.*)$");

private File dockerFolder = null;
private InputStream tarInputStream = null;
Expand Down Expand Up @@ -133,14 +137,40 @@ protected File buildDockerFolderTar() {
List<File> filesToAdd = new ArrayList<File>();
filesToAdd.add(dockerFile);

Map<String, String>environmentMap = new HashMap<String, String>();

int lineNumber = 0;

for (String cmd : dockerFileContent) {
final Matcher matcher = ADD_PATTERN.matcher(cmd.trim());

lineNumber++;

if (cmd.trim().isEmpty() || cmd.startsWith("#"))
continue; // skip emtpy and commend lines

final Matcher envMatcher = ENV_PATTERN.matcher(cmd.trim());

if (envMatcher.find()) {
if (envMatcher.groupCount() != 2)
throw new DockerException(String.format("Wrong ENV format on line [%d]", lineNumber));

String variable = envMatcher.group(1).trim();

String value = envMatcher.group(2).trim();

environmentMap.put(variable, value);
}


final Matcher matcher = ADD_OR_COPY_PATTERN.matcher(cmd.trim());
if (matcher.find()) {
if (matcher.groupCount() != 2) {
throw new DockerException(String.format("Wrong format on line [%s]", cmd));
if (matcher.groupCount() != 3) {
throw new DockerException(String.format("Wrong ADD or COPY format on line [%d]", lineNumber));
}

String resource = matcher.group(1).trim();
String extractedResource = matcher.group(2);

String resource = filterForEnvironmentVars(extractedResource, environmentMap);

if(isFileResource(resource)) {
File src = new File(resource);
Expand Down Expand Up @@ -170,6 +200,33 @@ protected File buildDockerFolderTar() {
}
}

private String filterForEnvironmentVars(String extractedResource,
Map<String, String> environmentMap) {

if (environmentMap.size() > 0) {

String currentResourceContent = extractedResource;

for (Map.Entry<String, String> entry : environmentMap.entrySet()) {

String variable = entry.getKey();

String replacementValue = entry.getValue();

// handle: $VARIABLE case
currentResourceContent = currentResourceContent.replaceAll("\\$" + variable, replacementValue);

// handle ${VARIABLE} case
currentResourceContent = currentResourceContent.replaceAll("\\$\\{" + variable + "\\}", replacementValue);

}

return currentResourceContent;
}
else
return extractedResource;
}

private static boolean isFileResource(String resource) {
URI uri;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
Expand Down Expand Up @@ -113,6 +115,7 @@ public void testDockerBuilderAddFolder() throws DockerException,
dockerfileBuild(baseDir, "Successfully executed testAddFolder.sh");
}


private String dockerfileBuild(File baseDir, String expectedText)
throws DockerException, IOException {

Expand Down Expand Up @@ -222,4 +225,12 @@ public void testNetCatDockerfileBuilder() throws DockerException,
dockerClient.stopContainerCmd(container.getId()).withTimeout(0).exec();

}

@Test
public void testAddAndCopySubstitution () throws DockerException, IOException {
File baseDir = new File(Thread.currentThread().getContextClassLoader()
.getResource("testENVSubstitution").getFile());
dockerfileBuild(baseDir, "Successfully executed testAddFolder.sh");

}
}
11 changes: 11 additions & 0 deletions src/test/resources/testENVSubstitution/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ubuntu

# Copy testrun.sh files into the container

ENV variable abc123
ADD ./testrun.sh /tmp/
ADD ./subst-file-$variable.txt /tmp/
COPY ./subst-file-2-${variable}.txt /tmp/
RUN cp /tmp/testrun.sh /usr/local/bin/ && chmod +x /usr/local/bin/testrun.sh

CMD ["testrun.sh"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Old File
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Old File
26 changes: 26 additions & 0 deletions src/test/resources/testENVSubstitution/testrun.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

echo "Hello Word" > hello.txt
echo "hello.txt Created"

TEST_FILE=/tmp/subst-file-abc123.txt
TEST_FILE_2=/tmp/subst-file-2-abc123.txt

if test -f $TEST_FILE
then

if test -f $TEST_FILE_2
then
exit 0
else
echo "$TEST_FILE_2 does not exist"
exit 1
fi

else

echo "$TEST_FILE does not exist"
exit 1

fi