private static string UseHttpPluginLocation(IConsoleLineWriter writer, INodeFileSystem fileSystem, ElasticsearchPlugin plugin, ElasticsearchVersion v)
        {
            var downloadLocation = Path.Combine(fileSystem.LocalFolder, $"{plugin.FolderName}-{v}.zip");

            DownloadPluginSnapshot(writer, downloadLocation, plugin, v);
            //transform downloadLocation to file uri and use that to install from
            return(new Uri(downloadLocation).AbsoluteUri);
        }
 private static void DeleteDirectory(IConsoleLineWriter w, string description, string path)
 {
     if (!Directory.Exists(path))
     {
         return;
     }
     w.WriteDiagnostic($"{{{nameof(CleanUpDirectoriesAfterNodeStopped)}}} attempting to delete [{description}]: {{{path}}}");
     Directory.Delete(path, true);
 }
        // TODO: Remove when https://github.com/elastic/elasticsearch-net-abstractions/pull/17 is published
        private static void ExecuteBinary(EphemeralClusterConfiguration config, IConsoleLineWriter writer, string binary, string description,
                                          StartedHandler startedHandler, params string[] arguments
                                          )
        {
            var command = $"{{{binary}}} {{{string.Join(" ", arguments)}}}";

            writer?.WriteDiagnostic($"{{{nameof(ExecuteBinary)}}} starting process [{description}] {command}");

            var timeout = TimeSpan.FromSeconds(420);
            var processStartArguments = new StartArguments(binary, arguments)
            {
                Environment = new Dictionary <string, string>
                {
                    { config.FileSystem.ConfigEnvironmentVariableName, config.FileSystem.ConfigPath },
                    { "ES_HOME", config.FileSystem.ElasticsearchHome }
                }
            };

            var result = Proc.Start(processStartArguments, timeout, new ConsoleOutWriter(), startedHandler);

            if (!result.Completed)
            {
                throw new Exception($"Timeout while executing {description} exceeded {timeout}");
            }

            if (result.ExitCode != 0)
            {
                throw new Exception($"Expected exit code 0 but received ({result.ExitCode}) while executing {description}: {command}");
            }

            var errorOut = result.ConsoleOut.Where(c => c.Error).ToList();

            // this manifested when calling certgen on versions smaller then 5.2.0
            if (errorOut.Any() && config.Version < "5.2.0")
            {
                errorOut = errorOut.Where(e => !e.Line.Contains("No log4j2 configuration file found")).ToList();
            }
            if (errorOut.Any(e => !string.IsNullOrWhiteSpace(e.Line)))
            {
                throw new Exception($"Received error out with exitCode ({result.ExitCode}) while executing {description}: {command}");
            }

            writer?.WriteDiagnostic($"{{{nameof(ExecuteBinary)}}} finished process [{description}] {{{result.ExitCode}}}");
        }
        private static void DownloadPluginSnapshot(IConsoleLineWriter writer, string downloadLocation, ElasticsearchPlugin plugin, ElasticsearchVersion v)
        {
            if (File.Exists(downloadLocation))
            {
                return;
            }
            var downloadUrl = plugin.DownloadUrl(v);

            writer?.WriteDiagnostic($"{{{nameof(DownloadPluginSnapshot)}}} downloading [{plugin.FolderName}] from {{{downloadUrl}}}");
            try
            {
                DownloadFile(downloadUrl, downloadLocation);
                writer?.WriteDiagnostic($"{{{nameof(DownloadPluginSnapshot)}}} downloaded [{plugin.FolderName}] to {{{downloadLocation}}}");
            }
            catch (Exception)
            {
                writer?.WriteDiagnostic($"{{{nameof(DownloadPluginSnapshot)}}} download failed! [{plugin.FolderName}] from {{{downloadUrl}}}");
                throw;
            }
        }
예제 #5
0
        protected static void ExecuteBinary(EphemeralClusterConfiguration config, IConsoleLineWriter writer, string binary, string description, params string[] arguments)
        {
            var command = $"{{{binary}}} {{{string.Join(" ", arguments)}}}";

            writer?.WriteDiagnostic($"{{{nameof(ExecuteBinary)}}} starting process [{description}] {command}");

            var timeout = TimeSpan.FromSeconds(420);
            var processStartArguments = new StartArguments(binary, arguments)
            {
                Environment = new Dictionary <string, string>
                {
                    { "ES_PATH_CONF", config.FileSystem.ConfigPath },
                    { "ES_HOME", config.FileSystem.ElasticsearchHome }
                }
            };
            var result = Proc.Start(processStartArguments, timeout, new ConsoleOutColorWriter());

            if (!result.Completed)
            {
                throw new Exception($"Timeout while executing {description} exceeded {timeout}");
            }

            if (result.ExitCode != 0)
            {
                throw new Exception($"Expected exit code 0 but recieved ({result.ExitCode}) while executing {description}: {command}");
            }

            var errorOut = result.ConsoleOut.Where(c => c.Error).ToList();

            if (errorOut.Any())
            {
                throw new Exception($"Recieved error out with exitCode ({result.ExitCode}) while executing {description}: {command}");
            }

            writer?.WriteDiagnostic($"{{{nameof(ExecuteBinary)}}} finished process [{description}] {{{result.ExitCode}}}");
        }
        private void GenerateUnusedCertificates(EphemeralClusterConfiguration config, string silentModeConfigFile, IConsoleLineWriter writer)
        {
            var name        = config.FileSystem.UnusedCertificateFolderName;
            var zipLocation = Path.Combine(config.FileSystem.ConfigPath, "x-pack", name) + ".zip";
            var @out        = config.Version.Major < 6 ? $"{name}.zip" : zipLocation;

            if (!File.Exists(config.FileSystem.UnusedCaCertificate))
            {
                ExecuteBinary(config, writer, config.FileSystem.CertGenBinary, "generating ssl certificates for this session",
                              "-in", silentModeConfigFile, "-out", @out);
            }

            if (Directory.Exists(config.FileSystem.UnusedCertificatesPath))
            {
                return;
            }
            Directory.CreateDirectory(config.FileSystem.UnusedCertificatesPath);
            ZipFile.ExtractToDirectory(zipLocation, config.FileSystem.UnusedCertificatesPath);
        }
