Skip to content

Commit def871f

Browse files
committed
Properly handle snapshotting installed plugins and modules
1 parent cb82aa9 commit def871f

File tree

1 file changed

+65
-33
lines changed

1 file changed

+65
-33
lines changed

buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/ElasticsearchNode.java

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.gradle.PropertyNormalization;
2828
import org.elasticsearch.gradle.Version;
2929
import org.elasticsearch.gradle.http.WaitForHttpResource;
30+
import org.gradle.api.Action;
3031
import org.gradle.api.Named;
3132
import org.gradle.api.Project;
3233
import org.gradle.api.file.FileCollection;
@@ -39,6 +40,7 @@
3940
import org.gradle.api.tasks.Nested;
4041
import org.gradle.api.tasks.PathSensitive;
4142
import org.gradle.api.tasks.PathSensitivity;
43+
import org.gradle.api.tasks.util.PatternFilterable;
4244

4345
import java.io.ByteArrayInputStream;
4446
import java.io.File;
@@ -326,7 +328,7 @@ public synchronized void start() {
326328
}
327329
createConfiguration();
328330

329-
if(plugins.isEmpty() == false) {
331+
if (plugins.isEmpty() == false) {
330332
logToProcessStdout("Installing " + plugins.size() + " plugins");
331333
plugins.forEach(plugin -> runElaticsearchBinScript(
332334
"elasticsearch-plugin",
@@ -363,9 +365,9 @@ public synchronized void start() {
363365
}
364366
credentials.forEach(paramMap -> runElaticsearchBinScript(
365367
"elasticsearch-users",
366-
paramMap.entrySet().stream()
367-
.flatMap(entry -> Stream.of(entry.getKey(), entry.getValue()))
368-
.toArray(String[]::new)
368+
paramMap.entrySet().stream()
369+
.flatMap(entry -> Stream.of(entry.getKey(), entry.getValue()))
370+
.toArray(String[]::new)
369371
));
370372
}
371373

@@ -411,19 +413,19 @@ private void copyExtraConfigFiles() {
411413
logToProcessStdout("Setting up " + extraConfigFiles.size() + " additional config files");
412414
}
413415
extraConfigFiles.forEach((destination, from) -> {
414-
if (Files.exists(from.toPath()) == false) {
415-
throw new TestClustersException("Can't create extra config file from " + from + " for " + this +
416-
" as it does not exist");
417-
}
418-
Path dst = configFile.getParent().resolve(destination);
419-
try {
420-
Files.createDirectories(dst.getParent());
421-
Files.copy(from.toPath(), dst, StandardCopyOption.REPLACE_EXISTING);
422-
LOGGER.info("Added extra config file {} for {}", destination, this);
423-
} catch (IOException e) {
424-
throw new UncheckedIOException("Can't create extra config file for", e);
425-
}
426-
});
416+
if (Files.exists(from.toPath()) == false) {
417+
throw new TestClustersException("Can't create extra config file from " + from + " for " + this +
418+
" as it does not exist");
419+
}
420+
Path dst = configFile.getParent().resolve(destination);
421+
try {
422+
Files.createDirectories(dst.getParent());
423+
Files.copy(from.toPath(), dst, StandardCopyOption.REPLACE_EXISTING);
424+
LOGGER.info("Added extra config file {} for {}", destination, this);
425+
} catch (IOException e) {
426+
throw new UncheckedIOException("Can't create extra config file for", e);
427+
}
428+
});
427429
}
428430

