summary refs log tree commit diff
path: root/SystemdCtl/Controllers/UnitController.cs
blob: da1c5340480cab50718fdc51869058cf90a069b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Runtime.InteropServices;
using System.Text;
using ArcaneLibs.Extensions;
using ArcaneLibs.Extensions.Streams;
using LibSystemdCli;
using LibSystemdCli.Models;
using Microsoft.AspNetCore.Mvc;

namespace SystemdCtl.Controllers;
[ApiController]
[Route("/api/")]
public class UnitController : ControllerBase
{
    [HttpGet("listUnits")]
    public async IAsyncEnumerable<SystemdUnitListItem> GetUnits()
    {
        await foreach (var unit in SystemdExecutor.GetUnits())
        {
            yield return unit;
            await Response.Body.FlushAsync();
        }
    }
    
    // [HttpGet("unit/{serviceName}/logs")]
    // public async IAsyncEnumerable<SystemdJournalLogItem> GetUnitLogs(string serviceName, [FromQuery] int contextLines = 100)
    // {
    //     await foreach (var log in SystemdExecutor.GetUnitLogs(serviceName, contextLines: contextLines))
    //     {
    //         Console.WriteLine(log.Message);
    //         yield return log;
    //         await Response.Body.FlushAsync();
    //     }
    // }   
    [HttpGet("unit/{serviceName}/logs")]
    public async Task GetUnitLogs(string serviceName, [FromQuery] int contextLines = 100)
    {
        Response.ContentType = "application/json";
        await Response.StartAsync();
        await Response.Body.WriteAsync("[\n"u8.ToArray());
        await foreach (var log in SystemdExecutor.GetUnitLogs(serviceName, contextLines: contextLines))
        {
            Console.WriteLine(log.Message);
            var bytes = Encoding.UTF8.GetBytes($"  {log.ToJson(indent: false)},\n");
            await Response.Body.WriteAsync(bytes);
            await Response.Body.FlushAsync();
        }
        await Response.Body.WriteAsync("]\n"u8.ToArray());
        await Response.Body.FlushAsync();
        await Response.CompleteAsync();
    }
    
    [HttpGet("unit/{serviceName}/dataStream")]
    public async Task GetUnitDataStream(string serviceName, [FromQuery] int contextLines = 100)
    {
        Response.ContentType = "application/json";
        await Response.StartAsync();
        await Response.Body.WriteAsync("[\n"u8.ToArray());
        var oldData = await SystemdExecutor.GetUnitData(serviceName);
        while(true)
        {
            var data = await SystemdExecutor.GetUnitData(serviceName);
            data.SetOldData(oldData);
            var bytes = Encoding.UTF8.GetBytes($"  {data.ToJson(indent: false)},\n");
            await Response.Body.WriteAsync(bytes);
            await Response.Body.FlushAsync();
            oldData = data;
            await Task.Delay(1000);
        }
        await Response.Body.WriteAsync("]\n"u8.ToArray());
        await Response.Body.FlushAsync();
        await Response.CompleteAsync();
    }
    
    [HttpGet("unit/{serviceName}/data")]
    public Task<SystemdServiceData> GetUnitData(string serviceName) => SystemdExecutor.GetUnitData(serviceName);
    
    [HttpGet("unit/{serviceName}/start")]
    public Task StartUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"start {serviceName}");
    
    [HttpGet("unit/{serviceName}/stop")]
    public Task StopUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"stop {serviceName}");
    
    [HttpGet("unit/{serviceName}/restart")]
    public Task RestartUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"restart {serviceName}");
    
    [HttpGet("unit/{serviceName}/reload")]
    public Task ReloadUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"reload {serviceName}");
    
    [HttpGet("unit/{serviceName}/enable")]
    public Task EnableUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"enable {serviceName}");
    
    [HttpGet("unit/{serviceName}/disable")]
    public Task DisableUnit(string serviceName) => CommandExecutor.ExecuteCommand("systemctl", $"disable {serviceName}");
}