Пример #1
0
        public int RunModules(
            [Operand(Name = "module-names", Description = "The space-separated list of module names.")] List <string> moduleNames,
            [Option(LongName = "param", ShortName = "p")] List <string> moduleSettingOverrides)
        {
            var commandContext = CommandLineHandler.Context;

            if (moduleNames == null || moduleNames.Count == 0)
            {
                CommandLineHandler.DisplayHelp("run modules");
                return((int)ExecutionResult.HostArgumentError);
            }

            var modules = new List <Module>();

            foreach (var moduleName in moduleNames)
            {
                commandContext.Logger.Information("loading module {Module}", moduleName);

                var loadResult = ModuleLoader.LoadModule(commandContext, moduleName, moduleSettingOverrides?.ToArray(), null, false, out var module);
                if (loadResult != ExecutionResult.Success)
                {
                    return((int)loadResult);
                }

                if (module.EnabledPlugins?.Count == 0)
                {
                    commandContext.Logger.Warning("skipping module {Module} due to it has no enabled plugins", moduleName);
                    ModuleLoader.UnloadModule(commandContext, module);
                    continue;
                }

                modules.Add(module);
            }

            var executionResult = ModuleExecuter.Execute(commandContext, modules.ToArray());

            foreach (var module in modules)
            {
                ModuleLoader.UnloadModule(commandContext, module);
            }

            return((int)executionResult);
        }
Пример #2
0
        public int ValidateModule(
            [Operand(Name = "names", Description = "The space-separated list of module names.")] List <string> moduleNames,
            [Option(LongName = "all", ShortName = "a")] bool all)
        {
            if (moduleNames == null || moduleNames.Count == 0)
            {
                if (!all)
                {
                    CommandLineHandler.DisplayHelp("test modules");
                    return((int)ExecutionResult.HostArgumentError);
                }
            }
            else if (all)
            {
                CommandLineHandler.DisplayHelp("test modules");
                return((int)ExecutionResult.HostArgumentError);
            }

            var commandContext = CommandLineHandler.Context;

            if (all)
            {
                moduleNames = ModuleLister.GetAllModules(commandContext);
            }

            foreach (var moduleName in moduleNames)
            {
                commandContext.Logger.Information("loading module {Module}", moduleName);

                ModuleLoader.LoadModule(commandContext, moduleName, null, null, true, out var module);
                if (module != null)
                {
                    ModuleLoader.UnloadModule(commandContext, module);
                    commandContext.Logger.Information("validation {ValidationResult} for {Module}", "PASSED", moduleName);
                }
                else
                {
                    commandContext.Logger.Information("validation {ValidationResult} for {Module}", "FAILED", moduleName);
                }
            }

            return((int)ExecutionResult.Success);
        }
Пример #3
0
        public void ProtectSecret([Operand(Name = "secret", Description = "The secret to protect.")] string secret)
        {
            var commandContext = CommandLineHandler.Context;

            if (commandContext.HostConfiguration.SecretProtector == null)
            {
                commandContext.Logger.Write(LogEventLevel.Fatal, "secret protector is not set in {HostConfigurationFileName}", PathHelpers.GetFriendlyPathName(commandContext.LoadedConfigurationFileName));
                return;
            }

            if (string.IsNullOrEmpty(secret))
            {
                CommandLineHandler.DisplayHelp("protect");
                return;
            }

            var protectedSecret = commandContext.HostConfiguration.SecretProtector.Encrypt(secret);

            Console.WriteLine("The protected secret is:");
            Console.WriteLine("-------------");
            Console.WriteLine(protectedSecret);
            Console.WriteLine("-------------");
        }
