Archived
0
0
Fork 0

add stopping of services

This commit is contained in:
Daryl Ronningen 2022-11-19 21:17:41 -08:00
parent 9771a55394
commit 4015f1b3e5
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
9 changed files with 89 additions and 31 deletions

View file

@ -5,10 +5,10 @@ using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using netsd.Utils; using netsd.Utils;
using NetsdRPCs;
using Serilog; using Serilog;
using Serilog.Core; using Serilog.Core;
using Serilog.Events; using Serilog.Events;
using StartGrpc;
namespace netsd.Handlers; namespace netsd.Handlers;
@ -39,15 +39,15 @@ public class Init
var app = _socketBuilder.Build(); var app = _socketBuilder.Build();
app.MapGrpcService<StartRPC>(); app.MapGrpcService<NetsdRPCs>();
app.Run(); app.Run();
} }
} }
public class StartRPC : StartRpc.StartRpcBase public class NetsdRPCs : Messages.MessagesBase
{ {
public override async Task<StartReply> StartService(StartRequest request, ServerCallContext context) public override async Task<StartStopReply> StartService(StartRequest request, ServerCallContext context)
{ {
Init.Logger.Information($"Received request to start service {Path.GetFileName(request.Path)}."); Init.Logger.Information($"Received request to start service {Path.GetFileName(request.Path)}.");
@ -59,6 +59,23 @@ public class StartRPC : StartRpc.StartRpcBase
Globals.Services.Add(startService); Globals.Services.Add(startService);
return new StartReply { Success = true }; return new StartStopReply { Success = true };
}
public override async Task<StartStopReply> StopService(StopRequest request, ServerCallContext context)
{
Init.Logger.Information($"Received request to stop service {request.Name}.");
var findService = Globals.Services.FindAll(service => service.Name == request.Name);
if (findService.Count == 0)
return new StartStopReply { Success = false, Error = $"Could not find service with name {request.Name}!" };
await findService.First().StopServiceAsync();
Globals.Services.Remove(findService.First());
Init.Logger.Information($"Stopped service {request.Name}!");
return new StartStopReply { Success = true };
} }
} }

View file

