Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit acd9754

Browse files
committed
Change how a tarball is generated when sending a build request. Instead of creating a temporary folder and copying all the files in a flat structure, read the files directly and preserve their path details.
Fixes a problem with adding files in a subfolder like: Step 2 : ADD files/authorized_keys /var/lib/jenkins/.ssh/ [Docker] ERROR: {"errorDetail":{"message":"files/authorized_keys: no such file or directory"},"error":"files/authorized_keys: no such file or directory"}
1 parent a4103ca commit acd9754

File tree

5 files changed

+45
-51
lines changed

5 files changed

+45
-51
lines changed

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

Lines changed: 7 additions & 13 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

@@ -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: 12 additions & 3 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()));
@@ -663,7 +665,14 @@ public void testDockerBuilderAddFile() throws DockerException, IOException {
663665
dockerfileBuild(baseDir, "Successfully executed testrun.sh");
664666
}
665667

666-
@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
667676
public void testDockerBuilderAddFolder() throws DockerException,
668677
IOException {
669678
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)