Hetzner Cloud integration: servers, traffic quota, backups, volumes and cost
One integration per project (read-only API token, read every 5 minutes). Alerts for a server that was running and is off, stuck statuses, traffic at 80%/100% of the monthly quota, rescue mode left on, and late backups. The Hetzner page lists servers, volumes, snapshots and a cost estimate; a server the app knows by its agent gets a Hetzner card on its own page. /metrics and docs updated. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5a2b320175
commit
24b708abbe
35 files changed
+9430
-7
No files matched your search
@@ -0,0 +1,102 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { hetznerState, integrations, servers } from "../db/schema.js";
|
||||
import { requireAuth } from "../auth/middleware.js";
|
||||
import { costOf, hetznerProblems, hetznerSyncStatuses, imagesOf, isHetznerFresh, matchAppServer, serversOf, volumesOf } from "../services/hetznerSync.js";
|
||||
import { getSettings } from "../services/settingsStore.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const hetznerRouter = Router();
|
||||
hetznerRouter.use(requireAuth);
|
||||
|
||||
const parseIps = (stored: string | null): string[] => {
|
||||
if (!stored) return [];
|
||||
try {
|
||||
const v = JSON.parse(stored);
|
||||
return Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const appServers = async () => (await db.select({ id: servers.id, name: servers.name, ips: servers.ipAddresses }).from(servers)).map((s) => ({ id: s.id, name: s.name, ips: parseIps(s.ips) }));
|
||||
|
||||
/** Every Hetzner project, how it is, from what the last read stored: its servers (and which of them the app also knows by its agent), volumes, snapshots, cost and what is wrong. Everyone signed in can see it. */
|
||||
hetznerRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const [rows, states, statuses, known, settings] = await Promise.all([
|
||||
db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "hetzner")).orderBy(integrations.name),
|
||||
db.select().from(hetznerState),
|
||||
hetznerSyncStatuses(),
|
||||
appServers(),
|
||||
getSettings(),
|
||||
]);
|
||||
const byId = new Map(states.map((s) => [s.integrationId, s]));
|
||||
const now = Date.now();
|
||||
res.json({
|
||||
projects: rows.map((r) => {
|
||||
const state = byId.get(r.id) ?? null;
|
||||
const status = statuses[r.id] ?? null;
|
||||
const fresh = isHetznerFresh(status ?? undefined, now);
|
||||
const stored = state ? serversOf(state) : [];
|
||||
const images = state ? imagesOf(state) : null;
|
||||
const names = new Map(stored.map((s) => [s.id, s.name]));
|
||||
return {
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
enabled: r.enabled,
|
||||
status,
|
||||
fresh,
|
||||
state: state
|
||||
? {
|
||||
servers: stored.map((s) => ({ ...s, linked: matchAppServer(s, known) })),
|
||||
/** Null when the volumes could not be listed. */
|
||||
volumes: volumesOf(state)?.map((v) => ({ ...v, serverName: v.serverId === null ? null : (names.get(v.serverId) ?? null) })) ?? null,
|
||||
images: images?.map((i) => ({ ...i, serverName: i.serverId === null ? null : (names.get(i.serverId) ?? null) })) ?? null,
|
||||
cost: costOf(state),
|
||||
updatedAt: state.updatedAt,
|
||||
}
|
||||
: null,
|
||||
problems: state && fresh ? hetznerProblems(r.id, r.name, stored, images, now, settings.healthChecks.backupStaleHours) : [],
|
||||
};
|
||||
}),
|
||||
});
|
||||
}));
|
||||
|
||||
/** The Hetzner server that is this server (by address, else by name), for the card on its page; null when none is. */
|
||||
hetznerRouter.get("/for-server/:id", asyncHandler(async (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const [self] = (await appServers()).filter((s) => s.id === id);
|
||||
if (!self) return res.status(404).json({ error: "not_found" });
|
||||
const [rows, states, statuses, settings] = await Promise.all([
|
||||
db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(eq(integrations.type, "hetzner")),
|
||||
db.select().from(hetznerState),
|
||||
hetznerSyncStatuses(),
|
||||
getSettings(),
|
||||
]);
|
||||
const now = Date.now();
|
||||
for (const r of rows) {
|
||||
const state = states.find((s) => s.integrationId === r.id);
|
||||
if (!state) continue;
|
||||
const stored = serversOf(state);
|
||||
const server = stored.find((s) => matchAppServer(s, [self]) !== null);
|
||||
if (!server) continue;
|
||||
const fresh = isHetznerFresh(statuses[r.id], now);
|
||||
const images = imagesOf(state);
|
||||
const newestBackup = (images ?? []).filter((i) => i.type === "backup" && i.serverId === server.id && i.created).map((i) => i.created as string).sort().pop() ?? null;
|
||||
return res.json({
|
||||
match: {
|
||||
integrationId: r.id,
|
||||
integrationName: r.name,
|
||||
fresh,
|
||||
updatedAt: state.updatedAt,
|
||||
server,
|
||||
newestBackup,
|
||||
snapshots: (images ?? []).filter((i) => i.type === "snapshot" && i.serverId === server.id).length,
|
||||
problems: fresh ? hetznerProblems(r.id, r.name, stored, images, now, settings.healthChecks.backupStaleHours).filter((p) => p.serverId === server.id) : [],
|
||||
},
|
||||
});
|
||||
}
|
||||
res.json({ match: null });
|
||||
}));
|
||||
@@ -36,6 +36,7 @@ import { forgetImmich, syncImmich } from "../services/immichSync.js";
|
||||
import { forgetPaperless, syncPaperless } from "../services/paperlessSync.js";
|
||||
import { forgetN8n, syncN8n } from "../services/n8nSync.js";
|
||||
import { forgetNetBox, syncNetBox } from "../services/netboxSync.js";
|
||||
import { forgetHetzner, syncHetzner } from "../services/hetznerSync.js";
|
||||
import { forgetNextcloud, syncNextcloud } from "../services/nextcloudSync.js";
|
||||
import { dockerVulnSyncStatuses, findingsOf, forgetDockerVulns, isDockerVulnFresh, syncDockerVulns } from "../services/dockerVulnSync.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
@@ -147,6 +148,7 @@ integrationsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
|
||||
if (type === "paperless") void syncPaperless(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "n8n") void syncN8n(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "netbox") void syncNetBox(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "hetzner") void syncHetzner(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "nextcloud") void syncNextcloud(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "dockhand") void syncDockerVulns(created.id).then(() => invalidateAlertsCache());
|
||||
|
||||
@@ -259,6 +261,7 @@ integrationsRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
if (updated.type === "paperless" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncPaperless(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "n8n" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncN8n(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "netbox" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncNetBox(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "hetzner" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncHetzner(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "nextcloud" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncNextcloud(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "dockhand" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncDockerVulns(updated.id).then(() => invalidateAlertsCache());
|
||||
|
||||
@@ -347,6 +350,7 @@ integrationsRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
if (existing.type === "paperless") await forgetPaperless(id);
|
||||
if (existing.type === "n8n") await forgetN8n(id);
|
||||
if (existing.type === "netbox") await forgetNetBox(id);
|
||||
if (existing.type === "hetzner") await forgetHetzner(id);
|
||||
if (existing.type === "nextcloud") await forgetNextcloud(id);
|
||||
if (existing.type === "dockhand") await forgetDockerVulns(id);
|
||||
await db.delete(integrations).where(eq(integrations.id, id));
|
||||
|
||||
Reference in new issue
Block a user