Beispiel #1
0
        private void InstallPlugins()
        {
            foreach (var plugin in ElasticsearchPlugins.Supported)
            {
                var installCommand   = plugin.Value;
                var installParameter = installCommand.InstallParameter != null?installCommand.InstallParameter(this.Version) : installCommand.Moniker;

                var folder       = installCommand.FolderName ?? installCommand.Moniker;
                var pluginFolder = Path.Combine(this.ElasticsearchHome, "plugins", folder);

                if (!Directory.Exists(this.ElasticsearchHome))
                {
                    continue;
                }

                // assume plugin already installed
                if (Directory.Exists(pluginFolder))
                {
                    continue;
                }

                Console.WriteLine($"Installing elasticsearch plugin: {installCommand.Moniker} ...");
                var timeout = TimeSpan.FromSeconds(120);
                var handle  = new ManualResetEvent(false);
                Task.Run(() =>
                {
                    using (var p = new ObservableProcess(this.PluginBinary, "install", installParameter))
                    {
                        var o = p.Start();
                        Console.WriteLine($"Calling: {this.PluginBinary} install {installParameter}");
                        o.Subscribe(c => Console.WriteLine(c.Data),
                                    (e) =>
                        {
                            Console.WriteLine($"Failed installing elasticsearch plugin: {installCommand.Moniker}");
                            handle.Set();
                            throw e;
                        },
                                    () =>
                        {
                            Console.WriteLine($"Finished installing elasticsearch plugin: {installCommand.Moniker} exit code: {p.ExitCode}");
                            handle.Set();
                        });
                        if (!handle.WaitOne(timeout, true))
                        {
                            throw new Exception($"Could not install {installCommand.Moniker} within {timeout}");
                        }
                    }
                });
                if (!handle.WaitOne(timeout, true))
                {
                    throw new Exception($"Could not install {installCommand.Moniker} within {timeout}");
                }
            }
        }
Beispiel #2
0
        private void InstallPlugins()
        {
            var pluginBat = Path.Combine(this.RoamingClusterFolder, "bin", "plugin") + ".bat";

            foreach (var plugin in SupportedPlugins)
            {
                var installPath  = plugin.Key;
                var localPath    = plugin.Value;
                var pluginFolder = Path.Combine(this.RoamingClusterFolder, "plugins", localPath);

                if (!Directory.Exists(this.RoamingClusterFolder))
                {
                    continue;
                }

                // assume plugin already installed
                if (Directory.Exists(pluginFolder))
                {
                    continue;
                }

                Console.WriteLine($"Installing elasticsearch plugin: {localPath} ...");
                var timeout = TimeSpan.FromSeconds(60);
                var handle  = new ManualResetEvent(false);
                Task.Run(() =>
                {
                    using (var p = new ObservableProcess(pluginBat, "install", installPath))
                    {
                        var o = p.Start();
                        Console.WriteLine($"Calling: {pluginBat} install {installPath}");
                        o.Subscribe(e => Console.WriteLine(e),
                                    (e) =>
                        {
                            Console.WriteLine($"Failed installing elasticsearch plugin: {localPath} ");
                            handle.Set();
                            throw e;
                        },
                                    () => {
                            Console.WriteLine($"Finished installing elasticsearch plugin: {localPath} exit code: {p.ExitCode}");
                            handle.Set();
                        });
                        if (!handle.WaitOne(timeout, true))
                        {
                            throw new ApplicationException($"Could not install ${installPath} within {timeout}");
                        }
                    }
                });
                if (!handle.WaitOne(timeout, true))
                {
                    throw new ApplicationException($"Could not install ${installPath} within {timeout}");
                }
            }
        }
