PlanTempusApp/SetupInfrastructure/Program.cs

232 lines
9 KiB
C#
Raw Normal View History

using Autofac;
2025-02-10 18:41:51 +01:00
using Insight.Database;
2025-02-21 17:03:49 +01:00
using Microsoft.Extensions.DependencyInjection;
2025-02-20 17:14:53 +01:00
using Microsoft.Extensions.Hosting;
2025-02-21 17:03:49 +01:00
using Npgsql;
using PlanTempus.Database.ConfigurationManagementSystem;
using PlanTempus.Database.Core.DCL;
using PlanTempus.Database.Core.DDL;
2025-02-10 18:41:51 +01:00
using System.Data;
2025-02-13 17:06:22 +01:00
using System.Diagnostics;
2025-03-10 15:56:22 +01:00
using PlanTempus.Core.Database.ConnectionFactory;
namespace PlanTempus.SetupInfrastructure
2025-01-24 18:04:35 +01:00
{
2025-03-07 16:17:30 +01:00
/// <summary>
/// SETUP APPLICATION USER NAMED sathumper
///
/// This should be handled on the Postgresql db server with a superadmin or similar.
///
/// Execute SQL CreateRole.txt
///
/// After that is executed it is time for running this main program
/// Remember to use the newly created sathumper
/// "ConnectionStrings": {
/// "DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=[uid];Password=[secret];"
/// </summary>
internal class Program
{
static async Task Main(string[] args)
{
try
{
var host = Host.CreateDefaultBuilder(args)
2025-02-21 17:03:49 +01:00
.UseEnvironment("Dev")
2025-03-07 16:17:30 +01:00
.UseServiceProviderFactory(
new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
{
var startup = new Startup();
2025-02-21 17:03:49 +01:00
startup.ConfigureContainer(builder);
})
.Build();
await host.StartAsync();
Console.WriteLine("Host has started.");
2025-02-21 17:03:49 +01:00
var console = host.Services.GetRequiredService<ConsoleService>();
console.Run();
await host.WaitForShutdownAsync();
}
catch (Exception ex)
{
Console.WriteLine($"Host failed to start: {ex}");
}
2025-02-21 17:03:49 +01:00
}
}
2025-02-10 18:41:51 +01:00
2025-03-07 16:17:30 +01:00
public class ConsoleService(
IDbConnectionFactory connectionFactory,
SetupDbAdmin setupDbAdmin,
SetupIdentitySystem setupIdentitySystem,
SetupConfiguration setupConfiguration,
SetupApplicationUser setupApplicationUser,
SetupOutbox setupOutbox)
2025-03-07 16:17:30 +01:00
{
static ConsoleColor _backgroundColor = Console.BackgroundColor;
static ConsoleColor _foregroundColor = Console.ForegroundColor;
2025-02-10 18:41:51 +01:00
bool IsSuperAdmin(ConnectionStringParameters parameters)
{
Console.WriteLine("Testing db access...");
2025-02-10 18:41:51 +01:00
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
2025-02-10 18:41:51 +01:00
2025-03-07 16:17:30 +01:00
using var conn = connectionFactory.Create(parameters);
var result = (dynamic)conn.QuerySql(query).Single();
2025-02-10 18:41:51 +01:00
string username = result.usename;
bool isSuperuser = (bool)result.usesuper;
2025-02-10 18:41:51 +01:00
if ((bool)result.usesuper)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine();
Console.BackgroundColor = ConsoleColor.Yellow;
Console.WriteLine("TEST SUCCESSFULLY");
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine();
Console.WriteLine("-------------------------------");
Console.WriteLine();
Console.WriteLine($"Username: {username}");
Console.WriteLine($"Super admin: true");
Console.WriteLine();
Console.WriteLine("-------------------------------");
2025-02-10 18:41:51 +01:00
Console.WriteLine("Press any key to start database setup");
Console.Read();
2025-02-10 18:41:51 +01:00
return true;
}
2025-02-10 18:41:51 +01:00
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine("TEST WAS NOT SUCCESSFULLY");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("-------------------------------");
Console.WriteLine();
Console.WriteLine($"Username: {username}");
Console.WriteLine($"Super admin: false");
Console.WriteLine();
Console.WriteLine("-------------------------------");
2025-02-10 18:41:51 +01:00
Console.WriteLine("User is required to be super admin");
Console.Read();
2025-02-10 18:41:51 +01:00
return false;
}
2025-03-07 16:17:30 +01:00
static void Welcome()
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine(@"__________.__ ___________
2025-02-12 17:38:01 +01:00
\______ \ | _____ ___\__ ___/___ _____ ______ __ __ ______
| ___/ | \__ \ / \| |_/ __ \ / \\____ \| | \/ ___/
| | | |__/ __ \| | \ |\ ___/| Y Y \ |_> > | /\___ \
|____| |____(____ /___| /____| \___ >__|_| / __/|____//____ >
\/ \/ \/ \/|__| \/
_________ __
/ _____/ _____/ |_ __ ________
\_____ \_/ __ \ __\ | \____ \
/ \ ___/| | | | / |_> >
/_______ /\___ >__| |____/| __/
\/ \/ |__|");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
}
2025-02-20 17:14:53 +01:00
2025-02-21 17:03:49 +01:00
public void Run()
{
Welcome();
string userPass;
try
{
do
{
Console.WriteLine("Input username:password for a superadmin role");
userPass = Console.ReadLine() ?? string.Empty;
2025-03-07 16:17:30 +01:00
} while (!userPass.Contains(':') || userPass.Split(":").Length != 2 ||
string.IsNullOrEmpty(userPass.Split(":")[0]) ||
string.IsNullOrEmpty(userPass.Split(":")[1]));
var superUser = new ConnectionStringParameters(
2025-03-04 23:54:55 +01:00
User: userPass.Split(":")[0],
Pwd: userPass.Split(":")[1]
);
if (IsSuperAdmin(superUser))
{
Console.ForegroundColor = ConsoleColor.Green;
var sw = new Stopwatch();
Console.Write("Database.Core.DCL.SetupDbAdmin...");
sw.Start();
2025-03-07 16:17:30 +01:00
setupDbAdmin.With(
new SetupDbAdmin.Command
{ Password = "3911", Schema = "system", User = "heimdall" }, superUser);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::");
Console.WriteLine("::");
Console.Write("Database.Core.DDL.SetupIdentitySystem...");
sw.Restart();
//use application user, we use that role from now.
2025-03-04 23:54:55 +01:00
var connParams = new ConnectionStringParameters(User: "heimdall", Pwd: "3911");
2025-03-07 16:17:30 +01:00
setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" }, connParams);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::");
Console.WriteLine("::");
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
sw.Restart();
2025-03-07 16:17:30 +01:00
setupConfiguration.With(new SetupConfiguration.Command(), connParams);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::");
Console.WriteLine("::");
Console.Write("Database.Core.DDL.SetupOutbox...");
sw.Restart();
setupOutbox.With(new SetupOutbox.Command { Schema = "system" }, connParams);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::");
Console.WriteLine("::");
Console.Write("Database.Core.DCL.SetupApplicationUser...");
sw.Start();
2025-03-07 16:17:30 +01:00
setupApplicationUser.With(
new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" },
superUser);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
//and a lot of other tables that we haven't defined yet
// input configurations!!! TODO:Missing
Console.ForegroundColor = _foregroundColor;
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine(e);
}
}
}
2025-03-07 16:17:30 +01:00
}