From f0f04543d05c18951805da3eefcd8d9ae0586fd3 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 6 Dec 2021 10:36:37 +0200 Subject: [PATCH] agent initialization --- build-amd64.sh | 2 +- .../NetworkResurrector.Agent.csproj | 15 +++- src/agent/NetworkResurrector.Agent/Program.cs | 88 ++++++++++++++++--- .../Properties/launchSettings.json | 17 ---- .../NetworkResurrector.Agent/appsettings.json | 13 ++- src/api/NetworkResurrector.Api/Program.cs | 2 +- .../NetworkResurrector.Server/Startup.cs | 2 + 7 files changed, 108 insertions(+), 31 deletions(-) diff --git a/build-amd64.sh b/build-amd64.sh index 9ba1f9d..aba9afe 100644 --- a/build-amd64.sh +++ b/build-amd64.sh @@ -1,7 +1,7 @@ #!/bin/bash echo "Welcome!" -version="1.0.4" +version="1.0.4.1" localRegistryPass="***REMOVED***" echo "Create docker image with version $version." diff --git a/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj b/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj index 2884716..3c60e0d 100644 --- a/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj +++ b/src/agent/NetworkResurrector.Agent/NetworkResurrector.Agent.csproj @@ -5,7 +5,20 @@ - + + + + + + + + + + + + + + diff --git a/src/agent/NetworkResurrector.Agent/Program.cs b/src/agent/NetworkResurrector.Agent/Program.cs index fe10858..d7627ce 100644 --- a/src/agent/NetworkResurrector.Agent/Program.cs +++ b/src/agent/NetworkResurrector.Agent/Program.cs @@ -1,11 +1,14 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Core; +using Serilog.Events; +using Serilog.Sinks.MSSqlServer; using System; -using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Linq; -using System.Threading.Tasks; namespace NetworkResurrector.Agent { @@ -13,14 +16,79 @@ namespace NetworkResurrector.Agent { public static void Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + var isConsole = Debugger.IsAttached || args.Contains("--console"); + if (!isConsole) + { + var pathToExe = Process.GetCurrentProcess().MainModule.FileName; + var pathToContentRoot = Path.GetDirectoryName(pathToExe); + Directory.SetCurrentDirectory(pathToContentRoot); + } + + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddEnvironmentVariables() + .Build(); + + var connectionString = configuration.GetConnectionString("DatabaseConnection"); + var loggingLevelParam = configuration.GetValue("Logging:LogLevel:Default"); + + var loggingLevelOk = Enum.TryParse(loggingLevelParam, out LogEventLevel loggingLevel); + if (!loggingLevelOk) + throw new Exception($"Logging level '{loggingLevelParam}' is not valid."); + + var loggingLevelSwitch = new LoggingLevelSwitch(loggingLevel); + + var columnOptions = new ColumnOptions(); + columnOptions.Store.Remove(StandardColumn.Properties); + columnOptions.Store.Remove(StandardColumn.MessageTemplate); + columnOptions.Store.Add(StandardColumn.LogEvent); + + var mssqlSinkOptions = new MSSqlServerSinkOptions() { AutoCreateSqlTable = true, TableName = "__Logs" }; + + Log.Logger = new LoggerConfiguration() + .MinimumLevel.ControlledBy(loggingLevelSwitch) + .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) + .Enrich.FromLogContext() + .WriteTo.Console() + .WriteTo.MSSqlServer(connectionString, mssqlSinkOptions, columnOptions: columnOptions) + .CreateLogger(); + + try + { + var urls = configuration.GetValue("Urls"); + var agentCode = configuration.GetValue("Agent:Code"); + + Log.Information($"Starting network resurrector agent {agentCode}..."); + Log.Information($"Network resurrector agent {agentCode} listening on {urls}"); + Log.Information($"Hostname: {Environment.MachineName}"); + Console.WriteLine("Application started. Press Ctrl+C to shut down."); + CreateHostBuilder(args, configuration, !isConsole).Build().Run(); + } + catch (Exception ex) + { + Log.Fatal(ex, "Network resurrector agent host terminated unexpectedly"); + } + finally + { + Log.CloseAndFlush(); + } } - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); + public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration, bool useWindowsService) + { + var builder = Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup() + .UseConfiguration(configuration) + .UseSerilog(); + }); + + if (useWindowsService) + builder.UseWindowsService(); + + return builder; + } } } diff --git a/src/agent/NetworkResurrector.Agent/Properties/launchSettings.json b/src/agent/NetworkResurrector.Agent/Properties/launchSettings.json index 4f86b50..2c308ea 100644 --- a/src/agent/NetworkResurrector.Agent/Properties/launchSettings.json +++ b/src/agent/NetworkResurrector.Agent/Properties/launchSettings.json @@ -1,22 +1,5 @@ { - "$schema": "http://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:58125", - "sslPort": 0 - } - }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "swagger", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, "NetworkResurrector.Agent": { "commandName": "Project", "dotnetRunMessages": "true", diff --git a/src/agent/NetworkResurrector.Agent/appsettings.json b/src/agent/NetworkResurrector.Agent/appsettings.json index d9d9a9b..07332ee 100644 --- a/src/agent/NetworkResurrector.Agent/appsettings.json +++ b/src/agent/NetworkResurrector.Agent/appsettings.json @@ -1,4 +1,9 @@ { + "Urls": "http://*:5068", + "ConnectionStrings": { + //"DatabaseConnection": "***REMOVED***" + "DatabaseConnection": "***REMOVED***" + }, "Logging": { "LogLevel": { "Default": "Information", @@ -6,5 +11,11 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Agent": { + "Code": "DEV_AGENT" + }, + "IdentityServer": { + "BaseAddress": "https://toodle.ddns.net/identity-server-api/" + } } diff --git a/src/api/NetworkResurrector.Api/Program.cs b/src/api/NetworkResurrector.Api/Program.cs index d3a1b2d..7cbc16c 100644 --- a/src/api/NetworkResurrector.Api/Program.cs +++ b/src/api/NetworkResurrector.Api/Program.cs @@ -58,7 +58,7 @@ namespace NetworkResurrector.Api { var urls = configuration.GetValue("urls"); Log.Information("Starting network resurrector API..."); - Log.Information($"Network resurrector API listening on {urls}"); + Log.Information($"Network resurrector API listening on {urls}"); Console.WriteLine("Application started. Press Ctrl+C to shut down."); CreateHostBuilder(args, configuration, !isConsole).Build().Run(); } diff --git a/src/server/NetworkResurrector.Server/Startup.cs b/src/server/NetworkResurrector.Server/Startup.cs index a8c5e24..7a6449e 100644 --- a/src/server/NetworkResurrector.Server/Startup.cs +++ b/src/server/NetworkResurrector.Server/Startup.cs @@ -33,6 +33,8 @@ namespace NetworkResurrector.Server // Add basic authentication services.AddIdentityAuthentication(_configuration.GetSection("IdentityServer")["BaseAddress"]); + services.AddHttpContextAccessor(); + // MediatR services.AddMediatR(GetMediatRAssemblies()); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));