Files
Heimdall/src/Heimdall.Web/Components/SiteInstallPanel.razor
T
Arnaud Dartois e757c2f846
Heimdall Gitea / Build and tests (pull_request) Failing after 1s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
fix(agent): pin state migration to manifest image
2026-10-22 19:17:30 +02:00

144 lines
6.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@* SiteInstallPanel.razor — génère les instructions d'installation d'un agent
Heimdall sur un site, façon Pangolin (choix de la méthode + code à copier).
Modes : Docker Compose (recommandé) ou script shell. *@
@using MudBlazor
<MudText Typo="Typo.h6" Class="mb-1">
<MudIcon Icon="@Icons.Material.Filled.Download" Size="Size.Small" Class="mr-1" />
Installer un agent sur ce site
</MudText>
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-4">
Déployez le connecteur d'agent Heimdall sur la machine qui doit surveiller ce site.
</MudText>
<MudTabs Class="mb-4" Elevation="0" Rounded="true">
<MudTabPanel Text="Docker Compose" Icon="@Icons.Material.Filled.IntegrationInstructions">
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-2">
Copiez ce fichier <code>docker-compose.yml</code> sur la machine cible puis lancez
<code>docker compose up -d</code>.
</MudText>
@if (_agentImageReference is null)
{
<MudAlert Severity="Severity.Warning">Le manifeste d’image de l’agent fourni par le Central est indisponible. Réessayez avant de copier le compose.</MudAlert>
}
else
{
<MudPaper Class="pa-3" Style="border-radius: 8px; background: #0d1117; overflow-x: auto;">
<pre class="mb-0" style="color:#c9d1d9;font-size:0.85rem;"><code>@ComposeYaml</code></pre>
</MudPaper>
<MudButton Variant="Variant.Filled" Color="Color.Primary" Class="mt-3"
OnClick="@(() => CopyCode(ComposeYaml))" StartIcon="@Icons.Material.Filled.ContentCopy">
Copier le docker-compose.yml
</MudButton>
}
</MudTabPanel>
<MudTabPanel Text="Script shell" Icon="@Icons.Material.Filled.Terminal">
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-2">
Exécutez cette commande sur la machine cible (Linux) :
</MudText>
<MudPaper Class="pa-3" Style="border-radius: 8px; background: #0d1117; overflow-x: auto;">
<pre class="mb-0" style="color:#c9d1d9;font-size:0.85rem;"><code>@ShellCommand</code></pre>
</MudPaper>
<MudButton Variant="Variant.Filled" Color="Color.Primary" Class="mt-3"
OnClick="@(() => CopyCode(ShellCommand))" StartIcon="@Icons.Material.Filled.ContentCopy">
Copier la commande
</MudButton>
</MudTabPanel>
</MudTabs>
@code {
/// <summary>Nom du site cible.</summary>
[Parameter, EditorRequired] public string SiteName { get; set; } = "";
/// <summary>Token d'enregistrement du site (affiché dans le compose).</summary>
[Parameter, EditorRequired] public string SiteToken { get; set; } = "";
/// <summary>URL publique du serveur Heimdall (ex: https://heimdall.tordais.cc).</summary>
[Parameter, EditorRequired] public string CentralUrl { get; set; } = "";
[Inject] private ISnackbar Snackbar { get; set; } = default!;
[Inject] private HeimdallApiClient Api { get; set; } = default!;
private string? _agentImageReference;
protected override async Task OnInitializedAsync()
{
try
{
var release = await Api.GetLatestUpdateAsync();
if (!string.IsNullOrWhiteSpace(release?.ContainerImage) && !string.IsNullOrWhiteSpace(release.ContainerImageDigest))
_agentImageReference = $"{release.ContainerImage}@{release.ContainerImageDigest}";
}
catch
{
// The compose stays hidden until Central provides a verified image manifest.
}
}
private string ComposeYaml
{
get
{
var safeName = Slugify(SiteName);
return $$"""
services:
heimdall-agent:
image: {{_agentImageReference}}
container_name: heimdall-agent-{{safeName}}
restart: unless-stopped
user: "1654:1654"
environment:
Agent__CentralUrl: {{CentralUrl}}
Agent__SiteToken: {{SiteToken}}
Agent__AgentName: {{SiteName}}
Agent__HeartbeatIntervalSeconds: "30"
Agent__StateDirectory: /var/lib/heimdall
volumes:
- agent_state:/var/lib/heimdall
depends_on:
agent-state-init:
condition: service_completed_successfully
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/healthz"]
interval: 30s
timeout: 5s
retries: 3
start_period: 15s
agent-state-init:
image: {{_agentImageReference}}
user: "0:0"
entrypoint: ["chown", "-R", "1654:1654", "/state"]
volumes:
- agent_state:/state
restart: "no"
volumes:
agent_state:
""";
}
}
private string ShellCommand
=> $"curl -sSL {ShellQuote(CentralUrl.TrimEnd('/') + "/install-agent.sh")} -o install-agent.sh && bash install-agent.sh --central-url {ShellQuote(CentralUrl)} --site-token {ShellQuote(SiteToken)} --agent-name {ShellQuote(SiteName)}";
private async Task CopyCode(string code)
{
await JsRuntime.InvokeVoidAsync("navigator.clipboard.writeText", code);
Snackbar.Add("Code copié dans le presse-papiers", Severity.Success);
}
[Inject] private IJSRuntime JsRuntime { get; set; } = default!;
private static string Slugify(string value)
{
var sb = new System.Text.StringBuilder();
foreach (var ch in value.ToLowerInvariant())
sb.Append(char.IsLetterOrDigit(ch) ? ch : '-');
return sb.ToString().Trim('-');
}
private static string ShellQuote(string value)
=> $"'{value.Replace("'", "'\\''", StringComparison.Ordinal)}'";
}