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"); }
internal static ModuleConfiguration LoadModuleConfiguration(CommandContext commandContext, string moduleName, string[] moduleSettingOverrides, string[] pluginListOverride) { var sharedFolder = Path.Combine(commandContext.HostConfiguration.ModulesFolder, "Shared"); var sharedConfigFileName = Path.Combine(sharedFolder, "shared-configuration.json"); var moduleFolder = Path.Combine(commandContext.HostConfiguration.ModulesFolder, moduleName); if (!Directory.Exists(moduleFolder)) { commandContext.Logger.Write(LogEventLevel.Fatal, "can't find the module folder: {Folder}", moduleFolder); commandContext.OpsLogger.Write(LogEventLevel.Fatal, "can't find the module folder: {Folder}", moduleFolder); return(null); } moduleFolder = Directory.GetDirectories(commandContext.HostConfiguration.ModulesFolder, moduleName, SearchOption.TopDirectoryOnly).FirstOrDefault(); moduleName = Path.GetFileName(moduleFolder); var configurationBuilder = new ConfigurationBuilder(); if (File.Exists(sharedConfigFileName)) { configurationBuilder.AddJsonFile(sharedConfigFileName); commandContext.Logger.Debug("using shared configuration file from {FileName}", PathHelpers.GetFriendlyPathName(sharedConfigFileName)); try { configurationBuilder.Build(); } catch (Exception ex) { throw new ConfigurationFileException(PathHelpers.GetFriendlyPathName(sharedConfigFileName), "can't read the configuration file", ex); } } var moduleConfigFileName = Path.Combine(moduleFolder, "module-configuration.json"); if (!File.Exists(moduleConfigFileName)) { commandContext.Logger.Write(LogEventLevel.Fatal, "can't find the module configuration file: {FileName}", moduleConfigFileName); commandContext.OpsLogger.Write(LogEventLevel.Fatal, "can't find the module configuration file: {FileName}", moduleConfigFileName); return(null); } configurationBuilder.AddJsonFile(moduleConfigFileName); commandContext.Logger.Debug("using module configuration file from {FileName}", PathHelpers.GetFriendlyPathName(moduleConfigFileName)); IConfigurationRoot configuration; try { configuration = configurationBuilder.Build(); AddCommandLineArgumentsToModuleConfiguration(configuration, moduleSettingOverrides); } catch (Exception ex) { throw new ConfigurationFileException(PathHelpers.GetFriendlyPathName(moduleConfigFileName), "can't read the configuration file", ex); } var pluginNamesToExecute = pluginListOverride; if (pluginNamesToExecute == null || pluginNamesToExecute.Length == 0) { pluginNamesToExecute = configuration.GetSection("Module:PluginsToExecute-" + Environment.MachineName).Get <string[]>(); } if (pluginNamesToExecute == null || pluginNamesToExecute.Length == 0) { pluginNamesToExecute = configuration.GetSection("Module:PluginsToExecute").Get <string[]>(); } var broken = false; foreach (var pluginName in pluginNamesToExecute.Where(x => x.Contains(',', StringComparison.InvariantCultureIgnoreCase) || x.Contains(' ', StringComparison.InvariantCultureIgnoreCase))) { commandContext.Logger.Write(LogEventLevel.Fatal, "plugin name can't contain comma or space character: [{Plugin}]", pluginName); broken = true; } if (broken) { return(null); } var allConnectionStrings = new List <NamedConnectionString>(); var sharedCs = new ConnectionStringCollection(); sharedCs.LoadFromConfiguration(configuration, "ConnectionStrings:Shared", commandContext.HostConfiguration.SecretProtector); foreach (var cs in sharedCs.All) { allConnectionStrings.Add(cs); } var moduleCs = new ConnectionStringCollection(); moduleCs.LoadFromConfiguration(configuration, "ConnectionStrings:Module", commandContext.HostConfiguration.SecretProtector); foreach (var cs in moduleCs.All) { allConnectionStrings.RemoveAll(x => x.Name == cs.Name); allConnectionStrings.Add(cs); } var relevantConnectionStrings = new ConnectionStringCollection(); if (allConnectionStrings?.Count > 0) { 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)); relevantConnectionStrings.Add(connectionString); } } return(new ModuleConfiguration() { ModuleName = moduleName, ConfigurationFileName = moduleConfigFileName, ModuleFolder = moduleFolder, Configuration = configuration, ConnectionStrings = relevantConnectionStrings, EnabledPluginList = pluginNamesToExecute .Select(name => name.Trim()) .Where(name => !name.StartsWith("!", StringComparison.InvariantCultureIgnoreCase)) .Where(plugin => plugin != null) .ToList(), SecretProtector = commandContext.HostConfiguration.SecretProtector, }); }