Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export const openapi: Swagger.SwaggerV3 = {
additionalProperties: true,
nullable: true,
},
exitHooks: {
type: "array",
items: { $ref: "#/components/schemas/ExitHook" },
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Channel, getQueue } from "@ctrlplane/events";
import { logger } from "@ctrlplane/logger";
import { Permission } from "@ctrlplane/validators/auth";

import { upsertExitHook } from "../_utils/upsertExitHook";
import { authn, authz } from "../../auth";
import { parseBody } from "../../body-parser";
import { request } from "../../middleware";
Expand Down Expand Up @@ -117,6 +118,11 @@ export const PATCH = request()
.returning()
.then(takeFirst);

const { exitHooks } = body;
if (exitHooks != null)
for (const eh of exitHooks)
await upsertExitHook(db, updatedDeployment, eh);

await getQueue(Channel.UpdateDeployment).add(updatedDeployment.id, {
new: updatedDeployment,
old: deployment,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import type { Tx } from "@ctrlplane/db";

import { buildConflictUpdateColumns, takeFirst } from "@ctrlplane/db";
import * as schema from "@ctrlplane/db/schema";
import { HookAction } from "@ctrlplane/validators/events";

type ExitHookInsert = {
name: string;
jobAgentId: string;
jobAgentConfig: Record<string, any>;
};

const upsertHook = (db: Tx, hookName: string, deploymentId: string) =>
db
.insert(schema.hook)
.values({
name: hookName,
action: HookAction.DeploymentResourceRemoved,
scopeType: "deployment",
scopeId: deploymentId,
})
.onConflictDoUpdate({
target: [schema.hook.name, schema.hook.scopeType, schema.hook.scopeId],
set: buildConflictUpdateColumns(schema.hook, ["action"]),
})
.returning()
.then(takeFirst);

const upsertRunbook = (
db: Tx,
exitHook: ExitHookInsert,
deploymentId: string,
) =>
db
.insert(schema.runbook)
.values({ ...exitHook, systemId: deploymentId })
.onConflictDoUpdate({
target: [schema.runbook.name, schema.runbook.systemId],
set: buildConflictUpdateColumns(schema.runbook, [
"jobAgentId",
"jobAgentConfig",
]),
})
.returning()
.then(takeFirst);

const upsertRunbookVariables = (db: Tx, runbookId: string) => {
const variableConfig = {
type: "string" as const,
inputType: "text" as const,
required: true,
};
const variables = [
{
key: "resourceId",
name: "Resource ID",
description: "The ID of the resource to be removed",
config: variableConfig,
required: true,
runbookId,
},
{
key: "deploymentId",
name: "Deployment ID",
description: "The ID of the deployment",
config: variableConfig,
required: true,
runbookId,
},
];

return db
.insert(schema.runbookVariable)
.values(variables)
.onConflictDoNothing();
};

const upsertRunhook = (db: Tx, hookId: string, runbookId: string) =>
db.insert(schema.runhook).values({ hookId, runbookId }).onConflictDoNothing();

export const upsertExitHook = async (
tx: Tx,
deployment: schema.Deployment,
exitHook: ExitHookInsert,
) => {
const hook = await upsertHook(tx, exitHook.name, deployment.id);
const runbook = await upsertRunbook(tx, exitHook, deployment.systemId);
await upsertRunbookVariables(tx, runbook.id);
await upsertRunhook(tx, hook.id, runbook.id);
};
31 changes: 31 additions & 0 deletions apps/webservice/src/app/api/v1/deployments/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ export const openapi: Swagger.SwaggerV3 = {
title: "Ctrlplane API",
version: "1.0.0",
},
components: {
schemas: {
ExitHook: {
type: "object",
properties: {
name: {
type: "string",
description: "The name of the exit hook",
example: "my-exit-hook",
},
jobAgentId: {
type: "string",
format: "uuid",
description: "The ID of the job agent to use for the exit hook",
example: "123e4567-e89b-12d3-a456-426614174000",
},
jobAgentConfig: {
type: "object",
description: "The configuration for the job agent",
example: { key: "value" },
additionalProperties: true,
},
},
required: ["name", "jobAgentId", "jobAgentConfig"],
},
},
},
paths: {
"/v1/deployments": {
post: {
Expand Down Expand Up @@ -67,6 +94,10 @@ export const openapi: Swagger.SwaggerV3 = {
example: { key: "value" },
additionalProperties: true,
},
exitHooks: {
type: "array",
items: { $ref: "#/components/schemas/ExitHook" },
},
},
required: ["systemId", "slug", "name"],
},
Expand Down
11 changes: 11 additions & 0 deletions apps/webservice/src/app/api/v1/deployments/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Permission } from "@ctrlplane/validators/auth";
import { authn, authz } from "../auth";
import { parseBody } from "../body-parser";
import { request } from "../middleware";
import { upsertExitHook } from "./_utils/upsertExitHook";

export const POST = request()
.use(authn)
Expand All @@ -35,6 +36,8 @@ export const POST = request()
)
.then(takeFirstOrNull);

const { exitHooks } = ctx.body;

if (existingDeployment != null) {
// Update existing deployment
const updatedDeployment = await ctx.db
Expand All @@ -49,6 +52,10 @@ export const POST = request()
old: existingDeployment,
});

if (exitHooks != null)
for (const eh of exitHooks)
await upsertExitHook(ctx.db, updatedDeployment, eh);

return NextResponse.json(updatedDeployment, { status: httpStatus.OK });
}

Expand All @@ -59,6 +66,10 @@ export const POST = request()
.returning()
.then(takeFirst);

if (exitHooks != null)
for (const eh of exitHooks)
await upsertExitHook(ctx.db, newDeployment, eh);

await getQueue(Channel.NewDeployment).add(
newDeployment.id,
newDeployment,
Expand Down
41 changes: 41 additions & 0 deletions openapi.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,12 @@
"type": "object",
"additionalProperties": true,
"nullable": true
},
"exitHooks": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ExitHook"
}
}
}
}
Expand Down Expand Up @@ -1126,6 +1132,12 @@
"key": "value"
},
"additionalProperties": true
},
"exitHooks": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ExitHook"
}
}
},
"required": [
Expand Down Expand Up @@ -4914,6 +4926,35 @@
"config"
]
},
"ExitHook": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the exit hook",
"example": "my-exit-hook"
},
"jobAgentId": {
"type": "string",
"format": "uuid",
"description": "The ID of the job agent to use for the exit hook",
"example": "123e4567-e89b-12d3-a456-426614174000"
},
"jobAgentConfig": {
"type": "object",
"description": "The configuration for the job agent",
"example": {
"key": "value"
},
"additionalProperties": true
}
},
"required": [
"name",
"jobAgentId",
"jobAgentConfig"
]
},
"JobWithTrigger": {
"allOf": [
{
Expand Down
2 changes: 2 additions & 0 deletions packages/db/drizzle/0107_small_deathbird.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE UNIQUE INDEX "name_scope_type_scope_id_unique" ON "hook" USING btree ("name","scope_type","scope_id");--> statement-breakpoint
CREATE UNIQUE INDEX "name_system_id_unique" ON "runbook" USING btree ("name","system_id");
Loading
Loading