Пример #4
0
        public void ValidateConnectionStrings(
            [Operand(Name = "names", Description = "The space-separated list of module names.")] List <string> moduleNames,
            [Option(LongName = "all", ShortName = "a")] bool all)
        {
            if (moduleNames == null || moduleNames.Count == 0)
            {
                if (!all)
                {
                    CommandLineHandler.DisplayHelp("test connection-strings");
                    return;
                }
            }
            else if (all)
            {
                CommandLineHandler.DisplayHelp("test connection-strings");
                return;
            }

            var commandContext = CommandLineHandler.Context;

            if (all)
            {
                moduleNames = ModuleLister.GetAllModules(commandContext);
            }

            var allConnectionStrings = new List <NamedConnectionString>();
            var index = 0;

            foreach (var moduleName in moduleNames)
            {
                var moduleConfiguration = ModuleConfigurationLoader.LoadModuleConfiguration(commandContext, moduleName, null, null);
                if (moduleConfiguration == null)
                {
                    continue;
                }

                if (index == 0)
                {
                    var sharedCs = new ConnectionStringCollection();
                    sharedCs.LoadFromConfiguration(moduleConfiguration.Configuration, "ConnectionStrings:Shared", commandContext.HostConfiguration.SecretProtector);
                    if (sharedCs.All.Any())
                    {
                        commandContext.Logger.Information("connection strings for: {Module}", "Shared");
                        foreach (var cs in sharedCs.All)
                        {
                            commandContext.Logger.Information("\t{ConnectionStringName} ({Provider})", cs.Name, cs.GetFriendlyProviderName());
                            allConnectionStrings.Add(cs);
                        }
                    }
                }

                commandContext.Logger.Information("connection strings for: {Module}", moduleName);

                var connectionStrings = new ConnectionStringCollection();
                connectionStrings.LoadFromConfiguration(moduleConfiguration.Configuration, "ConnectionStrings:Module", commandContext.HostConfiguration.SecretProtector);
                foreach (var cs in connectionStrings.All)
                {
                    commandContext.Logger.Information("\t{ConnectionStringName} ({Provider})", cs.Name, cs.GetFriendlyProviderName());
                    allConnectionStrings.RemoveAll(x => x.Name == cs.Name);
                    allConnectionStrings.Add(cs);
                }

                index++;
            }

            commandContext.Logger.Information("relevant connection strings");
            var originalNames = allConnectionStrings
                                .Select(x => x.Name.Split('-')[0])
                                .Distinct()
                                .ToList();

            foreach (var originalName in originalNames)
            {
                var connectionString = allConnectionStrings.Find(x => string.Equals(x.Name, originalName + "-" + Environment.MachineName, StringComparison.InvariantCultureIgnoreCase))
                                       ?? allConnectionStrings.Find(x => string.Equals(x.Name, originalName, StringComparison.InvariantCultureIgnoreCase));

                var knownFields = connectionString.GetKnownConnectionStringFields();
                if (knownFields == null)
                {
                    commandContext.Logger.Information("\ttesting: {ConnectionStringName} ({Provider})",
                                                      connectionString.Name, connectionString.GetFriendlyProviderName());
                }
                else
                {
                    var message = "\ttesting: {ConnectionStringName} ({Provider})";
                    var args    = new List <object>()
                    {
                        connectionString.Name,
                        connectionString.GetFriendlyProviderName(),
                    };

                    if (knownFields.Server != null)
                    {
                        message += ", server: {Server}";
                        args.Add(knownFields.Server);
                    }

                    if (knownFields.Port != null)
                    {
                        message += ", port: {Port}";
                        args.Add(knownFields.Port);
                    }

                    if (knownFields.Database != null)
                    {
                        message += ", database: {Database}";
                        args.Add(knownFields.Database);
                    }

                    if (knownFields.IntegratedSecurity != null)
                    {
                        message += ", integrated security: {IntegratedSecurity}";
                        args.Add(knownFields.IntegratedSecurity);
                    }

                    if (knownFields.UserId != null)
                    {
                        message += ", user: {UserId}";
                        args.Add(knownFields.UserId);
                    }

                    commandContext.Logger.Information(message, args.ToArray());
                }

                try
                {
                    EtlConnectionManager.TestConnection(connectionString);
                    commandContext.Logger.Information("\t\tPASSED");
                }
                catch (Exception ex)
                {
                    commandContext.Logger.Write(LogEventLevel.Fatal, "\t\t{ErrorMessage}", ex.FormatExceptionWithDetails(false));
                }
            }

            commandContext.Logger.Information("connection string test(s) finished");
        }