private static async Task InitializeSettings(IWebHost webHost, string[] args) { using var scope = webHost.Services.GetService <IServiceScopeFactory>().CreateScope(); var settingsService = scope.ServiceProvider.GetRequiredService <ISettingsService>(); var existsResult = settingsService.ConnectionStringExist(); if (!existsResult.Success)// do need to initialize database { // Find file var filePath = Path.Combine(Directory.GetCurrentDirectory(), "init.json"); if (File.Exists(filePath)) { Log.LogEvent($"Try initialize from {filePath}"); // Get content var startupContent = await File.ReadAllTextAsync(filePath); var startup = JsonConvert.DeserializeObject <StartupInitializeModel>(startupContent); // Apply settings var updateConnectionResult = await settingsService.UpdateConnectionString(startup.InitialSettings); if (!updateConnectionResult.Success) { throw new Exception("Init error: " + updateConnectionResult.Message); } var adminSettingsUpdateModel = new AdminSettingsModel { S3SettingsModel = startup.S3SettingsModel, SMTPSettingsModel = startup.SMTPSettingsModel, SdkSettingsModel = startup.SdkSettingsModel, SendGridSettingsModel = startup.SendGridSettingsModel, SwiftSettingsModel = startup.SwiftSettingsModel, }; var updateAdminSettingsResult = await settingsService.UpdateAdminSettings(adminSettingsUpdateModel); if (!updateAdminSettingsResult.Success) { throw new Exception("Init error: " + updateAdminSettingsResult.Message); } EnabledPlugins = PluginHelper.GetPlugins(_defaultConnectionString); DisabledPlugins = PluginHelper.GetDisablePlugins(_defaultConnectionString); // Enable plugins foreach (var pluginId in startup.PluginsList) { var pluginObject = DisabledPlugins.FirstOrDefault(x => x.PluginId == pluginId); if (pluginObject != null) { var contextFactory = new BaseDbContextFactory(); await using var dbContext = contextFactory.CreateDbContext(new[] { _defaultConnectionString }); var eformPlugin = await dbContext.EformPlugins .Where(x => x.Status == (int)PluginStatus.Disabled) .FirstOrDefaultAsync(x => x.PluginId == pluginObject.PluginId); if (eformPlugin != null) { eformPlugin.Status = (int)PluginStatus.Enabled; dbContext.EformPlugins.Update(eformPlugin); await dbContext.SaveChangesAsync(); var pluginMenu = pluginObject.GetNavigationMenu(scope.ServiceProvider); // Load to database all navigation menu from plugin by id var pluginMenuItemsLoader = new PluginMenuItemsLoader(dbContext, pluginId); pluginMenuItemsLoader.Load(pluginMenu); } } } // not need because settingsService.UpdateAdminSettings call restart // Restart(); // restart IF some plugins has been enabled } else if (args.Any()) { Log.LogEvent("Try initialize from args"); var defaultConfig = new ConfigurationBuilder() .AddCommandLine(args) .AddEnvironmentVariables(prefix: "ASPNETCORE_") .Build(); var firstName = defaultConfig.GetValue("FirstName", ""); var lastName = defaultConfig.GetValue("LastName", ""); var email = defaultConfig.GetValue("Email", ""); var password = defaultConfig.GetValue("Password", ""); var token = defaultConfig.GetValue("Token", ""); if (!string.IsNullOrEmpty(token) && !string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName) && !string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password)) { var sdkConnectionString = _defaultConnectionString.Replace("_Angular", "_SDK"); // get customer number const RegexOptions options = RegexOptions.Multiline | RegexOptions.CultureInvariant; const string pattern = @"[D|d]atabase=(\D*)(\d*)_Angular"; if (int.TryParse(Regex.Match(_defaultConnectionString, pattern, options).Groups[^ 1].Value, out var customerNumber)) { var adminTools = new AdminTools(sdkConnectionString); // Setup SDK DB await adminTools.DbSetup(token); var core = new Core(); await core.StartSqlOnly(sdkConnectionString); await core.SetSdkSetting(Settings.customerNo, customerNumber.ToString()); // setup admin var adminSetupModel = new AdminSetupModel() { DarkTheme = false, FirstName = firstName, LastName = lastName, Email = email, Password = password, }; var contextFactory = new BaseDbContextFactory(); await using var dbContext = contextFactory.CreateDbContext(new[] { _defaultConnectionString }); var connectionStringsSdk = scope.ServiceProvider.GetRequiredService <IDbOptions <ConnectionStringsSdk> >(); await connectionStringsSdk.UpdateDb( options => { options.SdkConnection = sdkConnectionString; }, dbContext); await SeedAdminHelper.SeedAdmin(adminSetupModel, "", dbContext); Restart(); } } } } }
public async Task <OperationResult> UpdateConnectionString(InitialSettingsModel initialSettingsModel) { var customerNo = initialSettingsModel.GeneralAppSetupSettingsModel.CustomerNo.ToString(); var dbNamePrefix = ""; if (initialSettingsModel.ConnectionStringSdk.PrefixAllDatabases) { dbNamePrefix = "Microting_"; } var sdkDbName = $"{dbNamePrefix}{customerNo}_SDK"; var angularDbName = $"{dbNamePrefix}{customerNo}_Angular"; var sdkConnectionString = $"host= {initialSettingsModel.ConnectionStringSdk.Host};" + $"Database={sdkDbName};{initialSettingsModel.ConnectionStringSdk.Auth}" + $"port={initialSettingsModel.ConnectionStringSdk.Port};" + "Convert Zero Datetime = true;SslMode=none;"; var angularConnectionString = $"host= {initialSettingsModel.ConnectionStringSdk.Host};" + $"Database={angularDbName};{initialSettingsModel.ConnectionStringSdk.Auth}" + $"port={initialSettingsModel.ConnectionStringSdk.Port};" + "Convert Zero Datetime = true;SslMode=none;"; if (!string.IsNullOrEmpty(_connectionStringsSdk.Value.SdkConnection)) { return(new OperationResult(false, _localizationService.GetString("ConnectionStringAlreadyExist"))); } try { Log.LogEvent($"SettingsService.ConnectionStringExist: connection string is {sdkConnectionString}"); var adminTools = new AdminTools(sdkConnectionString); // Setup SDK DB await adminTools.DbSetup(initialSettingsModel.ConnectionStringSdk.Token); // var core = await _coreHelper.GetCore(); Core core = new Core(); await core.StartSqlOnly(sdkConnectionString); await core.SetSdkSetting(Settings.customerNo, customerNo); } catch (Exception exception) { _logger.LogError(exception.Message); _logger.LogError(exception.StackTrace); if (exception.InnerException != null) { return(new OperationResult(false, exception.Message + " - " + exception.InnerException.Message)); } return(new OperationResult(false, exception.Message)); //return new OperationResult(false, // _localizationService.GetString("SDKConnectionStringIsInvalid")); } // Migrate DB var dbContextOptionsBuilder = new DbContextOptionsBuilder <BaseDbContext>(); try { dbContextOptionsBuilder.UseMySql(angularConnectionString, new MariaDbServerVersion( new Version(10, 4, 0)), b => b.EnableRetryOnFailure()); await using var dbContext = new BaseDbContext(dbContextOptionsBuilder.Options); await dbContext.Database.MigrateAsync(); if (initialSettingsModel.AdminSetupModel != null) { // Seed admin and demo users await SeedAdminHelper.SeedAdmin(initialSettingsModel.AdminSetupModel, initialSettingsModel.GeneralAppSetupSettingsModel.DefaultLocale, dbContext); } } catch (Exception exception) { _logger.LogError(exception.Message); _logger.LogError(exception.StackTrace); //return new OperationResult(false, // _localizationService.GetString("MainConnectionStringIsInvalid")); if (exception.Message == "Could not create the user") { return(new OperationResult(false, _localizationService.GetString(exception.Message))); } if (exception.InnerException != null) { return(new OperationResult(false, exception.Message + " - " + exception.InnerException.Message)); } return(new OperationResult(false, exception.Message)); } try { // Generate SigningKey var key = new byte[32]; RandomNumberGenerator.Create().GetBytes(key); var signingKey = Convert.ToBase64String(key); // Update Database settings await using var dbContext = new BaseDbContext(dbContextOptionsBuilder.Options); await _tokenOptions.UpdateDb((options) => { options.SigningKey = signingKey; }, dbContext); await _applicationSettings.UpdateDb( options => { options.DefaultLocale = initialSettingsModel.GeneralAppSetupSettingsModel.DefaultLocale; }, dbContext); await _connectionStringsSdk.UpdateDb((options) => { options.SdkConnection = sdkConnectionString; }, dbContext); // Update connection string _connectionStrings.UpdateFile((options) => { options.DefaultConnection = angularConnectionString; }); } catch (Exception exception) { _logger.LogError(exception.Message); _logger.LogError(exception.StackTrace); return(exception.InnerException != null ? new OperationResult(false, exception.Message + " - " + exception.InnerException.Message) : new OperationResult(false, exception.Message)); //return new OperationResult(false, // _localizationService.GetString("CouldNotWriteConnectionString")); } Program.Restart(); return(new OperationResult(true)); }