Beispiel #3
0
        public IObservable <ElasticsearchMessage> Start(string typeOfCluster, string[] additionalSettings = null)
        {
            if (!this.RunningIntegrations)
            {
                return(Observable.Empty <ElasticsearchMessage>());
            }

            this.Stop();

            var settingMarker = this.VersionInfo.ParsedVersion.Major >= 5 ? "-E " : "-D";
            var settings      = DefaultNodeSettings
                                .Concat(additionalSettings ?? Enumerable.Empty <string>())
                                .Select(s => $"{settingMarker}{s}")
                                .ToList();

            var easyRunBat = Path.Combine(this.RoamingFolder, $"run-{typeOfCluster.ToLowerInvariant()}.bat");

            if (!File.Exists(easyRunBat))
            {
                var badSettings = new[] { "node.name", "cluster.name" };
                var batSettings = string.Join(" ", settings.Where(s => !badSettings.Any(s.Contains)));
                File.WriteAllText(easyRunBat, $@"elasticsearch-{this.Version}\bin\elasticsearch.bat {batSettings}");
            }

#if DOTNETCORE
            var handle = new Signal(false);
#else
            var handle = new ManualResetEvent(false);
#endif
            var alreadyRunning = UseAlreadyRunningInstance(handle);
            if (alreadyRunning != null)
            {
                return(alreadyRunning);
            }

            this._process = new ObservableProcess(this.Binary, settings.ToArray());

            var observable = Observable.Using(() => this._process, process => process.Start())
                             .Select(consoleLine => new ElasticsearchMessage(consoleLine));
            this._processListener = observable.Subscribe(onNext: s => HandleConsoleMessage(s, handle));

            if (handle.WaitOne(this.HandleTimeout, true))
            {
                return(observable);
            }

            this.Stop();
            throw new Exception($"Could not start elasticsearch within {this.HandleTimeout}");
        }
        private void EnsureShieldAdmin()
        {
            if (!this._config.XPackEnabled)
            {
                return;
            }


            var folder = this.Version.Major >= 5 ? "x-pack" : "shield";
            var plugin = this.Version.Major >= 5 ? "users" : "esusers";

            EnsureRoles(folder);
            var timeout = TimeSpan.FromMinutes(1);

            var pluginBat = Path.Combine(this.ElasticsearchHome, "bin", folder, plugin) + ".bat";

            foreach (var cred in ShieldInformation.AllUsers)
            {
                var handle = new ManualResetEvent(false);
                Task.Run(() =>
                {
                    using (var p = new ObservableProcess(pluginBat, $"useradd {cred.Username} -p {cred.Password} -r {cred.Role}"))
                    {
                        var o = p.Start();
                        Console.WriteLine($"Calling: {pluginBat} useradd {cred.Username}");
                        o.Subscribe(
                            //c=>Console.WriteLine(c.Data),
                            c => { },
                            (e) =>
                        {
                            handle.Set();
                            throw e;
                        },
                            () =>
                        {
                            handle.Set();
                        });
                        if (!handle.WaitOne(timeout, true))
                        {
                            throw new Exception($"Could not add user {cred.Username} within {timeout}");
                        }
                    }
                });
                if (!handle.WaitOne(timeout, true))
                {
                    throw new Exception($"Could not add user {cred.Username} within {timeout}");
                }
            }
        }
        public IObservable <ElasticsearchConsoleOut> Start(string[] additionalSettings = null)
        {
            if (!this._config.RunIntegrationTests)
            {
                return(Observable.Empty <ElasticsearchConsoleOut>());
            }

            this.Stop();

            var settingMarker = this.Version.Major >= 5 ? "-E " : "-D";
            var settings      = DefaultNodeSettings
                                .Concat(additionalSettings ?? Enumerable.Empty <string>())
                                .Select(s => $"{settingMarker}{s}")
                                .ToList();

            this.FileSystem.BeforeStart(settings);

            var handle = new XplatManualResetEvent(false);

            var alreadyRunning = UseAlreadyRunningInstance(handle);

            if (alreadyRunning != null)
            {
                return(alreadyRunning);
            }

            this._process = new ObservableProcess(this.FileSystem.Binary, settings.ToArray());

            var observable = Observable.Using(() => this._process, process => process.Start())
                             .Select(c => new ElasticsearchConsoleOut(c.Error, c.Data));

            Console.WriteLine($"Starting: {_process.Binary} {_process.Arguments}");

            this._processListener = observable
                                    .Subscribe(s => this.HandleConsoleMessage(s, handle), (e) => this.Stop(), () => { });

            if (!handle.WaitOne(HandleTimeout, true))
            {
                this.Stop();
                throw new Exception($"Could not start elasticsearch within {HandleTimeout}");
            }

            if (this.Exception != null)
            {
                throw this.Exception;
            }
            return(observable);
        }
        public IObservable <ElasticsearchMessage> Start()
        {
            if (!this.RunningIntegrations)
            {
                return(Observable.Empty <ElasticsearchMessage>());
            }

            this.Stop();
            var timeout = TimeSpan.FromSeconds(60);
            var handle  = new ManualResetEvent(false);

            if (_doNotSpawnIfAlreadyRunning)
            {
                var alreadyUp = new ElasticClient().RootNodeInfo();
                if (alreadyUp.IsValid)
                {
                    this.Started = true;
                    this.Port    = 9200;
                    this.Info    = new ElasticsearchNodeInfo(alreadyUp.Version.Number, null, alreadyUp.Version.LuceneVersion);
                    this._blockingSubject.OnNext(handle);
                    if (!handle.WaitOne(timeout, true))
                    {
                        throw new ApplicationException($"Could launch tests on already running elasticsearch within {timeout}");
                    }
                    return(Observable.Empty <ElasticsearchMessage>());
                }
            }

            this._process = new ObservableProcess(this.Binary,
                                                  $"-Des.cluster.name={this.ClusterName}",
                                                  $"-Des.node.name={this.NodeName}",
                                                  $"-Des.path.repo={this.RepositoryPath}",
                                                  $"-Des.script.inline=on",
                                                  $"-Des.script.indexed=on"
                                                  );
            var observable = Observable.Using(() => this._process, process => process.Start())
                             .Select(consoleLine => new ElasticsearchMessage(consoleLine));

            this._processListener = observable.Subscribe(onNext: s => HandleConsoleMessage(s, handle));

            if (!handle.WaitOne(timeout, true))
            {
                this.Stop();
                throw new ApplicationException($"Could not start elasticsearch within {timeout}");
            }

            return(observable);
        }
        private void InstallPlugins()
        {
            var pluginBat = Path.Combine(this.RoamingClusterFolder, "bin", "plugin") + ".bat";

            foreach (var plugin in SupportedPlugins)
            {
                var installPath  = plugin.Key;
                var localPath    = plugin.Value;
                var pluginFolder = Path.Combine(this.RoamingClusterFolder, "bin", "plugins", localPath);
                if (!Directory.Exists(this.RoamingClusterFolder))
                {
                    continue;
                }

                var timeout = TimeSpan.FromSeconds(60);
                var handle  = new ManualResetEvent(false);
                Task.Factory.StartNew(() =>
                {
                    using (var p = new ObservableProcess(pluginBat, "install", installPath))
                    {
                        var o = p.Start();
                        o.Subscribe(Console.WriteLine,
                                    (e) =>
                        {
                            handle.Set();
                            throw e;
                        },
                                    () => handle.Set()
                                    );
                    }
                });
                if (!handle.WaitOne(timeout, true))
                {
                    throw new ApplicationException($"Could not install ${installPath} within {timeout}");
                }
            }
        }
        public IObservable <ElasticsearchMessage> Start(string[] additionalSettings = null)
        {
            if (!this.RunningIntegrations)
            {
                return(Observable.Empty <ElasticsearchMessage>());
            }

            this.Stop();
            var timeout = TimeSpan.FromSeconds(60);
            var handle  = new ManualResetEvent(false);

            if (_doNotSpawnIfAlreadyRunning)
            {
                var client    = TestClient.GetClient();
                var alreadyUp = client.RootNodeInfo();
                if (alreadyUp.IsValid)
                {
                    var checkPlugins = client.CatPlugins();

                    if (checkPlugins.IsValid)
                    {
                        foreach (var supportedPlugin in SupportedPlugins)
                        {
                            if (!checkPlugins.Records.Any(r => r.Component.Equals(supportedPlugin.Key)))
                            {
                                throw new ApplicationException($"Already running elasticsearch does not have supported plugin {supportedPlugin.Key} installed.");
                            }
                        }

                        this.Started = true;
                        this.Port    = 9200;
                        this.Info    = new ElasticsearchNodeInfo(alreadyUp.Version.Number, null, alreadyUp.Version.LuceneVersion);
                        this._blockingSubject.OnNext(handle);
                        if (!handle.WaitOne(timeout, true))
                        {
                            throw new ApplicationException($"Could not launch tests on already running elasticsearch within {timeout}");
                        }

                        return(Observable.Empty <ElasticsearchMessage>());
                    }
                }
            }
            var settings = new string[]
            {
                $"-Des.cluster.name={this.ClusterName}",
                $"-Des.node.name={this.NodeName}",
                $"-Des.path.repo={this.RepositoryPath}",
                $"-Des.script.inline=on",
                $"-Des.script.indexed=on"
            }.Concat(additionalSettings ?? Enumerable.Empty <string>());

            this._process = new ObservableProcess(this.Binary, settings.ToArray());

            var observable = Observable.Using(() => this._process, process => process.Start())
                             .Select(consoleLine => new ElasticsearchMessage(consoleLine));

            this._processListener = observable.Subscribe(onNext: s => HandleConsoleMessage(s, handle));

            if (!handle.WaitOne(timeout, true))
            {
                this.Stop();
                throw new ApplicationException($"Could not start elasticsearch within {timeout}");
            }

            return(observable);
        }
		public IObservable<ElasticsearchMessage> Start()
		{

			if (!this.RunningIntegrations) return Observable.Empty<ElasticsearchMessage>();

			this.Stop();
			var timeout = TimeSpan.FromSeconds(60);
			var handle = new ManualResetEvent(false);

			if (_doNotSpawnIfAlreadyRunning)
			{
			    var client = new ElasticClient();
			    var alreadyUp = client.RootNodeInfo();
				if (alreadyUp.IsValid)
				{
				    var checkPlugins = client.CatPlugins();

				    if (checkPlugins.IsValid)
				    {
				        foreach (var supportedPlugin in  SupportedPlugins)
				        {
				            if (!checkPlugins.Records.Any(r => r.Component.Equals(supportedPlugin.Key)))
				                throw new ApplicationException($"Already running elasticsearch does not have supported plugin {supportedPlugin.Key} installed.");
				        }

				        this.Started = true;
				        this.Port = 9200;
				        this.Info = new ElasticsearchNodeInfo(alreadyUp.Version.Number, null, alreadyUp.Version.LuceneVersion);
				        this._blockingSubject.OnNext(handle);
				        if (!handle.WaitOne(timeout, true))
				            throw new ApplicationException($"Could not launch tests on already running elasticsearch within {timeout}");

				        return Observable.Empty<ElasticsearchMessage>();
				    }
				}
			}

		    lock (_lock)
		    {
		        InstallPlugins();
		    }

			this._process = new ObservableProcess(this.Binary,
				$"-Des.cluster.name={this.ClusterName}",
				$"-Des.node.name={this.NodeName}",
				$"-Des.path.repo={this.RepositoryPath}",
				$"-Des.script.inline=on",
				$"-Des.script.indexed=on"
			);
			var observable = Observable.Using(() => this._process, process => process.Start())
				.Select(consoleLine => new ElasticsearchMessage(consoleLine));
			this._processListener = observable.Subscribe(onNext: s => HandleConsoleMessage(s, handle));

			if (!handle.WaitOne(timeout, true))
			{
				this.Stop();
				throw new ApplicationException($"Could not start elasticsearch within {timeout}");
			}

			return observable;
		}
		private void InstallPlugins()
		{
			var pluginBat = Path.Combine(this.RoamingClusterFolder, "bin", "plugin") + ".bat";
			foreach (var plugin in SupportedPlugins)
			{
				var installPath = plugin.Key;
				var localPath = plugin.Value;
				var pluginFolder = Path.Combine(this.RoamingClusterFolder, "plugins", localPath);

                if (!Directory.Exists(this.RoamingClusterFolder)) continue;

                // assume plugin already installed
			    if (Directory.Exists(pluginFolder)) continue;

				var timeout = TimeSpan.FromSeconds(60);
				var handle = new ManualResetEvent(false);
				Task.Run(() =>
				{
					using (var p = new ObservableProcess(pluginBat, "install", installPath))
					{
						var o = p.Start();
						o.Subscribe(Console.WriteLine,
							(e) =>
							{
								handle.Set();
								throw e;
							},
							() => handle.Set()
							);
					}
				});
				if (!handle.WaitOne(timeout, true))
					throw new ApplicationException($"Could not install ${installPath} within {timeout}");
			}
		}
		private void EnsureShieldAdmin()
		{
			if (!this._config.XPackEnabled) return;


			var folder = this.Version.Major >= 5 ? "x-pack" : "shield";
			var plugin = this.Version.Major >= 5 ? "users" : "esusers";

			EnsureRoles(folder);
			var timeout = TimeSpan.FromMinutes(1);

			var pluginBat = Path.Combine(this.ElasticsearchHome, "bin", folder, plugin) + ".bat";
			foreach (var cred in ShieldInformation.AllUsers)
			{
				var handle = new ManualResetEvent(false);
				Task.Run(() =>
				{
					using (var p = new ObservableProcess(pluginBat, $"useradd {cred.Username} -p {cred.Password} -r {cred.Role}"))
					{
						var o = p.Start();
						Console.WriteLine($"Calling: {pluginBat} useradd {cred.Username}");
						o.Subscribe(
							//c=>Console.WriteLine(c.Data),
							c => { },
							(e) =>
							{
								handle.Set();
								throw e;
							},
							() =>
							{
								handle.Set();
							});
						if (!handle.WaitOne(timeout, true))
							throw new Exception($"Could not add user {cred.Username} within {timeout}");
					}
				});
				if (!handle.WaitOne(timeout, true))
					throw new Exception($"Could not add user {cred.Username} within {timeout}");
			}
		}
		private void InstallPlugins()
		{
			foreach (var plugin in ElasticsearchPluginCollection.Supported.Where(plugin => plugin.IsValid(this.Version)))
			{
				var installParameter = plugin.InstallParamater(this.Version);
				var folder = plugin.FolderName;
				var pluginFolder = Path.Combine(this.ElasticsearchHome, "plugins", folder);

				if (!Directory.Exists(this.ElasticsearchHome)) continue;

				// assume plugin already installed
				if (Directory.Exists(pluginFolder)) continue;

				Console.WriteLine($"Installing elasticsearch plugin: {plugin.Moniker} ...");
				var timeout = TimeSpan.FromSeconds(120);
				var handle = new ManualResetEvent(false);
				Task.Run(() =>
				{
					using (var p = new ObservableProcess(this.PluginBinary, "install", installParameter))
					{
						var o = p.Start();
						Console.WriteLine($"Calling: {this.PluginBinary} install {installParameter}");
						o.Subscribe(c=>Console.WriteLine(c.Data),
							(e) =>
							{
								Console.WriteLine($"Failed installing elasticsearch plugin: {plugin.Moniker}");
								handle.Set();
								throw e;
							},
							() =>
							{
								Console.WriteLine($"Finished installing elasticsearch plugin: {plugin.Moniker} exit code: {p.ExitCode}");
								handle.Set();
							});
						if (!handle.WaitOne(timeout, true))
							throw new Exception($"Could not install {plugin.Moniker} within {timeout}");
					}
				});
				if (!handle.WaitOne(timeout, true))
					throw new Exception($"Could not install {plugin.Moniker} within {timeout}");
			}
		}