Stackwatch : suivi des mises à jour de stacks docker compose via SSH
Base Up And Down (auth, thème, dialogues) + moteur fleet (script distant POSIX, analyse pull + comparaison d'images, mise à jour d'une stack), interface de suivi. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
56e1bbe842
54 files changed
+10954
No files matched your search
@@ -0,0 +1,230 @@
|
||||
// Client de l'API Go (voir internal/api/api.go).
|
||||
|
||||
export type Role = "admin" | "user";
|
||||
export type Status = "active" | "pending" | "disabled";
|
||||
export type Registration = "closed" | "approval" | "open";
|
||||
|
||||
/** Session courante et réglages publics (GET /api/me, accessible sans connexion). */
|
||||
export interface Me {
|
||||
mode: "none" | "local" | "oidc" | "both";
|
||||
local: boolean;
|
||||
oidc: boolean;
|
||||
oidcName?: string;
|
||||
registration: Registration;
|
||||
/** Aucun administrateur : le premier compte créé le devient. */
|
||||
setup?: boolean;
|
||||
title: string;
|
||||
version?: string;
|
||||
user?: { id: string; name: string; email?: string; role: Role; provider?: "local" | "oidc" };
|
||||
admin: boolean;
|
||||
canChangePassword?: boolean;
|
||||
logout?: string;
|
||||
/** Administrateur : inscriptions à valider. */
|
||||
pending?: number;
|
||||
}
|
||||
|
||||
export interface Account {
|
||||
id: string;
|
||||
username?: string;
|
||||
name: string;
|
||||
email?: string;
|
||||
provider: "local" | "oidc";
|
||||
role: Role;
|
||||
status: Status;
|
||||
created: string;
|
||||
lastLogin?: string;
|
||||
}
|
||||
|
||||
// --- Suivi des stacks --------------------------------------------------------------
|
||||
|
||||
export type StackStatus = "uptodate" | "updates" | "stopped" | "error";
|
||||
|
||||
export interface Target {
|
||||
id: string;
|
||||
user: string;
|
||||
roots: string[];
|
||||
depth: number;
|
||||
}
|
||||
|
||||
export interface Service {
|
||||
name: string;
|
||||
image: string;
|
||||
running: string;
|
||||
latest: string;
|
||||
state: "ok" | "update" | "unknown";
|
||||
}
|
||||
|
||||
export interface Applied {
|
||||
at: string;
|
||||
by: string;
|
||||
ok: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface Stack {
|
||||
dir: string;
|
||||
name: string;
|
||||
file: string;
|
||||
status: StackStatus;
|
||||
error?: string;
|
||||
services?: Service[];
|
||||
applied?: Applied;
|
||||
}
|
||||
|
||||
export interface Scan {
|
||||
running: boolean;
|
||||
task?: "scan" | "apply";
|
||||
current?: string;
|
||||
started?: string;
|
||||
finished?: string;
|
||||
error?: string;
|
||||
warnings?: string[];
|
||||
stacks: Stack[];
|
||||
docker?: string;
|
||||
compose?: string;
|
||||
}
|
||||
|
||||
export interface ServerInput {
|
||||
id?: string;
|
||||
name: string;
|
||||
host: string;
|
||||
port: number;
|
||||
note?: string;
|
||||
enabled: boolean;
|
||||
targets: { id?: string; user: string; roots: string[]; depth: number }[];
|
||||
}
|
||||
|
||||
export interface Server extends ServerInput {
|
||||
id: string;
|
||||
targets: Target[];
|
||||
/** Résultat de chaque connexion, par identifiant de connexion. */
|
||||
scans: Record<string, Scan>;
|
||||
}
|
||||
|
||||
export interface Summary {
|
||||
servers: number;
|
||||
stacks: number;
|
||||
updates: number;
|
||||
errors: number;
|
||||
stopped: number;
|
||||
unreachable: number;
|
||||
scanning: number;
|
||||
}
|
||||
|
||||
export interface Fleet {
|
||||
servers: Server[];
|
||||
summary: Summary;
|
||||
/** Secondes entre deux analyses automatiques (0 : désactivées). */
|
||||
interval: number;
|
||||
/** Administrateur : clé publique SSH à autoriser sur les serveurs. */
|
||||
publicKey?: string;
|
||||
}
|
||||
|
||||
export interface PingResult {
|
||||
target: string;
|
||||
user: string;
|
||||
ok: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
public status: number,
|
||||
message: string,
|
||||
public code?: string,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
async function request(url: string, init?: RequestInit): Promise<Response> {
|
||||
const res = await fetch(url, init);
|
||||
if (!res.ok) {
|
||||
// Session OIDC expirée : reconnexion, puis retour sur la page courante.
|
||||
const login = res.status === 401 ? res.headers.get("X-Auth-Login") : null;
|
||||
if (login) location.assign(`${login}?next=${encodeURIComponent(location.pathname + location.search + location.hash)}`);
|
||||
let msg = res.statusText;
|
||||
let code: string | undefined;
|
||||
try {
|
||||
const j = await res.json();
|
||||
msg = j.error ?? msg;
|
||||
code = j.code;
|
||||
} catch {
|
||||
/* corps non JSON */
|
||||
}
|
||||
throw new ApiError(res.status, msg, code);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
const q = (p: string) => encodeURIComponent(p);
|
||||
|
||||
const json = (body: unknown, method = "POST"): RequestInit => ({
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
export const api = {
|
||||
me: async (): Promise<Me> => (await request("/api/me")).json(),
|
||||
|
||||
// --- Connexion ---------------------------------------------------------------
|
||||
login: async (username: string, password: string): Promise<void> => {
|
||||
await request("/auth/local/login", json({ username, password }));
|
||||
},
|
||||
|
||||
/** Renvoie « active » (connecté d'office) ou « pending » (à valider par un administrateur). */
|
||||
register: async (v: { username: string; name: string; email: string; password: string }): Promise<"active" | "pending"> => {
|
||||
const res = await request("/auth/local/register", json(v));
|
||||
return (await res.json()).status;
|
||||
},
|
||||
|
||||
// --- Serveurs et analyses ----------------------------------------------------
|
||||
fleet: async (): Promise<Fleet> => (await request("/api/fleet")).json(),
|
||||
|
||||
checkAll: async (): Promise<void> => {
|
||||
await request("/api/check", { method: "POST" });
|
||||
},
|
||||
|
||||
check: async (serverId: string, targetId?: string): Promise<void> => {
|
||||
await request(`/api/servers/${q(serverId)}/check${targetId ? `?target=${q(targetId)}` : ""}`, { method: "POST" });
|
||||
},
|
||||
|
||||
addServer: async (v: ServerInput): Promise<{ id: string }> => (await request("/api/servers", json(v))).json(),
|
||||
|
||||
updateServer: async (id: string, v: ServerInput): Promise<{ id: string }> => (await request(`/api/servers/${q(id)}`, json(v, "PUT"))).json(),
|
||||
|
||||
deleteServer: async (id: string): Promise<void> => {
|
||||
await request(`/api/servers/${q(id)}`, { method: "DELETE" });
|
||||
},
|
||||
|
||||
/** Teste la connexion SSH, docker et docker compose (id « - » : serveur pas encore enregistré, décrit par v). */
|
||||
testServer: async (id: string, v?: ServerInput): Promise<PingResult[]> =>
|
||||
(await request(`/api/servers/${q(id)}/test`, v ? json(v) : { method: "POST" })).json(),
|
||||
|
||||
applyStack: async (serverId: string, targetId: string, dir: string): Promise<void> => {
|
||||
await request(`/api/servers/${q(serverId)}/targets/${q(targetId)}/apply`, json({ dir }));
|
||||
},
|
||||
|
||||
// --- Compte ------------------------------------------------------------------
|
||||
changePassword: async (current: string, next: string): Promise<void> => {
|
||||
await request("/api/account/password", json({ current, new: next }));
|
||||
},
|
||||
|
||||
patchAccount: async (v: { name?: string; email?: string }): Promise<void> => {
|
||||
await request("/api/account", json(v, "PATCH"));
|
||||
},
|
||||
|
||||
// --- Administration ----------------------------------------------------------
|
||||
users: async (): Promise<Account[]> => (await request("/api/admin/users")).json(),
|
||||
|
||||
createUser: async (v: { username: string; name: string; email: string; password: string; role: Role }): Promise<Account> =>
|
||||
(await request("/api/admin/users", json(v))).json(),
|
||||
|
||||
patchUser: async (id: string, v: Partial<{ name: string; email: string; role: Role; status: Status; password: string }>): Promise<Account> =>
|
||||
(await request(`/api/admin/users/${q(id)}`, json(v, "PATCH"))).json(),
|
||||
|
||||
deleteUser: async (id: string): Promise<void> => {
|
||||
await request(`/api/admin/users/${q(id)}`, { method: "DELETE" });
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user