コード例 #1
0
        private static void CopyConfigDirectoryToHomeCacheConfigDirectory(IEphemeralCluster <EphemeralClusterConfiguration> cluster, ElasticsearchPlugin plugin)
        {
            if (plugin.SubProductName == "x-pack")
            {
                return;
            }
            if (!cluster.ClusterConfiguration.CacheEsHomeInstallation)
            {
                return;
            }
            var fs = cluster.FileSystem;
            var cachedEsHomeFolder = Path.Combine(fs.LocalFolder, cluster.GetCacheFolderName());
            var configTarget       = Path.Combine(cachedEsHomeFolder, "config");

            var configPluginPath       = Path.Combine(fs.ConfigPath, plugin.SubProductName);
            var configPluginPathCached = Path.Combine(configTarget, plugin.SubProductName);

            if (!Directory.Exists(configPluginPath) || Directory.Exists(configPluginPathCached))
            {
                return;
            }

            Directory.CreateDirectory(configPluginPathCached);
            CopyFolder(configPluginPath, configPluginPathCached);
        }
        private static void NewOrCachedCertificates(IEphemeralCluster <EphemeralClusterConfiguration> cluster, string name, string path, string silentModeConfigFile, IConsoleLineHandler 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);
        }
        public override void Run(IEphemeralCluster <EphemeralClusterConfiguration> cluster)
        {
            if (!cluster.ClusterConfiguration.CacheEsHomeInstallation)
            {
                return;
            }

            var fs = cluster.FileSystem;
            var cachedEsHomeFolder      = Path.Combine(fs.LocalFolder, cluster.GetCacheFolderName());
            var cachedelasticsearchYaml = Path.Combine(cachedEsHomeFolder, "config", "elasticsearch.yml");

            if (File.Exists(cachedelasticsearchYaml))
            {
                cluster.Writer?.WriteDiagnostic(
                    $"{{{nameof(CacheElasticsearchInstallation)}}} cached home already exists [{cachedEsHomeFolder}]");
                return;
            }

            var source = fs.ElasticsearchHome;
            var target = cachedEsHomeFolder;

            cluster.Writer?.WriteDiagnostic(
                $"{{{nameof(CacheElasticsearchInstallation)}}} caching {{{source}}} to [{target}]");
            CopyFolder(source, target, false);
        }
        public void Run(IEphemeralCluster <EphemeralClusterConfiguration> cluster, bool nodeStarted)
        {
            var fs = cluster.FileSystem;
            var w  = cluster.Writer;
            var v  = cluster.ClusterConfiguration.Version;

            DeleteDirectory(w, "cluster data", fs.DataPath);
            DeleteDirectory(w, "cluster config", fs.ConfigPath);
            DeleteDirectory(w, "cluster logs", fs.LogsPath);
            DeleteDirectory(w, "repositories", fs.RepositoryPath);
            var efs = fs as EphemeralFileSystem;

            if (!string.IsNullOrWhiteSpace(efs?.TempFolder))
            {
                DeleteDirectory(w, "cluster temp folder", efs.TempFolder);
            }

            if (efs != null)
            {
                var extractedFolder = Path.Combine(fs.LocalFolder, v.FolderInZip);
                if (extractedFolder != fs.ElasticsearchHome)
                {
                    DeleteDirectory(w, "ephemeral ES_HOME", fs.ElasticsearchHome);
                }
            }

            //if the node did not start make sure we delete the cached folder as we can not assume its in a good state
            if (cluster.ClusterConfiguration.CacheEsHomeInstallation && !nodeStarted)
            {
                var cachedEsHomeFolder = Path.Combine(fs.LocalFolder, cluster.GetCacheFolderName());
                DeleteDirectory(w, "cached installation - node failed to start", cachedEsHomeFolder);
            }
        }
        public void Run(IEphemeralCluster <EphemeralClusterConfiguration> cluster, bool nodeStarted)
        {
            var fs = cluster.FileSystem;
            var w  = cluster.Writer;
            var v  = cluster.ClusterConfiguration.Version;
            var a  = cluster.ClusterConfiguration.Artifact;

            if (cluster.ClusterConfiguration.NoCleanupAfterNodeStopped)
            {
                w.WriteDiagnostic(
                    $"{{{nameof(CleanUpDirectoriesAfterNodeStopped)}}} skipping cleanup as requested on cluster configuration");
                return;
            }

            DeleteDirectory(w, "cluster data", fs.DataPath);
            DeleteDirectory(w, "cluster config", fs.ConfigPath);
            DeleteDirectory(w, "cluster logs", fs.LogsPath);
            DeleteDirectory(w, "repositories", fs.RepositoryPath);
            var efs = fs as EphemeralFileSystem;

            if (!string.IsNullOrWhiteSpace(efs?.TempFolder))
            {
                DeleteDirectory(w, "cluster temp folder", efs.TempFolder);
            }

            if (efs != null)
            {
                var extractedFolder = Path.Combine(fs.LocalFolder, a.FolderInZip);
                if (extractedFolder != fs.ElasticsearchHome)
                {
                    DeleteDirectory(w, "ephemeral ES_HOME", fs.ElasticsearchHome);
                }
                //if the node was not started delete the cached extractedFolder
                if (!nodeStarted)
                {
                    DeleteDirectory(w, "cached extracted folder - node failed to start", extractedFolder);
                }
            }

            //if the node did not start make sure we delete the cached folder as we can not assume its in a good state
            var cachedEsHomeFolder = Path.Combine(fs.LocalFolder, cluster.GetCacheFolderName());

            if (cluster.ClusterConfiguration.CacheEsHomeInstallation && !nodeStarted)
            {
                DeleteDirectory(w, "cached installation - node failed to start", cachedEsHomeFolder);
            }
            else
            {
                w.WriteDiagnostic(
                    $"{{{nameof(CleanUpDirectoriesAfterNodeStopped)}}} Leaving [cached folder] on disk: {{{cachedEsHomeFolder}}}");
            }
        }
