From 77e6ff5a282b455ca35f38f5dfd3ef6b4ad5332d Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Wed, 6 Mar 2024 17:38:20 +0100 Subject: [PATCH 1/2] Align bootstrappers and move things into commands --- .../StartupModeTests.cs | 10 +- .../EndpointTemplates/DefaultServer.cs | 3 +- .../ServiceControlComponentRunner.cs | 6 +- .../ServiceControlComponentRunner.cs | 7 +- .../Infrastructure/When_instance_is_setup.cs | 7 +- .../Hosting/Commands/MaintCommand.cs | 9 -- .../Commands/MaintenanceModeCommand.cs} | 8 +- .../Hosting/Commands/SetupCommand.cs | 84 +++++++++++++- .../Infrastructure/Hosting/HostArguments.cs | 2 +- .../Infrastructure/SetupBootstrapper.cs | 94 --------------- .../EndpointTemplates/DefaultServer.cs | 2 +- .../ServiceControlComponentRunner.cs | 4 +- .../Hosting/Commands/SetupCommand.cs | 61 +++++++++- .../Infrastructure/SetupBootstrapper.cs | 76 ------------- .../Commands/MaintenanceModeCommand.cs | 6 +- .../Hosting/Commands/SetupCommand.cs | 92 ++++++++++++++- src/ServiceControl/MaintenanceBootstrapper.cs | 17 --- src/ServiceControl/SetupBootstrapper.cs | 107 ------------------ 18 files changed, 263 insertions(+), 332 deletions(-) delete mode 100644 src/ServiceControl.Audit/Infrastructure/Hosting/Commands/MaintCommand.cs rename src/ServiceControl.Audit/Infrastructure/{MaintenanceBootstrapper.cs => Hosting/Commands/MaintenanceModeCommand.cs} (82%) delete mode 100644 src/ServiceControl.Audit/Infrastructure/SetupBootstrapper.cs delete mode 100644 src/ServiceControl.Monitoring/Infrastructure/SetupBootstrapper.cs delete mode 100644 src/ServiceControl/MaintenanceBootstrapper.cs delete mode 100644 src/ServiceControl/SetupBootstrapper.cs diff --git a/src/ServiceControl.AcceptanceTests.RavenDB/StartupModeTests.cs b/src/ServiceControl.AcceptanceTests.RavenDB/StartupModeTests.cs index bdea8c92ed..3f08cd46a0 100644 --- a/src/ServiceControl.AcceptanceTests.RavenDB/StartupModeTests.cs +++ b/src/ServiceControl.AcceptanceTests.RavenDB/StartupModeTests.cs @@ -3,10 +3,12 @@ using System; using System.Threading.Tasks; using Hosting.Commands; + using Microsoft.Extensions.Hosting; using NServiceBus; using NUnit.Framework; using Particular.ServiceControl; using Particular.ServiceControl.Hosting; + using Persistence; using Persistence.RavenDB; using ServiceBus.Management.Infrastructure.Settings; using ServiceControl.AcceptanceTesting.InfrastructureConfig; @@ -41,10 +43,12 @@ public async Task InitializeSettings() [Test] public async Task CanRunMaintenanceMode() { - var bootstrapper = new MaintenanceBootstrapper(settings); - - var host = bootstrapper.HostBuilder.Build(); + // ideally we'd be using the MaintenanceModeCommand here but that indefinitely blocks due to the RunAsync + // not terminating. + var hostBuilder = Host.CreateApplicationBuilder(); + hostBuilder.Services.AddPersistence(settings, maintenanceMode: true); + using var host = hostBuilder.Build(); await host.StartAsync(); await host.StopAsync(); } diff --git a/src/ServiceControl.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs b/src/ServiceControl.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs index fb1493b96a..8847ec6ae0 100644 --- a/src/ServiceControl.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs +++ b/src/ServiceControl.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs @@ -7,12 +7,13 @@ using NServiceBus.AcceptanceTesting.Support; using NServiceBus.Features; using Particular.ServiceControl; + using Particular.ServiceControl.Commands; public class DefaultServer : IEndpointSetupTemplate { // TODO: Revisit the default server base having a bootstrapper reference public Task GetConfiguration(RunDescriptor runDescriptor, EndpointCustomizationConfiguration endpointConfiguration, Func configurationBuilderCustomization) => - new DefaultServerBase().GetConfiguration(runDescriptor, endpointConfiguration, async b => + new DefaultServerBase().GetConfiguration(runDescriptor, endpointConfiguration, async b => { b.DisableFeature(); diff --git a/src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs b/src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs index 7c655e25ef..d7d02c28e2 100644 --- a/src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs +++ b/src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs @@ -19,6 +19,8 @@ using NServiceBus.AcceptanceTesting.Support; using NServiceBus.Configuration.AdvancedExtensibility; using Particular.ServiceControl; + using Particular.ServiceControl.Commands; + using Particular.ServiceControl.Hosting; using RavenDB.Shared; using ServiceBus.Management.Infrastructure.Settings; @@ -95,8 +97,8 @@ async Task InitializeServiceControl(ScenarioContext context) using (new DiagnosticTimer($"Creating infrastructure for {instanceName}")) { - var setupBootstrapper = new SetupBootstrapper(settings); - await setupBootstrapper.Run(); + var setupCommand = new SetupCommand(); + await setupCommand.Execute(new HostArguments(Array.Empty()), settings); } var configuration = new EndpointConfiguration(instanceName); diff --git a/src/ServiceControl.Audit.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs b/src/ServiceControl.Audit.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs index df263aa1cd..fdd3bc9755 100644 --- a/src/ServiceControl.Audit.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs +++ b/src/ServiceControl.Audit.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs @@ -10,6 +10,8 @@ namespace ServiceControl.Audit.AcceptanceTests.TestSupport using AcceptanceTesting; using Auditing; using Infrastructure; + using Infrastructure.Hosting; + using Infrastructure.Hosting.Commands; using Infrastructure.Settings; using Infrastructure.WebApi; using Microsoft.AspNetCore.Builder; @@ -101,9 +103,8 @@ async Task InitializeServiceControl(ScenarioContext context) using (new DiagnosticTimer($"Creating infrastructure for {instanceName}")) { - - var setupBootstrapper = new SetupBootstrapper(settings); - await setupBootstrapper.Run(); + var setupCommand = new SetupCommand(); + await setupCommand.Execute(new HostArguments(Array.Empty()), settings); } var configuration = new EndpointConfiguration(instanceName); diff --git a/src/ServiceControl.Audit.UnitTests/Infrastructure/When_instance_is_setup.cs b/src/ServiceControl.Audit.UnitTests/Infrastructure/When_instance_is_setup.cs index 3217c28a44..946384e43d 100644 --- a/src/ServiceControl.Audit.UnitTests/Infrastructure/When_instance_is_setup.cs +++ b/src/ServiceControl.Audit.UnitTests/Infrastructure/When_instance_is_setup.cs @@ -5,6 +5,8 @@ using System.Threading; using System.Threading.Tasks; using Audit.Infrastructure; + using Audit.Infrastructure.Hosting; + using Audit.Infrastructure.Hosting.Commands; using Audit.Infrastructure.Settings; using Microsoft.Extensions.DependencyInjection; using NServiceBus; @@ -25,9 +27,8 @@ public async Task Should_provision_queues() ForwardAuditMessages = true, }; - var setupBootstrapper = new SetupBootstrapper(settings); - - await setupBootstrapper.Run(); + var setupCommand = new SetupCommand(); + await setupCommand.Execute(new HostArguments(Array.Empty()), settings); CollectionAssert.AreEquivalent(new[] { diff --git a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/MaintCommand.cs b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/MaintCommand.cs deleted file mode 100644 index 528e5c7105..0000000000 --- a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/MaintCommand.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace ServiceControl.Audit.Infrastructure.Hosting.Commands -{ - using System.Threading.Tasks; - - class MaintCommand : AbstractCommand - { - public override Task Execute(HostArguments args, Settings.Settings settings) => MaintenanceBootstrapper.Run(settings); - } -} \ No newline at end of file diff --git a/src/ServiceControl.Audit/Infrastructure/MaintenanceBootstrapper.cs b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/MaintenanceModeCommand.cs similarity index 82% rename from src/ServiceControl.Audit/Infrastructure/MaintenanceBootstrapper.cs rename to src/ServiceControl.Audit/Infrastructure/Hosting/Commands/MaintenanceModeCommand.cs index f40173a7c1..2fdd24a2fa 100644 --- a/src/ServiceControl.Audit/Infrastructure/MaintenanceBootstrapper.cs +++ b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/MaintenanceModeCommand.cs @@ -1,14 +1,14 @@ -namespace ServiceControl.Audit.Infrastructure +namespace ServiceControl.Audit.Infrastructure.Hosting.Commands { using System; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting.WindowsServices; - using ServiceControl.Audit.Persistence; + using Persistence; - static class MaintenanceBootstrapper + class MaintenanceModeCommand : AbstractCommand { - public static async Task Run(Settings.Settings settings) + public override async Task Execute(HostArguments args, Settings.Settings settings) { var persistenceConfiguration = PersistenceConfigurationFactory.LoadPersistenceConfiguration(settings.PersistenceType); var persistenceSettings = persistenceConfiguration.BuildPersistenceSettings(settings); diff --git a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/SetupCommand.cs b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/SetupCommand.cs index 09127cc8fa..0e5bae0b47 100644 --- a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/SetupCommand.cs +++ b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/SetupCommand.cs @@ -1,7 +1,12 @@ namespace ServiceControl.Audit.Infrastructure.Hosting.Commands { + using System.Collections.Generic; using System.Threading.Tasks; + using LicenseManagement; + using NServiceBus.Logging; + using Persistence; using Settings; + using Transports; class SetupCommand : AbstractCommand { @@ -9,7 +14,84 @@ public override async Task Execute(HostArguments args, Settings settings) { settings.SkipQueueCreation = args.SkipQueueCreation; - await new SetupBootstrapper(settings).Run(); + // Validate license: + if (!ValidateLicense(settings)) + { + return; + } + + var transportSettings = MapSettings(settings); + var transportCustomization = settings.LoadTransportCustomization(); + + if (settings.IngestAuditMessages) + { + if (settings.SkipQueueCreation) + { + Logger.Info("Skipping queue creation"); + } + else + { + var additionalQueues = new List { settings.AuditQueue }; + + if (settings.ForwardAuditMessages && settings.AuditLogQueue != null) + { + additionalQueues.Add(settings.AuditLogQueue); + } + + await transportCustomization.ProvisionQueues(transportSettings, additionalQueues); + } + } + + EventSource.Create(); + + var persistenceConfiguration = PersistenceConfigurationFactory.LoadPersistenceConfiguration(settings.PersistenceType); + var persistenceSettings = persistenceConfiguration.BuildPersistenceSettings(settings); + var persistence = persistenceConfiguration.Create(persistenceSettings); + var installer = persistence.CreateInstaller(); + + await installer.Install(); + } + + bool ValidateLicense(Settings settings) + { + if (!string.IsNullOrWhiteSpace(settings.LicenseFileText)) + { + if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText)) + { + Logger.Error(errorMessageForLicenseText); + return false; + } + + if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage)) + { + Logger.Error(importErrorMessage); + return false; + } + } + else + { + var license = LicenseManager.FindLicense(); + if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense)) + { + Logger.Error(errorMessageForFoundLicense); + return false; + } + } + + return true; } + + static TransportSettings MapSettings(Settings settings) + { + var transportSettings = new TransportSettings + { + EndpointName = settings.ServiceName, + ConnectionString = settings.TransportConnectionString, + MaxConcurrency = settings.MaximumConcurrencyLevel + }; + return transportSettings; + } + + static readonly ILog Logger = LogManager.GetLogger(); } } \ No newline at end of file diff --git a/src/ServiceControl.Audit/Infrastructure/Hosting/HostArguments.cs b/src/ServiceControl.Audit/Infrastructure/Hosting/HostArguments.cs index f93156a7ce..b1be5917dc 100644 --- a/src/ServiceControl.Audit/Infrastructure/Hosting/HostArguments.cs +++ b/src/ServiceControl.Audit/Infrastructure/Hosting/HostArguments.cs @@ -41,7 +41,7 @@ public HostArguments(string[] args) { Commands = [ - typeof(MaintCommand) + typeof(MaintenanceModeCommand) ]; executionMode = ExecutionMode.Maintenance; } diff --git a/src/ServiceControl.Audit/Infrastructure/SetupBootstrapper.cs b/src/ServiceControl.Audit/Infrastructure/SetupBootstrapper.cs deleted file mode 100644 index 073a0404ca..0000000000 --- a/src/ServiceControl.Audit/Infrastructure/SetupBootstrapper.cs +++ /dev/null @@ -1,94 +0,0 @@ -namespace ServiceControl.Audit.Infrastructure -{ - using System.Collections.Generic; - using System.Threading.Tasks; - using LicenseManagement; - using NServiceBus.Logging; - using ServiceControl.Audit.Persistence; - using Transports; - - class SetupBootstrapper(Settings.Settings settings) - { - public async Task Run() - { - // Validate license: - if (!ValidateLicense(settings)) - { - return; - } - - var transportSettings = MapSettings(settings); - var transportCustomization = settings.LoadTransportCustomization(); - - if (settings.IngestAuditMessages) - { - if (settings.SkipQueueCreation) - { - log.Info("Skipping queue creation"); - } - else - { - var additionalQueues = new List { settings.AuditQueue }; - - if (settings.ForwardAuditMessages && settings.AuditLogQueue != null) - { - additionalQueues.Add(settings.AuditLogQueue); - } - - await transportCustomization.ProvisionQueues(transportSettings, additionalQueues); - } - } - - EventSource.Create(); - - var persistenceConfiguration = PersistenceConfigurationFactory.LoadPersistenceConfiguration(settings.PersistenceType); - var persistenceSettings = persistenceConfiguration.BuildPersistenceSettings(settings); - var persistence = persistenceConfiguration.Create(persistenceSettings); - var installer = persistence.CreateInstaller(); - - await installer.Install(); - } - - bool ValidateLicense(Settings.Settings settings) - { - if (!string.IsNullOrWhiteSpace(settings.LicenseFileText)) - { - if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText)) - { - log.Error(errorMessageForLicenseText); - return false; - } - - if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage)) - { - log.Error(importErrorMessage); - return false; - } - } - else - { - var license = LicenseManager.FindLicense(); - if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense)) - { - log.Error(errorMessageForFoundLicense); - return false; - } - } - - return true; - } - - static TransportSettings MapSettings(Settings.Settings settings) - { - var transportSettings = new TransportSettings - { - EndpointName = settings.ServiceName, - ConnectionString = settings.TransportConnectionString, - MaxConcurrency = settings.MaximumConcurrencyLevel - }; - return transportSettings; - } - - static ILog log = LogManager.GetLogger(); - } -} \ No newline at end of file diff --git a/src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs b/src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs index 6ca5cf401f..58aee88eca 100644 --- a/src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs +++ b/src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs @@ -13,7 +13,7 @@ public class DefaultServer : IEndpointSetupTemplate { // TODO: Revisit the default server base having a bootstrapper reference public Task GetConfiguration(RunDescriptor runDescriptor, EndpointCustomizationConfiguration endpointConfiguration, Func configurationBuilderCustomization) => - new DefaultServerBase(new ConfigureEndpointLearningTransport()).GetConfiguration(runDescriptor, endpointConfiguration, async b => + new DefaultServerBase(new ConfigureEndpointLearningTransport()).GetConfiguration(runDescriptor, endpointConfiguration, async b => { b.DisableFeature(); diff --git a/src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs b/src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs index fed8cf7b28..57a4aab817 100644 --- a/src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs +++ b/src/ServiceControl.Monitoring.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs @@ -80,8 +80,8 @@ async Task InitializeServiceControl(ScenarioContext context) using (new DiagnosticTimer($"Creating infrastructure for {instanceName}")) { - var setupBootstrapper = new SetupBootstrapper(settings); - await setupBootstrapper.Run(); + var setupCommand = new SetupCommand(); + await setupCommand.Execute(settings); } var configuration = new EndpointConfiguration(instanceName); diff --git a/src/ServiceControl.Monitoring/Hosting/Commands/SetupCommand.cs b/src/ServiceControl.Monitoring/Hosting/Commands/SetupCommand.cs index 59d15965ca..e5cff36cef 100644 --- a/src/ServiceControl.Monitoring/Hosting/Commands/SetupCommand.cs +++ b/src/ServiceControl.Monitoring/Hosting/Commands/SetupCommand.cs @@ -2,13 +2,68 @@ namespace ServiceControl.Monitoring { using System.Threading.Tasks; using Infrastructure; + using LicenseManagement; + using NServiceBus.Logging; + using Transports; class SetupCommand : AbstractCommand { - public override async Task Execute(Settings settings) + public override Task Execute(Settings settings) { - await new SetupBootstrapper(settings) - .Run(); + if (!ValidateLicense(settings)) + { + return Task.CompletedTask; + } + + if (settings.SkipQueueCreation) + { + Logger.Info("Skipping queue creation"); + return Task.CompletedTask; + } + + var transportCustomization = settings.LoadTransportCustomization(); + + var transportSettings = new TransportSettings + { + RunCustomChecks = false, + ConnectionString = settings.ConnectionString, + EndpointName = settings.EndpointName, + ErrorQueue = settings.ErrorQueue, + MaxConcurrency = settings.MaximumConcurrencyLevel + }; + + return transportCustomization.ProvisionQueues(transportSettings, []); + } + + bool ValidateLicense(Monitoring.Settings settings) + { + if (!string.IsNullOrWhiteSpace(settings.LicenseFileText)) + { + if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText)) + { + Logger.Error(errorMessageForLicenseText); + return false; + } + + if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage)) + { + Logger.Error(importErrorMessage); + return false; + } + } + else + { + var license = LicenseManager.FindLicense(); + if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense)) + { + Logger.Error(errorMessageForFoundLicense); + return false; + } + } + + return true; } + + static ILog Logger = LogManager.GetLogger(); } } \ No newline at end of file diff --git a/src/ServiceControl.Monitoring/Infrastructure/SetupBootstrapper.cs b/src/ServiceControl.Monitoring/Infrastructure/SetupBootstrapper.cs deleted file mode 100644 index de1034678c..0000000000 --- a/src/ServiceControl.Monitoring/Infrastructure/SetupBootstrapper.cs +++ /dev/null @@ -1,76 +0,0 @@ -namespace ServiceControl.Monitoring.Infrastructure -{ - using System.Collections.Generic; - using System.Threading.Tasks; - using LicenseManagement; - using NServiceBus.Logging; - using ServiceControl.Transports; - - class SetupBootstrapper - { - public SetupBootstrapper(Monitoring.Settings settings) - { - this.settings = settings; - } - - public Task Run() - { - if (!ValidateLicense(settings)) - { - return Task.CompletedTask; - } - - if (settings.SkipQueueCreation) - { - Logger.Info("Skipping queue creation"); - return Task.CompletedTask; - } - - var transportCustomization = settings.LoadTransportCustomization(); - - var transportSettings = new TransportSettings - { - RunCustomChecks = false, - ConnectionString = settings.ConnectionString, - EndpointName = settings.EndpointName, - ErrorQueue = settings.ErrorQueue, - MaxConcurrency = settings.MaximumConcurrencyLevel - }; - - return transportCustomization.ProvisionQueues(transportSettings, []); - } - - bool ValidateLicense(Monitoring.Settings settings) - { - if (!string.IsNullOrWhiteSpace(settings.LicenseFileText)) - { - if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText)) - { - Logger.Error(errorMessageForLicenseText); - return false; - } - - if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage)) - { - Logger.Error(importErrorMessage); - return false; - } - } - else - { - var license = LicenseManager.FindLicense(); - if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense)) - { - Logger.Error(errorMessageForFoundLicense); - return false; - } - } - - return true; - } - - readonly Monitoring.Settings settings; - - static ILog Logger = LogManager.GetLogger(); - } -} \ No newline at end of file diff --git a/src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs b/src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs index 9f46f84bf1..7b8c215aee 100644 --- a/src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs +++ b/src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs @@ -7,16 +7,16 @@ using Particular.ServiceControl; using Particular.ServiceControl.Commands; using Particular.ServiceControl.Hosting; + using Persistence; using ServiceBus.Management.Infrastructure.Settings; class MaintenanceModeCommand : AbstractCommand { public override async Task Execute(HostArguments args, Settings settings) { - var bootstrapper = new MaintenanceBootstrapper(settings); - var hostBuilder = bootstrapper.HostBuilder; + var hostBuilder = Host.CreateApplicationBuilder(); + hostBuilder.Services.AddPersistence(settings, maintenanceMode: true); - // TODO: Move into the bootstrapper hostBuilder.Services.AddWindowsService(); if (WindowsServiceHelpers.IsWindowsService()) diff --git a/src/ServiceControl/Hosting/Commands/SetupCommand.cs b/src/ServiceControl/Hosting/Commands/SetupCommand.cs index fd61a420a5..dbaaf7c1b2 100644 --- a/src/ServiceControl/Hosting/Commands/SetupCommand.cs +++ b/src/ServiceControl/Hosting/Commands/SetupCommand.cs @@ -1,7 +1,13 @@ namespace Particular.ServiceControl.Commands { + using System.Runtime.InteropServices; using System.Threading.Tasks; + using global::ServiceControl.LicenseManagement; + using global::ServiceControl.Persistence; + using global::ServiceControl.Transports; using Hosting; + using NServiceBus.Logging; + using ServiceBus.Management.Infrastructure.Installers; using ServiceBus.Management.Infrastructure.Settings; class SetupCommand : AbstractCommand @@ -9,8 +15,90 @@ class SetupCommand : AbstractCommand public override async Task Execute(HostArguments args, Settings settings) { settings.SkipQueueCreation = args.SkipQueueCreation; - // TODO: Update to use the same pattern as the main Bootstrapper - await new SetupBootstrapper(settings).Run(); + + // Validate license: + if (!ValidateLicense(settings)) + { + return; + } + + var componentSetupContext = new ComponentInstallationContext(); + + foreach (ServiceControlComponent component in ServiceControlMainInstance.Components) + { + component.Setup(settings, componentSetupContext); + } + + foreach (var installationTask in componentSetupContext.InstallationTasks) + { + await installationTask(); + } + + var persistence = PersistenceFactory.Create(settings); + + var installer = persistence.CreateInstaller(); + + await installer.Install(); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + EventSourceCreator.Create(); + } + + if (settings.SkipQueueCreation) + { + Logger.Info("Skipping queue creation"); + } + else + { + var transportSettings = MapSettings(settings); + var transportCustomization = settings.LoadTransportCustomization(); + + await transportCustomization.ProvisionQueues(transportSettings, + componentSetupContext.Queues); + } } + + bool ValidateLicense(Settings settings) + { + if (!string.IsNullOrWhiteSpace(settings.LicenseFileText)) + { + if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText)) + { + Logger.Error(errorMessageForLicenseText); + return false; + } + + if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage)) + { + Logger.Error(importErrorMessage); + return false; + } + } + else + { + var license = LicenseManager.FindLicense(); + if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense)) + { + Logger.Error(errorMessageForFoundLicense); + return false; + } + } + + return true; + } + + static TransportSettings MapSettings(Settings settings) + { + var transportSettings = new TransportSettings + { + EndpointName = settings.ServiceName, + ConnectionString = settings.TransportConnectionString, + MaxConcurrency = settings.MaximumConcurrencyLevel + }; + return transportSettings; + } + + static readonly ILog Logger = LogManager.GetLogger(); } } \ No newline at end of file diff --git a/src/ServiceControl/MaintenanceBootstrapper.cs b/src/ServiceControl/MaintenanceBootstrapper.cs deleted file mode 100644 index 7abf4a88c2..0000000000 --- a/src/ServiceControl/MaintenanceBootstrapper.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Particular.ServiceControl -{ - using global::ServiceControl.Persistence; - using Microsoft.Extensions.Hosting; - using ServiceBus.Management.Infrastructure.Settings; - - class MaintenanceBootstrapper - { - public HostApplicationBuilder HostBuilder { get; set; } - - public MaintenanceBootstrapper(Settings settings) - { - HostBuilder = Host.CreateApplicationBuilder(); - HostBuilder.Services.AddPersistence(settings, maintenanceMode: true); - } - } -} \ No newline at end of file diff --git a/src/ServiceControl/SetupBootstrapper.cs b/src/ServiceControl/SetupBootstrapper.cs deleted file mode 100644 index 35463e7fe0..0000000000 --- a/src/ServiceControl/SetupBootstrapper.cs +++ /dev/null @@ -1,107 +0,0 @@ -namespace Particular.ServiceControl -{ - using System.Runtime.InteropServices; - using System.Threading.Tasks; - using global::ServiceControl.LicenseManagement; - using global::ServiceControl.Persistence; - using global::ServiceControl.Transports; - using NServiceBus.Logging; - using ServiceBus.Management.Infrastructure.Installers; - using ServiceBus.Management.Infrastructure.Settings; - - class SetupBootstrapper - { - public SetupBootstrapper(Settings settings) - { - this.settings = settings; - } - - public async Task Run() - { - // Validate license: - if (!ValidateLicense(settings)) - { - return; - } - - var componentSetupContext = new ComponentInstallationContext(); - - foreach (ServiceControlComponent component in ServiceControlMainInstance.Components) - { - component.Setup(settings, componentSetupContext); - } - - foreach (var installationTask in componentSetupContext.InstallationTasks) - { - await installationTask(); - } - - var persistence = PersistenceFactory.Create(settings); - - var installer = persistence.CreateInstaller(); - - await installer.Install(); - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - EventSourceCreator.Create(); - } - - if (settings.SkipQueueCreation) - { - log.Info("Skipping queue creation"); - } - else - { - var transportSettings = MapSettings(settings); - var transportCustomization = settings.LoadTransportCustomization(); - - await transportCustomization.ProvisionQueues(transportSettings, - componentSetupContext.Queues); - } - } - - bool ValidateLicense(Settings settings) - { - if (!string.IsNullOrWhiteSpace(settings.LicenseFileText)) - { - if (!LicenseManager.IsLicenseValidForServiceControlInit(settings.LicenseFileText, out var errorMessageForLicenseText)) - { - log.Error(errorMessageForLicenseText); - return false; - } - - if (!LicenseManager.TryImportLicenseFromText(settings.LicenseFileText, out var importErrorMessage)) - { - log.Error(importErrorMessage); - return false; - } - } - else - { - var license = LicenseManager.FindLicense(); - if (!LicenseManager.IsLicenseValidForServiceControlInit(license, out var errorMessageForFoundLicense)) - { - log.Error(errorMessageForFoundLicense); - return false; - } - } - - return true; - } - - static TransportSettings MapSettings(Settings settings) - { - var transportSettings = new TransportSettings - { - EndpointName = settings.ServiceName, - ConnectionString = settings.TransportConnectionString, - MaxConcurrency = settings.MaximumConcurrencyLevel - }; - return transportSettings; - } - - readonly Settings settings; - static ILog log = LogManager.GetLogger(); - } -} \ No newline at end of file From ef429eb9cd5e4a13d37f3a42af35f5aa62383329 Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Wed, 6 Mar 2024 17:47:28 +0100 Subject: [PATCH 2/2] Namespace cleanup --- .../TestSupport/EndpointTemplates/DefaultServer.cs | 2 +- .../TestSupport/ServiceControlComponentRunner.cs | 2 +- .../Hosting/Commands/CommandRunner.cs | 9 +-------- .../Hosting/Commands/SetupCommand.cs | 1 - .../Hosting/Commands/AbstractCommand.cs | 4 ++-- .../Hosting/Commands/CommandRunner.cs | 13 +++---------- .../Hosting/Commands/ImportFailedErrorsCommand.cs | 1 - .../Hosting/Commands/MaintenanceModeCommand.cs | 1 - src/ServiceControl/Hosting/Commands/RunCommand.cs | 7 ++++--- src/ServiceControl/Hosting/Commands/SetupCommand.cs | 11 ++++++----- src/ServiceControl/Hosting/HostArguments.cs | 1 - src/ServiceControl/Program.cs | 2 +- 12 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/ServiceControl.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs b/src/ServiceControl.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs index 8847ec6ae0..a4f9e22bcb 100644 --- a/src/ServiceControl.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs +++ b/src/ServiceControl.AcceptanceTests/TestSupport/EndpointTemplates/DefaultServer.cs @@ -3,11 +3,11 @@ using System; using System.Threading.Tasks; using AcceptanceTesting.EndpointTemplates; + using Hosting.Commands; using NServiceBus; using NServiceBus.AcceptanceTesting.Support; using NServiceBus.Features; using Particular.ServiceControl; - using Particular.ServiceControl.Commands; public class DefaultServer : IEndpointSetupTemplate { diff --git a/src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs b/src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs index d7d02c28e2..1fe2cca23c 100644 --- a/src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs +++ b/src/ServiceControl.AcceptanceTests/TestSupport/ServiceControlComponentRunner.cs @@ -6,6 +6,7 @@ using System.Text.Json; using System.Threading.Tasks; using AcceptanceTesting; + using Hosting.Commands; using Infrastructure.DomainEvents; using Infrastructure.WebApi; using Microsoft.AspNetCore.Builder; @@ -19,7 +20,6 @@ using NServiceBus.AcceptanceTesting.Support; using NServiceBus.Configuration.AdvancedExtensibility; using Particular.ServiceControl; - using Particular.ServiceControl.Commands; using Particular.ServiceControl.Hosting; using RavenDB.Shared; using ServiceBus.Management.Infrastructure.Settings; diff --git a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/CommandRunner.cs b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/CommandRunner.cs index b6798c1334..f70130f443 100644 --- a/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/CommandRunner.cs +++ b/src/ServiceControl.Audit/Infrastructure/Hosting/Commands/CommandRunner.cs @@ -4,13 +4,8 @@ using System.Collections.Generic; using System.Threading.Tasks; - class CommandRunner + class CommandRunner(List commands) { - public CommandRunner(List commands) - { - this.commands = commands; - } - public async Task Execute(HostArguments args, Settings.Settings settings) { foreach (var commandType in commands) @@ -19,7 +14,5 @@ public async Task Execute(HostArguments args, Settings.Settings settings) await command.Execute(args, settings); } } - - readonly List commands; } } \ No newline at end of file diff --git a/src/ServiceControl.Monitoring/Hosting/Commands/SetupCommand.cs b/src/ServiceControl.Monitoring/Hosting/Commands/SetupCommand.cs index e5cff36cef..9b49a38685 100644 --- a/src/ServiceControl.Monitoring/Hosting/Commands/SetupCommand.cs +++ b/src/ServiceControl.Monitoring/Hosting/Commands/SetupCommand.cs @@ -1,7 +1,6 @@ namespace ServiceControl.Monitoring { using System.Threading.Tasks; - using Infrastructure; using LicenseManagement; using NServiceBus.Logging; using Transports; diff --git a/src/ServiceControl/Hosting/Commands/AbstractCommand.cs b/src/ServiceControl/Hosting/Commands/AbstractCommand.cs index caf6fa0ec1..4178465847 100644 --- a/src/ServiceControl/Hosting/Commands/AbstractCommand.cs +++ b/src/ServiceControl/Hosting/Commands/AbstractCommand.cs @@ -1,7 +1,7 @@ -namespace Particular.ServiceControl.Commands +namespace ServiceControl.Hosting.Commands { using System.Threading.Tasks; - using Hosting; + using Particular.ServiceControl.Hosting; using ServiceBus.Management.Infrastructure.Settings; abstract class AbstractCommand diff --git a/src/ServiceControl/Hosting/Commands/CommandRunner.cs b/src/ServiceControl/Hosting/Commands/CommandRunner.cs index 7b46fbeaa9..1d9a88692e 100644 --- a/src/ServiceControl/Hosting/Commands/CommandRunner.cs +++ b/src/ServiceControl/Hosting/Commands/CommandRunner.cs @@ -1,18 +1,13 @@ -namespace Particular.ServiceControl.Commands +namespace ServiceControl.Hosting.Commands { using System; using System.Collections.Generic; using System.Threading.Tasks; - using Hosting; + using Particular.ServiceControl.Hosting; using ServiceBus.Management.Infrastructure.Settings; - class CommandRunner + class CommandRunner(List commands) { - public CommandRunner(List commands) - { - this.commands = commands; - } - public async Task Execute(HostArguments args, Settings settings) { foreach (var commandType in commands) @@ -21,7 +16,5 @@ public async Task Execute(HostArguments args, Settings settings) await command.Execute(args, settings); } } - - readonly List commands; } } \ No newline at end of file diff --git a/src/ServiceControl/Hosting/Commands/ImportFailedErrorsCommand.cs b/src/ServiceControl/Hosting/Commands/ImportFailedErrorsCommand.cs index 58445999f8..b6b6384181 100644 --- a/src/ServiceControl/Hosting/Commands/ImportFailedErrorsCommand.cs +++ b/src/ServiceControl/Hosting/Commands/ImportFailedErrorsCommand.cs @@ -9,7 +9,6 @@ using NServiceBus; using Operations; using Particular.ServiceControl; - using Particular.ServiceControl.Commands; using Particular.ServiceControl.Hosting; using ServiceBus.Management.Infrastructure.Settings; diff --git a/src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs b/src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs index 7b8c215aee..9dc55ad5f0 100644 --- a/src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs +++ b/src/ServiceControl/Hosting/Commands/MaintenanceModeCommand.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting.WindowsServices; using Particular.ServiceControl; - using Particular.ServiceControl.Commands; using Particular.ServiceControl.Hosting; using Persistence; using ServiceBus.Management.Infrastructure.Settings; diff --git a/src/ServiceControl/Hosting/Commands/RunCommand.cs b/src/ServiceControl/Hosting/Commands/RunCommand.cs index 31c5f01820..ec6ac6e7c6 100644 --- a/src/ServiceControl/Hosting/Commands/RunCommand.cs +++ b/src/ServiceControl/Hosting/Commands/RunCommand.cs @@ -1,12 +1,13 @@ -namespace Particular.ServiceControl.Commands +namespace ServiceControl.Hosting.Commands { using System.Threading.Tasks; - using global::ServiceControl; - using Hosting; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Hosting; using NServiceBus; + using Particular.ServiceControl; + using Particular.ServiceControl.Hosting; using ServiceBus.Management.Infrastructure.Settings; + using ServiceControl; class RunCommand : AbstractCommand { diff --git a/src/ServiceControl/Hosting/Commands/SetupCommand.cs b/src/ServiceControl/Hosting/Commands/SetupCommand.cs index dbaaf7c1b2..54d5fd2586 100644 --- a/src/ServiceControl/Hosting/Commands/SetupCommand.cs +++ b/src/ServiceControl/Hosting/Commands/SetupCommand.cs @@ -1,14 +1,15 @@ -namespace Particular.ServiceControl.Commands +namespace ServiceControl.Hosting.Commands { using System.Runtime.InteropServices; using System.Threading.Tasks; - using global::ServiceControl.LicenseManagement; - using global::ServiceControl.Persistence; - using global::ServiceControl.Transports; - using Hosting; using NServiceBus.Logging; + using Particular.ServiceControl; + using Particular.ServiceControl.Hosting; using ServiceBus.Management.Infrastructure.Installers; using ServiceBus.Management.Infrastructure.Settings; + using LicenseManagement; + using Persistence; + using Transports; class SetupCommand : AbstractCommand { diff --git a/src/ServiceControl/Hosting/HostArguments.cs b/src/ServiceControl/Hosting/HostArguments.cs index 0bde0c6345..5b785b9dff 100644 --- a/src/ServiceControl/Hosting/HostArguments.cs +++ b/src/ServiceControl/Hosting/HostArguments.cs @@ -5,7 +5,6 @@ namespace Particular.ServiceControl.Hosting using System.IO; using System.Linq; using System.Reflection; - using Commands; using global::ServiceControl.Configuration; using global::ServiceControl.Hosting.Commands; using ServiceBus.Management.Infrastructure.Settings; diff --git a/src/ServiceControl/Program.cs b/src/ServiceControl/Program.cs index 0845a6730b..6ece6b1c0d 100644 --- a/src/ServiceControl/Program.cs +++ b/src/ServiceControl/Program.cs @@ -5,8 +5,8 @@ using System.Reflection; using System.Runtime.Loader; using System.Threading.Tasks; - using Commands; using global::ServiceControl.Configuration; + using global::ServiceControl.Hosting.Commands; using global::ServiceControl.Persistence; using global::ServiceControl.Transports; using Hosting;