-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.ts
471 lines (398 loc) · 14.5 KB
/
setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* Copyright (c) 2023-2024, NeKz
*
* SPDX-License-Identifier: MIT
*/
import { Command } from 'https://deno.land/x/cliffy@v1.0.0-rc.2/command/mod.ts';
import { colors } from 'https://deno.land/x/cliffy@v1.0.0-rc.2/ansi/colors.ts';
import { Input, Secret, Select } from 'https://deno.land/x/cliffy@v1.0.0-rc.2/prompt/mod.ts';
type Environment = 'dev' | 'prod';
// Used for template file in docker/compose, for the public URI in .env files and for generating SSL certs.
const devHostname = 'autorender.portal2.local';
// Used to download files for prod setup.
const repositoryUrl = 'https://raw.githubusercontent.com/NeKzor/autorender/main/';
// Bot requires: VIEW_CHANNEL | SEND_MESSAGES | EMBED_LINKS | ATTACH_FILES | READ_MESSAGE_HISTORY
const botPermissions = 117760;
let inviteLink = '';
let volumeFolder = '';
let publicUri = '';
/** String as volume folder. */
const v = (strings: TemplateStringsArray, ...values: string[]) =>
volumeFolder + strings.reduce((acc, val, idx) => acc + val + (values[idx] ?? ''), '');
const cli = new Command()
.name('autorender-setup')
.version('1.0.0')
.description('Command line setup tool for the autorender project.')
.option('-p, --prod', 'Run setup for the production system.')
.option('-s, --sync', 'Sync latest files for the production system.');
const main = async () => {
const { options: { prod, sync } } = await cli.parse(Deno.args);
if (sync && !prod) {
console.error(
colors.bold('[-]'),
`Syncing files is not allowed. Did you mean to run this for a production system? Try: --prod --sync`,
);
Deno.exit(1);
}
const env: Environment = prod ? 'prod' : 'dev';
volumeFolder = prod ? '' : 'docker/volumes/';
if (!sync) {
await createDockerComposeFile(env);
await createConfigAndEnv(env);
await createEntryPointFiles(env);
await createDirectories();
}
if (prod) {
await downloadRemoteFiles();
await downloadStorageFiles();
} else {
await createSslCerts();
}
console.log(colors.bold('[+]'), colors.green(`Done`));
if (inviteLink) {
console.log();
console.log(colors.white(`Bot invitation link: ${inviteLink}`));
}
};
const createInviteLink = (discordClientId: string) => {
const params = new URLSearchParams({
client_id: discordClientId,
permissions: botPermissions.toString(),
scope: 'bot applications.commands',
});
inviteLink = `https://discord.com/api/oauth2/authorize?${params}`;
};
const tryStat = async (file: string) => {
try {
return await Deno.stat(file);
} catch {
return null;
}
};
const tryCopy = async (source: string, destination: string) => {
try {
const stat = await tryStat(destination);
if (stat) {
return false;
}
await Deno.copyFile(source, destination);
return true;
} catch (err) {
console.log(colors.bold('[-]'), colors.red(`Failed to copy "${source}" to "${destination}"`));
console.error(err);
return false;
}
};
const tryMkdir = async (path: string) => {
try {
const stat = await tryStat(path);
if (stat) {
return false;
}
await Deno.mkdir(path, { recursive: true });
return true;
} catch (err) {
console.log(colors.bold('[-]'), colors.red(`Failed to create directory "${path}"`));
console.error(err);
return false;
}
};
const getEnv = (env: string[], key: string) => {
key += '=';
const line = env.find((line) => line.startsWith(key));
if (line === undefined) {
return '';
}
return line.slice(line.indexOf('=') + 1);
};
const hasEnv = (env: string[], key: string) => {
const value = getEnv(env, key);
return value !== '' && value !== '""';
};
const setEnv = (env: string[], key: string, value: string) => {
key += '=';
const index = env.findIndex((line) => line.startsWith(key));
if (index !== -1) {
env[index] = key + value;
}
};
const trim = (line: string) => line.trim();
const downloadFromRepository = async (remote: string, local: string, skipIfExists?: boolean) => {
if (skipIfExists) {
const stat = await tryStat(local);
if (stat) {
return;
}
}
const url = repositoryUrl + remote;
const res = await fetch(url, {
headers: {
'User-Agent': cli.getName(),
},
});
console.log(colors.bold(`[${res.ok ? '+' : '-'}]`), `GET ${url} : ${res.status}`);
if (!res.ok || !res.body) {
console.log(colors.bold('[-]'), `Unable to download file from repository: ${res.statusText}`);
Deno.exit(1);
}
try {
await Deno.remove(local);
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
console.error(colors.bold('[-]'), `Unable to delete file: ${local}`);
console.error(err);
Deno.exit(1);
}
}
try {
using file = await Deno.open(local, { create: true, write: true, truncate: true });
await res.body.pipeTo(file.writable);
console.log(colors.bold('[+]'), ` File written to ${local}`);
} catch (err) {
console.error(err);
Deno.exit(1);
}
};
/**
* Gets a docker-compose file from the "docker/compose/" folder.
*/
const createDockerComposeFile = async (env: Environment) => {
if (env === 'prod') {
const template = await Select.prompt({
message: 'Choose a docker-compose template:',
options: [
{
name: 'autorender.nekz.me.yml',
value: 'docker/compose/autorender.nekz.me.yml',
},
{
name: 'autorender.portal2.sr.yml',
value: 'docker/compose/autorender.portal2.sr.yml',
},
],
});
await downloadFromRepository(template, `docker-compose.yml`, true);
publicUri = `https://${template.split('/').at(-1)!.slice(0, -4)}`;
} else {
await tryCopy(`docker/compose/${devHostname}.yml`, 'docker-compose.yml');
publicUri = `https://${devHostname}`;
}
};
const downloadRemoteFiles = async () => {
await downloadFromRepository('docker/volumes/initdb/_create.sql', 'initdb/_create.sql');
await downloadFromRepository('docker/volumes/initdb/_init.sql', 'initdb/_init.sql');
await downloadFromRepository('docker/volumes/initdb/_populate.sql', 'initdb/_populate.sql');
await downloadFromRepository('deno.json', 'deno.json');
await downloadFromRepository('deno.lock', 'deno.lock');
await downloadFromRepository('backup.ts', 'backup.ts');
};
/**
* Storage files:
* autorender.cfg -> .cfg file for clients
* portal2_benchmark.dem -> Benchmark demo for clients
* quickhud.zip -> Quickhud files for clients
* security.txt -> Security policy, see https://www.rfc-editor.org/rfc/rfc9116
*/
const downloadStorageFiles = async () => {
const remoteStorageFiles = 'docker/volumes/storage/files/';
await downloadFromRepository(`${remoteStorageFiles}404.webp`, 'storage/files/404.webp');
await downloadFromRepository(`${remoteStorageFiles}aptag_avatar.webp`, 'storage/files/aptag_avatar.webp');
await downloadFromRepository(`${remoteStorageFiles}autorender_avatar.webp`, 'storage/files/autorender_avatar.webp');
await downloadFromRepository(`${remoteStorageFiles}autorender.cfg`, 'storage/files/autorender.cfg');
await downloadFromRepository(`${remoteStorageFiles}autorender_logo.webp`, 'storage/files/autorender_logo.webp');
await downloadFromRepository(`${remoteStorageFiles}favicon.ico`, 'storage/files/favicon.ico');
await downloadFromRepository(`${remoteStorageFiles}mel_avatar.webp`, 'storage/files/mel_avatar.webp');
await downloadFromRepository(`${remoteStorageFiles}portal2_benchmark.dem`, 'storage/files/portal2_benchmark.dem');
await downloadFromRepository(`${remoteStorageFiles}quickhud.zip`, 'storage/files/quickhud.zip');
await downloadFromRepository(`${remoteStorageFiles}security.txt`, 'storage/files/security.txt');
};
const syncEnv = async (envFile: string, exampleEnvFile: string) => {
if (await tryStat(envFile)) {
const example = (await Deno.readTextFile(exampleEnvFile)).split('\n').map(trim);
const oldEnv = await Deno.readTextFile(envFile);
const env = oldEnv.split('\n').map(trim);
for (const line of example) {
const eq = line.indexOf('=');
if (eq === -1) {
continue;
}
const key = line.slice(0, eq + 1);
if (!env.some((line) => line.startsWith(key))) {
env.push(line);
console.log(`[+] ${colors.green('Added')} new key ${key.slice(0, -1)} to ${envFile}`);
}
}
const newEnv = env.join('\n') + (env.at(-1) !== '' ? '\n' : '');
oldEnv !== newEnv && await Deno.writeTextFile(envFile, newEnv);
} else {
await tryCopy(exampleEnvFile, envFile);
}
};
/**
* Configuration files:
* .env -> used by docker
* .env.bot -> used by the bot (mounted by docker)
* .env.server -> used by the server (mounted by docker)
*/
const createConfigAndEnv = async (env: Environment) => {
if (env === 'prod') {
await downloadFromRepository('.env.example', '.env.example');
await downloadFromRepository('src/bot/.env.example', v`.env.bot.example`);
await downloadFromRepository('src/server/.env.example', v`.env.server.example`);
await syncEnv('.env', '.env.example');
await syncEnv(v`.env.bot`, v`.env.bot.example`);
await syncEnv(v`.env.server`, v`.env.server.example`);
} else {
await syncEnv('.env', '.env.example');
await syncEnv(v`.env.bot`, 'src/bot/.env.example');
await syncEnv(v`.env.server`, 'src/server/.env.example');
}
const oldBotEnv = await Deno.readTextFile(v`.env.bot`);
const oldServerEnv = await Deno.readTextFile(v`.env.server`);
const botEnv = oldBotEnv.split('\n').map(trim);
const serverEnv = oldServerEnv.split('\n').map(trim);
if (env === 'dev') {
setEnv(serverEnv, 'HOT_RELOAD', 'true');
}
if (!hasEnv(serverEnv, 'AUTORENDER_BOT_TOKEN')) {
const autorenderBotToken = btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(12))));
setEnv(botEnv, 'AUTORENDER_BOT_TOKEN', autorenderBotToken);
setEnv(serverEnv, 'AUTORENDER_BOT_TOKEN', autorenderBotToken);
}
if (!hasEnv(serverEnv, 'COOKIE_SECRET_KEY')) {
const cookieSecretKey = btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(12))));
setEnv(serverEnv, 'COOKIE_SECRET_KEY', cookieSecretKey);
}
if (!hasEnv(serverEnv, 'DISCORD_USER_ID')) {
const discordUserId = await Input.prompt({ message: 'Your Discord User ID' });
discordUserId.length && setEnv(serverEnv, 'DISCORD_USER_ID', discordUserId);
}
let discordClientId = getEnv(serverEnv, 'DISCORD_CLIENT_ID');
if (!discordClientId.length) {
discordClientId = await Input.prompt({ message: 'Client ID of the Discord application' });
if (discordClientId.length) {
setEnv(serverEnv, 'DISCORD_CLIENT_ID', discordClientId);
setEnv(botEnv, 'DISCORD_BOT_ID', discordClientId);
}
} else {
if (!hasEnv(botEnv, 'DISCORD_BOT_ID')) {
console.warn(
'[-] Missing DISCORD_BOT_ID in .env.bot. The value should equal DISCORD_CLIENT_ID from .env.server.',
);
}
}
if (!hasEnv(serverEnv, 'DISCORD_CLIENT_SECRET')) {
const discordClientSecret = await Secret.prompt({ message: 'Client secret of the Discord application' });
discordClientSecret.length && setEnv(serverEnv, 'DISCORD_CLIENT_SECRET', discordClientSecret);
}
if (!hasEnv(botEnv, 'DISCORD_BOT_TOKEN')) {
const discordBotToken = await Secret.prompt({ message: 'Token of the Discord bot' });
discordBotToken.length && setEnv(botEnv, 'DISCORD_BOT_TOKEN', discordBotToken);
}
setEnv(botEnv, 'AUTORENDER_PUBLIC_URI', publicUri);
setEnv(serverEnv, 'AUTORENDER_PUBLIC_URI', publicUri);
const newBotEnv = botEnv.join('\n');
const newServerEnv = serverEnv.join('\n');
oldBotEnv !== newBotEnv && await Deno.writeTextFile(v`.env.bot`, newBotEnv);
oldServerEnv !== newServerEnv && await Deno.writeTextFile(v`.env.server`, newServerEnv);
if (discordClientId?.length) {
createInviteLink(discordClientId);
} else {
const discordClientId = botEnv
.find((line) => line.startsWith('DISCORD_BOT_ID'))
?.split('=', 2)
?.slice(1)
?.shift()
?.trim() ?? '';
if (discordClientId.length) {
createInviteLink(discordClientId);
}
}
const updatedEnvFiles = oldBotEnv !== newBotEnv || oldServerEnv !== newServerEnv;
console.log(colors.bold('[+]'), `${updatedEnvFiles ? 'Created or updated' : 'Skipped'} .env files`);
};
/**
* Entrypoint files for Docker:
* entrypoint.bot.sh -> Script when starting the bot
* entrypoint.server.sh -> Script when starting the server
*/
const createEntryPointFiles = async (env: Environment) => {
const botEntryPoint = v`entrypoint.bot.sh`;
const serverEntryPoint = v`entrypoint.server.sh`;
if (!await tryStat(botEntryPoint)) {
await Deno.writeTextFile(botEntryPoint, `deno task ${env}\n`);
await Deno.chmod(botEntryPoint, 0o755);
}
if (!await tryStat(serverEntryPoint)) {
await Deno.writeTextFile(serverEntryPoint, `deno task ${env}\n`);
await Deno.chmod(serverEntryPoint, 0o755);
}
};
/**
* Mounted directories:
* backups -> Folder for database dumps
* logs -> Bot and server logs
* kv -> Bot database
* mysql -> Server database
* storage -> Server file storage
*/
const createDirectories = async () => {
const dirs = [
v`backups`,
v`initdb`,
v`kv`,
v`logs/bot`,
v`logs/server`,
v`mysql`,
v`storage/demos`,
v`storage/files`,
v`storage/inputs`,
v`storage/previews`,
v`storage/thumbnails`,
v`storage/users`,
v`storage/videos`,
];
let created = 0;
for (const dir of dirs) {
created += await tryMkdir(dir) ? 1 : 0;
}
console.log(colors.bold('[+]'), `${created ? 'Created or updated' : 'Skipped'} directories`);
};
/**
* Self-signed certificate for development only.
*/
const createSslCerts = async () => {
const stat = await tryStat(v`ssl/${devHostname}.crt`);
if (stat) {
console.log(colors.bold('[+]'), colors.white(`Skipped self-signed certificate`));
return;
}
await tryMkdir(v`ssl`);
const mkcert = new Deno.Command('mkcert', {
args: [
`-cert-file`,
v`ssl/${devHostname}.crt`,
`-key-file`,
v`ssl/${devHostname}.key`,
devHostname,
],
});
try {
const process = mkcert.spawn();
const { code } = await process.output();
if (code === 0) {
console.log(colors.bold('[+]'), colors.white(`Created self-signed certificate`));
} else {
console.log(colors.bold('[-]'), colors.red(`Failed to create self-signed certificate`));
}
} catch (err) {
console.error(err);
if (err instanceof Deno.errors.NotFound) {
console.log(
colors.bold('[-]'),
colors.red(`mkcert does not seem to be installed. Failed to generate ssl certificates`),
);
}
}
};
await main();