429431
private void installModules() {
@@ -479,9 +481,9 @@ public void user(Map<String, String> userSpec) {
479481
if (keys.isEmpty() == false) {
480482
throw new TestClustersException("Unknown keys in user definition " + keys + " for " + this);
481483
}
482-
Map<String,String> cred = new LinkedHashMap<>();
483-
cred.put("useradd", userSpec.getOrDefault("username","test_user"));
484-
cred.put("-p", userSpec.getOrDefault("password","x-pack-test-password"));
484+
Map<String, String> cred = new LinkedHashMap<>();
485+
cred.put("useradd", userSpec.getOrDefault("username", "test_user"));
486+
cred.put("-p", userSpec.getOrDefault("password", "x-pack-test-password"));
485487
cred.put("-r", userSpec.getOrDefault("role", "superuser"));
486488
credentials.add(cred);
487489
}
@@ -691,7 +693,7 @@ private void logProcessInfo(String prefix, ProcessHandle.Info info) {
691693

692694
private void logFileContents(String description, Path from) {
693695
LOGGER.error("{} `{}`", description, this);
694-
try(Stream<String> lines = Files.lines(from, StandardCharsets.UTF_8)) {
696+
try (Stream<String> lines = Files.lines(from, StandardCharsets.UTF_8)) {
695697
lines
696698
.map(line -> " " + line)
697699
.forEach(LOGGER::error);
@@ -727,7 +729,7 @@ private void createWorkingDir(Path distroExtractDir) throws IOException {
727729
* We remove write permissions to make sure files are note mistakenly edited ( e.x. the config file ) and changes
728730
* reflected across all copies. Permissions are retained to be able to replace the links.
729731
*
730-
* @param sourceRoot where to copy from
732+
* @param sourceRoot where to copy from
731733
* @param destinationRoot destination to link to
732734
*/
733735
private void syncWithLinks(Path sourceRoot, Path destinationRoot) {
@@ -765,7 +767,7 @@ private void syncWithLinks(Path sourceRoot, Path destinationRoot) {
765767
}
766768
}
767769

768-
private void createConfiguration() {
770+
private void createConfiguration() {
769771
String nodeName = nameCustomization.apply(safeName(name));
770772
if (nodeName != null) {
771773
defaultConfig.put("node.name", nodeName);
@@ -794,15 +796,15 @@ private void createConfiguration() {
794796
// over and the REST client will not retry on circuit breaking exceptions yet (see #31986 for details). Once the REST client
795797
// can retry on circuit breaking exceptions, we can revert again to the default configuration.
796798
if (Version.fromString(version).getMajor() >= 7) {
797-
defaultConfig.put("indices.breaker.total.use_real_memory", "false");
799+
defaultConfig.put("indices.breaker.total.use_real_memory", "false");
798800
}
799801
// Don't wait for state, just start up quickly. This will also allow new and old nodes in the BWC case to become the master
800-
defaultConfig.put("discovery.initial_state_timeout", "0s");
802+
defaultConfig.put("discovery.initial_state_timeout", "0s");
801803

802804
HashSet<String> overriden = new HashSet<>(defaultConfig.keySet());
803805
overriden.retainAll(settings.keySet());
804806
overriden.removeAll(OVERRIDABLE_SETTINGS);
805-
if (overriden.isEmpty() ==false) {
807+
if (overriden.isEmpty() == false) {
806808
throw new IllegalArgumentException(
807809
"Testclusters does not allow the following settings to be changed:" + overriden + " for " + this
808810
);
@@ -834,7 +836,7 @@ private void createConfiguration() {
834836

835837
private void checkFrozen() {
836838
if (configurationFrozen.get()) {
837-
throw new IllegalStateException("Configuration for " + this + " can not be altered, already locked");
839+
throw new IllegalStateException("Configuration for " + this + " can not be altered, already locked");
838840
}
839841
}
840842

@@ -859,7 +861,7 @@ private List<String> getHttpPortInternal() {
859861
}
860862

861863
private List<String> readPortsFile(Path file) throws IOException {
862-
try(Stream<String> lines = Files.lines(file, StandardCharsets.UTF_8)) {
864+
try (Stream<String> lines = Files.lines(file, StandardCharsets.UTF_8)) {
863865
return lines.map(String::trim).collect(Collectors.toList());
864866
}
865867
}
@@ -868,15 +870,45 @@ private Path getExtractedDistributionDir() {
868870
return artifactsExtractDir.resolve(distribution.getGroup()).resolve("elasticsearch-" + getVersion());
869871
}
870872

873+
private List<File> getInstalledFileSet(Action<? super PatternFilterable> filter) {
874+
return Stream.concat(
875+
plugins.stream().filter(uri -> uri.getScheme().equalsIgnoreCase("file")).map(File::new),
876+
modules.stream()
877+
)
878+
.filter(File::exists)
879+
// TODO: We may be able to simplify this with Gradle 5.6
880+
// https://docs.gradle.org/nightly/release-notes.html#improved-handling-of-zip-archives-on-classpaths
881+
.map(zipFile -> project.zipTree(zipFile).matching(filter))
882+
.flatMap(tree -> tree.getFiles().stream())
883+
.collect(Collectors.toList());
884+
}
885+
886+
@Input
887+
private Set<URI> getRemotePlugins() {
888+
Set<URI> file = plugins.stream().filter(uri -> uri.getScheme().equalsIgnoreCase("file") == false).collect(Collectors.toSet());
889+
return file;
890+
}
891+
892+
@Classpath
893+
private List<File> getInstalledClasspath() {
894+
return getInstalledFileSet(filter -> filter.include("**/*.jar"));
895+
}
896+
897+
@InputFiles
898+
@PathSensitive(PathSensitivity.RELATIVE)
899+
private List<File> getInstalledFiles() {
900+
return getInstalledFileSet(filter -> filter.exclude("**/*.jar"));
901+
}
902+
871903
@Classpath
872-
private Set<File> getRuntimeClasspath() {
904+
private Set<File> getDistributionClasspath() {
873905
return project.fileTree(getExtractedDistributionDir()).matching(filter -> filter.include("**/*.jar")).getFiles();
874906
}
875907

876908
@InputFiles
877909
@PathSensitive(PathSensitivity.RELATIVE)
878910
private FileCollection getDistributionFiles() {
879-
return project.fileTree(getExtractedDistributionDir()).minus(project.files(getRuntimeClasspath()));
911+
return project.fileTree(getExtractedDistributionDir()).minus(project.files(getDistributionClasspath()));
880912
}
881913

882914
@Nested
@@ -933,9 +965,9 @@ void waitForAllConditions() {
933965
ADDITIONAL_CONFIG_TIMEOUT_UNIT.toMillis(ADDITIONAL_CONFIG_TIMEOUT *
934966
(
935967
plugins.size() +
936-
keystoreFiles.size() +
937-
keystoreSettings.size() +
938-
credentials.size()
968+
keystoreFiles.size() +
969+
keystoreSettings.size() +
970+
credentials.size()
939971
)
940972
),
941973
TimeUnit.MILLISECONDS,

0 commit comments

Comments
 (0)