Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.gradle.testclusters;

public class ExtensionsProperties {
private String name;
private String uniqueId;
private String hostAddress;
private String port;
private String version;
private String opensearchVersion;
private String minimumCompatibleVersion;

public ExtensionsProperties(
String name,
String uniqueId,
String hostAddress,
String port,
String version,
String opensearchVersion,
String minimumCompatibleVersion
) {
this.name = name;
this.uniqueId = uniqueId;
this.hostAddress = hostAddress;
this.port = port;
this.version = version;
this.opensearchVersion = opensearchVersion;
this.minimumCompatibleVersion = minimumCompatibleVersion;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUniqueId() {
return uniqueId;
}

public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}

public String getHostAddress() {
return hostAddress;
}

public void setHostAddress(String hostAddress) {
this.hostAddress = hostAddress;
}

public String getPort() {
return port;
}

public void setPort(String port) {
this.port = port;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getOpensearchVersion() {
return opensearchVersion;
}

public void setOpensearchVersion(String opensearchVersion) {
this.opensearchVersion = opensearchVersion;
}

public String getMinimumCompatibleVersion() {
return minimumCompatibleVersion;
}

public void setMinimumCompatibleVersion(String minimumCompatibleVersion) {
this.minimumCompatibleVersion = minimumCompatibleVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ public void setTestDistribution(TestDistribution distribution) {
nodes.all(each -> each.setTestDistribution(distribution));
}

@Override
public void extension(ExtensionsProperties extension) {
nodes.all(each -> each.extension(extension));
}

@Override
public void plugin(Provider<RegularFile> plugin) {
nodes.all(each -> each.plugin(plugin));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.opensearch.gradle.Version;
import org.opensearch.gradle.VersionProperties;
import org.opensearch.gradle.info.BuildParams;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.gradle.api.Action;
import org.gradle.api.Named;
import org.gradle.api.NamedDomainObjectContainer;
Expand Down Expand Up @@ -92,6 +94,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -141,6 +144,7 @@ public class OpenSearchNode implements TestClusterConfiguration {
private final Map<String, Configuration> pluginAndModuleConfigurations = new HashMap<>();
private final List<Provider<File>> plugins = new ArrayList<>();
private final List<Provider<File>> modules = new ArrayList<>();
private final List<ExtensionsProperties> extensions = new ArrayList<>();
final LazyPropertyMap<String, CharSequence> settings = new LazyPropertyMap<>("Settings", this);
private final LazyPropertyMap<String, CharSequence> keystoreSettings = new LazyPropertyMap<>("Keystore", this);
private final LazyPropertyMap<String, File> keystoreFiles = new LazyPropertyMap<>("Keystore files", this, FileEntry::new);
Expand Down Expand Up @@ -341,6 +345,42 @@ public void module(String moduleProjectPath) {
module(maybeCreatePluginOrModuleDependency(moduleProjectPath));
}

@Override
public void extension(ExtensionsProperties extensions) {
this.extensions.add(extensions);
}

public void writeExtensionFiles() {
try {
// Creates extensions.yml in the target directory
Path destination = getDistroDir().resolve("extensions").resolve("extensions.yml");
if (!Files.exists(getDistroDir().resolve("extensions"))) {
Files.createDirectory(getDistroDir().resolve("extensions"));
}
DumperOptions dumperOptions = new DumperOptions();
TestExtensionsList extensionsList = new TestExtensionsList(this.extensions);
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(dumperOptions);
Files.write(destination, yaml.dump(extensionsList).getBytes());

/*
* SnakeYaml creates a Yaml file with an unnecessary line at the top with the class name
* This section of code removes that line while keeping everything else the same.
*/

Scanner scanner = new Scanner(destination);
scanner.nextLine();
StringBuilder extensionsString = new StringBuilder();
while (scanner.hasNextLine()) {
extensionsString.append("\n" + scanner.nextLine());
}
Files.write(destination, extensionsString.toString().getBytes());

} catch (IOException e) {
throw new UncheckedIOException("Failed to write to extensions.yml", e);
}
}

@Override
public void keystore(String key, String value) {
keystoreSettings.put(key, value);
Expand Down Expand Up @@ -511,6 +551,10 @@ public synchronized void start() {
logToProcessStdout("installed plugins");
}

if (!extensions.isEmpty()) {
writeExtensionFiles();
}

logToProcessStdout("Creating opensearch keystore with password set to [" + keystorePassword + "]");
if (keystorePassword.length() > 0) {
runOpenSearchBinScriptWithInput(keystorePassword + "\n" + keystorePassword, "opensearch-keystore", "create", "-p");
Expand Down Expand Up @@ -785,6 +829,10 @@ private void startOpenSearchProcess() {
environment.clear();
environment.putAll(getOpenSearchEnvironment());

if (!extensions.isEmpty()) {
environment.put("OPENSEARCH_JAVA_OPTS", "-Dopensearch.experimental.feature.extensions.enabled=true");
}

// don't buffer all in memory, make sure we don't block on the default pipes
processBuilder.redirectError(ProcessBuilder.Redirect.appendTo(stderrFile.toFile()));
processBuilder.redirectOutput(ProcessBuilder.Redirect.appendTo(stdoutFile.toFile()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public interface TestClusterConfiguration {

void setTestDistribution(TestDistribution distribution);

void extension(ExtensionsProperties extension);

void plugin(Provider<RegularFile> plugin);

void plugin(String pluginProjectPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.gradle.testclusters;

import java.util.List;

public class TestExtensionsList {
private List<ExtensionsProperties> extensions;

public TestExtensionsList(List<ExtensionsProperties> extensionsList) {
extensions = extensionsList;
}

public List<ExtensionsProperties> getExtensions() {
return extensions;
}

public void setExtensions(List<ExtensionsProperties> extensionsList) {
extensions = extensionsList;
}
}