/
ArtifactManager.java
230 lines (183 loc) · 7.33 KB
/
ArtifactManager.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.hubspot.singularity.s3.base;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;
import java.util.List;
import org.slf4j.Logger;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hashing;
import com.hubspot.deploy.Artifact;
import com.hubspot.deploy.EmbeddedArtifact;
import com.hubspot.deploy.ExternalArtifact;
import com.hubspot.deploy.RemoteArtifact;
import com.hubspot.deploy.S3Artifact;
import com.hubspot.singularity.runner.base.shared.ProcessFailedException;
import com.hubspot.singularity.runner.base.shared.SimpleProcessManager;
import com.hubspot.singularity.s3.base.config.SingularityS3Configuration;
public class ArtifactManager extends SimpleProcessManager {
private final Path cacheDirectory;
private final Logger log;
private final S3ArtifactDownloader s3ArtifactDownloader;
public ArtifactManager(SingularityS3Configuration configuration, Logger log) {
super(log);
this.cacheDirectory = Paths.get(configuration.getArtifactCacheDirectory());
this.log = log;
this.s3ArtifactDownloader = new S3ArtifactDownloader(configuration, log);
}
private long getSize(Path path) {
try {
return Files.size(path);
} catch (IOException ioe) {
throw new RuntimeException(String.format("Couldnt get file size of %s", path), ioe);
}
}
private boolean filesSizeMatches(RemoteArtifact artifact, Path path) {
return !artifact.getFilesize().isPresent() || (artifact.getFilesize().get() == getSize(path));
}
private boolean md5Matches(Artifact artifact, Path path) {
return !artifact.getMd5sum().isPresent() || artifact.getMd5sum().get().equals(calculateMd5sum(path));
}
private void checkFilesize(RemoteArtifact artifact, Path path) {
if (!filesSizeMatches(artifact, path)) {
throw new RuntimeException(String.format("Filesize %s (%s) does not match expected (%s)", getSize(path), path, artifact.getFilesize()));
}
}
private void checkMd5(Artifact artifact, Path path) {
if (!md5Matches(artifact, path)) {
throw new RuntimeException(String.format("Md5sum %s (%s) does not match expected (%s)", calculateMd5sum(path), path, artifact.getMd5sum().get()));
}
}
private Path createTempPath(String filename) {
try {
return Files.createTempFile(cacheDirectory, filename, null);
} catch (IOException e) {
throw new RuntimeException(String.format("Couldn't create temporary file for %s", filename), e);
}
}
private void downloadAndCheck(RemoteArtifact artifact, Path downloadTo) {
if (artifact instanceof ExternalArtifact) {
downloadExternalArtifact((ExternalArtifact) artifact, downloadTo);
} else if (artifact instanceof S3Artifact) {
downloadS3Artifact((S3Artifact) artifact, downloadTo);
} else {
throw new IllegalArgumentException("Unknown artifact type: " + artifact.getClass());
}
checkFilesize(artifact, downloadTo);
checkMd5(artifact, downloadTo);
}
public void extract(EmbeddedArtifact embeddedArtifact, Path directory) {
final Path extractTo = directory.resolve(embeddedArtifact.getFilename());
final Path parent = extractTo.getParent();
try {
if (parent != null) {
Files.createDirectories(parent);
}
} catch (IOException e) {
throw new RuntimeException(String.format("Couldn't extract %s, unable to create directory %s", embeddedArtifact.getName(), parent), e);
}
log.info("Extracting {} bytes of {} to {}", embeddedArtifact.getContent().length, embeddedArtifact.getName(), extractTo);
try (SeekableByteChannel byteChannel = Files.newByteChannel(extractTo, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE))) {
byteChannel.write(ByteBuffer.wrap(embeddedArtifact.getContent()));
} catch (IOException e) {
throw new RuntimeException(String.format("Couldn't extract %s", embeddedArtifact.getName()), e);
}
checkMd5(embeddedArtifact, extractTo);
}
private Path downloadAndCache(RemoteArtifact artifact, String filename) {
Path tempFilePath = createTempPath(filename);
downloadAndCheck(artifact, tempFilePath);
Path cachedPath = getCachedPath(filename);
try {
Files.move(tempFilePath, cachedPath, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
throw new RuntimeException(String.format("Couldn't move %s to %s", tempFilePath, cachedPath), e);
}
return cachedPath;
}
private Path getCachedPath(String filename) {
return cacheDirectory.resolve(filename);
}
private boolean checkCached(RemoteArtifact artifact, Path cachedPath) {
if (!Files.exists(cachedPath)) {
log.debug("Cached {} did not exist", cachedPath);
return false;
}
if (!filesSizeMatches(artifact, cachedPath)) {
log.debug("Cached {} ({}) did not match file size {}", cachedPath, getSize(cachedPath), artifact.getFilesize());
return false;
}
if (!md5Matches(artifact, cachedPath)) {
log.debug("Cached {} ({}) did not match md5 {}", cachedPath, calculateMd5sum(cachedPath), artifact.getMd5sum().get());
return false;
}
return true;
}
public Path fetch(RemoteArtifact artifact) {
String filename = artifact.getFilename();
Path cachedPath = getCachedPath(filename);
if (!checkCached(artifact, cachedPath)) {
downloadAndCache(artifact, filename);
} else {
log.info("Using cached file {}", cachedPath);
}
return cachedPath;
}
private void downloadExternalArtifact(ExternalArtifact externalArtifact, Path downloadTo) {
downloadUri(externalArtifact.getUrl(), downloadTo);
}
private void downloadS3Artifact(S3Artifact s3Artifact, Path downloadTo) {
s3ArtifactDownloader.download(s3Artifact, downloadTo);
}
private void downloadUri(String uri, Path path) {
log.info("Downloading {} to {}", uri, path);
final List<String> command = ImmutableList.of(
"wget",
uri,
"-O",
path.toString(),
"-nv",
"--no-check-certificate");
runCommandAndThrowRuntimeException(command);
}
public void copy(Path source, Path destination) {
log.info("Copying {} to {}", source, destination);
try {
Files.copy(source, destination.resolve(source.getFileName()));
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
public void untar(Path source, Path destination) {
log.info("Untarring {} to {}", source, destination);
final List<String> command = ImmutableList.of(
"tar",
"-oxzf",
source.toString(),
"-C",
destination.toString());
runCommandAndThrowRuntimeException(command);
}
private void runCommandAndThrowRuntimeException(List<String> command) {
try {
super.runCommand(command);
} catch (InterruptedException | ProcessFailedException e) {
throw Throwables.propagate(e);
}
}
private String calculateMd5sum(Path path) {
try {
HashCode hc = com.google.common.io.Files.hash(path.toFile(), Hashing.md5());
return hc.toString();
} catch (IOException e) {
throw Throwables.propagate(e);
}
}
}