ShutdownMachineHandler
parent
b08748221a
commit
e92c143258
|
@ -1,5 +1,8 @@
|
|||
using MediatR;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NetworkResurrector.Agent.PublishedLanguage.Dto;
|
||||
using NetworkResurrector.Agent.Wrapper.Services;
|
||||
using NetworkResurrector.Api.Domain.Constants;
|
||||
using NetworkResurrector.Api.Domain.Repositories;
|
||||
using NetworkResurrector.Api.PublishedLanguage.Commands;
|
||||
|
@ -16,12 +19,16 @@ namespace NetworkResurrector.Api.Application.CommandHandlers
|
|||
private readonly ILogger<ShutdownMachineHandler> _logger;
|
||||
private readonly IResurrectorService _resurrectorService;
|
||||
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;
|
||||
_resurrectorService=resurrectorService;
|
||||
_repository=repository;
|
||||
_resurrectorAgentService=resurrectorAgentService;
|
||||
_configuration=configuration;
|
||||
}
|
||||
|
||||
public async Task<MachineShutdown> Handle(ShutdownMachine command, CancellationToken cancellationToken)
|
||||
|
@ -32,18 +39,28 @@ namespace NetworkResurrector.Api.Application.CommandHandlers
|
|||
|
||||
//log activity
|
||||
|
||||
var ipAddressOrMachineName = machine.IPv4Address ?? machine.MachineName;
|
||||
MachineShutdown result;
|
||||
|
||||
switch (powerConfiguration.Performer.PerformerCode)
|
||||
{
|
||||
case PowerActionPerformers.NETWORK_RESURRECTOR_SERVER:
|
||||
var pingResult = await _resurrectorService.Shutdown(machine.IPv4Address ?? machine.MachineName);
|
||||
result = new MachineShutdown(pingResult.Success, pingResult.Status);
|
||||
if (command.Delay.HasValue || command.Force)
|
||||
_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;
|
||||
|
||||
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;
|
||||
|
||||
default:
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration
|
|||
{
|
||||
builder.ToTable("Machine").HasKey(key => key.MachineId);
|
||||
builder.Property(z => z.MachineId).ValueGeneratedOnAdd();
|
||||
builder.HasOne(z => z.Agent).WithOne();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,5 +8,7 @@
|
|||
public string MACAddress { get; set; }
|
||||
public string IPv4Address { get; set; }
|
||||
public string Description { get; set; }
|
||||
|
||||
public MachineAgent Agent { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,5 +6,7 @@ namespace NetworkResurrector.Api.PublishedLanguage.Commands
|
|||
public class ShutdownMachine : Command<MachineShutdown>
|
||||
{
|
||||
public int MachineId { get; set; }
|
||||
public int? Delay { get; set; }
|
||||
public bool Force { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ using Microsoft.Extensions.Hosting;
|
|||
using NDB.Extensions.Swagger;
|
||||
using NDB.Extensions.Swagger.Constants;
|
||||
using NDB.Security.Authentication.Identity;
|
||||
using NetworkResurrector.Agent.Wrapper;
|
||||
using NetworkResurrector.Api.Application;
|
||||
using NetworkResurrector.Api.Domain.Data;
|
||||
using NetworkResurrector.Server.Wrapper;
|
||||
|
@ -49,6 +50,9 @@ namespace NetworkResurrector.Api
|
|||
// Add network resurrector server services
|
||||
services.UseResurrectorServices(_configuration.GetSection("NetworkResurrectorServer")["BaseAddress"]);
|
||||
|
||||
// Add network resurrector agent services
|
||||
services.UseResurrectorAgentServices();
|
||||
|
||||
// Data access
|
||||
services.AddDataAccess();
|
||||
|
||||
|
|
|
@ -12,6 +12,9 @@
|
|||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"Service": {
|
||||
"Code": "NETWORK_RESURRECTOR_API"
|
||||
},
|
||||
"IdentityServer": {
|
||||
//"BaseAddress": "http://localhost:5063/"
|
||||
"BaseAddress": "https://toodle.ddns.net/identity-server-api/"
|
||||
|
|
Loading…
Reference in New Issue