| 1 | import { randomBytes, randomUUID } from "crypto"; |
| 2 | import { eq } |
| 3 | import { account, db |
| 4 | import { getAuth, hashPassword |
| 5 | import { env } |
| 6 | import { encrypt } |
| 7 | import { buildWingsConfigYaml } |
| 8 | import { getEffectiveAppUrl } |
| 9 | import { recordActivity } |
| 10 | |
| 11 | export class BootstrapError |
| 12 | constructor( |
| 13 | public code: |
| 14 | message: string, |
| 15 | ) { |
| 16 | super(message); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | type BootstrapInput |
| 21 | email: string |
| 22 | password: string |
| 23 | name?: string |
| 24 | locationName?: string |
| 25 | wings?: { |
| 26 | fqdn: string |
| 27 | nodeName?: string |
| 28 | scheme?: " |
| 29 | daemonListen?: number |
| 30 | memory?: number |
| 31 | disk?: number |
| 32 | uploadSize?: number |
| 33 | }; |
| 34 | }; |
| 35 | |
| 36 | export async function bootstrapInstaller |
| 37 | const setupRow = |
| 38 | where: eq(settings |
| 39 | }); |
| 40 | if (setupRow?.value |
| 41 | throw new BootstrapError( |
| 42 | } |
| 43 | |
| 44 | const [existingUser] |
| 45 | if (existingUser) { |
| 46 | throw new BootstrapError( |
| 47 | } |
| 48 | |
| 49 | const adminName = |
| 50 | |
| 51 | let adminId: |
| 52 | if (env.TURNSTILE_SECRET_KEY |
| 53 | adminId = randomUUID() |
| 54 | const hash = |
| 55 | await db.insert |
| 56 | id: adminId, |
| 57 | name: adminName, |
| 58 | email: input.email |
| 59 | emailVerified: true, |
| 60 | role: "admin", |
| 61 | }); |
| 62 | await db.insert |
| 63 | id: randomUUID(), |
| 64 | accountId: adminId, |
| 65 | providerId: "credential", |
| 66 | password: hash, |
| 67 | userId: adminId, |
| 68 | }); |
| 69 | } else { |
| 70 | const auth = |
| 71 | const created = |
| 72 | body: { name: adminName |
| 73 | }); |
| 74 | adminId = created. |
| 75 | } |
| 76 | |
| 77 | await db.update |
| 78 | |
| 79 | const locationId = |
| 80 | await db.insert |
| 81 | id: locationId, |
| 82 | name: input.locationName |
| 83 | short: "DEFAULT", |
| 84 | }); |
| 85 | |
| 86 | let wings: |
| 87 | if (input.wings |
| 88 | const id = |
| 89 | const uuid = |
| 90 | const tokenId = |
| 91 | const token = |
| 92 | const fqdn = |
| 93 | const scheme = |
| 94 | const memory = |
| 95 | const disk = |
| 96 | const uploadSize = |
| 97 | const daemonListen = |
| 98 | |
| 99 | await db.insert |
| 100 | id, |
| 101 | uuid, |
| 102 | tokenId, |
| 103 | token: encrypt(token |
| 104 | name: input.wings |
| 105 | locationId, |
| 106 | fqdn, |
| 107 | scheme, |
| 108 | memory, |
| 109 | disk, |
| 110 | uploadSize, |
| 111 | daemonListen, |
| 112 | }); |
| 113 | |
| 114 | recordActivity({ |
| 115 | eventType: "admin:node.create", |
| 116 | userId: adminId, |
| 117 | nodeId: id, |
| 118 | properties: { name: input |
| 119 | }); |
| 120 | |
| 121 | const appUrl = |
| 122 | wings = { |
| 123 | nodeId: id, |
| 124 | configYaml: buildWingsConfigYaml( |
| 125 | { |
| 126 | uuid, |
| 127 | tokenId, |
| 128 | token, |
| 129 | scheme, |
| 130 | daemonListen, |
| 131 | daemonSFTP: 2022, |
| 132 | daemonBase: "/var/lib/pterodactyl", |
| 133 | uploadSize, |
| 134 | }, |
| 135 | appUrl, |
| 136 | { port: 8080, sslEnabled |
| 137 | ), |
| 138 | }; |
| 139 | } |
| 140 | |
| 141 | await db |
| 142 | .insert(settings) |
| 143 | .values({ key: |
| 144 | .onDuplicateKeyUpdate({ set: |
| 145 | |
| 146 | return { appUrl: |
| 147 | } |