@Override public void execute(ClusterConfiguration config) { if (StringUtils.isBlank(config.getPathInitScript())) { // nothing to do; return return; } String filePath = config.getPathInitScript(); validateFile(filePath); // we'll run all commands against the first node in the cluster ElasticsearchClient client = new ElasticsearchClient.Builder() .withInstanceConfiguration(config.getInstanceConfigurationList().get(0)) .withHostname("localhost") .build(); Path path = Paths.get(filePath); if ("json".equalsIgnoreCase(FilenameUtils.getExtension(filePath))) { parseJson(client, config.getLog(), path); } else { parseScript(client, config.getLog(), path); } }
public ClusterConfiguration build() { ClusterConfiguration config = new ClusterConfiguration( instanceConfigurationList, artifactResolver, artifactInstaller, log); config.flavour = flavour; config.version = version; config.downloadUrl = downloadUrl; config.clusterName = clusterName; config.pathConf = pathConf; config.plugins = plugins; config.pathInitScript = pathInitScript; config.keepExistingData = keepExistingData; config.timeout = timeout; config.clientSocketTimeout = clientSocketTimeout; config.setAwait = setAwait; config.autoCreateIndex = autoCreateIndex; config.getInstanceConfigurationList().forEach(c -> c.setClusterConfiguration(config)); return config; } }
public Builder withInstanceConfiguration(InstanceConfiguration config) { this.log = config.getClusterConfiguration().getLog(); this.socketTimeout = config.getClusterConfiguration().getClientSocketTimeout(); this.port = config.getHttpPort(); return this; }
@Override public void execute(ClusterConfiguration config) { String flavour = config.getFlavour(); String version = config.getVersion(); config.getLog().debug( "Checking flavour '" + flavour + "' against version '" + version + "'"); if (StringUtils.isNotBlank(flavour) && VersionUtil.isBetween_5_0_0_and_6_2_x(version)) { throw new ElasticsearchSetupException(String.format( "The flavour property is not supported for Elasticsearch [5.0.0 - 6.3.0).")); } }
@Override public void execute(ClusterConfiguration config) { if (config.isSetAwait()) { new ExecutionLock(config.getLog()).lock(); } }
throws ArtifactException, IOException String flavour = config.getFlavour(); String version = config.getVersion(); String artifactId = getArtifactId(flavour, version); config.getLog().debug("Artifact ref: " + artifactReference); PluginArtifactResolver artifactResolver = config.getArtifactResolver(); try config.getLog().debug("Resolving artifact against the local maven repo (stage 1)"); return artifactResolver .resolveArtifact(artifactReference.getArtifactCoordinates()); config.getLog().debug("Artifact not found; downloading and installing it"); config.getLog().debug("Installing " + tempFile + " in the local maven repo"); config.getArtifactInstaller().installArtifact( artifactReference.getGroupId(), artifactReference.getArtifactId(), tempFile); config.getLog().debug("Resolving artifact against the local maven repo (stage 2)"); return artifactResolver .resolveArtifact(artifactReference.getArtifactCoordinates());
@Override public void execute(ClusterConfiguration config) { // the instances have already started; // waiting just 10 seconds for them to form the cluster int timeout = 10; ElasticsearchClient client = new ElasticsearchClient.Builder() .withInstanceConfiguration(config.getInstanceConfigurationList().get(0)) .withHostname("localhost") .build(); Monitor monitor = new Monitor(client, config.getLog()); monitor.waitToStartCluster( config.getClusterName(), config.getInstanceConfigurationList().size(), timeout); } }
@Override public void execute(ClusterConfiguration config) { int instanceCount = config.getInstanceConfigurationList().size(); if (instanceCount < 1) { throw new ElasticsearchSetupException(String.format( "The number of instances should not be smaller than 1; you configured: %d", instanceCount)); } }
@Before public void setup() { when(config.getLog()).thenReturn(log); when(instanceConfig.getClusterConfiguration()).thenReturn(config); when(config.getInstanceConfigurationList()).thenReturn(Arrays.asList(instanceConfig)); }
@Override public void execute(InstanceConfiguration config) { int timeout = config.getClusterConfiguration().getTimeout(); ElasticsearchClient client = new ElasticsearchClient.Builder() .withInstanceConfiguration(config) .withHostname("localhost") .build(); Monitor monitor = new Monitor(client, config.getClusterConfiguration().getLog()); monitor.waitToStartInstance( config.getBaseDir(), config.getClusterConfiguration().getClusterName(), timeout); } }
@Override public void execute(InstanceConfiguration config) if (config.getClusterConfiguration().getPlugins().size() > 0) if (VersionUtil.isEqualOrGreater_6_4_0(config.getClusterConfiguration().getVersion())) Log log = config.getClusterConfiguration().getLog(); for (PluginConfiguration plugin : config.getClusterConfiguration().getPlugins())
"-Ecluster.name=" + config.getClusterConfiguration().getClusterName(), false); cmd.addArgument("-Ehttp.port=" + config.getHttpPort(), false); List<String> hosts = config.getClusterConfiguration().getInstanceConfigurationList() .stream() .filter(config -> config != this.config) if (config.getClusterConfiguration().isAutoCreateIndex() == false)
/** * Download the artifact from the download repository. * @param artifactReference * @param config * @return the downloaded file * @throws IOException */ private File downloadArtifact( ElasticsearchArtifact artifactReference, ClusterConfiguration config) throws IOException { String filename = String.format( ELASTICSEARCH_FILENAME, artifactReference.getArtifactId(), artifactReference.getVersion()); File tempFile = new File(FilesystemUtil.getTempDirectory(), filename); tempFile.deleteOnExit(); FileUtils.deleteQuietly(tempFile); URL downloadUrl = new URL( StringUtils.isBlank(config.getDownloadUrl()) ? String.format(ELASTICSEARCH_DOWNLOAD_URL, filename) : config.getDownloadUrl()); config.getLog().debug("Downloading " + downloadUrl + " to " + tempFile); FileUtils.copyURLToFile(downloadUrl, tempFile); return tempFile; }
private void cleanUp(File unpackDirectory, ClusterConfiguration config) { if (unpackDirectory != null) { try { FileUtils.deleteDirectory(unpackDirectory); } catch (IOException e) { config.getLog().error( String.format( "Could not delete Elasticsearch upack directory : ", unpackDirectory.getAbsolutePath()), e); } } }
@Test public void testExecuteWithoutFile() { when(config.getPathInitScript()).thenReturn(""); step.execute(config); verify(step, never()).parseJson(any(ElasticsearchClient.class), eq(log), any(Path.class)); verify(step, never()).parseScript(any(ElasticsearchClient.class), eq(log), any(Path.class)); }
@Override public void execute(InstanceConfiguration config) Log log = config.getClusterConfiguration().getLog(); if (VersionUtil.isEqualOrGreater_6_4_0(config.getClusterConfiguration().getVersion()))
@Override public void execute(ClusterConfiguration config) { String clusterName = config.getClusterName(); if (clusterName == null) { throw new ElasticsearchSetupException(String.format( "Please provide a cluster name.")); } if (clusterName.matches("[a-zA-Z0-9.-]+") == false) { throw new ElasticsearchSetupException(String.format( "elasticsearch-maven-plugin supports only alphanumeric with dots and dashes cluster names. You configured: %s.", clusterName)); } }
@Override public void execute(ClusterConfiguration config) { String baseDir = config.getInstanceConfigurationList().get(0).getBaseDir(); try { Validate.notBlank(baseDir); new File(baseDir).getCanonicalPath(); // this should catch erroneous paths } catch (Exception e) { throw new ElasticsearchSetupException(String.format( "The value of the 'baseDir' parameter ('%1$s') is not a valid file path.", baseDir)); } }
public ForkedElasticsearchProcessDestroyer(final InstanceConfiguration config) { this.log = config.getClusterConfiguration().getLog(); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { ForkedElasticsearchProcessDestroyer.this.terminateProcess(config); } }); }
@Test public void testExecuteScriptFile() { String filePath = "folder/init.script"; when(config.getPathInitScript()).thenReturn(filePath); doNothing().when(step).validateFile(filePath); doNothing() .when(step) .parseScript(any(ElasticsearchClient.class), eq(log), any(Path.class)); step.execute(config); verify(step).validateFile(filePath); ArgumentCaptor<Path> pathCaptor = ArgumentCaptor.forClass(Path.class); verify(step).parseScript(any(ElasticsearchClient.class), eq(log), pathCaptor.capture()); assertEquals(filePath, pathCaptor.getValue().toString().replace('\\', '/')); }