예제 #7
0
        private static void GenerateUnusedCertificates(IEphemeralCluster <EphemeralClusterConfiguration> cluster, string silentModeConfigFile, IConsoleLineWriter writer)
        {
            var config = cluster.ClusterConfiguration;
            var name   = config.FileSystem.UnusedCertificateFolderName;
            var path   = config.FileSystem.UnusedCertificatesPath;

            NewOrCachedCertificates(cluster, name, path, silentModeConfigFile, writer);
        }
예제 #8
0
        private static void UnpackCertificatesZip(string zipLocation, string outFolder, IConsoleLineWriter writer)
        {
            if (Directory.Exists(outFolder))
            {
                return;
            }

            writer.WriteDiagnostic($"{{{nameof(GenerateCertificatesTask)}}} unzipping certificates to {outFolder}");
            Directory.CreateDirectory(outFolder);
            ZipFile.ExtractToDirectory(zipLocation, outFolder);
        }
예제 #9
0
        private static void GenerateCertificate(EphemeralClusterConfiguration config, string name, string path, string zipLocation, string silentModeConfigFile, IConsoleLineWriter writer)
        {
            var @out   = config.Version.Major < 6 ? $"{name}.zip" : zipLocation;
            var fs     = config.FileSystem;
            var binary = config.Version >= "6.3.0"
                                ? Path.Combine(fs.ElasticsearchHome, "bin", "elasticsearch-certgen") + BinarySuffix
                                : Path.Combine(fs.ElasticsearchHome, "bin", "x-pack", "certgen") + BinarySuffix;


            if (!Directory.Exists(path))
            {
                ExecuteBinary(config, writer, binary, "generating ssl certificates for this session",
                              "-in", silentModeConfigFile, "-out", @out);
            }
            if (config.Version.Major < 6)
            {
                var badLocation = Path.Combine(config.FileSystem.ElasticsearchHome, "config", "x-pack", @out);
                writer.WriteDiagnostic($"{{{nameof(GenerateCertificatesTask)}}} moving {badLocation} to {@out}");
                File.Move(badLocation, zipLocation);
            }
        }
예제 #10
0
        private static void NewOrCachedCertificates(IEphemeralCluster <EphemeralClusterConfiguration> cluster, string name, string path, string silentModeConfigFile, IConsoleLineWriter writer)
        {
            var config             = cluster.ClusterConfiguration;
            var cachedEsHomeFolder = Path.Combine(config.FileSystem.LocalFolder, cluster.GetCacheFolderName());
            var zipLocationCache   = Path.Combine(cachedEsHomeFolder, name) + ".zip";

            if (File.Exists(zipLocationCache))
            {
                writer.WriteDiagnostic($"{{{nameof(GenerateCertificatesTask)}}} using cached certificates from {zipLocationCache}");
                UnpackCertificatesZip(zipLocationCache, path, writer);
                return;
            }

            var zipLocation = config.Version >= "6.3.0"
                                ? Path.Combine(config.FileSystem.ConfigPath, name) + ".zip"
                                : Path.Combine(config.FileSystem.ConfigPath, "x-pack", name) + ".zip";

            GenerateCertificate(config, name, path, zipLocation, silentModeConfigFile, writer);

            if (!File.Exists(zipLocationCache))
            {
                writer.WriteDiagnostic($"{{{nameof(GenerateCertificatesTask)}}} caching {zipLocation} in ES_HOME {zipLocationCache}");
                File.Copy(zipLocation, zipLocationCache);
            }

            UnpackCertificatesZip(zipLocation, path, writer);
        }
예제 #11
0
 public static void WriteError(this IConsoleLineWriter writer, string message, string node) => writer?.Write(Error(node != null ? $"[{node}] {message}" : message));
예제 #12
0
 public static void WriteError(this IConsoleLineWriter writer, string message) => writer.Write(Error(message));
예제 #13
0
 public static void WriteDiagnostic(this IConsoleLineWriter writer, string message, string node) => writer?.Write(Info(node != null ? $"[{node}] {message}" : message));
예제 #14
0
 public static void WriteDiagnostic(this IConsoleLineWriter writer, string message) => writer.Write(Info(message));