diff --git a/netsd/Handlers/Init.cs b/netsd/Handlers/Init.cs index 533ad15..86fc178 100644 --- a/netsd/Handlers/Init.cs +++ b/netsd/Handlers/Init.cs @@ -5,10 +5,10 @@ using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using netsd.Utils; +using NetsdRPCs; using Serilog; using Serilog.Core; using Serilog.Events; -using StartGrpc; namespace netsd.Handlers; @@ -39,15 +39,15 @@ public class Init var app = _socketBuilder.Build(); - app.MapGrpcService(); + app.MapGrpcService(); app.Run(); } } -public class StartRPC : StartRpc.StartRpcBase +public class NetsdRPCs : Messages.MessagesBase { - public override async Task StartService(StartRequest request, ServerCallContext context) + public override async Task StartService(StartRequest request, ServerCallContext context) { 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); - return new StartReply { Success = true }; + return new StartStopReply { Success = true }; + } + + public override async Task 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 }; } } diff --git a/netsd/Handlers/Start.cs b/netsd/Handlers/Start.cs index 3280d9b..6249890 100644 --- a/netsd/Handlers/Start.cs +++ b/netsd/Handlers/Start.cs @@ -1,11 +1,11 @@ using netsd.Utils; -using StartGrpc; +using NetsdRPCs; 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) { @@ -21,7 +21,7 @@ public class Start 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 }); Console.WriteLine($"{reply.Success}"); diff --git a/netsd/Handlers/Stop.cs b/netsd/Handlers/Stop.cs new file mode 100644 index 0000000..4ecdb2d --- /dev/null +++ b/netsd/Handlers/Stop.cs @@ -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}"); + } +} diff --git a/netsd/Program.cs b/netsd/Program.cs index ecd896b..3269ec0 100644 --- a/netsd/Program.cs +++ b/netsd/Program.cs @@ -4,27 +4,35 @@ using netsd.Handlers; var initCommand = new Command("init", "Run and bring up all default services"); var startCommand = new Command("start", "Starts up a netsd service"); -var startServiceName = new Argument("file", "The name of the service you want to start"); +var startServiceName = new Argument("name", "The name of the service you want to start"); var startServicePath = new Option("--file", "The path to the service file"); +var stopCommand = new Command("stop", "Stops a running netsd service"); +var stopServiceName = new Argument("name", "The name of the service you want to stop"); + startServiceName.SetDefaultValue(""); startServicePath.AddAlias("-f"); startCommand.AddArgument(startServiceName); startCommand.AddOption(startServicePath); +stopCommand.AddArgument(stopServiceName); + var rootCommand = new RootCommand("Linux service manager written in C#"); rootCommand.AddCommand(initCommand); rootCommand.AddCommand(startCommand); +rootCommand.AddCommand(stopCommand); initCommand.SetHandler(() => new Init()); startCommand.SetHandler(async (file, name) => { if (name.Length != 0) - await new Start().StartAsync(ServiceName: name); + await Start.StartAsync(ServiceName: name); else - await new Start().StartAsync(file.FullName); + await Start.StartAsync(file.FullName); }, startServicePath, startServiceName); +stopCommand.SetHandler(async name => { await Stop.StopAsync(name); }, stopServiceName); + return await rootCommand.InvokeAsync(args); diff --git a/netsd/Protos/Start.proto b/netsd/Protos/Start.proto deleted file mode 100644 index 558de5b..0000000 --- a/netsd/Protos/Start.proto +++ /dev/null @@ -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; -} diff --git a/netsd/Protos/rpcs.proto b/netsd/Protos/rpcs.proto new file mode 100644 index 0000000..f69aeb1 --- /dev/null +++ b/netsd/Protos/rpcs.proto @@ -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; +} diff --git a/netsd/Utils/ServiceParser.cs b/netsd/Utils/ServiceParser.cs index f360104..3bf8a14 100644 --- a/netsd/Utils/ServiceParser.cs +++ b/netsd/Utils/ServiceParser.cs @@ -18,3 +18,4 @@ public class ServiceParser return sv; } } + diff --git a/netsd/Utils/StartService.cs b/netsd/Utils/StartService.cs index 7bd1654..0f049c3 100644 --- a/netsd/Utils/StartService.cs +++ b/netsd/Utils/StartService.cs @@ -9,9 +9,12 @@ public class StartService private readonly ServiceParser _parseServiceFile; private readonly Process _process; private readonly string _servicePath; + public readonly string Name; private StartService(string servicePath, ServiceParser parseServiceFile) { + Name = Path.GetFileName(servicePath).Split(".")[0]; + _parseServiceFile = parseServiceFile; _servicePath = servicePath; _process = new Process(); @@ -43,6 +46,13 @@ public class StartService return new StartService(servicePath, parseServiceFile); } + public async Task StopServiceAsync() + { + _process.Kill(); + await _process.WaitForExitAsync(); + _process.Dispose(); + } + public async void StartServiceAsync() { _process.Start(); diff --git a/netsd/netsd.csproj b/netsd/netsd.csproj index 38299be..33bf334 100644 --- a/netsd/netsd.csproj +++ b/netsd/netsd.csproj @@ -42,7 +42,7 @@ - +