| 1 | import { randomUUID } from "crypto"; |
| 2 | import { and, eq |
| 3 | import { z } |
| 4 | import { ORPCError } |
| 5 | import { db } |
| 6 | import { |
| 7 | billingSubscriptions, |
| 8 | eggVariables, |
| 9 | eggs, |
| 10 | nodeAllocations, |
| 11 | nodes, |
| 12 | serverVariables, |
| 13 | servers, |
| 14 | subusers, |
| 15 | } from "@struxa/db"; |
| 16 | import { createWingsClient } |
| 17 | import { safeDecrypt } |
| 18 | import { signWsToken } |
| 19 | import { buildInvocation } |
| 20 | import { recordActivity } |
| 21 | import { notifyAdmins, notifyServerOwner |
| 22 | import { adminProcedure, protectedProcedure |
| 23 | |
| 24 | export const serversRouter = |
| 25 | list: protectedProcedure.handler |
| 26 | const userId = |
| 27 | const isAdmin = |
| 28 | |
| 29 | const baseColumns = |
| 30 | id: true, uuid: |
| 31 | suspended: true, powerState: |
| 32 | } as const; |
| 33 | |
| 34 | const userListSelect = |
| 35 | columns: baseColumns, |
| 36 | with: { |
| 37 | allocation: { columns: { |
| 38 | egg: { columns: { |
| 39 | }, |
| 40 | } as const; |
| 41 | |
| 42 | const adminListSelect = |
| 43 | columns: baseColumns, |
| 44 | with: { |
| 45 | allocation: { columns: { |
| 46 | egg: { columns: { |
| 47 | node: { columns: { |
| 48 | }, |
| 49 | } as const; |
| 50 | |
| 51 | const rows = |
| 52 | ? await db. |
| 53 | : await db. |
| 54 | where: eq(servers |
| 55 | ...userListSelect, |
| 56 | }); |
| 57 | |
| 58 | if (!isAdmin) |
| 59 | const subuserServerIds = |
| 60 | await db.query |
| 61 | where: eq(subusers |
| 62 | }) |
| 63 | ).map((s |
| 64 | |
| 65 | const subuserServers = |
| 66 | ? await db. |
| 67 | where: and( |
| 68 | ...subuserServerIds.map |
| 69 | ), |
| 70 | ...userListSelect, |
| 71 | }) |
| 72 | : []; |
| 73 | |
| 74 | return [ |
| 75 | ...rows, |
| 76 | ...subuserServers.filter |
| 77 | ]; |
| 78 | } |
| 79 | |
| 80 | return rows; |
| 81 | }), |
| 82 | |
| 83 | listStatuses: protectedProcedure.handler |
| 84 | const userId = |
| 85 | const isAdmin = |
| 86 | |
| 87 | const serverQuery = |
| 88 | columns: { uuid: true |
| 89 | with: { node: true |
| 90 | }; |
| 91 | |
| 92 | const ownServers = |
| 93 | ? await db. |
| 94 | : await db. |
| 95 | |
| 96 | let allServers: |
| 97 | |
| 98 | if (!isAdmin) |
| 99 | const subuserEntries = |
| 100 | where: eq(subusers |
| 101 | columns: { serverId: true |
| 102 | }); |
| 103 | if (subuserEntries.length |
| 104 | const subuserIds = |
| 105 | const subuserServers = |
| 106 | where: inArray(servers |
| 107 | ...serverQuery, |
| 108 | }); |
| 109 | const ownUuids = |
| 110 | allServers = [... |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Group by node to make one request per node instead of one per server |
| 115 | const byNode = |
| 116 | for (const server |
| 117 | const node = |
| 118 | if (!byNode.has |
| 119 | byNode.set(server |
| 120 | } |
| 121 | byNode.get(server |
| 122 | } |
| 123 | |
| 124 | const statuses: |
| 125 | await Promise.allSettled |
| 126 | [...byNode.values |
| 127 | try { |
| 128 | const allowed = |
| 129 | const client = |
| 130 | const util = |
| 131 | for (const [uuid |
| 132 | if (allowed.has |
| 133 | statuses[uuid] = |
| 134 | } |
| 135 | } |
| 136 | } catch { |
| 137 | // Node unreachable — its servers are absent from the map, UI treats them as offline |
| 138 | } |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | return statuses as |
| 143 | }), |
| 144 | |
| 145 | update: adminProcedure |
| 146 | .input( |
| 147 | z.object({ |
| 148 | id: z.string |
| 149 | name: z.string |
| 150 | description: z.string |
| 151 | memory: z.number |
| 152 | disk: z.number |
| 153 | cpu: z.number |
| 154 | swap: z.number |
| 155 | io: z.number |
| 156 | threads: z.string |
| 157 | oomDisabled: z.boolean |
| 158 | image: z.string |
| 159 | startup: z.string |
| 160 | userId: z.string |
| 161 | nodeId: z.string |
| 162 | allocationId: z.string |
| 163 | }), |
| 164 | ) |
| 165 | .handler(async ( |
| 166 | const { |
| 167 | id, startup, |
| 168 | name, description, |
| 169 | userId, nodeId, |
| 170 | } = input; |
| 171 | |
| 172 | const server = |
| 173 | where: eq(servers |
| 174 | }); |
| 175 | if (!server) |
| 176 | |
| 177 | const updates: |
| 178 | name?: string |
| 179 | description?: string |
| 180 | memory?: number |
| 181 | disk?: number |
| 182 | cpu?: number |
| 183 | swap?: number |
| 184 | io?: number |
| 185 | threads?: string |
| 186 | oomDisabled?: boolean |
| 187 | image?: string |
| 188 | startup?: string |
| 189 | invocation?: string |
| 190 | userId?: string |
| 191 | nodeId?: string |
| 192 | allocationId?: string |
| 193 | } = {}; |
| 194 | |
| 195 | if (name !== |
| 196 | if (description !== |
| 197 | if (memory !== |
| 198 | if (disk !== |
| 199 | if (cpu !== |
| 200 | if (swap !== |
| 201 | if (io !== |
| 202 | if (threads !== |
| 203 | if (oomDisabled !== |
| 204 | if (image !== |
| 205 | if (userId !== |
| 206 | if (nodeId !== |
| 207 | |
| 208 | if (allocationId !== |
| 209 | await db.update |
| 210 | await db.update |
| 211 | updates.allocationId = |
| 212 | } |
| 213 | |
| 214 | if (startup !== |
| 215 | updates.startup = |
| 216 | const svars = |
| 217 | where: eq(serverVariables |
| 218 | with: { variable: { |
| 219 | }); |
| 220 | const varMap: |
| 221 | for (const sv |
| 222 | varMap[sv.variable |
| 223 | } |
| 224 | updates.invocation = |
| 225 | } |
| 226 | |
| 227 | if (Object.keys |
| 228 | await db.update |
| 229 | } |
| 230 | recordActivity({ eventType: |
| 231 | }), |
| 232 | |
| 233 | suspend: adminProcedure |
| 234 | .input(z.object |
| 235 | .handler(async ( |
| 236 | const server = |
| 237 | where: eq(servers |
| 238 | with: { node: true |
| 239 | }); |
| 240 | if (!server) |
| 241 | |
| 242 | await db.update |
| 243 | |
| 244 | if (input.suspended |
| 245 | const client = |
| 246 | try { |
| 247 | await client.sendPowerAction |
| 248 | } catch { |
| 249 | // Wings unreachable — DB flag is set; enforcement happens at next Wings contact |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | recordActivity({ eventType: |
| 254 | }), |
| 255 | |
| 256 | updateVariables: adminProcedure |
| 257 | .input( |
| 258 | z.object({ |
| 259 | id: z.string |
| 260 | variables: z.record |
| 261 | startup: z.string |
| 262 | }), |
| 263 | ) |
| 264 | .handler(async ( |
| 265 | const server = |
| 266 | where: eq(servers |
| 267 | }); |
| 268 | if (!server) |
| 269 | |
| 270 | const svars = |
| 271 | where: eq(serverVariables |
| 272 | with: { variable: { |
| 273 | }); |
| 274 | |
| 275 | await Promise.all |
| 276 | svars |
| 277 | .filter((sv) |
| 278 | .map((sv) |
| 279 | db |
| 280 | .update(serverVariables) |
| 281 | .set({ variableValue: |
| 282 | .where(eq(serverVariables |
| 283 | ), |
| 284 | ); |
| 285 | |
| 286 | const startupCommand = |
| 287 | const varMap: |
| 288 | for (const sv |
| 289 | varMap[sv.variable |
| 290 | input.variables[sv |
| 291 | sv.variableValue ?? |
| 292 | sv.variable.defaultValue |
| 293 | ""; |
| 294 | } |
| 295 | |
| 296 | const serverUpdates: |
| 297 | invocation: buildInvocation(startupCommand |
| 298 | }; |
| 299 | if (input.startup |
| 300 | |
| 301 | await db.update |
| 302 | recordActivity({ eventType: |
| 303 | }), |
| 304 | |
| 305 | updateStartupVariable: protectedProcedure |
| 306 | .input( |
| 307 | z.object({ |
| 308 | serverId: z.string |
| 309 | envVariable: z.string |
| 310 | value: z.string |
| 311 | }), |
| 312 | ) |
| 313 | .handler(async ( |
| 314 | const server = |
| 315 | where: eq(servers |
| 316 | }); |
| 317 | if (!server) |
| 318 | |
| 319 | const userId = |
| 320 | const isAdmin = |
| 321 | if (!isAdmin && |
| 322 | const sub = |
| 323 | where: and(eq |
| 324 | }); |
| 325 | if (!sub) |
| 326 | } |
| 327 | |
| 328 | const eggVar = |
| 329 | where: and( |
| 330 | eq(eggVariables.eggId |
| 331 | eq(eggVariables.envVariable |
| 332 | ), |
| 333 | }); |
| 334 | if (!eggVar) |
| 335 | if (!eggVar.userEditable |
| 336 | |
| 337 | await db |
| 338 | .update(serverVariables) |
| 339 | .set({ variableValue: |
| 340 | .where( |
| 341 | and( |
| 342 | eq(serverVariables.serverId |
| 343 | eq(serverVariables.variableId |
| 344 | ), |
| 345 | ); |
| 346 | |
| 347 | const allVars = |
| 348 | where: eq(serverVariables |
| 349 | with: { variable: { |
| 350 | }); |
| 351 | const varMap: |
| 352 | for (const sv |
| 353 | varMap[sv.variable |
| 354 | } |
| 355 | await db |
| 356 | .update(servers) |
| 357 | .set({ invocation: |
| 358 | .where(eq(servers |
| 359 | |
| 360 | recordActivity({ |
| 361 | eventType: "server:settings", |
| 362 | userId: context.session |
| 363 | serverId: server.id |
| 364 | ip: context.ip |
| 365 | properties: { envVariable: input |
| 366 | }); |
| 367 | }), |
| 368 | |
| 369 | updateDockerImage: protectedProcedure |
| 370 | .input( |
| 371 | z.object({ |
| 372 | serverId: z.string |
| 373 | image: z.string |
| 374 | }), |
| 375 | ) |
| 376 | .handler(async ( |
| 377 | const server = |
| 378 | where: eq(servers |
| 379 | with: { egg: { |
| 380 | }); |
| 381 | if (!server) |
| 382 | |
| 383 | const userId = |
| 384 | const isAdmin = |
| 385 | if (!isAdmin && |
| 386 | const sub = |
| 387 | where: and(eq |
| 388 | }); |
| 389 | if (!sub) |
| 390 | } |
| 391 | |
| 392 | let validImages: |
| 393 | try { |
| 394 | const parsed = |
| 395 | validImages = Object. |
| 396 | } catch {} |
| 397 | |
| 398 | if (validImages.length |
| 399 | throw new ORPCError( |
| 400 | } |
| 401 | |
| 402 | await db |
| 403 | .update(servers) |
| 404 | .set({ image: |
| 405 | .where(eq(servers |
| 406 | |
| 407 | recordActivity({ |
| 408 | eventType: "server:settings", |
| 409 | userId: context.session |
| 410 | serverId: server.id |
| 411 | ip: context.ip |
| 412 | properties: { image: input |
| 413 | }); |
| 414 | }), |
| 415 | |
| 416 | rename: protectedProcedure |
| 417 | .input(z.object |
| 418 | .handler(async ( |
| 419 | const server = |
| 420 | where: eq(servers |
| 421 | }); |
| 422 | if (!server) |
| 423 | |
| 424 | const userId = |
| 425 | const isAdmin = |
| 426 | if (!isAdmin && |
| 427 | const sub = |
| 428 | where: and(eq |
| 429 | }); |
| 430 | if (!sub) |
| 431 | } |
| 432 | |
| 433 | await db.update |
| 434 | |
| 435 | recordActivity({ |
| 436 | eventType: "server:settings", |
| 437 | userId: context.session |
| 438 | serverId: server.id |
| 439 | ip: context.ip |
| 440 | properties: { name: input |
| 441 | }); |
| 442 | }), |
| 443 | |
| 444 | get: protectedProcedure |
| 445 | .input(z.object |
| 446 | .handler(async ( |
| 447 | const server = |
| 448 | where: eq(servers |
| 449 | columns: { |
| 450 | id: true, uuid: |
| 451 | status: true, suspended: |
| 452 | cpu: true, swap: |
| 453 | image: true, startup: |
| 454 | subscriptionId: true, |
| 455 | }, |
| 456 | with: { |
| 457 | allocation: { columns: { |
| 458 | node: { columns: { |
| 459 | egg: { |
| 460 | columns: { id: true |
| 461 | with: { |
| 462 | variables: { |
| 463 | columns: { |
| 464 | id: true, name: |
| 465 | defaultValue: true, userViewable: |
| 466 | }, |
| 467 | }, |
| 468 | }, |
| 469 | }, |
| 470 | }, |
| 471 | }); |
| 472 | if (!server) |
| 473 | |
| 474 | const userId = |
| 475 | const isAdmin = |
| 476 | if (!isAdmin && |
| 477 | const sub = |
| 478 | where: and( |
| 479 | eq(subusers.userId |
| 480 | eq(subusers.serverId |
| 481 | ), |
| 482 | }); |
| 483 | if (!sub) |
| 484 | } |
| 485 | |
| 486 | const variables = |
| 487 | where: eq(serverVariables |
| 488 | columns: { variableId: true |
| 489 | with: { |
| 490 | variable: { |
| 491 | columns: { |
| 492 | id: true, name: |
| 493 | defaultValue: true, userViewable: |
| 494 | }, |
| 495 | }, |
| 496 | }, |
| 497 | }); |
| 498 | |
| 499 | const { userId: |
| 500 | return { ...serverFields |
| 501 | }), |
| 502 | |
| 503 | adminGet: adminProcedure |
| 504 | .input(z.object |
| 505 | .handler(async ( |
| 506 | const server = |
| 507 | where: eq(servers |
| 508 | columns: { |
| 509 | id: true, uuid: |
| 510 | status: true, suspended: |
| 511 | cpu: true, swap: |
| 512 | image: true, startup: |
| 513 | userId: true, nodeId: |
| 514 | }, |
| 515 | with: { |
| 516 | allocation: { columns: { |
| 517 | node: { columns: { |
| 518 | user: { columns: { |
| 519 | egg: { |
| 520 | columns: { id: true |
| 521 | with: { |
| 522 | variables: { |
| 523 | columns: { |
| 524 | id: true, name: |
| 525 | defaultValue: true, userViewable: |
| 526 | }, |
| 527 | }, |
| 528 | }, |
| 529 | }, |
| 530 | }, |
| 531 | }); |
| 532 | if (!server) |
| 533 | |
| 534 | const variables = |
| 535 | where: eq(serverVariables |
| 536 | columns: { variableId: true |
| 537 | with: { |
| 538 | variable: { |
| 539 | columns: { |
| 540 | id: true, name: |
| 541 | defaultValue: true, userViewable: |
| 542 | }, |
| 543 | }, |
| 544 | }, |
| 545 | }); |
| 546 | |
| 547 | return { ...server |
| 548 | }), |
| 549 | |
| 550 | power: protectedProcedure |
| 551 | .input( |
| 552 | z.object({ |
| 553 | serverId: z.string |
| 554 | action: z.enum |
| 555 | }), |
| 556 | ) |
| 557 | .handler(async ( |
| 558 | const server = |
| 559 | where: eq(servers |
| 560 | with: { node: true |
| 561 | }); |
| 562 | if (!server) |
| 563 | |
| 564 | const userId = |
| 565 | const isAdmin = |
| 566 | if (!isAdmin && |
| 567 | const sub = |
| 568 | where: and( |
| 569 | eq(subusers.userId |
| 570 | eq(subusers.serverId |
| 571 | ), |
| 572 | }); |
| 573 | if (!sub) |
| 574 | } |
| 575 | |
| 576 | if (server.suspended |
| 577 | throw new ORPCError( |
| 578 | } |
| 579 | |
| 580 | const client = |
| 581 | await client.sendPowerAction |
| 582 | |
| 583 | recordActivity({ |
| 584 | eventType: `server:power.${input |
| 585 | userId: context.session |
| 586 | serverId: server.id |
| 587 | ip: context.ip |
| 588 | }); |
| 589 | notifyServerOwner(server.userId |
| 590 | serverName: server.name |
| 591 | serverUuid: server.uuid |
| 592 | action: input.action |
| 593 | actor: context.session |
| 594 | }); |
| 595 | }), |
| 596 | |
| 597 | reinstall: protectedProcedure |
| 598 | .input(z.object |
| 599 | .handler(async ( |
| 600 | const server = |
| 601 | where: eq(servers |
| 602 | with: { node: true |
| 603 | }); |
| 604 | if (!server) |
| 605 | |
| 606 | const isAdmin = |
| 607 | if (!isAdmin && |
| 608 | throw new ORPCError( |
| 609 | } |
| 610 | |
| 611 | if (input.eggId |
| 612 | if (!isAdmin) |
| 613 | if (!server.subscriptionId |
| 614 | const sub = |
| 615 | where: and( |
| 616 | eq(billingSubscriptions.id |
| 617 | or( |
| 618 | eq(billingSubscriptions.status |
| 619 | eq(billingSubscriptions.status |
| 620 | ), |
| 621 | ), |
| 622 | with: { plan: { |
| 623 | }); |
| 624 | if (!sub) |
| 625 | const limits = |
| 626 | const allowedEggs = |
| 627 | if (!allowedEggs.includes |
| 628 | throw new ORPCError( |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | const newEgg = |
| 633 | where: eq(eggs |
| 634 | with: { variables: true |
| 635 | }); |
| 636 | if (!newEgg) |
| 637 | |
| 638 | let dockerImages: |
| 639 | try { dockerImages = |
| 640 | const newImage = |
| 641 | if (!newImage) |
| 642 | |
| 643 | const existingVars = |
| 644 | where: eq(serverVariables |
| 645 | with: { variable: { |
| 646 | }); |
| 647 | const existingVarMap = |
| 648 | existingVars.map((v |
| 649 | ); |
| 650 | |
| 651 | const varMap: |
| 652 | for (const v |
| 653 | varMap[v.envVariable |
| 654 | } |
| 655 | |
| 656 | const newInvocation = |
| 657 | |
| 658 | await db.transaction |
| 659 | await tx.delete |
| 660 | if (newEgg.variables |
| 661 | await tx.insert |
| 662 | newEgg.variables.map |
| 663 | id: randomUUID(), |
| 664 | serverId: server.id |
| 665 | variableId: v.id |
| 666 | variableValue: varMap[v |
| 667 | })), |
| 668 | ); |
| 669 | } |
| 670 | await tx |
| 671 | .update(servers) |
| 672 | .set({ |
| 673 | eggId: input.eggId |
| 674 | image: newImage, |
| 675 | startup: newEgg.startup |
| 676 | invocation: newInvocation, |
| 677 | status: "installing", |
| 678 | }) |
| 679 | .where(eq(servers |
| 680 | }); |
| 681 | } else { |
| 682 | await db |
| 683 | .update(servers) |
| 684 | .set({ status: |
| 685 | .where(eq(servers |
| 686 | } |
| 687 | |
| 688 | const previousStatus = |
| 689 | |
| 690 | try { |
| 691 | const client = |
| 692 | await client.reinstallServer |
| 693 | } catch (e) |
| 694 | await db |
| 695 | .update(servers) |
| 696 | .set({ status: |
| 697 | .where(eq(servers |
| 698 | throw e; |
| 699 | } |
| 700 | |
| 701 | recordActivity({ |
| 702 | eventType: "server:reinstall", |
| 703 | userId: context.session |
| 704 | serverId: server.id |
| 705 | ip: context.ip |
| 706 | }); |
| 707 | }), |
| 708 | |
| 709 | listPlanEggs: protectedProcedure |
| 710 | .input(z.object |
| 711 | .handler(async ( |
| 712 | const server = |
| 713 | where: eq(servers |
| 714 | }); |
| 715 | if (!server) |
| 716 | |
| 717 | const isAdmin = |
| 718 | if (!isAdmin && |
| 719 | const subuser = |
| 720 | where: and(eq |
| 721 | }); |
| 722 | if (!subuser) |
| 723 | } |
| 724 | |
| 725 | if (!server.subscriptionId |
| 726 | |
| 727 | const sub = |
| 728 | where: and( |
| 729 | eq(billingSubscriptions.id |
| 730 | or( |
| 731 | eq(billingSubscriptions.status |
| 732 | eq(billingSubscriptions.status |
| 733 | ), |
| 734 | ), |
| 735 | with: { plan: { |
| 736 | }); |
| 737 | if (!sub) |
| 738 | |
| 739 | const limits = |
| 740 | const eggIds = |
| 741 | if (eggIds.length |
| 742 | |
| 743 | const rows = |
| 744 | where: inArray(eggs |
| 745 | columns: { id: true |
| 746 | }); |
| 747 | return rows; |
| 748 | }), |
| 749 | |
| 750 | create: adminProcedure |
| 751 | .input( |
| 752 | z.object({ |
| 753 | name: z.string |
| 754 | description: z.string |
| 755 | userId: z.string |
| 756 | nodeId: z.string |
| 757 | eggId: z.string |
| 758 | allocationId: z.string |
| 759 | memory: z.number |
| 760 | disk: z.number |
| 761 | cpu: z.number |
| 762 | swap: z.number |
| 763 | io: z.number |
| 764 | threads: z.string |
| 765 | oomDisabled: z.boolean |
| 766 | image: z.string |
| 767 | startOnCompletion: z.boolean |
| 768 | skipScripts: z.boolean |
| 769 | variableValues: z.record |
| 770 | }), |
| 771 | ) |
| 772 | .handler(async ( |
| 773 | const allocation = |
| 774 | where: eq(nodeAllocations |
| 775 | }); |
| 776 | if (!allocation) |
| 777 | if (allocation.serverId |
| 778 | throw new ORPCError( |
| 779 | } |
| 780 | |
| 781 | const egg = |
| 782 | where: eq(eggs |
| 783 | with: { variables: true |
| 784 | }); |
| 785 | if (!egg) |
| 786 | |
| 787 | const node = |
| 788 | where: eq(nodes |
| 789 | }); |
| 790 | if (!node) |
| 791 | |
| 792 | const varMap: |
| 793 | for (const v |
| 794 | varMap[v.envVariable |
| 795 | input.variableValues?.[v |
| 796 | } |
| 797 | |
| 798 | const invocation = |
| 799 | const id = |
| 800 | const uuid = |
| 801 | const uuidShort = |
| 802 | |
| 803 | await db.insert |
| 804 | id, |
| 805 | uuid, |
| 806 | uuidShort, |
| 807 | name: input.name |
| 808 | description: input.description |
| 809 | userId: input.userId |
| 810 | nodeId: input.nodeId |
| 811 | eggId: input.eggId |
| 812 | allocationId: input.allocationId |
| 813 | memory: input.memory |
| 814 | disk: input.disk |
| 815 | cpu: input.cpu |
| 816 | swap: input.swap |
| 817 | io: input.io |
| 818 | threads: input.threads |
| 819 | oomDisabled: input.oomDisabled |
| 820 | image: input.image |
| 821 | startup: egg.startup |
| 822 | skipScripts: input.skipScripts |
| 823 | invocation, |
| 824 | status: "installing", |
| 825 | }); |
| 826 | |
| 827 | if (egg.variables |
| 828 | await db.insert |
| 829 | egg.variables.map |
| 830 | id: randomUUID(), |
| 831 | serverId: id, |
| 832 | variableId: v.id |
| 833 | variableValue: resolveVar(input |
| 834 | })), |
| 835 | ); |
| 836 | } |
| 837 | |
| 838 | await db |
| 839 | .update(nodeAllocations) |
| 840 | .set({ serverId: |
| 841 | .where(eq(nodeAllocations |
| 842 | |
| 843 | const environment: |
| 844 | for (const v |
| 845 | environment[v.envVariable |
| 846 | input.variableValues, |
| 847 | v.envVariable, |
| 848 | v.defaultValue, |
| 849 | ); |
| 850 | } |
| 851 | |
| 852 | const client = |
| 853 | try { |
| 854 | await client.createServer |
| 855 | uuid, |
| 856 | start_on_completion: input.startOnCompletion |
| 857 | environment, |
| 858 | limits: { |
| 859 | cpu: input.cpu |
| 860 | memory: input.memory |
| 861 | disk: input.disk |
| 862 | swap: input.swap |
| 863 | io: input.io |
| 864 | threads: input.threads |
| 865 | oom_disabled: input.oomDisabled |
| 866 | }, |
| 867 | container: { |
| 868 | image: input.image |
| 869 | oom_killer: !input.oomDisabled |
| 870 | }, |
| 871 | }); |
| 872 | } catch (err) |
| 873 | // Roll back DB changes so the allocation isn't left orphaned |
| 874 | await db.delete |
| 875 | await db |
| 876 | .update(nodeAllocations) |
| 877 | .set({ serverId: |
| 878 | .where(eq(nodeAllocations |
| 879 | throw err; |
| 880 | } |
| 881 | |
| 882 | recordActivity({ eventType: |
| 883 | notifyAdmins("server-created", |
| 884 | |
| 885 | return db.query |
| 886 | }), |
| 887 | |
| 888 | delete: adminProcedure |
| 889 | .input( |
| 890 | z.object({ |
| 891 | id: z.string |
| 892 | purgeData: z.boolean |
| 893 | }), |
| 894 | ) |
| 895 | .handler(async ( |
| 896 | const server = |
| 897 | where: eq(servers |
| 898 | with: { node: true |
| 899 | }); |
| 900 | if (!server) |
| 901 | |
| 902 | const client = |
| 903 | try { |
| 904 | await client.deleteServer |
| 905 | } catch { |
| 906 | // If Wings returns 404 the server doesn't exist on Wings — proceed with DB cleanup |
| 907 | } |
| 908 | |
| 909 | await db |
| 910 | .update(nodeAllocations) |
| 911 | .set({ serverId: |
| 912 | .where(eq(nodeAllocations |
| 913 | |
| 914 | await db.delete |
| 915 | recordActivity({ eventType: |
| 916 | notifyAdmins("server-deleted", |
| 917 | }), |
| 918 | |
| 919 | getWebSocketToken: protectedProcedure |
| 920 | .input(z.object |
| 921 | .handler(async ( |
| 922 | const server = |
| 923 | where: eq(servers |
| 924 | with: { node: true |
| 925 | }); |
| 926 | if (!server) |
| 927 | |
| 928 | const userId = |
| 929 | const isAdmin = |
| 930 | |
| 931 | if (!isAdmin && |
| 932 | const sub = |
| 933 | where: and( |
| 934 | eq(subusers.userId |
| 935 | eq(subusers.serverId |
| 936 | ), |
| 937 | }); |
| 938 | if (!sub) |
| 939 | } |
| 940 | |
| 941 | const permissions = |
| 942 | isAdmin || server. |
| 943 | ? ["*"] |
| 944 | : await getSubuserPermissions( |
| 945 | |
| 946 | const node = |
| 947 | const token = |
| 948 | { user_uuid: userId, |
| 949 | safeDecrypt(node.token |
| 950 | ); |
| 951 | |
| 952 | const wsScheme = |
| 953 | const socketUrl = |
| 954 | |
| 955 | return { token, |
| 956 | }), |
| 957 | }; |
| 958 | |
| 959 | function resolveVar( |
| 960 | values: Record |
| 961 | key: string, |
| 962 | defaultValue: string |
| 963 | ): string { |
| 964 | if (values && |
| 965 | return defaultValue ?? |
| 966 | } |
| 967 | |
| 968 | async function getSubuserPermissions |
| 969 | userId: string, |
| 970 | serverId: string, |
| 971 | ): Promise<string |
| 972 | const record = |
| 973 | where: and(eq |
| 974 | }); |
| 975 | if (!record) |
| 976 | try { |
| 977 | return JSON.parse |
| 978 | } catch { |
| 979 | return []; |
| 980 | } |
| 981 | } |