use of org.codelibs.elasticsearch.runner.node.ClusterRunnerNode in project elasticsearch-cluster-runner by codelibs.
the class ElasticsearchClusterRunner method execute.
protected void execute(final int id) {
final String nodeName = "Node " + id;
final Path homePath = Paths.get(basePath, nodeName.replace(' ', '_').toLowerCase(Locale.ROOT));
final Path confPath = this.confPath == null ? homePath.resolve(CONFIG_DIR) : Paths.get(this.confPath);
final Path logsPath = this.logsPath == null ? homePath.resolve(LOGS_DIR) : Paths.get(this.logsPath);
final Path dataPath = this.dataPath == null ? homePath.resolve(DATA_DIR) : Paths.get(this.dataPath);
createDir(homePath);
createDir(confPath);
createDir(logsPath);
createDir(dataPath);
final Settings.Builder builder = builder();
if (settingsBuilder != null) {
settingsBuilder.build(id, builder);
}
putIfAbsent(builder, "path.home", homePath.toAbsolutePath().toString());
putIfAbsent(builder, "path.data", dataPath.toAbsolutePath().toString());
putIfAbsent(builder, "path.logs", logsPath.toAbsolutePath().toString());
final Path esConfPath = confPath.resolve(ELASTICSEARCH_YAML);
if (!esConfPath.toFile().exists()) {
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_DIR + "/" + ELASTICSEARCH_YAML)) {
Files.copy(is, esConfPath, StandardCopyOption.REPLACE_EXISTING);
} catch (final IOException e) {
throw new ClusterRunnerException("Could not create: " + esConfPath, e);
}
}
if (!disableESLogger) {
final Path logConfPath = confPath.resolve(LOG4J2_PROPERTIES);
if (!logConfPath.toFile().exists()) {
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_DIR + "/" + LOG4J2_PROPERTIES)) {
Files.copy(is, logConfPath, StandardCopyOption.REPLACE_EXISTING);
} catch (final IOException e) {
throw new ClusterRunnerException("Could not create: " + logConfPath, e);
}
}
}
try {
final String pluginPath = builder.get("path.plugins");
if (pluginPath != null) {
final Path sourcePath = Paths.get(pluginPath);
final Path targetPath = homePath.resolve("plugins");
Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
Files.createDirectories(targetPath.resolve(sourcePath.relativize(dir)));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
Files.copy(file, targetPath.resolve(sourcePath.relativize(file)), StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
builder.remove("path.plugins");
}
final int httpPort = getAvailableHttpPort(id);
putIfAbsent(builder, "cluster.name", clusterName);
putIfAbsent(builder, NODE_NAME, nodeName);
putIfAbsent(builder, HTTP_PORT, String.valueOf(httpPort));
putIfAbsent(builder, "index.store.type", indexStoreType);
if (!builder.keys().contains("node.roles")) {
builder.putList("node.roles", "master", "data");
}
print("Node Name: " + builder.get(NODE_NAME));
print("HTTP Port: " + builder.get(HTTP_PORT));
print("Data Directory: " + dataPath);
print("Log Directory: " + logsPath);
final Settings settings = builder.build();
final Environment environment = InternalSettingsPreparer.prepareEnvironment(settings, Collections.emptyMap(), confPath, () -> nodeName);
if (!disableESLogger) {
LogConfigurator.registerErrorListener();
final String envNodeName = Node.NODE_NAME_SETTING.get(environment.settings());
try {
LogConfigurator.setNodeName(envNodeName);
} catch (final IllegalStateException e) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to set {} to a log configuration.", envNodeName, e);
}
}
LogConfigurator.configure(environment);
}
createDir(environment.modulesFile());
createDir(environment.pluginsFile());
final Node node = new ClusterRunnerNode(environment, pluginList);
node.start();
nodeList.add(node);
envList.add(environment);
} catch (final Exception e) {
throw new ClusterRunnerException("Failed to start node " + id, e);
}
}
use of org.codelibs.elasticsearch.runner.node.ClusterRunnerNode in project elasticsearch-cluster-runner by codelibs.
the class ElasticsearchClusterRunner method startNode.
/**
* Start a closed node.
*
* @param i the number of nodes
* @return true if the node is started.
*/
public boolean startNode(final int i) {
if (i >= nodeList.size()) {
return false;
}
if (!nodeList.get(i).isClosed()) {
return false;
}
final Node node = new ClusterRunnerNode(envList.get(i), pluginList);
try {
node.start();
nodeList.set(i, node);
return true;
} catch (final NodeValidationException e) {
print(e.getLocalizedMessage());
}
return false;
}
Aggregations