Shutdown service

master
Tudor Stanciu 2022-01-03 09:36:19 +02:00
parent 806b94c3c4
commit 8f237cd2b7
7 changed files with 85 additions and 18 deletions

View File

@ -41,3 +41,11 @@ thin-ovm
####################################################################################################################################################### #######################################################################################################################################################
- masinile sunt afisate in ordinea numarului de porniri - masinile sunt afisate in ordinea numarului de porniri
Agent:
Win:
https://www.programmerall.com/article/5237127008/
https://www.programmerall.com/article/6418715755/
Linux:
https://www.programmerall.com/article/3856917681/

View File

@ -24,7 +24,9 @@
<Version>1.0.3</Version> <Version>1.0.3</Version>
<Content> <Content>
Added NetworkResurrector.Agent service Added NetworkResurrector.Agent service
Upgrade services to net5.0 Upgrade all services to net5.0
NetworkResurrector.Agent is a service that will be installed on a host machine with the scope to execute operations like shutdown, restart, etc directly.
The system will be able to execute these operations in multiple ways. This is just the first one and all of them are handled by the NetworkResurrector.Server. For example, if the user wants to shutdown a clean Windows machine, he can use the agent and configure the server to call it, but in case of a proxmox machine, the server can be configured to execute a http request directly to the OS with the shutdown action (without the need for the agent).
</Content> </Content>
</Note> </Note>
</ReleaseNotes> </ReleaseNotes>

View File

@ -1,5 +1,8 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NetworkResurrector.Agent.Application.Services.Abstractions; using NetworkResurrector.Agent.Application.Services.Abstractions;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace NetworkResurrector.Agent.Application.Services namespace NetworkResurrector.Agent.Application.Services
{ {
@ -14,7 +17,26 @@ namespace NetworkResurrector.Agent.Application.Services
public void Run() public void Run()
{ {
_logger.LogInformation("In development..."); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
ShutdownWindows();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
// Do something
}
else throw new Exception("ShutdownService: Cannot shutdown host. Unknown operating system.");
}
private void ShutdownWindows()
{
var psi = new ProcessStartInfo("shutdown", "/s /t 0")
{
CreateNoWindow = true,
UseShellExecute = false
};
Process.Start(psi);
} }
} }
} }

View File

@ -1,10 +1,4 @@
using System; namespace NetworkResurrector.Agent.Domain.Errors
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NetworkResurrector.Agent.Domain.Errors
{ {
public struct Errors public struct Errors
{ {

View File

@ -0,0 +1,22 @@
using Microsoft.Extensions.Configuration;
using Serilog.Core;
using Serilog.Events;
namespace NetworkResurrector.Agent.Extensions.Serilog
{
public class AgentCodeEventEnricher : ILogEventEnricher
{
private readonly IConfiguration _configuration;
public AgentCodeEventEnricher(IConfiguration configuration)
{
_configuration = configuration;
}
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
var agentCode = _configuration.GetValue<string>("Agent:Code") ?? "N/A";
logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty("Agent", agentCode));
}
}
}

View File

@ -0,0 +1,20 @@
using Serilog.Sinks.MSSqlServer;
namespace NetworkResurrector.Agent.Extensions.Serilog.Sinks.MSSqlServer
{
public static class ColumnOptionsExtension
{
public static ColumnOptions AsStandard(this ColumnOptions columnOptions)
{
columnOptions.Store.Remove(StandardColumn.Properties);
columnOptions.Store.Remove(StandardColumn.MessageTemplate);
columnOptions.Store.Add(StandardColumn.LogEvent);
columnOptions.AdditionalColumns = new System.Collections.ObjectModel.Collection<SqlColumn>
{
new SqlColumn() { ColumnName = "Agent", DataType = System.Data.SqlDbType.VarChar, DataLength = 100 }
};
return columnOptions;
}
}
}

View File

@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NetworkResurrector.Agent.Extensions.Serilog;
using NetworkResurrector.Agent.Extensions.Serilog.Sinks.MSSqlServer;
using Serilog; using Serilog;
using Serilog.Core; using Serilog.Core;
using Serilog.Events; using Serilog.Events;
@ -39,17 +41,14 @@ namespace NetworkResurrector.Agent
var loggingLevelSwitch = new LoggingLevelSwitch(loggingLevel); var loggingLevelSwitch = new LoggingLevelSwitch(loggingLevel);
var columnOptions = new ColumnOptions(); var columnOptions = new ColumnOptions().AsStandard();
columnOptions.Store.Remove(StandardColumn.Properties); var mssqlSinkOptions = new MSSqlServerSinkOptions() { AutoCreateSqlTable = true, TableName = "__Logs_Agent" };
columnOptions.Store.Remove(StandardColumn.MessageTemplate);
columnOptions.Store.Add(StandardColumn.LogEvent);
var mssqlSinkOptions = new MSSqlServerSinkOptions() { AutoCreateSqlTable = true, TableName = "__Logs" };
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(loggingLevelSwitch) .MinimumLevel.ControlledBy(loggingLevelSwitch)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext() .Enrich.FromLogContext()
.Enrich.With(new AgentCodeEventEnricher(configuration))
.WriteTo.Console() .WriteTo.Console()
.WriteTo.MSSqlServer(connectionString, mssqlSinkOptions, columnOptions: columnOptions) .WriteTo.MSSqlServer(connectionString, mssqlSinkOptions, columnOptions: columnOptions)
.CreateLogger(); .CreateLogger();