mirror of
https://github.com/fosrl/pangolin.git
synced 2026-01-29 06:10:47 +00:00
♻️ applyBlueprint core logic now saves the blueprint in the DB
This commit is contained in:
@@ -1,22 +1,35 @@
|
||||
import { db, newts, Target } from "@server/db";
|
||||
import { db, newts, blueprints, Blueprint } from "@server/db";
|
||||
import { Config, ConfigSchema } from "./types";
|
||||
import { ProxyResourcesResults, updateProxyResources } from "./proxyResources";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import logger from "@server/logger";
|
||||
import { resources, targets, sites } from "@server/db";
|
||||
import { eq, and, asc, or, ne, count, isNotNull } from "drizzle-orm";
|
||||
import { sites } from "@server/db";
|
||||
import { eq, and, isNotNull } from "drizzle-orm";
|
||||
import { addTargets as addProxyTargets } from "@server/routers/newt/targets";
|
||||
import { addTargets as addClientTargets } from "@server/routers/client/targets";
|
||||
import {
|
||||
ClientResourcesResults,
|
||||
updateClientResources
|
||||
} from "./clientResources";
|
||||
import { BlueprintSource } from "@server/routers/blueprints/types";
|
||||
import { stringify as stringifyYaml } from "yaml";
|
||||
import { faker } from "@faker-js/faker";
|
||||
|
||||
export async function applyBlueprint(
|
||||
orgId: string,
|
||||
configData: unknown,
|
||||
siteId?: number
|
||||
): Promise<void> {
|
||||
type ApplyBlueprintArgs = {
|
||||
orgId: string;
|
||||
configData: unknown;
|
||||
name?: string;
|
||||
siteId?: number;
|
||||
source?: BlueprintSource;
|
||||
};
|
||||
|
||||
export async function applyBlueprint({
|
||||
orgId,
|
||||
configData,
|
||||
siteId,
|
||||
name,
|
||||
source = "API"
|
||||
}: ApplyBlueprintArgs): Promise<Blueprint> {
|
||||
// Validate the input data
|
||||
const validationResult = ConfigSchema.safeParse(configData);
|
||||
if (!validationResult.success) {
|
||||
@@ -24,6 +37,9 @@ export async function applyBlueprint(
|
||||
}
|
||||
|
||||
const config: Config = validationResult.data;
|
||||
let blueprintSucceeded: boolean = false;
|
||||
let blueprintMessage: string;
|
||||
let error: any | null = null;
|
||||
|
||||
try {
|
||||
let proxyResourcesResults: ProxyResourcesResults = [];
|
||||
@@ -120,10 +136,41 @@ export async function applyBlueprint(
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Failed to update database from config: ${error}`);
|
||||
throw error;
|
||||
|
||||
blueprintSucceeded = true;
|
||||
blueprintMessage = "success";
|
||||
} catch (err) {
|
||||
blueprintSucceeded = false;
|
||||
blueprintMessage = `Failed to update blueprint from config: ${err}`;
|
||||
logger.error(blueprintMessage);
|
||||
error = err;
|
||||
}
|
||||
|
||||
let blueprint: Blueprint | null = null;
|
||||
await db.transaction(async (trx) => {
|
||||
const newBlueprint = await trx
|
||||
.insert(blueprints)
|
||||
.values({
|
||||
orgId,
|
||||
name:
|
||||
name ??
|
||||
`${faker.word.adjective()} ${faker.word.adjective()} ${faker.word.noun()}`,
|
||||
contents: stringifyYaml(configData),
|
||||
createdAt: Math.floor(Date.now() / 1000),
|
||||
succeeded: blueprintSucceeded,
|
||||
message: blueprintMessage,
|
||||
source: source
|
||||
})
|
||||
.returning();
|
||||
|
||||
blueprint = newBlueprint[0];
|
||||
});
|
||||
|
||||
if (!blueprint || !blueprintSucceeded) {
|
||||
throw error ?? "Unknown Server Error";
|
||||
}
|
||||
|
||||
return blueprint;
|
||||
}
|
||||
|
||||
// await updateDatabaseFromConfig("org_i21aifypnlyxur2", {
|
||||
|
||||
Reference in New Issue
Block a user