ShutdownMachineHandler

master
Tudor Stanciu 2022-01-14 19:24:08 +02:00
parent b08748221a
commit e92c143258
7 changed files with 34 additions and 4 deletions

View File

@ -1,5 +1,8 @@
using MediatR; using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NetworkResurrector.Agent.PublishedLanguage.Dto;
using NetworkResurrector.Agent.Wrapper.Services;
using NetworkResurrector.Api.Domain.Constants; using NetworkResurrector.Api.Domain.Constants;
using NetworkResurrector.Api.Domain.Repositories; using NetworkResurrector.Api.Domain.Repositories;
using NetworkResurrector.Api.PublishedLanguage.Commands; using NetworkResurrector.Api.PublishedLanguage.Commands;
@ -16,12 +19,16 @@ namespace NetworkResurrector.Api.Application.CommandHandlers
private readonly ILogger<ShutdownMachineHandler> _logger; private readonly ILogger<ShutdownMachineHandler> _logger;
private readonly IResurrectorService _resurrectorService; private readonly IResurrectorService _resurrectorService;
private readonly INetworkRepository _repository; private readonly INetworkRepository _repository;
private readonly IResurrectorAgentService _resurrectorAgentService;
private readonly IConfiguration _configuration;
public ShutdownMachineHandler(ILogger<ShutdownMachineHandler> logger, IResurrectorService resurrectorService, INetworkRepository repository) public ShutdownMachineHandler(ILogger<ShutdownMachineHandler> logger, IResurrectorService resurrectorService, INetworkRepository repository, IResurrectorAgentService resurrectorAgentService, IConfiguration configuration)
{ {
_logger=logger; _logger=logger;
_resurrectorService=resurrectorService; _resurrectorService=resurrectorService;
_repository=repository; _repository=repository;
_resurrectorAgentService=resurrectorAgentService;
_configuration=configuration;
} }
public async Task<MachineShutdown> Handle(ShutdownMachine command, CancellationToken cancellationToken) public async Task<MachineShutdown> Handle(ShutdownMachine command, CancellationToken cancellationToken)
@ -32,18 +39,28 @@ namespace NetworkResurrector.Api.Application.CommandHandlers
//log activity //log activity
var ipAddressOrMachineName = machine.IPv4Address ?? machine.MachineName;
MachineShutdown result; MachineShutdown result;
switch (powerConfiguration.Performer.PerformerCode) switch (powerConfiguration.Performer.PerformerCode)
{ {
case PowerActionPerformers.NETWORK_RESURRECTOR_SERVER: case PowerActionPerformers.NETWORK_RESURRECTOR_SERVER:
var pingResult = await _resurrectorService.Shutdown(machine.IPv4Address ?? machine.MachineName); if (command.Delay.HasValue || command.Force)
result = new MachineShutdown(pingResult.Success, pingResult.Status); _logger.LogWarning($"Shutdown options are not implemented for '{PowerActionPerformers.NETWORK_RESURRECTOR_SERVER}' performer.");
var shutdownResult = await _resurrectorService.Shutdown(ipAddressOrMachineName);
result = new MachineShutdown(shutdownResult.Success, shutdownResult.Status);
break; break;
case PowerActionPerformers.NETWORK_RESURRECTOR_AGENT: case PowerActionPerformers.NETWORK_RESURRECTOR_AGENT:
if (machine.Agent == null)
throw new Exception($"Cannot use network resurrector agent as shutdown performer for machine '{ipAddressOrMachineName}' because it is not configured.");
result = new MachineShutdown(true, ""); var owner = new ActionOwner(_configuration.GetValue<string>("Service:Code"));
var options = new ActionOptions(command.Delay ?? 0, command.Force);
var shutdownResultByAgent = await _resurrectorAgentService.Shutdown(ipAddressOrMachineName, machine.Agent.AgentPort, owner, options);
var status = shutdownResultByAgent.Success ? $"Machine '{ipAddressOrMachineName}' successfully shutdown." : $"Machine '{ipAddressOrMachineName}' could not be shutdown.";
result = new MachineShutdown(shutdownResultByAgent.Success, status);
break; break;
default: default:

View File

@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="NDB.Application.DataContracts" Version="$(NDBApplicationPackageVersion)" /> <PackageReference Include="NDB.Application.DataContracts" Version="$(NDBApplicationPackageVersion)" />
<PackageReference Include="NetworkResurrector.Agent.Wrapper" Version="1.0.3" />
<PackageReference Include="NetworkResurrector.Server.Wrapper" Version="1.0.3.2" /> <PackageReference Include="NetworkResurrector.Server.Wrapper" Version="1.0.3.2" />
</ItemGroup> </ItemGroup>

View File

@ -10,6 +10,7 @@ namespace NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration
{ {
builder.ToTable("Machine").HasKey(key => key.MachineId); builder.ToTable("Machine").HasKey(key => key.MachineId);
builder.Property(z => z.MachineId).ValueGeneratedOnAdd(); builder.Property(z => z.MachineId).ValueGeneratedOnAdd();
builder.HasOne(z => z.Agent).WithOne();
} }
} }
} }

View File

@ -8,5 +8,7 @@
public string MACAddress { get; set; } public string MACAddress { get; set; }
public string IPv4Address { get; set; } public string IPv4Address { get; set; }
public string Description { get; set; } public string Description { get; set; }
public MachineAgent Agent { get; set; }
} }
} }

View File

@ -6,5 +6,7 @@ namespace NetworkResurrector.Api.PublishedLanguage.Commands
public class ShutdownMachine : Command<MachineShutdown> public class ShutdownMachine : Command<MachineShutdown>
{ {
public int MachineId { get; set; } public int MachineId { get; set; }
public int? Delay { get; set; }
public bool Force { get; set; }
} }
} }

View File

@ -9,6 +9,7 @@ using Microsoft.Extensions.Hosting;
using NDB.Extensions.Swagger; using NDB.Extensions.Swagger;
using NDB.Extensions.Swagger.Constants; using NDB.Extensions.Swagger.Constants;
using NDB.Security.Authentication.Identity; using NDB.Security.Authentication.Identity;
using NetworkResurrector.Agent.Wrapper;
using NetworkResurrector.Api.Application; using NetworkResurrector.Api.Application;
using NetworkResurrector.Api.Domain.Data; using NetworkResurrector.Api.Domain.Data;
using NetworkResurrector.Server.Wrapper; using NetworkResurrector.Server.Wrapper;
@ -49,6 +50,9 @@ namespace NetworkResurrector.Api
// Add network resurrector server services // Add network resurrector server services
services.UseResurrectorServices(_configuration.GetSection("NetworkResurrectorServer")["BaseAddress"]); services.UseResurrectorServices(_configuration.GetSection("NetworkResurrectorServer")["BaseAddress"]);
// Add network resurrector agent services
services.UseResurrectorAgentServices();
// Data access // Data access
services.AddDataAccess(); services.AddDataAccess();

View File

@ -12,6 +12,9 @@
} }
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"Service": {
"Code": "NETWORK_RESURRECTOR_API"
},
"IdentityServer": { "IdentityServer": {
//"BaseAddress": "http://localhost:5063/" //"BaseAddress": "http://localhost:5063/"
"BaseAddress": "https://toodle.ddns.net/identity-server-api/" "BaseAddress": "https://toodle.ddns.net/identity-server-api/"