Skip to content

Commit 8ef2ad8

Browse files
committed
Merge pull request #3 from hackmann/build-add-file-in-subfolder
Build add file in subfolder
2 parents e418e2e + acd9754 commit 8ef2ad8

File tree

5 files changed

+54
-58
lines changed

5 files changed

+54
-58
lines changed

src/main/java/com/kpelykh/docker/client/DockerClient.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.io.IOException;
3838
import java.io.InputStream;
3939
import java.io.StringWriter;
40+
import java.util.ArrayList;
4041
import java.util.List;
4142
import java.util.UUID;
4243

@@ -187,7 +188,7 @@ public ClientResponse pull(String repository, String tag, String registry) throw
187188
Preconditions.checkNotNull(repository, "Repository was not specified");
188189

189190
if (StringUtils.countMatches(repository, ":") == 1) {
190-
String repositoryTag[] = StringUtils.split(repository);
191+
String repositoryTag[] = StringUtils.split(repository, ':');
191192
repository = repositoryTag[0];
192193
tag = repositoryTag[1];
193194

@@ -805,7 +806,6 @@ public ClientResponse build(File dockerFolder, String tag, boolean noCache) thro
805806
String archiveNameWithOutExtension = UUID.randomUUID().toString();
806807

807808
File dockerFolderTar = null;
808-
File tmpDockerContextFolder = null;
809809

810810
try {
811811
File dockerFile = new File(dockerFolder, "Dockerfile");
@@ -815,10 +815,8 @@ public ClientResponse build(File dockerFolder, String tag, boolean noCache) thro
815815
throw new DockerException(String.format("Dockerfile %s is empty", dockerFile));
816816
}
817817

818-
//Create tmp docker context folder
819-
tmpDockerContextFolder = new File(FileUtils.getTempDirectoryPath(), "docker-java-build" + archiveNameWithOutExtension);
820-
821-
FileUtils.copyFileToDirectory(dockerFile, tmpDockerContextFolder);
818+
List<File> filesToAdd = new ArrayList<File>();
819+
filesToAdd.add(dockerFile);
822820

823821
for (String cmd : dockerFileContent) {
824822
if (StringUtils.startsWithIgnoreCase(cmd.trim(), "ADD")) {
@@ -833,21 +831,20 @@ public ClientResponse build(File dockerFolder, String tag, boolean noCache) thro
833831
}
834832

835833
if (!src.exists()) {
836-
throw new DockerException(String.format("Source file %s doesnt' exist", src));
834+
throw new DockerException(String.format("Source file %s doesn't exist", src));
837835
}
838836
if (src.isDirectory()) {
839-
FileUtils.copyDirectoryToDirectory(src, tmpDockerContextFolder);
837+
filesToAdd.addAll(FileUtils.listFiles(src, null, true));
840838
} else {
841-
FileUtils.copyFileToDirectory(src, tmpDockerContextFolder);
839+
filesToAdd.add(src);
842840
}
843841
}
844842
}
845843

846-
dockerFolderTar = CompressArchiveUtil.archiveTARFiles(tmpDockerContextFolder, archiveNameWithOutExtension);
844+
dockerFolderTar = CompressArchiveUtil.archiveTARFiles(dockerFolder, filesToAdd, archiveNameWithOutExtension);
847845

848846
} catch (IOException ex) {
849847
FileUtils.deleteQuietly(dockerFolderTar);
850-
FileUtils.deleteQuietly(tmpDockerContextFolder);
851848
throw new DockerException("Error occurred while preparing Docker context folder.", ex);
852849
}
853850

@@ -869,9 +866,6 @@ public ClientResponse build(File dockerFolder, String tag, boolean noCache) thro
869866
throw new DockerException(e);
870867
} finally {
871868
FileUtils.deleteQuietly(dockerFolderTar);
872-
FileUtils.deleteQuietly(tmpDockerContextFolder);
873869
}
874-
875870
}
876-
877871
}

src/main/java/com/kpelykh/docker/client/utils/CompressArchiveUtil.java

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,36 @@
33
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
44
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
55
import org.apache.commons.io.FileUtils;
6-
import org.apache.commons.io.filefilter.RegexFileFilter;
7-
import org.apache.commons.lang.StringUtils;
86

97
import java.io.*;
10-
import java.util.Collection;
11-
12-
import static org.apache.commons.io.filefilter.FileFilterUtils.*;
138

