Compare commits
39 Commits
v0.1.3
...
7c85e338fa
| Author | SHA1 | Date | |
|---|---|---|---|
| 7c85e338fa | |||
| c01401eb07 | |||
| 7c06f7ee06 | |||
| 6eed3c764e | |||
| f207440ae6 | |||
| ef187f8750 | |||
| 570b5abfa0 | |||
| 8f938f999e | |||
| 2249d1a776 | |||
| 37de21f06b | |||
| b9d5ade0f1 | |||
| 921b4a9664 | |||
| 0e9555e3f7 | |||
| 19c7aaaca1 | |||
| 068c67d0d9 | |||
| 4c6635f989 | |||
| 98080df509 | |||
| 286f72eac7 | |||
| d49977815d | |||
| b012693c21 | |||
| e385eb94f4 | |||
| 285ff89cb0 | |||
| fe45d901e4 | |||
| ba6e201adc | |||
| 5867cfc3e1 | |||
| 8771e1ee02 | |||
| 2be4331a6e | |||
| 98754be109 | |||
| 9ee2750534 | |||
| 9920c94a8b | |||
| b7bc477d2e | |||
| ab11665665 | |||
| 37468b6785 | |||
| 471767c4ed | |||
| 596baba5ef | |||
| 12390031f9 | |||
| daed8c1462 | |||
| 7e5e295590 | |||
| cb91ca3159 |
@@ -5,21 +5,20 @@ on:
|
||||
push:
|
||||
branches: [ "development", "main", "staging" ]
|
||||
tags: [ "v*.*.*" ]
|
||||
pull_request:
|
||||
branches: [ "development", "main", "staging" ]
|
||||
|
||||
env:
|
||||
DOTNET_VERSION: '8.0.x'
|
||||
DOCKER_IMAGE_NAME: watcher-server
|
||||
REGISTRY_URL: git.triggermeelmo.com
|
||||
DOCKER_PLATFORMS: 'linux/amd64,linux/arm64'
|
||||
TAG: ${{ github.ref == 'refs/heads/main' && 'latest' || github.ref == 'refs/heads/development' && 'development' || github.ref_type == 'tag' && github.ref_name || 'pr' }}
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
dotnet-build-and-test:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
RUNNER_TOOL_CACHE: /toolcache # Runner Tool Cache
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
@@ -44,11 +43,12 @@ jobs:
|
||||
|
||||
set-tag:
|
||||
name: Set Tag Name
|
||||
needs: [dotnet-build-and-test]
|
||||
#if: ${{ !failure() && !cancelled() && github.event_name != 'pull_request' }}
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
RUNNER_TOOL_CACHE: /toolcache
|
||||
outputs:
|
||||
tag_name: ${{ steps.set_tag.outputs.tag_name }}
|
||||
should_tag: ${{ steps.set_tag.outputs.should_tag }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
@@ -72,21 +72,33 @@ jobs:
|
||||
major=$((major + 1))
|
||||
minor=0
|
||||
patch=0
|
||||
new_tag="v${major}.${minor}.${patch}"
|
||||
echo "tag_name=${new_tag}" >> $GITHUB_OUTPUT
|
||||
echo "should_tag=true" >> $GITHUB_OUTPUT
|
||||
echo "Creating new major version tag: ${new_tag}"
|
||||
|
||||
elif [[ "${GITHUB_REF}" == "refs/heads/development" ]]; then
|
||||
minor=$((minor + 1))
|
||||
patch=0
|
||||
else
|
||||
patch=$((patch + 1))
|
||||
fi
|
||||
|
||||
new_tag="v${major}.${minor}.${patch}"
|
||||
echo "tag_name=${new_tag}" >> $GITHUB_OUTPUT
|
||||
echo "should_tag=true" >> $GITHUB_OUTPUT
|
||||
echo "Creating new minor version tag: ${new_tag}"
|
||||
|
||||
elif [[ "${GITHUB_REF}" == "refs/heads/staging" ]]; then
|
||||
patch=$((patch + 1))
|
||||
new_tag="v${major}.${minor}.${patch}"
|
||||
echo "tag_name=${new_tag}" >> $GITHUB_OUTPUT
|
||||
echo "should_tag=true" >> $GITHUB_OUTPUT
|
||||
echo "Creating new patch version tag: ${new_tag}"
|
||||
fi
|
||||
|
||||
docker-build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
RUNNER_TOOL_CACHE: /toolcache # Runner Tool Cache
|
||||
needs: [build-and-test, set-tag]
|
||||
needs: [dotnet-build-and-test, set-tag]
|
||||
if: |
|
||||
needs.set-tag.outputs.should_tag == 'true' &&
|
||||
github.event_name != 'pull_request'
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
@@ -110,10 +122,11 @@ jobs:
|
||||
|
||||
tag:
|
||||
name: Create Tag
|
||||
needs: [docker-build-and-push]
|
||||
needs: [docker-build-and-push, set-tag]
|
||||
if: |
|
||||
needs.set-tag.outputs.should_tag == 'true' &&
|
||||
github.event_name != 'pull_request'
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
RUNNER_TOOL_CACHE: /toolcache
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
@@ -128,5 +141,6 @@ jobs:
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
echo "Creating new tag: ${{ needs.set-tag.outputs.tag_name }}"
|
||||
git tag ${{ needs.set-tag.outputs.tag_name }}
|
||||
git push origin ${{ needs.set-tag.outputs.tag_name }}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -80,10 +81,8 @@ public class MetricDto
|
||||
|
||||
public class ServiceDto
|
||||
{
|
||||
public int ServerId { get; set; } // Vom Watcher-Server zugewiesene ID des Hosts
|
||||
public String? ContainerId { get; set; }
|
||||
public String? Image { get; set; }
|
||||
public String? Name { get; set; }
|
||||
public required int Server_id { get; set; } // Vom Watcher-Server zugewiesene ID des Hosts
|
||||
public required JsonContent Containers { get; set; }
|
||||
}
|
||||
|
||||
[ApiController]
|
||||
@@ -234,33 +233,77 @@ public class MonitoringController : Controller
|
||||
.Select(e => e.ErrorMessage)
|
||||
.ToList();
|
||||
|
||||
_logger.LogError("Ungültiger ServiceDetection-Payload.");
|
||||
return BadRequest(new { error = "Ungültiger Payload", details = errors });
|
||||
_logger.LogError("Invalid ServiceDetection-Payload.");
|
||||
return BadRequest(new { error = "Invalid Payload", details = errors });
|
||||
}
|
||||
|
||||
// Metrics für Container, die zu ServerId gehören, löschen
|
||||
// Json zu was brauchbarem machen
|
||||
string containersJson = await dto.Containers.ReadAsStringAsync();
|
||||
List<Container> newContainers = JsonSerializer.Deserialize<List<Container>>(containersJson)?? new List<Container>();;
|
||||
|
||||
// Container, die zu ServerId gehören löschen
|
||||
foreach (Container c in newContainers)
|
||||
{
|
||||
c.ServerId = dto.Server_id;
|
||||
// Debug Logs
|
||||
// TODO entfernen wenn fertig getestet
|
||||
Console.WriteLine("---------");
|
||||
Console.WriteLine("ServerId: " + c.ServerId);
|
||||
Console.WriteLine("ContainerId: " + c.ContainerId);
|
||||
Console.WriteLine("Name: " + c.Name);
|
||||
Console.WriteLine("Image: " + c.Image);
|
||||
Console.WriteLine("---------");
|
||||
|
||||
// Container neu in Datenbank einlesen
|
||||
}
|
||||
|
||||
// Container Objekte erstellen
|
||||
//List<Container> newContainers = ParseServiceDiscoveryInput(dto.Server_id, containers);
|
||||
|
||||
// Liste aller Container, die bereits der übergebenen ServerId zugewiesen sind
|
||||
List<Container> existingContainers = _context.Containers
|
||||
.Where(c => c.ServerId == dto.Server_id)
|
||||
.ToList();
|
||||
|
||||
|
||||
// Logik, um Container, die mit dem Payload kamen zu verarbeiten
|
||||
foreach (Container c in newContainers)
|
||||
{
|
||||
// Überprüfen, ob ein übergebener Container bereits für den Host registriert ist
|
||||
if (existingContainers.Contains(c))
|
||||
{
|
||||
_logger.LogInformation("Container with id " + c.ContainerId + " already exists.");
|
||||
}
|
||||
// Container auf einen Host/Server registrieren
|
||||
else
|
||||
{
|
||||
// Container in Datenbank einlesen
|
||||
try
|
||||
{
|
||||
Container container = new Container
|
||||
{
|
||||
ServerId = dto.ServerId,
|
||||
ContainerId = dto.ContainerId,
|
||||
Image = dto.Name,
|
||||
Name = dto.Name
|
||||
};
|
||||
|
||||
_logger.LogInformation(container.Name + " added for Host " + container.ServerId);
|
||||
|
||||
_context.Containers.Add(container);
|
||||
_context.Containers.Add(c);
|
||||
await _context.SaveChangesAsync();
|
||||
_logger.LogInformation(c.Name + " added for Host " + c.ServerId);
|
||||
}
|
||||
catch (SqliteException e)
|
||||
{
|
||||
_logger.LogError("ServiceDiscovery failed: " + e.Message);
|
||||
_logger.LogError("Error writing new Containers to Database: " + e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Logik um abgeschaltene Container aus der Datenbank zu entfernen
|
||||
foreach (Container c in existingContainers)
|
||||
{
|
||||
// Abfrage, ob bereits vorhandener Container im Payload vorhanden war
|
||||
if (!newContainers.Contains(c))
|
||||
{
|
||||
// Container entfernen
|
||||
_context.Containers.Remove(c);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
// Metrics für den Container entfernen
|
||||
//Todo
|
||||
|
||||
_logger.LogInformation("Container " + c.Name + " (" + c.Id + ") on Host-Id " + c.ServerId + " was successfully removed from the database.");
|
||||
}
|
||||
}
|
||||
|
||||
return Ok();
|
||||
@@ -268,7 +311,6 @@ public class MonitoringController : Controller
|
||||
}
|
||||
|
||||
// Durchschnittliche Werte Berechnen
|
||||
[HttpGet("median")]
|
||||
public async Task<IActionResult> CalculateMedian(string Metric, int HoursToMonitor, int ServerId)
|
||||
{
|
||||
// Aktuelle Zeit - X Stunden = letzter Wert, der berücksichtigt werden soll
|
||||
@@ -368,4 +410,13 @@ public class MonitoringController : Controller
|
||||
return metric_input;
|
||||
}
|
||||
|
||||
private List<Container> ParseServiceDiscoveryInput(int server_id, List<Container> containers)
|
||||
{
|
||||
List<Container> containerList = new List<Container>();
|
||||
|
||||
// JSON-Objekt auslesen und Container-Objekte erstellen
|
||||
|
||||
|
||||
return containerList;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
using Serilog;
|
||||
|
||||
@@ -36,11 +37,17 @@ builder.Services.AddHttpContextAccessor();
|
||||
|
||||
// Storage Singleton
|
||||
builder.Services.AddSingleton<IDashboardStore, DashboardStore>();
|
||||
builder.Services.AddSingleton<ISystemStore, SystemStore>();
|
||||
|
||||
// Background Services
|
||||
builder.Services.AddHostedService<NetworkCheck>();
|
||||
builder.Services.AddHostedService<DatabaseCheck>();
|
||||
|
||||
// Swagger API-Dokumentation
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
{
|
||||
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Watcher-Server API", Version = "v1" });
|
||||
});
|
||||
|
||||
// ---------- Konfiguration ----------
|
||||
DotNetEnv.Env.Load();
|
||||
@@ -204,19 +211,27 @@ using (var scope = app.Services.CreateScope())
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
// 🔹 Swagger aktivieren
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Watcher-Server API v1");
|
||||
options.RoutePrefix = "api/v1/swagger";
|
||||
});
|
||||
|
||||
// 🔹 Authentifizierung & Autorisierung
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseStaticFiles();
|
||||
|
||||
// 🔹 MVC-Routing
|
||||
app.MapControllerRoute(
|
||||
name: "default",
|
||||
pattern: "{controller=Home}/{action=Index}/{id?}"
|
||||
|
||||
9
Watcher/Services/ISystemStore.cs
Normal file
9
Watcher/Services/ISystemStore.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Watcher.Services;
|
||||
|
||||
public interface ISystemStore
|
||||
{
|
||||
Boolean NewVersionAvailable { get; set; }
|
||||
|
||||
Double DatabaseSize { get; set; }
|
||||
|
||||
}
|
||||
42
Watcher/Services/SystemMangement.cs
Normal file
42
Watcher/Services/SystemMangement.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Watcher.Services;
|
||||
|
||||
public class SystemManagement : BackgroundService
|
||||
{
|
||||
private readonly ILogger<NetworkCheck> _logger;
|
||||
|
||||
private ISystemStore _SystemStore;
|
||||
|
||||
public SystemManagement(ILogger<NetworkCheck> logger, ISystemStore SystemStore)
|
||||
{
|
||||
_logger = logger;
|
||||
_SystemStore = SystemStore;
|
||||
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
// Todo: Umstellen auf einmal alle 24h
|
||||
var timer = new PeriodicTimer(TimeSpan.FromSeconds(30));
|
||||
|
||||
while (await timer.WaitForNextTickAsync(stoppingToken))
|
||||
{
|
||||
// Hintergrundprozess abwarten
|
||||
await checkForNewDockerImageVersion();
|
||||
|
||||
// 5 Sekdunden Offset
|
||||
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
|
||||
}
|
||||
}
|
||||
|
||||
public Task checkForNewDockerImageVersion()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task createDailySqliteBackup()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
9
Watcher/Services/SystemStore.cs
Normal file
9
Watcher/Services/SystemStore.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Watcher.Services;
|
||||
|
||||
public class SystemStore: ISystemStore
|
||||
{
|
||||
public Boolean NewVersionAvailable { get; set; }
|
||||
|
||||
public Double DatabaseSize { get; set; }
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.6" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user