From 8f237cd2b7653b3ce0ee869e7c143d5e6b8a7712 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 3 Jan 2022 09:36:19 +0200 Subject: [PATCH] Shutdown service --- Notes.txt | 10 +++++++- ReleaseNotes.xml | 8 ++++--- .../Services/ShutdownService.cs | 24 ++++++++++++++++++- .../Constants/Errors.cs | 8 +------ .../Serilog/AgentCodeEventEnricher.cs | 22 +++++++++++++++++ .../MSSqlServer/ColumnOptionsExtension.cs | 20 ++++++++++++++++ src/agent/NetworkResurrector.Agent/Program.cs | 11 ++++----- 7 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 src/agent/NetworkResurrector.Agent/Extensions/Serilog/AgentCodeEventEnricher.cs create mode 100644 src/agent/NetworkResurrector.Agent/Extensions/Serilog/Sinks/MSSqlServer/ColumnOptionsExtension.cs diff --git a/Notes.txt b/Notes.txt index a22e7e4..d8b5d70 100644 --- a/Notes.txt +++ b/Notes.txt @@ -40,4 +40,12 @@ thin-ovm ####################################################################################################################################################### -- masinile sunt afisate in ordinea numarului de porniri \ No newline at end of file +- 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/ \ No newline at end of file diff --git a/ReleaseNotes.xml b/ReleaseNotes.xml index dfc8c36..7e61ace 100644 --- a/ReleaseNotes.xml +++ b/ReleaseNotes.xml @@ -23,8 +23,10 @@ 1.0.3 - Added NetworkResurrector.Agent service - Upgrade services to net5.0 - + Added NetworkResurrector.Agent service + 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). + \ No newline at end of file diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/ShutdownService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/ShutdownService.cs index 1672cde..683d270 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/ShutdownService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/ShutdownService.cs @@ -1,5 +1,8 @@ using Microsoft.Extensions.Logging; using NetworkResurrector.Agent.Application.Services.Abstractions; +using System; +using System.Diagnostics; +using System.Runtime.InteropServices; namespace NetworkResurrector.Agent.Application.Services { @@ -14,7 +17,26 @@ namespace NetworkResurrector.Agent.Application.Services 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); } } } diff --git a/src/agent/NetworkResurrector.Agent.Domain/Constants/Errors.cs b/src/agent/NetworkResurrector.Agent.Domain/Constants/Errors.cs index b7e6149..65bb0cd 100644 --- a/src/agent/NetworkResurrector.Agent.Domain/Constants/Errors.cs +++ b/src/agent/NetworkResurrector.Agent.Domain/Constants/Errors.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NetworkResurrector.Agent.Domain.Errors +namespace NetworkResurrector.Agent.Domain.Errors { public struct Errors { diff --git a/src/agent/NetworkResurrector.Agent/Extensions/Serilog/AgentCodeEventEnricher.cs b/src/agent/NetworkResurrector.Agent/Extensions/Serilog/AgentCodeEventEnricher.cs new file mode 100644 index 0000000..c673ded --- /dev/null +++ b/src/agent/NetworkResurrector.Agent/Extensions/Serilog/AgentCodeEventEnricher.cs @@ -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("Agent:Code") ?? "N/A"; + logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty("Agent", agentCode)); + } + } +} diff --git a/src/agent/NetworkResurrector.Agent/Extensions/Serilog/Sinks/MSSqlServer/ColumnOptionsExtension.cs b/src/agent/NetworkResurrector.Agent/Extensions/Serilog/Sinks/MSSqlServer/ColumnOptionsExtension.cs new file mode 100644 index 0000000..be057af --- /dev/null +++ b/src/agent/NetworkResurrector.Agent/Extensions/Serilog/Sinks/MSSqlServer/ColumnOptionsExtension.cs @@ -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 + { + new SqlColumn() { ColumnName = "Agent", DataType = System.Data.SqlDbType.VarChar, DataLength = 100 } + }; + + return columnOptions; + } + } +} diff --git a/src/agent/NetworkResurrector.Agent/Program.cs b/src/agent/NetworkResurrector.Agent/Program.cs index d7627ce..d279ac6 100644 --- a/src/agent/NetworkResurrector.Agent/Program.cs +++ b/src/agent/NetworkResurrector.Agent/Program.cs @@ -1,6 +1,8 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; +using NetworkResurrector.Agent.Extensions.Serilog; +using NetworkResurrector.Agent.Extensions.Serilog.Sinks.MSSqlServer; using Serilog; using Serilog.Core; using Serilog.Events; @@ -39,17 +41,14 @@ namespace NetworkResurrector.Agent 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" }; + var columnOptions = new ColumnOptions().AsStandard(); + var mssqlSinkOptions = new MSSqlServerSinkOptions() { AutoCreateSqlTable = true, TableName = "__Logs_Agent" }; Log.Logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(loggingLevelSwitch) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .Enrich.FromLogContext() + .Enrich.With(new AgentCodeEventEnricher(configuration)) .WriteTo.Console() .WriteTo.MSSqlServer(connectionString, mssqlSinkOptions, columnOptions: columnOptions) .CreateLogger();