| 1 | import { and, count, eq } from "drizzle-orm"; |
| 2 | import { db } |
| 3 | import { |
| 4 | eggs, |
| 5 | eggVariables, |
| 6 | nodeAllocations, |
| 7 | serverMounts, |
| 8 | servers, |
| 9 | serverVariables, |
| 10 | } from "@struxa/db"; |
| 11 | import { user as |
| 12 | import { |
| 13 | buildEmailServiceFromSettings, |
| 14 | getEmailTemplate, |
| 15 | DEFAULT_TEMPLATES, |
| 16 | substituteVars, |
| 17 | sendEmail, |
| 18 | } from "@struxa/auth/email"; |
| 19 | |
| 20 | export interface RawServer |
| 21 | settings: { |
| 22 | uuid: string |
| 23 | meta: { |
| 24 | suspended: boolean |
| 25 | invocation: string |
| 26 | skip_egg_scripts: boolean |
| 27 | environment: Record |
| 28 | labels: Record |
| 29 | backups: string |
| 30 | schedules: unknown |
| 31 | allocations: { |
| 32 | force_outgoing_ip: boolean |
| 33 | default: { |
| 34 | mappings: Record |
| 35 | }; |
| 36 | build: { |
| 37 | memory_limit: number |
| 38 | swap: number |
| 39 | disk_space: number |
| 40 | io_weight: number |
| 41 | cpu_limit: number |
| 42 | threads: string |
| 43 | oom_disabled: boolean |
| 44 | }; |
| 45 | mounts: Array |
| 46 | egg: { |
| 47 | container: { |
| 48 | }; |
| 49 | process_configuration: { |
| 50 | startup: { |
| 51 | stop: { |
| 52 | configs: unknown |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | export interface PaginatedServersResponse |
| 57 | data: RawServer |
| 58 | meta: { |
| 59 | current_page: number |
| 60 | from: number |
| 61 | last_page: number |
| 62 | per_page: number |
| 63 | to: number |
| 64 | total: number |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | function parseProcessConfiguration(egg: |
| 69 | let done: |
| 70 | let stopType = |
| 71 | let stopValue: |
| 72 | let configs: |
| 73 | |
| 74 | try { |
| 75 | const startup = |
| 76 | if (startup.done |
| 77 | done = Array. |
| 78 | } |
| 79 | } catch {} |
| 80 | |
| 81 | try { |
| 82 | const raw = |
| 83 | const parsed = |
| 84 | if (typeof parsed |
| 85 | stopType = "command" |
| 86 | stopValue = parsed |
| 87 | } else if (parsed |
| 88 | stopType = parsed. |
| 89 | stopValue = parsed. |
| 90 | } |
| 91 | } catch {} |
| 92 | |
| 93 | if (stopValue === |
| 94 | stopValue = egg. |
| 95 | } |
| 96 | |
| 97 | try { |
| 98 | const filesRaw = |
| 99 | parser?: string |
| 100 | find?: Record |
| 101 | }>; |
| 102 | configs = Object. |
| 103 | file, |
| 104 | parser: cfg.parser |
| 105 | create_new: true, |
| 106 | replace: Object.entries |
| 107 | match, |
| 108 | replace_with, |
| 109 | update_existing: true, |
| 110 | })), |
| 111 | })); |
| 112 | } catch {} |
| 113 | |
| 114 | return { |
| 115 | startup: { done, |
| 116 | stop: { type: stopType |
| 117 | configs, |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | async function buildServerConfig( |
| 122 | server: typeof |
| 123 | ): Promise<RawServer |
| 124 | const [egg, |
| 125 | db.query.eggs |
| 126 | db.query.nodeAllocations |
| 127 | where: eq(nodeAllocations |
| 128 | }), |
| 129 | db.query.serverVariables |
| 130 | where: eq(serverVariables |
| 131 | }), |
| 132 | db.query.eggVariables |
| 133 | where: eq(eggVariables |
| 134 | }), |
| 135 | db.query.serverMounts |
| 136 | where: eq(serverMounts |
| 137 | with: { mount: true |
| 138 | }), |
| 139 | ]); |
| 140 | |
| 141 | if (!egg || |
| 142 | throw new Error( |
| 143 | } |
| 144 | |
| 145 | const overrides = |
| 146 | |
| 147 | const environment: |
| 148 | for (const eggVar |
| 149 | environment[eggVar.envVariable |
| 150 | overrides.get(eggVar |
| 151 | } |
| 152 | |
| 153 | const denylist: |
| 154 | try { |
| 155 | const parsed = |
| 156 | for (const cfg |
| 157 | if (Array.isArray |
| 158 | } |
| 159 | } catch {} |
| 160 | |
| 161 | const mappings: |
| 162 | mappings[allocation.ip |
| 163 | |
| 164 | return { |
| 165 | settings: { |
| 166 | uuid: server.uuid |
| 167 | meta: { |
| 168 | name: server.name |
| 169 | description: server.description |
| 170 | }, |
| 171 | suspended: server.suspended |
| 172 | invocation: server.invocation |
| 173 | skip_egg_scripts: server.skipScripts |
| 174 | environment, |
| 175 | labels: {}, |
| 176 | backups: [], |
| 177 | schedules: [], |
| 178 | allocations: { |
| 179 | force_outgoing_ip: false, |
| 180 | default: { ip: allocation |
| 181 | mappings, |
| 182 | }, |
| 183 | build: { |
| 184 | memory_limit: server.memory |
| 185 | swap: server.swap |
| 186 | disk_space: server.disk |
| 187 | io_weight: server.io |
| 188 | cpu_limit: server.cpu |
| 189 | threads: server.threads |
| 190 | oom_disabled: server.oomDisabled |
| 191 | }, |
| 192 | mounts: mountRows.map |
| 193 | source: sm.mount |
| 194 | target: sm.mount |
| 195 | read_only: sm.mount |
| 196 | })), |
| 197 | egg: { id: egg |
| 198 | container: { image: server |
| 199 | }, |
| 200 | process_configuration: parseProcessConfiguration(egg |
| 201 | }; |
| 202 | } |
| 203 | |
| 204 | export async function getServerConfig |
| 205 | serverUuid: string, |
| 206 | nodeId: string, |
| 207 | ): Promise<RawServer |
| 208 | const server = |
| 209 | where: and(eq |
| 210 | }); |
| 211 | if (!server) |
| 212 | return buildServerConfig(server |
| 213 | } |
| 214 | |
| 215 | export async function getServerList |
| 216 | nodeId: string, |
| 217 | page: number, |
| 218 | perPage: number, |
| 219 | ): Promise<PaginatedServersResponse |
| 220 | const offset = |
| 221 | |
| 222 | const [rows, |
| 223 | db.query.servers |
| 224 | where: eq(servers |
| 225 | limit: perPage, |
| 226 | offset, |
| 227 | }), |
| 228 | db.select({ |
| 229 | ]); |
| 230 | |
| 231 | const total = |
| 232 | const lastPage = |
| 233 | const from = |
| 234 | const to = |
| 235 | |
| 236 | const data = |
| 237 | |
| 238 | return { |
| 239 | data, |
| 240 | meta: { |
| 241 | current_page: page, |
| 242 | from, |
| 243 | last_page: lastPage, |
| 244 | per_page: perPage, |
| 245 | to, |
| 246 | total, |
| 247 | }, |
| 248 | }; |
| 249 | } |
| 250 | |
| 251 | export async function resetNodeServers |
| 252 | await db |
| 253 | .update(servers) |
| 254 | .set({ status: |
| 255 | .where(and(eq |
| 256 | } |
| 257 | |
| 258 | export async function getInstallScript |
| 259 | serverUuid: string, |
| 260 | nodeId: string, |
| 261 | ): Promise<{ |
| 262 | const server = |
| 263 | where: and(eq |
| 264 | }); |
| 265 | if (!server) |
| 266 | |
| 267 | const egg = |
| 268 | if (!egg) |
| 269 | |
| 270 | const rawEntrypoint = |
| 271 | const entrypoint = |
| 272 | |
| 273 | return { |
| 274 | container_image: egg.scriptContainer |
| 275 | entrypoint, |
| 276 | script: egg.scriptInstall |
| 277 | }; |
| 278 | } |
| 279 | |
| 280 | export async function completeInstall |
| 281 | serverUuid: string, |
| 282 | nodeId: string, |
| 283 | successful: boolean, |
| 284 | ): Promise<boolean |
| 285 | const server = |
| 286 | where: and(eq |
| 287 | }); |
| 288 | if (!server) |
| 289 | |
| 290 | await db |
| 291 | .update(servers) |
| 292 | .set({ status: |
| 293 | .where(eq(servers |
| 294 | |
| 295 | if (successful && |
| 296 | void (async () |
| 297 | try { |
| 298 | const svc = |
| 299 | if (!svc) |
| 300 | const owner = |
| 301 | if (!owner) |
| 302 | const rows = |
| 303 | const s = |
| 304 | const appName = |
| 305 | const vars = |
| 306 | const custom = |
| 307 | const html = |
| 308 | ? substituteVars(custom, |
| 309 | : DEFAULT_TEMPLATES["server-install" |
| 310 | await sendEmail(svc |
| 311 | } catch { |
| 312 | // non-critical — don't fail the install callback |
| 313 | } |
| 314 | })(); |
| 315 | } |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | export function buildInvocation( |
| 321 | startup: string, |
| 322 | variables: Record |
| 323 | ): string { |
| 324 | return startup.replace |
| 325 | } |