Add basic blueprints

This commit is contained in:
Owen
2025-09-10 15:33:56 -07:00
parent a4571a80ae
commit 800b1f1520
11 changed files with 1642 additions and 3 deletions

View File

@@ -0,0 +1,73 @@
import { db, newts } from "@server/db";
import { MessageHandler } from "../ws";
import { exitNodes, Newt, resources, sites, Target, targets } from "@server/db";
import { eq, and, sql, inArray } from "drizzle-orm";
import logger from "@server/logger";
import { applyBlueprint } from "@server/lib/blueprints/applyBlueprint";
export const handleApplyBlueprintMessage: MessageHandler = async (context) => {
const { message, client, sendToClient } = context;
const newt = client as Newt;
logger.debug("Handling apply blueprint message!");
if (!newt) {
logger.warn("Newt not found");
return;
}
if (!newt.siteId) {
logger.warn("Newt has no site!"); // TODO: Maybe we create the site here?
return;
}
// get the site
const [site] = await db
.select()
.from(sites)
.where(eq(sites.siteId, newt.siteId));
if (!site) {
logger.warn("Site not found for newt");
return;
}
const { blueprint } = message.data;
if (!blueprint) {
logger.warn("No blueprint provided");
return;
}
logger.debug(`Received blueprint: ${blueprint}`);
try {
const blueprintParsed = JSON.parse(blueprint);
// Update the blueprint in the database
await applyBlueprint(site.orgId, blueprintParsed, site.siteId);
} catch (error) {
logger.error(`Failed to update database from config: ${error}`);
return {
message: {
type: "newt/blueprint/results",
data: {
success: false,
message: `Failed to update database from config: ${error}`
}
},
broadcast: false, // Send to all clients
excludeSender: false // Include sender in broadcast
};
}
return {
message: {
type: "newt/blueprint/results",
data: {
success: true,
message: "Config updated successfully"
}
},
broadcast: false, // Send to all clients
excludeSender: false // Include sender in broadcast
};
};

View File

@@ -10,6 +10,7 @@ import {
getNextAvailableClientSubnet
} from "@server/lib/ip";
import { selectBestExitNode, verifyExitNodeOrgAccess } from "@server/lib/exitNodes";
import { fetchContainers } from "./dockerSocket";
export type ExitNodePingResult = {
exitNodeId: number;
@@ -76,6 +77,15 @@ export const handleNewtRegisterMessage: MessageHandler = async (context) => {
return;
}
logger.debug(`Docker socket enabled: ${oldSite.dockerSocketEnabled}`);
if (oldSite.dockerSocketEnabled) {
logger.debug(
"Site has docker socket enabled - requesting docker containers"
);
fetchContainers(newt.newtId);
}
let siteSubnet = oldSite.subnet;
let exitNodeIdToQuery = oldSite.exitNodeId;
if (exitNodeId && (oldSite.exitNodeId !== exitNodeId || !oldSite.subnet)) {

View File

@@ -2,6 +2,7 @@ import { MessageHandler } from "../ws";
import logger from "@server/logger";
import { dockerSocketCache } from "./dockerSocket";
import { Newt } from "@server/db";
import { applyNewtDockerBlueprint } from "@server/lib/blueprints/applyNewtDockerBlueprint";
export const handleDockerStatusMessage: MessageHandler = async (context) => {
const { message, client, sendToClient } = context;
@@ -57,4 +58,15 @@ export const handleDockerContainersMessage: MessageHandler = async (
} else {
logger.warn(`Newt ${newt.newtId} does not have Docker containers`);
}
if (!newt.siteId) {
logger.warn("Newt has no site!");
return;
}
await applyNewtDockerBlueprint(
newt.siteId,
newt.newtId,
containers
);
};

View File

@@ -4,4 +4,5 @@ export * from "./handleNewtRegisterMessage";
export * from "./handleReceiveBandwidthMessage";
export * from "./handleGetConfigMessage";
export * from "./handleSocketMessages";
export * from "./handleNewtPingRequestMessage";
export * from "./handleNewtPingRequestMessage";
export * from "./handleApplyBlueprintMessage";

View File

@@ -4,7 +4,8 @@ import {
handleGetConfigMessage,
handleDockerStatusMessage,
handleDockerContainersMessage,
handleNewtPingRequestMessage
handleNewtPingRequestMessage,
handleApplyBlueprintMessage
} from "../newt";
import {
handleOlmRegisterMessage,
@@ -23,7 +24,8 @@ export const messageHandlers: Record<string, MessageHandler> = {
"olm/ping": handleOlmPingMessage,
"newt/socket/status": handleDockerStatusMessage,
"newt/socket/containers": handleDockerContainersMessage,
"newt/ping/request": handleNewtPingRequestMessage
"newt/ping/request": handleNewtPingRequestMessage,
"newt/blueprint/apply": handleApplyBlueprintMessage,
};
startOlmOfflineChecker(); // this is to handle the offline check for olms