@ -1,11 +1,11 @@
using netsd.Utils; using netsd.Utils;
using StartGrpc; using NetsdRPCs;
namespace netsd.Handlers; namespace netsd.Handlers;
public class Start public static class Start
{ {
public async Task StartAsync(string? ServiceFilePath = null, string? ServiceName = null) public static async Task StartAsync(string? ServiceFilePath = null, string? ServiceName = null)
{ {
if (ServiceFilePath == null && ServiceName != null) if (ServiceFilePath == null && ServiceName != null)
{ {
@ -21,7 +21,7 @@ public class Start
using var channel = UDSConnector.CreateChannel(); using var channel = UDSConnector.CreateChannel();
var client = new StartRpc.StartRpcClient(channel); var client = new Messages.MessagesClient(channel);
var reply = await client.StartServiceAsync(new StartRequest { Path = ServiceFilePath }); var reply = await client.StartServiceAsync(new StartRequest { Path = ServiceFilePath });
Console.WriteLine($"{reply.Success}"); Console.WriteLine($"{reply.Success}");

17
netsd/Handlers/Stop.cs Normal file
View file

@ -0,0 +1,17 @@
using netsd.Utils;
using NetsdRPCs;
namespace netsd.Handlers;
public static class Stop
{
public static async Task StopAsync(string ServiceName)
{
using var channel = UDSConnector.CreateChannel();
var client = new Messages.MessagesClient(channel);
var reply = await client.StopServiceAsync(new StopRequest { Name = ServiceName });
Console.WriteLine($"{reply.Success}");
}
}

View file

@ -4,27 +4,35 @@ using netsd.Handlers;
var initCommand = new Command("init", "Run and bring up all default services"); var initCommand = new Command("init", "Run and bring up all default services");
var startCommand = new Command("start", "Starts up a netsd service"); var startCommand = new Command("start", "Starts up a netsd service");
var startServiceName = new Argument<string>("file", "The name of the service you want to start"); var startServiceName = new Argument<string>("name", "The name of the service you want to start");
var startServicePath = new Option<FileInfo>("--file", "The path to the service file"); var startServicePath = new Option<FileInfo>("--file", "The path to the service file");
var stopCommand = new Command("stop", "Stops a running netsd service");
var stopServiceName = new Argument<string>("name", "The name of the service you want to stop");
startServiceName.SetDefaultValue(""); startServiceName.SetDefaultValue("");
startServicePath.AddAlias("-f"); startServicePath.AddAlias("-f");
startCommand.AddArgument(startServiceName); startCommand.AddArgument(startServiceName);
startCommand.AddOption(startServicePath); startCommand.AddOption(startServicePath);
stopCommand.AddArgument(stopServiceName);
var rootCommand = new RootCommand("Linux service manager written in C#"); var rootCommand = new RootCommand("Linux service manager written in C#");
rootCommand.AddCommand(initCommand); rootCommand.AddCommand(initCommand);
rootCommand.AddCommand(startCommand); rootCommand.AddCommand(startCommand);
rootCommand.AddCommand(stopCommand);
initCommand.SetHandler(() => new Init()); initCommand.SetHandler(() => new Init());
startCommand.SetHandler(async (file, name) => startCommand.SetHandler(async (file, name) =>
{ {
if (name.Length != 0) if (name.Length != 0)
await new Start().StartAsync(ServiceName: name); await Start.StartAsync(ServiceName: name);
else else
await new Start().StartAsync(file.FullName); await Start.StartAsync(file.FullName);
}, startServicePath, startServiceName); }, startServicePath, startServiceName);
stopCommand.SetHandler(async name => { await Stop.StopAsync(name); }, stopServiceName);
return await rootCommand.InvokeAsync(args); return await rootCommand.InvokeAsync(args);

View file

@ -1,18 +0,0 @@
syntax = "proto3";
option csharp_namespace = "StartGrpc";
package netsd;
service StartRpc {
rpc StartService (StartRequest) returns (StartReply);
}
message StartRequest {
string Path = 1;
}
message StartReply {
bool Success = 1;
optional string Error = 2;
}

23
netsd/Protos/rpcs.proto Normal file
View file

@ -0,0 +1,23 @@
syntax = "proto3";
option csharp_namespace = "NetsdRPCs";
package netsd;
service Messages {
rpc StartService (StartRequest) returns (StartStopReply);
rpc StopService (StopRequest) returns (StartStopReply);
}
message StartRequest {
string Path = 1;
}
message StartStopReply {
bool Success = 1;
optional string Error = 2;
}
message StopRequest {
string Name = 1;
}

View file

@ -18,3 +18,4 @@ public class ServiceParser
return sv; return sv;
} }
} }

View file

@ -9,9 +9,12 @@ public class StartService
private readonly ServiceParser _parseServiceFile; private readonly ServiceParser _parseServiceFile;
private readonly Process _process; private readonly Process _process;
private readonly string _servicePath; private readonly string _servicePath;
public readonly string Name;
private StartService(string servicePath, ServiceParser parseServiceFile) private StartService(string servicePath, ServiceParser parseServiceFile)
{ {
Name = Path.GetFileName(servicePath).Split(".")[0];
_parseServiceFile = parseServiceFile; _parseServiceFile = parseServiceFile;
_servicePath = servicePath; _servicePath = servicePath;
_process = new Process(); _process = new Process();
@ -43,6 +46,13 @@ public class StartService
return new StartService(servicePath, parseServiceFile); return new StartService(servicePath, parseServiceFile);
} }
public async Task StopServiceAsync()
{
_process.Kill();
await _process.WaitForExitAsync();
_process.Dispose();
}
public async void StartServiceAsync() public async void StartServiceAsync()
{ {
_process.Start(); _process.Start();

View file

@ -42,7 +42,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Protobuf Include="Protos\Start.proto" /> <Protobuf Include="Protos\rpcs.proto" />
</ItemGroup> </ItemGroup>
</Project> </Project>