agent initialization

master
Tudor Stanciu 2021-12-06 10:36:37 +02:00
parent 67d05683d0
commit f0f04543d0
7 changed files with 108 additions and 31 deletions

View File

@ -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."

View File

@ -5,7 +5,20 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="NDB.Extensions.Swagger" Version="$(NDBExtensionsSwaggerPackageVersion)" />
<PackageReference Include="NDB.Security.Authentication.Identity" Version="$(NDBSecurityAuthenticationPackageVersion)" />
<PackageReference Include="Serilog.AspNetCore" Version="$(SerilogPackageVersion)" />
<PackageReference Include="Serilog.Extensions.Logging" Version="$(SerilogExtensionsPackageVersion)" />
<PackageReference Include="Serilog.Sinks.Console" Version="$(SerilogSinksConsolePackageVersion)" />
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="$(SerilogSinksMSSqlServerPackageVersion)" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="$(AutoMapperExtensionsPackageVersion)" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="$(MediatRPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="$(MicrosoftExtensionsHostingPackageVersion)" />
</ItemGroup>
</Project>

View File

@ -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<string>("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<string>("Urls");
var agentCode = configuration.GetValue<string>("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<Startup>();
});
public static IHostBuilder CreateHostBuilder(string[] args, IConfiguration configuration, bool useWindowsService)
{
var builder = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseConfiguration(configuration)
.UseSerilog();
});
if (useWindowsService)
builder.UseWindowsService();
return builder;
}
}
}

View File

@ -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",

View File

@ -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/"
}
}

View File

@ -58,7 +58,7 @@ namespace NetworkResurrector.Api
{
var urls = configuration.GetValue<string>("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();
}

View File

@ -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<,>));