238 lines
8.8 KiB
C#
238 lines
8.8 KiB
C#
using Autofac;
|
|
using Insight.Database;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Npgsql;
|
|
using PlanTempus.Database.ConfigurationManagementSystem;
|
|
using PlanTempus.Database.Core.DCL;
|
|
using PlanTempus.Database.Core.DDL;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
|
|
namespace PlanTempus.SetupInfrastructure
|
|
{
|
|
/// <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)
|
|
.UseEnvironment("Dev")
|
|
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
|
|
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
|
|
{
|
|
var startup = new Startup();
|
|
startup.ConfigureContainer(builder);
|
|
})
|
|
|
|
.Build();
|
|
|
|
await host.StartAsync();
|
|
Console.WriteLine("Host has started.");
|
|
|
|
|
|
var console = host.Services.GetRequiredService<ConsoleService>();
|
|
console.Run();
|
|
|
|
await host.WaitForShutdownAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Host failed to start: {ex}");
|
|
}
|
|
}
|
|
}
|
|
public class ConsoleService
|
|
{
|
|
|
|
static ConsoleColor _backgroundColor = Console.BackgroundColor;
|
|
static ConsoleColor _foregroundColor = Console.ForegroundColor;
|
|
|
|
private readonly SetupDbAdmin _setupDbAdmin;
|
|
private readonly SetupIdentitySystem _setupIdentitySystem;
|
|
private readonly SetupConfiguration _setupConfiguration;
|
|
private readonly SetupApplicationUser _setupApplicationUser;
|
|
|
|
public ConsoleService(SetupDbAdmin setupDbAdmin, SetupIdentitySystem setupIdentitySystem, SetupConfiguration setupConfiguration, SetupApplicationUser setupApplicationUser)
|
|
{
|
|
_setupDbAdmin = setupDbAdmin;
|
|
_setupIdentitySystem = setupIdentitySystem;
|
|
_setupConfiguration = setupConfiguration;
|
|
_setupApplicationUser = setupApplicationUser;
|
|
}
|
|
static bool IsSuperAdmin()
|
|
{
|
|
//test db access
|
|
Console.WriteLine("Testing db access...");
|
|
|
|
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
|
|
|
|
var conn = new NpgsqlConnection();
|
|
|
|
var result = (dynamic)conn.QuerySql(query).Single();
|
|
|
|
string username = result.usename;
|
|
bool isSuperuser = (bool)result.usesuper;
|
|
|
|
if ((bool)result.usesuper)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
Console.BackgroundColor = ConsoleColor.Yellow;
|
|
Console.WriteLine();
|
|
Console.WriteLine("TEST SUCCESSFULLY");
|
|
Console.WriteLine();
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
Console.BackgroundColor = _backgroundColor;
|
|
Console.WriteLine("-------------------------------");
|
|
Console.WriteLine();
|
|
Console.WriteLine($"Username: {username}");
|
|
Console.WriteLine($"Super admin: true");
|
|
Console.WriteLine();
|
|
Console.WriteLine("-------------------------------");
|
|
|
|
|
|
Console.WriteLine("Press any key to start database setup");
|
|
Console.Read();
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
Console.BackgroundColor = ConsoleColor.Red;
|
|
Console.WriteLine();
|
|
Console.WriteLine("TEST WAS NOT SUCCESSFULLY");
|
|
Console.WriteLine();
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
Console.BackgroundColor = _backgroundColor;
|
|
Console.WriteLine("-------------------------------");
|
|
Console.WriteLine();
|
|
Console.WriteLine($"Username: {username}");
|
|
Console.WriteLine($"Super admin: false");
|
|
Console.WriteLine();
|
|
Console.WriteLine("-------------------------------");
|
|
|
|
|
|
Console.WriteLine("User is required to be super admin");
|
|
Console.Read();
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
static void Welcome()
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Blue;
|
|
Console.WriteLine(@"__________.__ ___________
|
|
\______ \ | _____ ___\__ ___/___ _____ ______ __ __ ______
|
|
| ___/ | \__ \ / \| |_/ __ \ / \\____ \| | \/ ___/
|
|
| | | |__/ __ \| | \ |\ ___/| Y Y \ |_> > | /\___ \
|
|
|____| |____(____ /___| /____| \___ >__|_| / __/|____//____ >
|
|
\/ \/ \/ \/|__| \/
|
|
_________ __
|
|
/ _____/ _____/ |_ __ ________
|
|
\_____ \_/ __ \ __\ | \____ \
|
|
/ \ ___/| | | | / |_> >
|
|
/_______ /\___ >__| |____/| __/
|
|
\/ \/ |__|");
|
|
Console.WriteLine();
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
|
|
}
|
|
|
|
public void Run()
|
|
{
|
|
Welcome();
|
|
string userPass;
|
|
|
|
try
|
|
{
|
|
do
|
|
{
|
|
Console.WriteLine("Input username:password for a superadmin role");
|
|
userPass = Console.ReadLine() ?? string.Empty;
|
|
} while (!userPass.Contains(":") || userPass.Split(":").Length != 2 ||
|
|
string.IsNullOrEmpty(userPass.Split(":")[0]) ||
|
|
string.IsNullOrEmpty(userPass.Split(":")[1]));
|
|
|
|
//var ctp = new Startup.ConnectionStringTemplateParameters(
|
|
// user: userPass.Split(":")[0],
|
|
// pwd: userPass.Split(":")[1]
|
|
//);
|
|
//_container = new Startup().ConfigureContainer(ctp);
|
|
|
|
if (IsSuperAdmin())
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
var sw = new Stopwatch();
|
|
|
|
Console.Write("Database.Core.DCL.SetupDbAdmin...");
|
|
sw.Start();
|
|
|
|
_setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
|
|
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
|
|
|
|
|
Console.WriteLine("::");
|
|
Console.WriteLine("::");
|
|
Console.Write("Database.Core.DDL.SetupIdentitySystem...");
|
|
sw.Restart();
|
|
|
|
//create new container with application user, we use that role from now.
|
|
//_container = new Startup().ConfigureContainer(new Startup.ConnectionStringTemplateParameters("heimdall", "3911"));
|
|
|
|
_setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" });
|
|
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
|
|
|
Console.WriteLine("::");
|
|
Console.WriteLine("::");
|
|
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
|
|
sw.Restart();
|
|
_setupConfiguration.With(new SetupConfiguration.Command());
|
|
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
|
|
|
|
|
Console.WriteLine("::");
|
|
Console.WriteLine("::");
|
|
Console.Write("Database.Core.DCL.SetupApplicationUser...");
|
|
sw.Start();
|
|
|
|
_setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
|
|
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(e);
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|