| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1 | import { PrismaClient } from "@prisma/client"; |
| 2 | import express from "express"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 3 | import fs from "node:fs"; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 4 | import { env } from "node:process"; |
| 5 | import axios from "axios"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 6 | import { GithubClient } from "./github.js"; |
| 7 | import { AppManager } from "./app_manager.js"; |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 8 | import { z } from "zod"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 9 | import { ProjectMonitor, WorkerSchema } from "./project_monitor.js"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 10 | import tmp from "tmp"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 11 | import { NodeJSAnalyzer } from "./lib/nodejs.js"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 12 | import shell from "shelljs"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 13 | import { RealFileSystem } from "./lib/fs.js"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 14 | import path from "node:path"; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 15 | import { Env, generateDodoConfig, ConfigSchema, AppNode, ConfigWithInput, configToGraph, Network } from "config"; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 16 | |
| 17 | async function generateKey(root: string): Promise<[string, string]> { |
| 18 | const privKeyPath = path.join(root, "key"); |
| 19 | const pubKeyPath = path.join(root, "key.pub"); |
| 20 | if (shell.exec(`ssh-keygen -t ed25519 -f ${privKeyPath} -N ""`).code !== 0) { |
| 21 | throw new Error("Failed to generate SSH key pair"); |
| 22 | } |
| 23 | const publicKey = await fs.promises.readFile(pubKeyPath, "utf8"); |
| 24 | const privateKey = await fs.promises.readFile(privKeyPath, "utf8"); |
| 25 | return [publicKey, privateKey]; |
| 26 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 27 | |
| 28 | const db = new PrismaClient(); |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 29 | const appManager = new AppManager(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 30 | |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 31 | const projectMonitors = new Map<number, ProjectMonitor>(); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 32 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 33 | const handleProjectCreate: express.Handler = async (req, resp) => { |
| 34 | try { |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 35 | const tmpDir = tmp.dirSync().name; |
| 36 | const [publicKey, privateKey] = await generateKey(tmpDir); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 37 | const { id } = await db.project.create({ |
| 38 | data: { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 39 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 40 | name: req.body.name, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 41 | deployKey: privateKey, |
| 42 | deployKeyPublic: publicKey, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 43 | }, |
| 44 | }); |
| 45 | resp.status(200); |
| 46 | resp.header("Content-Type", "application/json"); |
| 47 | resp.write( |
| 48 | JSON.stringify({ |
| gio | 74ab785 | 2025-05-13 13:19:31 +0000 | [diff] [blame] | 49 | id: id.toString(), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 50 | }), |
| 51 | ); |
| 52 | } catch (e) { |
| 53 | console.log(e); |
| 54 | resp.status(500); |
| 55 | } finally { |
| 56 | resp.end(); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | const handleProjectAll: express.Handler = async (req, resp) => { |
| 61 | try { |
| 62 | const r = await db.project.findMany({ |
| 63 | where: { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 64 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 65 | }, |
| 66 | }); |
| 67 | resp.status(200); |
| 68 | resp.header("Content-Type", "application/json"); |
| 69 | resp.write( |
| 70 | JSON.stringify( |
| 71 | r.map((p) => ({ |
| 72 | id: p.id.toString(), |
| 73 | name: p.name, |
| 74 | })), |
| 75 | ), |
| 76 | ); |
| 77 | } catch (e) { |
| 78 | console.log(e); |
| 79 | resp.status(500); |
| 80 | } finally { |
| 81 | resp.end(); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | const handleSave: express.Handler = async (req, resp) => { |
| 86 | try { |
| 87 | await db.project.update({ |
| 88 | where: { |
| 89 | id: Number(req.params["projectId"]), |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 90 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 91 | }, |
| 92 | data: { |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 93 | draft: JSON.stringify(req.body), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 94 | }, |
| 95 | }); |
| 96 | resp.status(200); |
| 97 | } catch (e) { |
| 98 | console.log(e); |
| 99 | resp.status(500); |
| 100 | } finally { |
| 101 | resp.end(); |
| 102 | } |
| 103 | }; |
| 104 | |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 105 | function handleSavedGet(state: "deploy" | "draft"): express.Handler { |
| 106 | return async (req, resp) => { |
| 107 | try { |
| 108 | const r = await db.project.findUnique({ |
| 109 | where: { |
| 110 | id: Number(req.params["projectId"]), |
| 111 | userId: resp.locals.userId, |
| 112 | }, |
| 113 | select: { |
| 114 | state: true, |
| 115 | draft: true, |
| 116 | }, |
| 117 | }); |
| 118 | if (r == null) { |
| 119 | resp.status(404); |
| 120 | return; |
| 121 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 122 | resp.status(200); |
| 123 | resp.header("content-type", "application/json"); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 124 | let currentState: Record<string, unknown> | null = null; |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 125 | if (state === "deploy") { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 126 | if (r.state == null) { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 127 | currentState = { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 128 | nodes: [], |
| 129 | edges: [], |
| 130 | viewport: { x: 0, y: 0, zoom: 1 }, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 131 | }; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 132 | } else { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 133 | currentState = JSON.parse(Buffer.from(r.state).toString("utf8")); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 134 | } |
| 135 | } else { |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 136 | if (r.draft == null) { |
| 137 | if (r.state == null) { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 138 | currentState = { |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 139 | nodes: [], |
| 140 | edges: [], |
| 141 | viewport: { x: 0, y: 0, zoom: 1 }, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 142 | }; |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 143 | } else { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 144 | currentState = JSON.parse(Buffer.from(r.state).toString("utf8")); |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 145 | } |
| 146 | } else { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 147 | currentState = JSON.parse(Buffer.from(r.draft).toString("utf8")); |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 148 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 149 | } |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 150 | const env = await getEnv(Number(req.params["projectId"]), resp.locals.userId, resp.locals.username); |
| 151 | if (currentState) { |
| 152 | const config = generateDodoConfig( |
| 153 | req.params["projectId"].toString(), |
| 154 | currentState.nodes as AppNode[], |
| 155 | env, |
| 156 | ); |
| 157 | resp.send({ |
| 158 | state: currentState, |
| 159 | config, |
| 160 | }); |
| 161 | } |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 162 | } catch (e) { |
| 163 | console.log(e); |
| 164 | resp.status(500); |
| 165 | } finally { |
| 166 | resp.end(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 167 | } |
| gio | 818da4e | 2025-05-12 14:45:35 +0000 | [diff] [blame] | 168 | }; |
| 169 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 170 | |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 171 | const projectDeleteReqSchema = z.object({ |
| 172 | state: z.optional(z.nullable(z.string())), |
| 173 | }); |
| 174 | |
| 175 | const handleProjectDelete: express.Handler = async (req, resp) => { |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 176 | try { |
| 177 | const projectId = Number(req.params["projectId"]); |
| 178 | const p = await db.project.findUnique({ |
| 179 | where: { |
| 180 | id: projectId, |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 181 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 182 | }, |
| 183 | select: { |
| 184 | instanceId: true, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 185 | githubToken: true, |
| 186 | deployKeyPublic: true, |
| 187 | state: true, |
| 188 | draft: true, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 189 | }, |
| 190 | }); |
| 191 | if (p === null) { |
| 192 | resp.status(404); |
| 193 | return; |
| 194 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 195 | const parseResult = projectDeleteReqSchema.safeParse(req.body); |
| 196 | if (!parseResult.success) { |
| 197 | resp.status(400); |
| 198 | resp.write(JSON.stringify({ error: "Invalid request body", issues: parseResult.error.format() })); |
| 199 | return; |
| gio | e440db8 | 2025-05-13 12:21:44 +0000 | [diff] [blame] | 200 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 201 | if (p.githubToken && p.deployKeyPublic) { |
| 202 | const allRepos = [ |
| 203 | ...new Set([ |
| 204 | ...extractGithubRepos(p.state), |
| 205 | ...extractGithubRepos(p.draft), |
| 206 | ...extractGithubRepos(parseResult.data.state), |
| 207 | ]), |
| 208 | ]; |
| 209 | if (allRepos.length > 0) { |
| 210 | const diff: RepoDiff = { toDelete: allRepos, toAdd: [] }; |
| 211 | const github = new GithubClient(p.githubToken); |
| 212 | await manageGithubRepos(github, diff, p.deployKeyPublic, env.PUBLIC_ADDR); |
| 213 | console.log( |
| 214 | `Attempted to remove deploy keys for project ${projectId} from associated GitHub repositories.`, |
| 215 | ); |
| 216 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 217 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 218 | if (p.instanceId !== null) { |
| 219 | if (!(await appManager.removeInstance(p.instanceId))) { |
| 220 | resp.status(500); |
| 221 | resp.write(JSON.stringify({ error: "Failed to remove deployment from cluster" })); |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | await db.project.delete({ |
| 226 | where: { |
| 227 | id: projectId, |
| 228 | }, |
| 229 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 230 | resp.status(200); |
| 231 | } catch (e) { |
| 232 | console.log(e); |
| 233 | resp.status(500); |
| 234 | } finally { |
| 235 | resp.end(); |
| 236 | } |
| 237 | }; |
| 238 | |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 239 | function extractGithubRepos(serializedState: string | null | undefined): string[] { |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 240 | if (!serializedState) { |
| 241 | return []; |
| 242 | } |
| 243 | try { |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 244 | const stateObj = JSON.parse(serializedState); |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 245 | const githubNodes = stateObj.nodes.filter( |
| 246 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 247 | (n: any) => n.type === "github" && n.data?.repository?.id, |
| 248 | ); |
| 249 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 250 | return githubNodes.map((n: any) => n.data.repository.sshURL); |
| 251 | } catch (error) { |
| 252 | console.error("Failed to parse state or extract GitHub repos:", error); |
| 253 | return []; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | type RepoDiff = { |
| 258 | toAdd?: string[]; |
| 259 | toDelete?: string[]; |
| 260 | }; |
| 261 | |
| 262 | function calculateRepoDiff(oldRepos: string[], newRepos: string[]): RepoDiff { |
| 263 | const toAdd = newRepos.filter((repo) => !oldRepos.includes(repo)); |
| 264 | const toDelete = oldRepos.filter((repo) => !newRepos.includes(repo)); |
| 265 | return { toAdd, toDelete }; |
| 266 | } |
| 267 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 268 | async function manageGithubRepos( |
| 269 | github: GithubClient, |
| 270 | diff: RepoDiff, |
| 271 | deployKey: string, |
| 272 | publicAddr?: string, |
| 273 | ): Promise<void> { |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 274 | for (const repoUrl of diff.toDelete ?? []) { |
| 275 | try { |
| 276 | await github.removeDeployKey(repoUrl, deployKey); |
| 277 | console.log(`Removed deploy key from repository ${repoUrl}`); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 278 | if (publicAddr) { |
| 279 | const webhookCallbackUrl = `${publicAddr}/api/webhook/github/push`; |
| 280 | await github.removePushWebhook(repoUrl, webhookCallbackUrl); |
| 281 | console.log(`Removed push webhook from repository ${repoUrl}`); |
| 282 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 283 | } catch (error) { |
| 284 | console.error(`Failed to remove deploy key from repository ${repoUrl}:`, error); |
| 285 | } |
| 286 | } |
| 287 | for (const repoUrl of diff.toAdd ?? []) { |
| 288 | try { |
| 289 | await github.addDeployKey(repoUrl, deployKey); |
| 290 | console.log(`Added deploy key to repository ${repoUrl}`); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 291 | if (publicAddr) { |
| 292 | const webhookCallbackUrl = `${publicAddr}/api/webhook/github/push`; |
| 293 | await github.addPushWebhook(repoUrl, webhookCallbackUrl); |
| 294 | console.log(`Added push webhook to repository ${repoUrl}`); |
| 295 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 296 | } catch (error) { |
| 297 | console.error(`Failed to add deploy key from repository ${repoUrl}:`, error); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 302 | const handleDeploy: express.Handler = async (req, resp) => { |
| 303 | try { |
| 304 | const projectId = Number(req.params["projectId"]); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 305 | const p = await db.project.findUnique({ |
| 306 | where: { |
| 307 | id: projectId, |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 308 | // userId: resp.locals.userId, TODO(gio): validate |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 309 | }, |
| 310 | select: { |
| 311 | instanceId: true, |
| 312 | githubToken: true, |
| 313 | deployKey: true, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 314 | deployKeyPublic: true, |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 315 | state: true, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 316 | }, |
| 317 | }); |
| 318 | if (p === null) { |
| 319 | resp.status(404); |
| 320 | return; |
| 321 | } |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 322 | const config = ConfigSchema.safeParse(req.body.config); |
| 323 | if (!config.success) { |
| 324 | resp.status(400); |
| 325 | resp.write(JSON.stringify({ error: "Invalid configuration", issues: config.error.format() })); |
| 326 | return; |
| 327 | } |
| 328 | const state = req.body.state |
| 329 | ? JSON.stringify(req.body.state) |
| 330 | : JSON.stringify( |
| 331 | configToGraph( |
| 332 | config.data, |
| 333 | getNetworks(resp.locals.username), |
| 334 | p.state ? JSON.parse(Buffer.from(p.state).toString("utf8")) : null, |
| 335 | ), |
| 336 | ); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 337 | await db.project.update({ |
| 338 | where: { |
| 339 | id: projectId, |
| 340 | }, |
| 341 | data: { |
| 342 | draft: state, |
| 343 | }, |
| 344 | }); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 345 | let deployKey: string | null = p.deployKey; |
| 346 | let deployKeyPublic: string | null = p.deployKeyPublic; |
| 347 | if (deployKeyPublic == null) { |
| 348 | [deployKeyPublic, deployKey] = await generateKey(tmp.dirSync().name); |
| 349 | await db.project.update({ |
| 350 | where: { id: projectId }, |
| 351 | data: { deployKeyPublic, deployKey }, |
| 352 | }); |
| 353 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 354 | let diff: RepoDiff | null = null; |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 355 | const cfg: ConfigWithInput = { |
| 356 | ...config.data, |
| 357 | input: { |
| 358 | appId: projectId.toString(), |
| 359 | managerAddr: env.INTERNAL_API_ADDR!, |
| 360 | key: { |
| 361 | public: deployKeyPublic!, |
| 362 | private: deployKey!, |
| 363 | }, |
| 364 | }, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 365 | }; |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 366 | try { |
| 367 | if (p.instanceId == null) { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 368 | const deployResponse = await appManager.deploy(cfg); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 369 | await db.project.update({ |
| 370 | where: { |
| 371 | id: projectId, |
| 372 | }, |
| 373 | data: { |
| 374 | state, |
| 375 | draft: null, |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 376 | instanceId: deployResponse.id, |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 377 | access: JSON.stringify(deployResponse.access), |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 378 | }, |
| 379 | }); |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 380 | diff = { toAdd: extractGithubRepos(state) }; |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 381 | } else { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 382 | const deployResponse = await appManager.update(p.instanceId, cfg); |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 383 | diff = calculateRepoDiff(extractGithubRepos(p.state), extractGithubRepos(state)); |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 384 | await db.project.update({ |
| 385 | where: { |
| 386 | id: projectId, |
| 387 | }, |
| 388 | data: { |
| 389 | state, |
| 390 | draft: null, |
| 391 | access: JSON.stringify(deployResponse.access), |
| 392 | }, |
| 393 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 394 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 395 | if (diff && p.githubToken && deployKey) { |
| 396 | const github = new GithubClient(p.githubToken); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 397 | await manageGithubRepos(github, diff, deployKeyPublic!, env.PUBLIC_ADDR); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 398 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 399 | resp.status(200); |
| 400 | } catch (error) { |
| 401 | console.error("Deployment error:", error); |
| 402 | resp.status(500); |
| 403 | resp.write(JSON.stringify({ error: "Deployment failed" })); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 404 | } |
| 405 | } catch (e) { |
| 406 | console.log(e); |
| 407 | resp.status(500); |
| 408 | } finally { |
| 409 | resp.end(); |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | const handleStatus: express.Handler = async (req, resp) => { |
| 414 | try { |
| 415 | const projectId = Number(req.params["projectId"]); |
| 416 | const p = await db.project.findUnique({ |
| 417 | where: { |
| 418 | id: projectId, |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 419 | userId: resp.locals.userId, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 420 | }, |
| 421 | select: { |
| 422 | instanceId: true, |
| 423 | }, |
| 424 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 425 | if (p === null) { |
| 426 | resp.status(404); |
| 427 | return; |
| 428 | } |
| 429 | if (p.instanceId == null) { |
| 430 | resp.status(404); |
| 431 | return; |
| 432 | } |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 433 | try { |
| 434 | const status = await appManager.getStatus(p.instanceId); |
| 435 | resp.status(200); |
| 436 | resp.write(JSON.stringify(status)); |
| 437 | } catch (error) { |
| 438 | console.error("Error getting status:", error); |
| 439 | resp.status(500); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 440 | } |
| 441 | } catch (e) { |
| 442 | console.log(e); |
| 443 | resp.status(500); |
| 444 | } finally { |
| 445 | resp.end(); |
| 446 | } |
| 447 | }; |
| 448 | |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 449 | const handleConfigGet: express.Handler = async (req, resp) => { |
| 450 | try { |
| 451 | const projectId = Number(req.params["projectId"]); |
| 452 | const project = await db.project.findUnique({ |
| 453 | where: { |
| 454 | id: projectId, |
| 455 | }, |
| 456 | select: { |
| 457 | state: true, |
| 458 | }, |
| 459 | }); |
| 460 | |
| 461 | if (!project || !project.state) { |
| 462 | resp.status(404).send({ error: "No deployed configuration found." }); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | const state = JSON.parse(Buffer.from(project.state).toString("utf8")); |
| 467 | const env = await getEnv(projectId, resp.locals.userId, resp.locals.username); |
| 468 | const config = generateDodoConfig(projectId.toString(), state.nodes, env); |
| 469 | |
| 470 | if (!config) { |
| 471 | resp.status(500).send({ error: "Failed to generate configuration." }); |
| 472 | return; |
| 473 | } |
| 474 | resp.status(200).json(config); |
| 475 | } catch (e) { |
| 476 | console.log(e); |
| 477 | resp.status(500).send({ error: "Internal server error" }); |
| 478 | } finally { |
| 479 | console.log("config get done"); |
| 480 | resp.end(); |
| 481 | } |
| 482 | }; |
| 483 | |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 484 | const handleRemoveDeployment: express.Handler = async (req, resp) => { |
| 485 | try { |
| 486 | const projectId = Number(req.params["projectId"]); |
| 487 | const p = await db.project.findUnique({ |
| 488 | where: { |
| 489 | id: projectId, |
| 490 | userId: resp.locals.userId, |
| 491 | }, |
| 492 | select: { |
| 493 | instanceId: true, |
| 494 | githubToken: true, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 495 | deployKeyPublic: true, |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 496 | state: true, |
| 497 | draft: true, |
| 498 | }, |
| 499 | }); |
| 500 | if (p === null) { |
| 501 | resp.status(404); |
| 502 | resp.write(JSON.stringify({ error: "Project not found" })); |
| 503 | return; |
| 504 | } |
| 505 | if (p.instanceId == null) { |
| 506 | resp.status(400); |
| 507 | resp.write(JSON.stringify({ error: "Project not deployed" })); |
| 508 | return; |
| 509 | } |
| 510 | const removed = await appManager.removeInstance(p.instanceId); |
| 511 | if (!removed) { |
| 512 | resp.status(500); |
| 513 | resp.write(JSON.stringify({ error: "Failed to remove deployment from cluster" })); |
| 514 | return; |
| 515 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 516 | if (p.githubToken && p.deployKeyPublic && p.state) { |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 517 | try { |
| 518 | const github = new GithubClient(p.githubToken); |
| 519 | const repos = extractGithubRepos(p.state); |
| 520 | const diff = { toDelete: repos, toAdd: [] }; |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 521 | await manageGithubRepos(github, diff, p.deployKeyPublic, env.PUBLIC_ADDR); |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 522 | } catch (error) { |
| 523 | console.error("Error removing GitHub deploy keys:", error); |
| 524 | } |
| 525 | } |
| 526 | await db.project.update({ |
| 527 | where: { |
| 528 | id: projectId, |
| 529 | }, |
| 530 | data: { |
| 531 | instanceId: null, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 532 | deployKeyPublic: null, |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 533 | access: null, |
| gio | bd37a2b | 2025-05-15 04:28:42 +0000 | [diff] [blame] | 534 | state: null, |
| 535 | draft: p.draft ?? p.state, |
| 536 | }, |
| 537 | }); |
| 538 | resp.status(200); |
| 539 | resp.write(JSON.stringify({ success: true })); |
| 540 | } catch (e) { |
| 541 | console.error("Error removing deployment:", e); |
| 542 | resp.status(500); |
| 543 | resp.write(JSON.stringify({ error: "Internal server error" })); |
| 544 | } finally { |
| 545 | resp.end(); |
| 546 | } |
| 547 | }; |
| 548 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 549 | const handleGithubRepos: express.Handler = async (req, resp) => { |
| 550 | try { |
| 551 | const projectId = Number(req.params["projectId"]); |
| 552 | const project = await db.project.findUnique({ |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 553 | where: { |
| 554 | id: projectId, |
| 555 | userId: resp.locals.userId, |
| 556 | }, |
| 557 | select: { |
| 558 | githubToken: true, |
| 559 | }, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 560 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 561 | if (!project?.githubToken) { |
| 562 | resp.status(400); |
| 563 | resp.write(JSON.stringify({ error: "GitHub token not configured" })); |
| 564 | return; |
| 565 | } |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 566 | const github = new GithubClient(project.githubToken); |
| 567 | const repositories = await github.getRepositories(); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 568 | resp.status(200); |
| 569 | resp.header("Content-Type", "application/json"); |
| 570 | resp.write(JSON.stringify(repositories)); |
| 571 | } catch (e) { |
| 572 | console.log(e); |
| 573 | resp.status(500); |
| 574 | resp.write(JSON.stringify({ error: "Failed to fetch repositories" })); |
| 575 | } finally { |
| 576 | resp.end(); |
| 577 | } |
| 578 | }; |
| 579 | |
| 580 | const handleUpdateGithubToken: express.Handler = async (req, resp) => { |
| 581 | try { |
| 582 | const projectId = Number(req.params["projectId"]); |
| 583 | const { githubToken } = req.body; |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 584 | await db.project.update({ |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 585 | where: { |
| 586 | id: projectId, |
| 587 | userId: resp.locals.userId, |
| 588 | }, |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 589 | data: { githubToken }, |
| 590 | }); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 591 | resp.status(200); |
| 592 | } catch (e) { |
| 593 | console.log(e); |
| 594 | resp.status(500); |
| 595 | } finally { |
| 596 | resp.end(); |
| 597 | } |
| 598 | }; |
| 599 | |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 600 | const getNetworks = (username?: string | undefined): Network[] => { |
| 601 | return [ |
| 602 | { |
| 603 | name: "Trial", |
| 604 | domain: "trial.dodoapp.xyz", |
| 605 | hasAuth: false, |
| 606 | }, |
| 607 | // TODO(gio): Remove |
| 608 | ].concat( |
| 609 | username === "gio" || 1 == 1 |
| 610 | ? [ |
| 611 | { |
| 612 | name: "Public", |
| 613 | domain: "v1.dodo.cloud", |
| 614 | hasAuth: true, |
| 615 | }, |
| 616 | { |
| 617 | name: "Private", |
| 618 | domain: "p.v1.dodo.cloud", |
| 619 | hasAuth: true, |
| 620 | }, |
| 621 | ] |
| 622 | : [], |
| 623 | ); |
| 624 | }; |
| 625 | |
| 626 | const getEnv = async (projectId: number, userId: string, username: string): Promise<Env> => { |
| 627 | const project = await db.project.findUnique({ |
| 628 | where: { |
| 629 | id: projectId, |
| 630 | userId, |
| 631 | }, |
| 632 | select: { |
| 633 | deployKeyPublic: true, |
| 634 | githubToken: true, |
| 635 | access: true, |
| 636 | instanceId: true, |
| 637 | }, |
| 638 | }); |
| 639 | if (!project) { |
| 640 | throw new Error("Project not found"); |
| 641 | } |
| 642 | const monitor = projectMonitors.get(projectId); |
| 643 | const serviceNames = monitor ? monitor.getAllServiceNames() : []; |
| 644 | const services = serviceNames.map((name: string) => ({ |
| 645 | name, |
| 646 | workers: [...(monitor ? monitor.getWorkerStatusesForService(name) : new Map()).entries()].map( |
| 647 | ([id, status]) => ({ |
| 648 | ...status, |
| 649 | id, |
| 650 | }), |
| 651 | ), |
| 652 | })); |
| 653 | return { |
| 654 | managerAddr: env.INTERNAL_API_ADDR, |
| 655 | deployKeyPublic: project.deployKeyPublic == null ? undefined : project.deployKeyPublic, |
| 656 | instanceId: project.instanceId == null ? undefined : project.instanceId, |
| 657 | access: JSON.parse(project.access ?? "[]"), |
| 658 | integrations: { |
| 659 | github: !!project.githubToken, |
| 660 | }, |
| 661 | networks: getNetworks(username), |
| 662 | services, |
| 663 | user: { |
| 664 | id: userId, |
| 665 | username: username, |
| 666 | }, |
| 667 | }; |
| 668 | }; |
| 669 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 670 | const handleEnv: express.Handler = async (req, resp) => { |
| 671 | const projectId = Number(req.params["projectId"]); |
| 672 | try { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 673 | const env = await getEnv(projectId, resp.locals.userId, resp.locals.username); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 674 | resp.status(200); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 675 | resp.write(JSON.stringify(env)); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 676 | } catch (error) { |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 677 | console.error("Error getting env:", error); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 678 | resp.status(500); |
| 679 | resp.write(JSON.stringify({ error: "Internal server error" })); |
| 680 | } finally { |
| 681 | resp.end(); |
| 682 | } |
| 683 | }; |
| 684 | |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 685 | const handleServiceLogs: express.Handler = async (req, resp) => { |
| 686 | try { |
| 687 | const projectId = Number(req.params["projectId"]); |
| 688 | const service = req.params["service"]; |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 689 | const workerId = req.params["workerId"]; |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 690 | const project = await db.project.findUnique({ |
| 691 | where: { |
| 692 | id: projectId, |
| 693 | userId: resp.locals.userId, |
| 694 | }, |
| 695 | }); |
| 696 | if (project == null) { |
| 697 | resp.status(404); |
| 698 | resp.write(JSON.stringify({ error: "Project not found" })); |
| 699 | return; |
| 700 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 701 | const monitor = projectMonitors.get(projectId); |
| 702 | if (!monitor || !monitor.hasLogs()) { |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 703 | resp.status(404); |
| 704 | resp.write(JSON.stringify({ error: "No logs found for this project" })); |
| 705 | return; |
| 706 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 707 | const serviceLog = monitor.getWorkerLog(service, workerId); |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 708 | if (!serviceLog) { |
| 709 | resp.status(404); |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 710 | resp.write(JSON.stringify({ error: "No logs found for this service/worker" })); |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 711 | return; |
| 712 | } |
| gio | 3a921b8 | 2025-05-10 07:36:09 +0000 | [diff] [blame] | 713 | resp.status(200); |
| 714 | resp.write(JSON.stringify({ logs: serviceLog })); |
| 715 | } catch (e) { |
| 716 | console.log(e); |
| 717 | resp.status(500); |
| 718 | resp.write(JSON.stringify({ error: "Failed to get service logs" })); |
| 719 | } finally { |
| 720 | resp.end(); |
| 721 | } |
| 722 | }; |
| 723 | |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 724 | const handleRegisterWorker: express.Handler = async (req, resp) => { |
| 725 | try { |
| 726 | const projectId = Number(req.params["projectId"]); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 727 | const result = WorkerSchema.safeParse(req.body); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 728 | if (!result.success) { |
| 729 | resp.status(400); |
| 730 | resp.write( |
| 731 | JSON.stringify({ |
| 732 | error: "Invalid request data", |
| 733 | details: result.error.format(), |
| 734 | }), |
| 735 | ); |
| 736 | return; |
| 737 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 738 | let monitor = projectMonitors.get(projectId); |
| 739 | if (!monitor) { |
| 740 | monitor = new ProjectMonitor(); |
| 741 | projectMonitors.set(projectId, monitor); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 742 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 743 | monitor.registerWorker(result.data); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 744 | resp.status(200); |
| 745 | resp.write( |
| 746 | JSON.stringify({ |
| 747 | success: true, |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 748 | }), |
| 749 | ); |
| 750 | } catch (e) { |
| 751 | console.log(e); |
| 752 | resp.status(500); |
| 753 | resp.write(JSON.stringify({ error: "Failed to register worker" })); |
| 754 | } finally { |
| 755 | resp.end(); |
| 756 | } |
| 757 | }; |
| 758 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 759 | async function reloadProject(projectId: number): Promise<boolean> { |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 760 | const monitor = projectMonitors.get(projectId); |
| 761 | const projectWorkers = monitor ? monitor.getWorkerAddresses() : []; |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 762 | const workerCount = projectWorkers.length; |
| 763 | if (workerCount === 0) { |
| 764 | return true; |
| 765 | } |
| 766 | const results = await Promise.all( |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 767 | projectWorkers.map(async (workerAddress: string) => { |
| 768 | try { |
| 769 | const { data } = await axios.get(`http://${workerAddress}/reload`); |
| 770 | return data.every((s: { status: string }) => s.status === "ok"); |
| 771 | } catch (error) { |
| 772 | console.error(`Failed to reload worker ${workerAddress}:`, error); |
| 773 | return false; |
| 774 | } |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 775 | }), |
| 776 | ); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 777 | return results.reduce((acc: boolean, curr: boolean) => acc && curr, true); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 780 | const handleReload: express.Handler = async (req, resp) => { |
| 781 | try { |
| 782 | const projectId = Number(req.params["projectId"]); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 783 | const projectAuth = await db.project.findFirst({ |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 784 | where: { |
| 785 | id: projectId, |
| 786 | userId: resp.locals.userId, |
| 787 | }, |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 788 | select: { id: true }, |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 789 | }); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 790 | if (!projectAuth) { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 791 | resp.status(404); |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 792 | return; |
| 793 | } |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 794 | const success = await reloadProject(projectId); |
| 795 | if (success) { |
| 796 | resp.status(200); |
| 797 | } else { |
| 798 | resp.status(500); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 799 | } |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 800 | } catch (e) { |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 801 | console.error(e); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 802 | resp.status(500); |
| gio | 7d81370 | 2025-05-08 18:29:52 +0000 | [diff] [blame] | 803 | } |
| 804 | }; |
| 805 | |
| gio | 918780d | 2025-05-22 08:24:41 +0000 | [diff] [blame] | 806 | const handleReloadWorker: express.Handler = async (req, resp) => { |
| 807 | const projectId = Number(req.params["projectId"]); |
| 808 | const serviceName = req.params["serviceName"]; |
| 809 | const workerId = req.params["workerId"]; |
| 810 | |
| 811 | const projectMonitor = projectMonitors.get(projectId); |
| 812 | if (!projectMonitor) { |
| 813 | resp.status(404).send({ error: "Project monitor not found" }); |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | try { |
| 818 | await projectMonitor.reloadWorker(serviceName, workerId); |
| 819 | resp.status(200).send({ message: "Worker reload initiated" }); |
| 820 | } catch (error) { |
| 821 | console.error(`Failed to reload worker ${workerId} in service ${serviceName} for project ${projectId}:`, error); |
| 822 | const errorMessage = error instanceof Error ? error.message : "Unknown error"; |
| 823 | resp.status(500).send({ error: `Failed to reload worker: ${errorMessage}` }); |
| 824 | } |
| 825 | }; |
| 826 | |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 827 | const analyzeRepoReqSchema = z.object({ |
| 828 | address: z.string(), |
| 829 | }); |
| 830 | |
| 831 | const handleAnalyzeRepo: express.Handler = async (req, resp) => { |
| 832 | const projectId = Number(req.params["projectId"]); |
| 833 | const project = await db.project.findUnique({ |
| 834 | where: { |
| 835 | id: projectId, |
| 836 | userId: resp.locals.userId, |
| 837 | }, |
| 838 | select: { |
| 839 | githubToken: true, |
| 840 | deployKey: true, |
| 841 | deployKeyPublic: true, |
| 842 | }, |
| 843 | }); |
| 844 | if (!project) { |
| 845 | resp.status(404).send({ error: "Project not found" }); |
| 846 | return; |
| 847 | } |
| 848 | if (!project.githubToken) { |
| 849 | resp.status(400).send({ error: "GitHub token not configured" }); |
| 850 | return; |
| 851 | } |
| gio | 8e74dc0 | 2025-06-13 10:19:26 +0000 | [diff] [blame] | 852 | let tmpDir: tmp.DirResult | null = null; |
| 853 | try { |
| 854 | let deployKey: string | null = project.deployKey; |
| 855 | let deployKeyPublic: string | null = project.deployKeyPublic; |
| 856 | if (!deployKeyPublic) { |
| 857 | [deployKeyPublic, deployKey] = await generateKey(tmp.dirSync().name); |
| 858 | await db.project.update({ |
| 859 | where: { id: projectId }, |
| 860 | data: { |
| 861 | deployKeyPublic: deployKeyPublic, |
| 862 | deployKey: deployKey, |
| 863 | }, |
| 864 | }); |
| 865 | } |
| 866 | const github = new GithubClient(project.githubToken); |
| 867 | const result = analyzeRepoReqSchema.safeParse(req.body); |
| 868 | if (!result.success) { |
| 869 | resp.status(400).send({ error: "Invalid request data" }); |
| 870 | return; |
| 871 | } |
| 872 | const { address } = result.data; |
| 873 | tmpDir = tmp.dirSync({ |
| 874 | unsafeCleanup: true, |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 875 | }); |
| gio | 8e74dc0 | 2025-06-13 10:19:26 +0000 | [diff] [blame] | 876 | await github.addDeployKey(address, deployKeyPublic); |
| 877 | await fs.promises.writeFile(path.join(tmpDir.name, "key"), deployKey!, { |
| 878 | mode: 0o600, |
| 879 | }); |
| 880 | shell.exec( |
| 881 | `GIT_SSH_COMMAND='ssh -i ${tmpDir.name}/key -o IdentitiesOnly=yes' git clone ${address} ${tmpDir.name}/code`, |
| 882 | ); |
| 883 | const fsc = new RealFileSystem(`${tmpDir.name}/code`); |
| 884 | const analyzer = new NodeJSAnalyzer(); |
| 885 | const info = await analyzer.analyze(fsc, "/"); |
| 886 | resp.status(200).send([info]); |
| 887 | } catch (e) { |
| 888 | console.error(e); |
| 889 | resp.status(500).send({ error: "Failed to analyze repository" }); |
| 890 | } finally { |
| 891 | if (tmpDir) { |
| 892 | tmpDir.removeCallback(); |
| 893 | } |
| 894 | resp.end(); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 895 | } |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 896 | }; |
| 897 | |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 898 | const auth = (req: express.Request, resp: express.Response, next: express.NextFunction) => { |
| 899 | const userId = req.get("x-forwarded-userid"); |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 900 | const username = req.get("x-forwarded-user"); |
| 901 | if (userId == null || username == null) { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 902 | resp.status(401); |
| 903 | resp.write("Unauthorized"); |
| 904 | resp.end(); |
| 905 | return; |
| 906 | } |
| 907 | resp.locals.userId = userId; |
| gio | 3ed5959 | 2025-05-14 16:51:09 +0000 | [diff] [blame] | 908 | resp.locals.username = username; |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 909 | next(); |
| 910 | }; |
| 911 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 912 | const handleGithubPushWebhook: express.Handler = async (req, resp) => { |
| 913 | try { |
| 914 | // TODO(gio): Implement GitHub signature verification for security |
| 915 | const webhookSchema = z.object({ |
| 916 | repository: z.object({ |
| 917 | ssh_url: z.string(), |
| 918 | }), |
| 919 | }); |
| 920 | |
| 921 | const result = webhookSchema.safeParse(req.body); |
| 922 | if (!result.success) { |
| 923 | console.warn("GitHub webhook: Invalid payload:", result.error.issues); |
| 924 | resp.status(400).json({ error: "Invalid webhook payload" }); |
| 925 | return; |
| 926 | } |
| 927 | const { ssh_url: addr } = result.data.repository; |
| 928 | const allProjects = await db.project.findMany({ |
| 929 | select: { |
| 930 | id: true, |
| 931 | state: true, |
| 932 | }, |
| 933 | where: { |
| 934 | instanceId: { |
| 935 | not: null, |
| 936 | }, |
| 937 | }, |
| 938 | }); |
| 939 | // TODO(gio): This should run in background |
| 940 | new Promise<boolean>((resolve, reject) => { |
| 941 | setTimeout(() => { |
| 942 | const projectsToReloadIds: number[] = []; |
| 943 | for (const project of allProjects) { |
| 944 | if (project.state && project.state.length > 0) { |
| 945 | const projectRepos = extractGithubRepos(project.state); |
| 946 | if (projectRepos.includes(addr)) { |
| 947 | projectsToReloadIds.push(project.id); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | Promise.all(projectsToReloadIds.map((id) => reloadProject(id))) |
| 952 | .then((results) => { |
| 953 | resolve(results.reduce((acc, curr) => acc && curr, true)); |
| 954 | }) |
| 955 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 956 | .catch((reason: any) => reject(reason)); |
| 957 | }, 10); |
| 958 | }); |
| 959 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 960 | } catch (error: any) { |
| 961 | console.error(error); |
| 962 | resp.status(500); |
| 963 | } |
| 964 | }; |
| 965 | |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 966 | const handleValidateConfig: express.Handler = async (req, resp) => { |
| 967 | try { |
| 968 | const validationResult = ConfigSchema.safeParse(req.body); |
| 969 | if (!validationResult.success) { |
| 970 | resp.status(400); |
| 971 | resp.header("Content-Type", "application/json"); |
| 972 | resp.write(JSON.stringify({ success: false, errors: validationResult.error.flatten() })); |
| 973 | } else { |
| 974 | resp.status(200); |
| 975 | resp.header("Content-Type", "application/json"); |
| 976 | resp.write(JSON.stringify({ success: true })); |
| 977 | } |
| 978 | } catch (e) { |
| 979 | console.log(e); |
| 980 | resp.status(500); |
| 981 | } finally { |
| 982 | resp.end(); |
| 983 | } |
| 984 | }; |
| 985 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 986 | async function start() { |
| 987 | await db.$connect(); |
| 988 | const app = express(); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 989 | app.set("json spaces", 2); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 990 | app.use(express.json()); // Global JSON parsing |
| 991 | |
| 992 | // Public webhook route - no auth needed |
| 993 | app.post("/api/webhook/github/push", handleGithubPushWebhook); |
| 994 | |
| 995 | // Authenticated project routes |
| 996 | const projectRouter = express.Router(); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 997 | projectRouter.use(auth); |
| 998 | projectRouter.post("/:projectId/analyze", handleAnalyzeRepo); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 999 | projectRouter.post("/:projectId/saved", handleSave); |
| 1000 | projectRouter.get("/:projectId/saved/deploy", handleSavedGet("deploy")); |
| 1001 | projectRouter.get("/:projectId/saved/draft", handleSavedGet("draft")); |
| 1002 | projectRouter.post("/:projectId/deploy", handleDeploy); |
| 1003 | projectRouter.get("/:projectId/status", handleStatus); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 1004 | projectRouter.get("/:projectId/config", handleConfigGet); |
| gio | a71316d | 2025-05-24 09:41:36 +0400 | [diff] [blame] | 1005 | projectRouter.delete("/:projectId", handleProjectDelete); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1006 | projectRouter.get("/:projectId/repos/github", handleGithubRepos); |
| 1007 | projectRouter.post("/:projectId/github-token", handleUpdateGithubToken); |
| 1008 | projectRouter.get("/:projectId/env", handleEnv); |
| gio | 918780d | 2025-05-22 08:24:41 +0000 | [diff] [blame] | 1009 | projectRouter.post("/:projectId/reload/:serviceName/:workerId", handleReloadWorker); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1010 | projectRouter.post("/:projectId/reload", handleReload); |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 1011 | projectRouter.get("/:projectId/logs/:service/:workerId", handleServiceLogs); |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1012 | projectRouter.post("/:projectId/remove-deployment", handleRemoveDeployment); |
| 1013 | projectRouter.get("/", handleProjectAll); |
| 1014 | projectRouter.post("/", handleProjectCreate); |
| gio | 918780d | 2025-05-22 08:24:41 +0000 | [diff] [blame] | 1015 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1016 | app.use("/api/project", projectRouter); // Mount the authenticated router |
| 1017 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1018 | app.use("/", express.static("../front/dist")); |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 1019 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1020 | const internalApi = express(); |
| 1021 | internalApi.use(express.json()); |
| 1022 | internalApi.post("/api/project/:projectId/workers", handleRegisterWorker); |
| gio | c31bf14 | 2025-06-16 07:48:20 +0000 | [diff] [blame^] | 1023 | internalApi.get("/api/project/:projectId/config", handleConfigGet); |
| 1024 | internalApi.post("/api/project/:projectId/deploy", handleDeploy); |
| 1025 | internalApi.post("/api/validate-config", handleValidateConfig); |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 1026 | |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1027 | app.listen(env.DODO_PORT_WEB, () => { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 1028 | console.log("Web server started on port", env.DODO_PORT_WEB); |
| 1029 | }); |
| 1030 | |
| gio | 76d8ae6 | 2025-05-19 15:21:54 +0000 | [diff] [blame] | 1031 | internalApi.listen(env.DODO_PORT_API, () => { |
| gio | 09fcab5 | 2025-05-12 14:05:07 +0000 | [diff] [blame] | 1032 | console.log("Internal API server started on port", env.DODO_PORT_API); |
| gio | d002661 | 2025-05-08 13:00:36 +0000 | [diff] [blame] | 1033 | }); |
| 1034 | } |
| 1035 | |
| 1036 | start(); |