forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyArchiveToContainerCmdImpl.java
More file actions
147 lines (120 loc) · 4.81 KB
/
Copy pathCopyArchiveToContainerCmdImpl.java
File metadata and controls
147 lines (120 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.github.dockerjava.core.command;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
import com.github.dockerjava.api.exception.DockerClientException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.core.util.CompressArchiveUtil;
public class CopyArchiveToContainerCmdImpl extends AbstrDockerCmd<CopyArchiveToContainerCmd, Void> implements
CopyArchiveToContainerCmd {
private String containerId;
private String remotePath = ".";
private InputStream tarInputStream;
private String hostResource;
private boolean noOverwriteDirNonDir = false;
private boolean dirChildrenOnly = false;
public CopyArchiveToContainerCmdImpl(CopyArchiveToContainerCmd.Exec exec, String containerId) {
super(exec);
withContainerId(containerId);
}
@Override
public CopyArchiveToContainerCmd withContainerId(String containerId) {
checkNotNull(containerId, "containerId was not specified");
this.containerId = containerId;
return this;
}
@Override
public CopyArchiveToContainerCmd withHostResource(String hostResource) {
checkNotNull(hostResource, "hostResource was not specified");
this.hostResource = hostResource;
return this;
}
@Override
public CopyArchiveToContainerCmd withNoOverwriteDirNonDir(boolean noOverwriteDirNonDir) {
this.noOverwriteDirNonDir = noOverwriteDirNonDir;
return this;
}
@Override
public CopyArchiveToContainerCmd withRemotePath(String remotePath) {
checkNotNull(remotePath, "remotePath was not specified");
this.remotePath = remotePath;
return this;
}
@Override
public CopyArchiveToContainerCmd withTarInputStream(InputStream tarInputStream) {
checkNotNull(tarInputStream, "tarInputStream was not specified");
this.tarInputStream = tarInputStream;
return this;
}
@Override
public CopyArchiveToContainerCmd withDirChildrenOnly(boolean dirChildrenOnly) {
this.dirChildrenOnly = dirChildrenOnly;
return this;
}
@Override
public InputStream getTarInputStream() {
return tarInputStream;
}
@Override
public String getContainerId() {
return this.containerId;
}
@Override
public String getHostResource() {
return this.hostResource;
}
@Override
public boolean isNoOverwriteDirNonDir() {
return this.noOverwriteDirNonDir;
}
@Override
public String getRemotePath() {
return this.remotePath;
}
@Override
public boolean isDirChildrenOnly() {
return this.dirChildrenOnly;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("cp ").append(hostResource).append(" ").append(containerId).append(":")
.append(remotePath).toString();
}
private InputStream buildUploadStream(String hostResource, boolean dirChildrenOnly) throws IOException {
Path toUpload = Files.createTempFile("docker-java", ".tar.gz");
CompressArchiveUtil.tar(Paths.get(hostResource), toUpload, true, dirChildrenOnly);
return Files.newInputStream(toUpload);
}
/**
* @throws com.github.dockerjava.api.exception.NotFoundException
* No such container
*/
@Override
public Void exec() throws NotFoundException {
if (StringUtils.isNotEmpty(this.hostResource)) {
// User set host resource and not directly a stream
if (this.tarInputStream != null) {
throw new DockerClientException(
"Only one of host resource or tar input stream should be defined to perform the copy, not both");
}
// We compress the given path, call exec so that the stream is consumed and then close it our self
try (InputStream uploadStream = buildUploadStream(this.hostResource, this.dirChildrenOnly)) {
this.tarInputStream = uploadStream;
return super.exec();
} catch (IOException e) {
throw new DockerClientException("Unable to perform tar on host resource " + this.hostResource, e);
}
} else if (this.tarInputStream == null) {
throw new DockerClientException(
"One of host resource or tar input stream must be defined to perform the copy");
}
// User set a stream, so we will just consume it and let the user close it by him self
return super.exec();
}
}