コード例 #6
0
        public override void Run(IEphemeralCluster <EphemeralClusterConfiguration> cluster)
        {
            var fs = cluster.FileSystem;

            var java8Home = Environment.GetEnvironmentVariable("JAVA8_HOME");

            if (cluster.ClusterConfiguration.Version < "6.0.0" && !string.IsNullOrWhiteSpace(java8Home))
            {
                //EnvironmentVariableTarget.Process only using this overload
                Environment.SetEnvironmentVariable("JAVA_HOME", java8Home);
                cluster.Writer?.WriteDiagnostic(
                    $"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} Forcing [JAVA8_HOME] as [JAVA_HOME] since we are on Elasticsearch <6.0.0");
            }

            var envVarName = cluster.ClusterConfiguration.JavaHomeEnvironmentVariable;
            var javaHome   = Environment.GetEnvironmentVariable(envVarName);

            //7.0.0 ships with its own JDK
            if (cluster.ClusterConfiguration.Version < "7.0.0" && string.IsNullOrWhiteSpace(javaHome))
            {
                cluster.Writer?.WriteDiagnostic(
                    $"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} [{envVarName}] is not SET exiting..");
                throw new Exception(
                          "The elasticsearch bat files are resilient to JAVA_HOME not being set, however the shield tooling is not");
            }

            var cachedEsHomeFolder = Path.Combine(fs.LocalFolder, cluster.GetCacheFolderName());
            var jdkFolder          = Path.Combine(cachedEsHomeFolder, "jdk");

            if (Directory.Exists(jdkFolder))
            {
                //prefer bundled jdk
                cluster.Writer?.WriteDiagnostic(
                    $"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} [{envVarName}] is set to bundled jdk: {{{jdkFolder}}} ");
                Environment.SetEnvironmentVariable("JAVA_HOME", jdkFolder);
            }
            else if (cluster.ClusterConfiguration.Version >= "7.0.0" && !string.IsNullOrWhiteSpace(javaHome))
            {
                cluster.Writer?.WriteDiagnostic(
                    $"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} [{envVarName}] is set; clearing value for process to prefer bundled jdk...");
                Environment.SetEnvironmentVariable(envVarName, null);
            }
            else
            {
                cluster.Writer?.WriteDiagnostic(
                    $"{{{nameof(EnsureJavaHomeEnvironmentVariableIsSet)}}} {envVarName} is not set proceeding or using default JDK");
            }
        }
        private static void CopyHomeConfigToEphemeralConfig(IEphemeralCluster <EphemeralClusterConfiguration> cluster, EphemeralFileSystem ephemeralFileSystem, INodeFileSystem fs)
        {
            var target                  = ephemeralFileSystem.ConfigPath;
            var cachedEsHomeFolder      = Path.Combine(fs.LocalFolder, cluster.GetCacheFolderName());
            var cachedElasticsearchYaml = Path.Combine(cachedEsHomeFolder, "config", "elasticsearch.yaml");

            var homeSource = cluster.ClusterConfiguration.CacheEsHomeInstallation && File.Exists(cachedElasticsearchYaml) ? cachedEsHomeFolder : fs.ElasticsearchHome;
            var source     = Path.Combine(homeSource, "config");

            if (!Directory.Exists(source))
            {
                cluster.Writer?.WriteDiagnostic($"{{{nameof(CreateEphemeralDirectory)}}} source config {{{source}}} does not exist nothing to copy");
                return;
            }

            cluster.Writer?.WriteDiagnostic($"{{{nameof(CreateEphemeralDirectory)}}} copying cached {{{source}}} as to [{target}]");
            CopyFolder(source, target);
        }
        public override void Run(IEphemeralCluster <EphemeralClusterConfiguration> cluster)
        {
            if (!cluster.ClusterConfiguration.CacheEsHomeInstallation)
            {
                return;
            }

            var fs = cluster.FileSystem;
            var cachedEsHomeFolder = Path.Combine(fs.LocalFolder, cluster.GetCacheFolderName());

            if (!Directory.Exists(cachedEsHomeFolder))
            {
                return;
            }

            var source = cachedEsHomeFolder;
            var target = fs.ElasticsearchHome;

            cluster.Writer?.WriteDiagnostic($"{{{nameof(CopyCachedEsInstallation)}}} using cached ES_HOME {{{source}}} and copying it to [{target}]");
            CopyFolder(source, target);
        }
        public override void Run(IEphemeralCluster <EphemeralClusterConfiguration> cluster)
        {
            if (!cluster.ClusterConfiguration.EnableSecurity)
            {
                return;
            }

            var config     = cluster.ClusterConfiguration;
            var fileSystem = cluster.FileSystem;
            var v          = config.Version;

            var xpackConfigFolder       = v >= "6.3.0" ? fileSystem.ConfigPath : Path.Combine(fileSystem.ConfigPath, "x-pack");;
            var xpackConfigFolderCached = v >= "6.3.0"
                                ? Path.Combine(fileSystem.LocalFolder, cluster.GetCacheFolderName(), "config")
                                : Path.Combine(fileSystem.LocalFolder, cluster.GetCacheFolderName(), "config", "x-pack");

            var usersFile            = Path.Combine(xpackConfigFolder, "users");
            var usersFileCached      = usersFile.Replace(xpackConfigFolder, xpackConfigFolderCached);
            var usersRolesFile       = Path.Combine(xpackConfigFolder, "users_roles");
            var usersRolesFileCached = usersRolesFile.Replace(xpackConfigFolder, xpackConfigFolderCached);

            if (File.Exists(usersFileCached))
            {
                cluster.Writer?.WriteDiagnostic($"{{{nameof(EnsureSecurityUsersInDefaultRealmAreAdded)}}} using cached users and users_roles files from {{{xpackConfigFolderCached}}}");
                if (!Directory.Exists(xpackConfigFolder))
                {
                    Directory.CreateDirectory(xpackConfigFolder);
                }
                if (!File.Exists(usersFile))
                {
                    File.Copy(usersFileCached, usersFile);
                }
                if (!File.Exists(usersRolesFile))
                {
                    File.Copy(usersRolesFileCached, usersRolesFile);
                }
            }
            else
            {
                var folder = v.Major >= 5
                                        ? v >= "6.3.0" ? string.Empty : "x-pack"
                                        : "shield";
                var binary = v.Major >= 5
                                        ? v >= "6.3.0" ? "elasticsearch-users" : "users"
                                        : "esusers";

                var h            = fileSystem.ElasticsearchHome;
                var pluginFolder = v >= "6.3.0" ? Path.Combine(h, "bin") : Path.Combine(h, "bin", folder);
                var pluginBat    = Path.Combine(pluginFolder, binary) + BinarySuffix;

                foreach (var cred in ClusterAuthentication.AllUsers)
                {
                    ExecuteBinary(cluster.ClusterConfiguration, cluster.Writer, pluginBat, $"adding user {cred.Username}", $"useradd {cred.Username} -p {cred.Password} -r {cred.Role}");
                }

                if (!Directory.Exists(xpackConfigFolderCached))
                {
                    Directory.CreateDirectory(xpackConfigFolderCached);
                }

                File.Copy(usersFile, usersFileCached);
                File.Copy(usersRolesFile, usersRolesFileCached);
            }
        }