- Site detail: generate registration token with copy/paste docker-compose snippet - Docker snippet: pre-filled with token + agent hostname, ready to copy - Agents integration tests: register with valid token, heartbeat with API key, invalid token rejection (3 new tests) - TokenResult DTO for API client - IJSRuntime clipboard copy for docker-compose snippet
188 lines
7.3 KiB
Plaintext
188 lines
7.3 KiB
Plaintext
@page "/sites/{Id:guid}"
|
|
@attribute [Authorize]
|
|
@layout MainLayout
|
|
@inject HeimdallApiClient Api
|
|
@inject ISnackbar Snackbar
|
|
@inject NavigationManager Nav
|
|
@inject IJSRuntime Js
|
|
|
|
<MudText Typo="Typo.h4" Class="mb-6">
|
|
<MudIcon Icon="@Icons.Material.Filled.Videocam" Class="mr-2" />
|
|
Site : @_site?.Name
|
|
</MudText>
|
|
|
|
@if (_loading)
|
|
{
|
|
<MudProgressCircular Indeterminate="true" />
|
|
}
|
|
else if (_site is null)
|
|
{
|
|
<MudAlert Severity="Severity.Warning">Site introuvable</MudAlert>
|
|
}
|
|
else
|
|
{
|
|
<MudPaper Elevation="2" Class="pa-6 mb-6" Style="border-radius: 12px;">
|
|
<MudText Typo="Typo.h6" Class="mb-4">Informations</MudText>
|
|
<MudGrid>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Nom</MudText>
|
|
<MudText Typo="Typo.body1">@_site.Name</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Adresse</MudText>
|
|
<MudText Typo="Typo.body1">@(_site.Address ?? "-")</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Fuseau horaire</MudText>
|
|
<MudText Typo="Typo.body1">@_site.Timezone</MudText>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="6">
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Statut</MudText>
|
|
<MudText Color="@(_site.Enabled ? Color.Success : Color.Default)">
|
|
@(_site.Enabled ? "● Actif" : "○ Inactif")
|
|
</MudText>
|
|
</MudItem>
|
|
</MudGrid>
|
|
</MudPaper>
|
|
|
|
@* Token d'enregistrement *@
|
|
<MudPaper Elevation="2" Class="pa-6 mb-6" Style="border-radius: 12px;">
|
|
<MudText Typo="Typo.h6" Class="mb-4">
|
|
<MudIcon Icon="@Icons.Material.Filled.Key" Class="mr-1" />
|
|
Token d'enregistrement agent
|
|
</MudText>
|
|
|
|
@if (!string.IsNullOrEmpty(_generatedToken))
|
|
{
|
|
<MudAlert Severity="Severity.Warning" Class="mb-4">
|
|
<strong>Token d'enregistrement :</strong><br />
|
|
<code class="d-block mt-2 p-2" style="background:#fff;word-break:break-all;font-size:0.9rem;">
|
|
@_generatedToken
|
|
</code>
|
|
<MudText Typo="Typo.body2" Color="Color.Warning" Class="mt-2">
|
|
⚠ Copiez ce token maintenant. Il ne sera plus jamais affiché.
|
|
</MudText>
|
|
<MudText Typo="Typo.body2" Class="mt-1">
|
|
Expire le : @_tokenExpiresAt.ToString("g")
|
|
</MudText>
|
|
</MudAlert>
|
|
}
|
|
|
|
<MudButton Variant="Variant.Filled" Color="Color.Warning" OnClick="@GenerateToken" Disabled="@_generating">
|
|
@if (_generating) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
|
|
<MudIcon Icon="@Icons.Material.Filled.Refresh" Class="mr-1" />
|
|
@(!string.IsNullOrEmpty(_generatedToken) ? "Régénérer" : "Générer un token")
|
|
</MudButton>
|
|
|
|
@if (!string.IsNullOrEmpty(_generatedToken))
|
|
{
|
|
<MudText Typo="Typo.subtitle1" Class="mt-6 mb-2">Docker Compose — Agent</MudText>
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-2">
|
|
Copiez ce bloc dans votre fichier <code>docker-compose.yml</code> pour connecter un agent à ce site :
|
|
</MudText>
|
|
|
|
<MudPaper Class="pa-4" Style="background:#1a1a2e; border-radius:8px;" Elevation="0">
|
|
<pre style="color:#e0e0e0; font-size:0.85rem; margin:0; white-space:pre-wrap;"><code>@_dockerComposeSnippet</code></pre>
|
|
</MudPaper>
|
|
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" Color="Color.Primary" Class="mt-2"
|
|
OnClick="@CopyDockerCompose">
|
|
<MudIcon Icon="@Icons.Material.Filled.ContentCopy" Class="mr-1" Size="Size.Small" />
|
|
Copier le docker-compose
|
|
</MudButton>
|
|
}
|
|
</MudPaper>
|
|
|
|
@* Navigation rapide *@
|
|
<MudText Typo="Typo.h6" Class="mb-4">Gestion</MudText>
|
|
<MudGrid>
|
|
<MudItem xs="12" sm="4">
|
|
<MudPaper Elevation="3" Class="pa-4 text-center" Style="border-radius: 12px; cursor: pointer;"
|
|
@onclick="@(() => Nav.NavigateTo($"/sites/{Id}/cameras"))">
|
|
<MudIcon Icon="@Icons.Material.Filled.Videocam" Size="Size.Large" Color="Color.Primary" />
|
|
<MudText Typo="Typo.h6" Class="mt-2">Caméras</MudText>
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Gérer les caméras</MudText>
|
|
</MudPaper>
|
|
</MudItem>
|
|
<MudItem xs="12" sm="4">
|
|
<MudPaper Elevation="3" Class="pa-4 text-center" Style="border-radius: 12px; cursor: pointer;"
|
|
@onclick="@(() => Nav.NavigateTo($"/sites/{Id}/agents"))">
|
|
<MudIcon Icon="@Icons.Material.Filled.Dns" Size="Size.Large" Color="Color.Secondary" />
|
|
<MudText Typo="Typo.h6" Class="mt-2">Agents</MudText>
|
|
<MudText Typo="Typo.body2" Color="Color.Secondary">Voir les agents</MudText>
|
|
</MudPaper>
|
|
</MudItem>
|
|
</MudGrid>
|
|
|
|
<MudButton Variant="Variant.Outlined" Color="Color.Default" Class="mt-4"
|
|
OnClick="@(() => Nav.NavigateTo("/sites"))">
|
|
← Retour aux sites
|
|
</MudButton>
|
|
}
|
|
|
|
@code {
|
|
[Parameter] public Guid Id { get; set; }
|
|
|
|
private SiteItem? _site;
|
|
private bool _loading = true;
|
|
private bool _generating;
|
|
private string? _generatedToken;
|
|
private DateTime _tokenExpiresAt;
|
|
private string _dockerComposeSnippet = "";
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
_site = await Api.GetSiteAsync(Id);
|
|
}
|
|
catch (Exception ex) { Snackbar.Add($"Erreur : {ex.Message}", Severity.Error); }
|
|
finally { _loading = false; }
|
|
}
|
|
|
|
private async Task GenerateToken()
|
|
{
|
|
_generating = true;
|
|
try
|
|
{
|
|
var result = await Api.GenerateSiteTokenAsync(Id);
|
|
if (result is null)
|
|
{
|
|
Snackbar.Add("Erreur lors de la génération du token", Severity.Error);
|
|
return;
|
|
}
|
|
_generatedToken = result.Token;
|
|
_tokenExpiresAt = result.ExpiresAt;
|
|
_dockerComposeSnippet = GenerateDockerComposeSnippet(_generatedToken);
|
|
Snackbar.Add("Token généré ! Copiez le docker-compose ci-dessous.", Severity.Warning);
|
|
}
|
|
catch (Exception ex) { Snackbar.Add($"Erreur : {ex.Message}", Severity.Error); }
|
|
finally { _generating = false; }
|
|
}
|
|
|
|
private string GenerateDockerComposeSnippet(string token)
|
|
{
|
|
var hostname = $"agent-{_site?.Name?.ToLower().Replace(" ", "-") ?? "demo"}";
|
|
return $$"""
|
|
heimdall-agent:
|
|
build:
|
|
context: .
|
|
dockerfile: docker/Dockerfile.agent
|
|
environment:
|
|
HEIMDALL_CENTRAL_URL: "http://heimdall-central:8080"
|
|
HEIMDALL_SITE_TOKEN: "{{token}}"
|
|
HEIMDALL_AGENT_NAME: "{{hostname}}"
|
|
restart: unless-stopped
|
|
""";
|
|
}
|
|
|
|
private async Task CopyDockerCompose()
|
|
{
|
|
if (!string.IsNullOrEmpty(_dockerComposeSnippet))
|
|
{
|
|
await Js.InvokeVoidAsync("navigator.clipboard.writeText", _dockerComposeSnippet);
|
|
Snackbar.Add("Docker-compose copié dans le presse-papier", Severity.Success);
|
|
}
|
|
}
|
|
}
|