Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
<script lang="ts">
import { goto } from '$app/navigation';
import { toast } from 'svelte-sonner';
import ArcaneTable from '#lib/components/arcane-table/arcane-table.svelte';
import type { ColumnSpec, MobileFieldVisibility } from '#lib/components/arcane-table/index.js';
import { UniversalMobileCard } from '#lib/components/arcane-table/index.js';
import RowActionsMenu from '#lib/components/arcane-table/row-actions-menu.svelte';
import { openConfirmDialog } from '#lib/components/confirm-dialog/index.js';
import { Badge } from '#lib/components/ui/badge/index.js';
import * as DropdownMenu from '#lib/components/ui/dropdown-menu/index.js';
import {
UsersIcon,
EnvironmentsIcon,
InspectIcon,
TrashIcon,
EdgeConnectionIcon,
AddIcon,
CloseIcon,
TagIcon
} from '#lib/icons/index.js';
import { m } from '#lib/paraglide/messages.js';
import { swarmService } from '#lib/services/swarm-service.js';
import { environmentStore } from '#lib/stores/environment.store.svelte.js';
import type { Paginated, SearchPaginationSortRequest } from '#lib/types/shared.js';
import type { SwarmNodeAgentDeployment, SwarmNodeSummary } from '#lib/types/swarm.js';
import { extractApiErrorMessage, handleApiResultWithCallbacks } from '#lib/utils/api.js';
import { hasPermission } from '#lib/utils/auth.js';
import { capitalizeFirstLetter } from '#lib/utils/formatting.js';
import { tryCatch } from '#lib/utils/try-catch.js';
import { getSwarmNodeAgentActionLabel, getSwarmNodeAgentLabel, getSwarmNodeAgentVariant } from '../agent-status';
import SwarmNodeAgentDialog from './swarm-node-agent-dialog.svelte';
import SwarmNodeLabelDialog from './swarm-node-label-dialog.svelte';
let {
nodes = $bindable(),
requestOptions = $bindable()
}: {
nodes: Paginated<SwarmNodeSummary>;
requestOptions: SearchPaginationSortRequest;
} = $props();
const currentEnvId = $derived(environmentStore.selected?.id);
const canManageNodes = $derived(hasPermission('swarm:nodes', currentEnvId));
let isLoading = $state(false);
let isAgentDialogOpen = $state(false);
let selectedNode = $state<SwarmNodeSummary | null>(null);
let agentDeploymentError = $state('');
let agentDeployment = $state<SwarmNodeAgentDeployment | null>(null);
let isAgentDeploymentLoading = $state(false);
let isAddLabelDialogOpen = $state(false);
let nodeToLabel = $state<SwarmNodeSummary | null>(null);
function statusVariant(state: string): 'green' | 'red' | 'amber' | 'gray' {
if (state === 'ready') return 'green';
if (state === 'down') return 'red';
if (state === 'unknown') return 'amber';
return 'gray';
}
function availabilityVariant(state: string): 'green' | 'amber' | 'red' | 'gray' {
if (state === 'active') return 'green';
if (state === 'pause') return 'amber';
if (state === 'drain') return 'red';
return 'gray';
}
async function refreshNodes(options = requestOptions) {
const environmentId = await environmentStore.getCurrentEnvironmentId();
const result = await swarmService.getNodes(options);
if (environmentId !== currentEnvId) return nodes;
nodes = result;
if (selectedNode) {
selectedNode = nodes.data.find((node) => node.id === selectedNode?.id) ?? selectedNode;
}
return nodes;
}
async function loadAgentDeployment(node: SwarmNodeSummary, rotate = false) {
if (isAgentDeploymentLoading) return;
isAgentDeploymentLoading = true;
agentDeploymentError = '';
try {
const operationResult = await tryCatch(
(async () => {
agentDeployment = await swarmService.getNodeAgentDeployment(node.id, rotate);
await refreshNodes();
})()
);
if (operationResult.error !== null) {
const error = operationResult.error;
agentDeployment = null;
agentDeploymentError = extractApiErrorMessage(error);
throw error;
}
} finally {
isAgentDeploymentLoading = false;
}
}
function openAgentDialog(node: SwarmNodeSummary) {
selectedNode = node;
agentDeployment = null;
agentDeploymentError = '';
isAgentDialogOpen = true;
}
function provisionNodeEnvironment() {
if (!selectedNode) return;
void tryCatch(loadAgentDeployment(selectedNode)).then((result) => (result.error ? undefined : result.data));
}
async function attachEnvironment(environmentId: string) {
const node = selectedNode;
if (!node) return;
const replaceDeployment = node.agent.bindingKind === 'dedicated';
const attach = async () => {
isAgentDeploymentLoading = true;
try {
const operationResult = await tryCatch(
(async () => {
await swarmService.bindNodeAgent(node.id, { environmentId, rebind: true, replaceDeployment });
toast.success(m.swarm_node_agent_attach_success());
await refreshNodes();
})()
);
if (operationResult.error !== null) {
const error = operationResult.error;
agentDeploymentError = extractApiErrorMessage(error);
}
} finally {
isAgentDeploymentLoading = false;
}
};
if (!replaceDeployment) {
await attach();
return;
}
openConfirmDialog({
title: m.swarm_node_agent_replace_title(),
message: m.swarm_node_agent_replace_description(),
confirm: { label: m.swarm_node_agent_replace_action(), destructive: true, action: attach }
});
}
function detachEnvironment() {
const node = selectedNode;
if (!node) return;
openConfirmDialog({
title: m.swarm_node_agent_detach_title(),
message: m.swarm_node_agent_detach_description(),
confirm: {
label: m.swarm_node_agent_detach_action(),
destructive: true,
action: async () => {
await swarmService.detachNodeAgent(node.id);
await refreshNodes();
}
}
});
}
function removeDedicatedDeployment() {
const node = selectedNode;
if (!node) return;
openConfirmDialog({
title: m.swarm_node_agent_remove_deployment_title(),
message: m.swarm_node_agent_remove_deployment_description(),
confirm: {
label: m.common_remove(),
destructive: true,
action: async () => {
await swarmService.removeNodeAgentDeployment(node.id);
agentDeployment = null;
await refreshNodes();
}
}
});
}
function refreshAgentDeployment() {
if (!selectedNode) return;
void tryCatch(loadAgentDeployment(selectedNode)).then((result) => (result.error ? undefined : result.data));
}
function regenerateAgentDeployment() {
const node = selectedNode;
if (!node) return;
openConfirmDialog({
title: m.environments_regenerate_dialog_title(),
message: m.environments_regenerate_dialog_message(),
confirm: {
label: m.environments_regenerate_api_key(),
destructive: true,
action: async () => {
const operationResult = await tryCatch(
(async () => {
await loadAgentDeployment(node, true);
toast.success(m.environments_regenerate_key_success());
})()
);
if (operationResult.error !== null) {
toast.error(m.environments_regenerate_key_failed());
}
}
}
});
}
function inspectNodeTasks(node: SwarmNodeSummary) {
goto(`/swarm/tasks?nodeId=${encodeURIComponent(node.id)}&search=${encodeURIComponent(node.hostname)}`);
}
async function mutateNode(action: () => Promise<void>, successMessage: string, failureMessage: string) {
isLoading = true;
await handleApiResultWithCallbacks({
result: await tryCatch(action()),
message: failureMessage,
setLoadingState: (v) => (isLoading = v),
onSuccess: async () => {
toast.success(successMessage);
await refreshNodes();
}
});
}
async function setAvailability(node: SwarmNodeSummary, availability: 'active' | 'pause' | 'drain') {
await mutateNode(
() => swarmService.updateNode(node.id, { availability }),
m.common_update_success({ resource: m.swarm_node() }),
m.swarm_node_update_failed({ name: node.hostname })
);
}
async function promoteNode(node: SwarmNodeSummary) {
await mutateNode(
() => swarmService.promoteNode(node.id),
m.swarm_node_promote_success({ name: node.hostname }),
m.swarm_node_promote_failed({ name: node.hostname })
);
}
async function demoteNode(node: SwarmNodeSummary) {
await mutateNode(
() => swarmService.demoteNode(node.id),
m.swarm_node_demote_success({ name: node.hostname }),
m.swarm_node_demote_failed({ name: node.hostname })
);
}
function removeNode(node: SwarmNodeSummary) {
openConfirmDialog({
title: m.common_delete_title({ resource: m.swarm_node() }),
message: m.common_delete_confirm({ resource: m.swarm_node() }),
confirm: {
label: m.common_delete(),
destructive: true,
action: async () => {
mutateNode(
() => swarmService.removeNode(node.id, true),
m.swarm_node_remove_success({ name: node.hostname }),
m.swarm_node_remove_failed({ name: node.hostname })
);
}
}
});
}
function openAddLabelDialog(node: SwarmNodeSummary) {
nodeToLabel = node;
isAddLabelDialogOpen = true;
}
async function addLabel(key: string, value: string) {
if (!nodeToLabel) return;
const labels = { ...(nodeToLabel.labels ?? {}), [key]: value };
await mutateNode(
() => swarmService.updateNode(nodeToLabel!.id, { labels }),
m.common_update_success({ resource: m.swarm_node() }),
m.swarm_node_update_failed({ name: nodeToLabel.hostname })
);
}
function removeLabel(node: SwarmNodeSummary, key: string) {
openConfirmDialog({
title: m.common_remove_title({ resource: `label "${key}"` }),
message: m.common_remove_confirm({ resource: `label "${key}"` }),
confirm: {
label: m.common_remove(),
destructive: true,
action: async () => {
const labels = { ...(node.labels ?? {}) };
delete labels[key];
await mutateNode(
() => swarmService.updateNode(node.id, { labels }),
m.common_update_success({ resource: m.swarm_node() }),
m.swarm_node_update_failed({ name: node.hostname })
);
}
}
});
}
const columns = [
{ accessorKey: 'id', title: m.common_id(), hidden: true },
{ accessorKey: 'hostname', title: m.hostname(), sortable: true },
{ accessorKey: 'role', title: m.common_role(), sortable: true, cell: RoleCell },
{ accessorKey: 'status', title: m.common_status(), sortable: true, cell: StatusCell },
{ accessorKey: 'availability', title: m.swarm_availability(), sortable: true, cell: AvailabilityCell },
{ accessorKey: 'labels', title: m.common_labels(), cell: LabelsCell },
{
id: 'agent',
title: m.agent(),
accessorFn: (node) => node.agent?.state ?? 'none',
cell: AgentCell
},
{ accessorKey: 'engineVersion', title: m.swarm_engine_version(), sortable: true }
] satisfies ColumnSpec<SwarmNodeSummary>[];
const mobileFields = [
{ id: 'role', label: m.common_role(), defaultVisible: true },
{ id: 'status', label: m.common_status(), defaultVisible: true },
{ id: 'availability', label: m.swarm_availability(), defaultVisible: true },
{ id: 'labels', label: m.common_labels(), defaultVisible: true },
{ id: 'agent', label: m.agent(), defaultVisible: true },
{ id: 'engineVersion', label: m.swarm_engine_version(), defaultVisible: false }
];
let mobileFieldVisibility = $state<Record<string, boolean>>({});
</script>
{#snippet RoleCell({ value }: { value: unknown })}
<span class="text-sm">{capitalizeFirstLetter(String(value ?? ''))}</span>
{/snippet}
{#snippet StatusCell({ value }: { value: unknown })}
<Badge variant={statusVariant(String(value ?? ''))} minWidth="20">{String(value ?? m.common_unknown())}</Badge>
{/snippet}
{#snippet AvailabilityCell({ value }: { value: unknown })}
<Badge variant={availabilityVariant(String(value ?? ''))} minWidth="20">{String(value ?? m.common_unknown())}</Badge>
{/snippet}
{#snippet AgentCell({ value }: { value: unknown })}
<Badge variant={getSwarmNodeAgentVariant(String(value ?? 'none') as SwarmNodeSummary['agent']['state'])} minWidth="20"
>{getSwarmNodeAgentLabel(String(value ?? 'none') as SwarmNodeSummary['agent']['state'])}</Badge
>
{/snippet}
{#snippet LabelsCell({ item }: { item: SwarmNodeSummary })}
<div class="flex flex-wrap items-center gap-1.5">
{#each Object.entries(item.systemLabels ?? {}) as [key, value] (key)}
<div class="group relative overflow-hidden rounded-lg">
<Badge variant="gray" class="max-w-50"
><span class="block min-w-0 truncate">{`${key}${value ? `=${value}` : ''}`}</span></Badge
>
</div>
{/each}
{#each Object.entries(item.labels ?? {}) as [key, value] (key)}
<div class="group relative overflow-hidden rounded-lg">
<Badge variant="blue" class="max-w-50"
><span class="block min-w-0 truncate">{`${key}${value ? `=${value}` : ''}`}</span></Badge
>
{#if canManageNodes}
<button
class="absolute inset-0 flex cursor-pointer items-center justify-end rounded-lg bg-info/10 pr-1 opacity-0 backdrop-blur-2xs transition-opacity group-hover:opacity-100"
onclick={() => removeLabel(item, key)}
title={m.common_remove()}
>
<div class="scale-90 rounded-full bg-destructive p-0.5 shadow-lg transition-transform group-hover:scale-100">
<CloseIcon class="size-3 text-white" />
</div>
</button>
{/if}
</div>
{/each}
{#if canManageNodes}
<button
class="inline-flex items-center gap-1 rounded border border-dashed border-border px-2 py-0.5 text-2xs font-medium transition-colors hover:border-primary hover:text-primary"
onclick={() => openAddLabelDialog(item)}
>
<AddIcon class="size-3" />
{m.common_add_button({ resource: 'Label' })}
</button>
{/if}
</div>
{/snippet}
{#snippet NodeMobileCardSnippet({
item,
mobileFieldVisibility
}: {
item: SwarmNodeSummary;
mobileFieldVisibility: MobileFieldVisibility;
})}
<UniversalMobileCard
{item}
icon={() => ({
component: UsersIcon,
variant: item.role === 'manager' ? 'purple' : 'blue'
})}
title={(item: SwarmNodeSummary) => item.hostname}
subtitle={(item: SwarmNodeSummary) => ((mobileFieldVisibility['engineVersion'] ?? false) ? (item.engineVersion ?? '') : null)}
badges={[
(item: SwarmNodeSummary) =>
(mobileFieldVisibility['status'] ?? true) ? { variant: statusVariant(item.status), text: item.status } : null,
(item: SwarmNodeSummary) =>
(mobileFieldVisibility['agent'] ?? true)
? {
variant: getSwarmNodeAgentVariant(item.agent?.state),
text: getSwarmNodeAgentLabel(item.agent?.state)
}
: null
]}
fields={[
{
label: m.common_role(),
getValue: (item: SwarmNodeSummary) => capitalizeFirstLetter(item.role),
icon: EnvironmentsIcon,
iconVariant: 'gray' as const,
show: mobileFieldVisibility['role'] ?? true
},
{
label: m.swarm_availability(),
getValue: (item: SwarmNodeSummary) => capitalizeFirstLetter(item.availability),
icon: EnvironmentsIcon,
iconVariant: 'gray' as const,
show: mobileFieldVisibility['availability'] ?? true
},
{
label: m.common_labels(),
getValue: (item: SwarmNodeSummary) =>
`${Object.keys(item.labels ?? {}).length + Object.keys(item.systemLabels ?? {}).length} labels`,
icon: TagIcon,
iconVariant: 'gray' as const,
show: mobileFieldVisibility['labels'] ?? true
},
{
label: m.agent(),
getValue: (item: SwarmNodeSummary) => getSwarmNodeAgentLabel(item.agent?.state),
icon: EdgeConnectionIcon,
iconVariant: 'gray' as const,
show: mobileFieldVisibility['agent'] ?? true
}
]}
rowActions={RowActions}
/>
{/snippet}
{#snippet RowActions({ item }: { item: SwarmNodeSummary })}
<RowActionsMenu triggerClass="relative size-8 p-0" iconClass="">
<DropdownMenu.Item onclick={() => inspectNodeTasks(item)}>
<InspectIcon class="size-4" />
{m.common_inspect()}
</DropdownMenu.Item>
{#if canManageNodes}
<DropdownMenu.Item onclick={() => openAgentDialog(item)}>
<EdgeConnectionIcon class="size-4" />
{getSwarmNodeAgentActionLabel(item.agent?.state)}
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Item onclick={() => promoteNode(item)} disabled={isLoading || item.role === 'manager'}>
{m.swarm_node_promote()}
</DropdownMenu.Item>
<DropdownMenu.Item onclick={() => demoteNode(item)} disabled={isLoading || item.role !== 'manager'}>
{m.swarm_node_demote()}
</DropdownMenu.Item>
<DropdownMenu.Item onclick={() => setAvailability(item, 'drain')} disabled={isLoading || item.availability === 'drain'}>
{m.drain()}
</DropdownMenu.Item>
<DropdownMenu.Item onclick={() => setAvailability(item, 'active')} disabled={isLoading || item.availability === 'active'}>
{m.swarm_node_activate()}
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Item variant="destructive" onclick={() => removeNode(item)} disabled={isLoading}>
<TrashIcon class="size-4" />
{m.common_delete()}
</DropdownMenu.Item>
{/if}
</RowActionsMenu>
{/snippet}
<ArcaneTable
persistKey="arcane-swarm-nodes-table"
items={nodes}
bind:requestOptions
bind:mobileFieldVisibility
selectionDisabled={true}
onRefresh={refreshNodes}
{columns}
{mobileFields}
rowActions={RowActions}
mobileCard={NodeMobileCardSnippet}
/>
<SwarmNodeAgentDialog
bind:open={isAgentDialogOpen}
node={selectedNode}
deployment={agentDeployment}
errorMessage={agentDeploymentError}
isLoading={isAgentDeploymentLoading}
onRefresh={refreshAgentDeployment}
onRegenerate={regenerateAgentDeployment}
onProvision={provisionNodeEnvironment}
onAttach={attachEnvironment}
onDetach={detachEnvironment}
onRemoveDeployment={removeDedicatedDeployment}
/>
<SwarmNodeLabelDialog bind:open={isAddLabelDialogOpen} onAdd={addLabel} />