diff --git a/data-node/src/main/java/org/graylog/datanode/OpensearchDistribution.java b/data-node/src/main/java/org/graylog/datanode/OpensearchDistribution.java index 7fb0dba48d..1e108feaf1 100644 --- a/data-node/src/main/java/org/graylog/datanode/OpensearchDistribution.java +++ b/data-node/src/main/java/org/graylog/datanode/OpensearchDistribution.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; +import java.util.Locale; import java.util.Objects; import java.util.Optional; import java.util.regex.Matcher; @@ -56,6 +57,9 @@ public record OpensearchDistribution(Path directory, String version, @Nullable S } private static OpensearchDistribution detectInSubdirectory(Path directory, String osArch) { + + final var archCode = archCode(osArch); + final List opensearchDistributions; try ( var files = Files.list(directory); @@ -65,20 +69,28 @@ public record OpensearchDistribution(Path directory, String version, @Nullable S .flatMap(f -> parse(f).stream()) .toList(); } catch (IOException e) { - throw new RuntimeException("Failed to list content of provided directory " + directory.toAbsolutePath(), e); + throw createErrorMessage(directory, archCode, "Failed to list content of provided directory", e); } if (opensearchDistributions.isEmpty()) { - throw new IllegalArgumentException("Could not detect any opensearch distribution in " + directory.toAbsolutePath()); + throw createErrorMessage(directory, archCode, "Could not detect any opensearch distribution"); } LOG.info("Found following opensearch distributions: " + opensearchDistributions.stream().map(d -> d.directory().toAbsolutePath()).toList()); - final var archCode = archCode(osArch); - return findByArchitecture(opensearchDistributions, archCode) .orElseGet(() -> findWithoutArchitecture(opensearchDistributions) - .orElseThrow(() -> new IllegalArgumentException("No Opensearch distribution found for architecture " + osArch))); + .orElseThrow(() -> createErrorMessage(directory, archCode, "No Opensearch distribution found for your system architecture"))); + } + + private static IllegalArgumentException createErrorMessage(Path directory, String osArch, String message) { + return createErrorMessage(directory, osArch, message, null); + } + + + private static IllegalArgumentException createErrorMessage(Path directory, String archCode, String errorMessage, Exception cause) { + final String message = String.format(Locale.ROOT, "%s. Directory used for Opensearch detection: %s. Please configure opensearch_location to a directory that contains an opensearch distribution for your architecture %s. You can download Opensearch from https://opensearch.org/downloads.html . Please extract the downloaded distribution and point opensearch_location configuration option to that directory.", errorMessage, directory.toAbsolutePath(), archCode); + return new IllegalArgumentException(message, cause); } public static String archCode(final String osArch) { diff --git a/data-node/src/test/java/org/graylog/datanode/OpensearchDistributionTest.java b/data-node/src/test/java/org/graylog/datanode/OpensearchDistributionTest.java index 54ea18ba08..226c600eb2 100644 --- a/data-node/src/test/java/org/graylog/datanode/OpensearchDistributionTest.java +++ b/data-node/src/test/java/org/graylog/datanode/OpensearchDistributionTest.java @@ -30,6 +30,9 @@ class OpensearchDistributionTest { @TempDir private Path tempDir; + @TempDir + private Path emptyTempDir; + @TempDir private Path tempDirWithoutArch; @@ -43,6 +46,16 @@ class OpensearchDistributionTest { Files.createDirectory(tempDir.resolve(".config")); // just to include something which is not OS dist } + @Test + void testFailedDetectionInDirectory() { + Assertions.assertThatThrownBy(() -> OpensearchDistribution.detectInDirectory(emptyTempDir.resolve("nonexistent"), "amd64")) + .hasMessageStartingWith("Failed to list content of provided directory"); + + + Assertions.assertThatThrownBy(() -> OpensearchDistribution.detectInDirectory(emptyTempDir, "amd64")) + .hasMessageStartingWith("Could not detect any opensearch distribution"); + } + @Test void testDetection() throws IOException { final OpensearchDistribution x64 = OpensearchDistribution.detectInDirectory(tempDir, "amd64");