149
public class CompressArchiveUtil {
1510

16-
public static File archiveTARFiles(File baseDir, String archiveNameWithOutExtension) throws IOException {
17-
18-
File tarFile = null;
19-
20-
tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar");
21-
22-
Collection<File> files =
23-
FileUtils.listFiles(
24-
baseDir,
25-
new RegexFileFilter("^(.*?)"),
26-
and(directoryFileFilter(), notFileFilter(nameFileFilter(baseDir.getName()))));
27-
28-
byte[] buf = new byte[1024];
29-
int len;
30-
31-
{
32-
TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
11+
public static File archiveTARFiles(File base, Iterable<File> files, String archiveNameWithOutExtension) throws IOException {
12+
File tarFile = new File(FileUtils.getTempDirectoryPath(), archiveNameWithOutExtension + ".tar");
13+
TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(tarFile));
14+
try {
3315
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
3416
for (File file : files) {
3517
TarArchiveEntry tarEntry = new TarArchiveEntry(file);
36-
tarEntry.setName(StringUtils.substringAfter(file.toString(), baseDir.getPath()));
18+
tarEntry.setName(relativize(base, file));
3719

3820
tos.putArchiveEntry(tarEntry);
3921

4022
if (!file.isDirectory()) {
41-
FileInputStream fin = new FileInputStream(file);
42-
BufferedInputStream in = new BufferedInputStream(fin);
43-
44-
while ((len = in.read(buf)) != -1) {
45-
tos.write(buf, 0, len);
46-
}
47-
48-
in.close();
23+
FileUtils.copyFile(file, tos);
4924
}
5025
tos.closeArchiveEntry();
51-
5226
}
27+
} finally {
5328
tos.close();
5429
}
5530

56-
57-
return tarFile;
58-
}
31+
return tarFile;
32+
}
33+
34+
private static String relativize(File base, File absolute) {
35+
String relative = base.toURI().relativize(absolute.toURI()).getPath();
36+
return relative;
37+
}
5938
}

src/test/java/com/kpelykh/docker/client/test/DockerClientTest.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public class DockerClientTest extends Assert {
4040

4141
private DockerClient dockerClient;
4242

43-
private List<String> tmpImgs = new ArrayList<String>();
44-
private List<String> tmpContainers = new ArrayList<String>();
43+
private List<String> tmpImgs;
44+
private List<String> tmpContainers;
4545

4646
@BeforeTest
4747
public void beforeTest() throws DockerException {
@@ -64,6 +64,8 @@ public void afterTest() {
6464

6565
@BeforeMethod
6666
public void beforeMethod(Method method) {
67+
tmpContainers = new ArrayList<String>();
68+
tmpImgs = new ArrayList<String>();
6769
LOG.info(String
6870
.format("################################## STARTING %s ##################################",
6971
method.getName()));
@@ -474,7 +476,11 @@ public void removeContainer() throws DockerException {
474476
@Test
475477
public void testPullImage() throws DockerException, IOException {
476478

477-
String testImage = "kpelykh/vimbase";
479+
// This should be an image that is not used by other repositories already
480+
// pulled down, preferably small in size. If tag is not used pull will
481+
// download all images in that repository but tmpImgs will only
482+
// deleted 'latest' image but not images with other tags
483+
String testImage = "hackmann/empty";
478484

479485
LOG.info("Removing image: {}", testImage);
480486
dockerClient.removeImage(testImage);
@@ -486,6 +492,7 @@ public void testPullImage() throws DockerException, IOException {
486492

487493
LOG.info("Pulling image: {}", testImage);
488494

495+
tmpImgs.add(testImage);
489496
ClientResponse response = dockerClient.pull(testImage);
490497

491498
StringWriter logwriter = new StringWriter();
@@ -503,15 +510,12 @@ public void testPullImage() throws DockerException, IOException {
503510
}
504511

505512
String fullLog = logwriter.toString();
506-
assertThat(fullLog,
507-
containsString("Pulling repository kpelykh/vimbase"));
508-
509-
tmpImgs.add(testImage);
513+
assertThat(fullLog, containsString("Download complete"));
510514

511515
info = dockerClient.info();
512516
LOG.info("Client info after pull, {}", info.toString());
513517

514-
assertThat(imgCount + 1, equalTo(info.getImages()));
518+
assertThat(imgCount, lessThan(info.getImages()));
515519

516520
ImageInspectResponse imageInspectResponse = dockerClient
517521
.inspectImage(testImage);
@@ -661,7 +665,14 @@ public void testDockerBuilderAddFile() throws DockerException, IOException {
661665
dockerfileBuild(baseDir, "Successfully executed testrun.sh");
662666
}
663667

664-
@Test
668+
@Test
669+
public void testDockerBuilderAddFileInSubfolder() throws DockerException, IOException {
670+
File baseDir = new File(Thread.currentThread().getContextClassLoader()
671+
.getResource("testAddFileInSubfolder").getFile());
672+
dockerfileBuild(baseDir, "Successfully executed testrun.sh");
673+
}
674+
675+
@Test
665676
public void testDockerBuilderAddFolder() throws DockerException,
666677
IOException {
667678
File baseDir = new File(Thread.currentThread().getContextClassLoader()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM ubuntu
2+
3+
# Copy testrun.sh files into the container
4+
5+
ADD ./files/testrun.sh /tmp/
6+
7+
RUN cp /tmp/testrun.sh /usr/local/bin/ && chmod +x /usr/local/bin/testrun.sh
8+
9+
CMD ["testrun.sh"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
echo "Successfully executed testrun.sh"

0 commit comments

Comments
 (0)