summary refs log tree commit diff
path: root/scripts/first_setup.js
blob: 4ce6e7de3dfb963a6456a50aaa094303ea9b7cde (plain) (blame)
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
#!/usr/bin/node
const path = require("path");
const fs = require("fs");
const { stdout, exit } = require("process");
const { execIn } = require("./utils.js");
const { ask } = require("./utils/ask.js");


const data = { env: [], config: { register: {} }, extra_pkgs: [] };
let rights = [];

process.on("SIGINT", function () {
	console.log("Caught interrupt signal");
	process.exit();
});

console.log("Welcome to Fosscord!");
console.log("Please remember this is pre-release software!");
console.log("We will guide you through some important setup steps.");
console.log();

if (fs.existsSync("package-lock.json")) fs.rmSync("package-lock.json");
if (fs.existsSync("yarn.lock")) fs.rmSync("yarn.lock");

async function main() {
	printTitle("Step 1: Database setup");
	console.log("1. PostgreSQL (recommended)");
	console.log("2. MariaDB/MySQL");
	console.log("3. SQLite (not recommended, but good for a simple test)");

	while (!data.db) {
		let answer = await ask("Please select a database type: ");
		if (answer == "1") {
			data.db = "postgres";
			data.extra_pkgs.push("pg");
		} else if (answer == "2") {
			data.db = "mariadb";
			data.extra_pkgs.push("mysql2");
		} else if (answer == "3") {
			data.db = "sqlite";
			data.extra_pkgs.push("sqlite3");
		} else {
			console.log("Invalid choice!");
		}
	}

	printTitle("Step 2: Database credentials");
	if (data.db != "sqlite") {
		console.log("Please enter your database credentials.");
		console.log("You can leave the password field empty if you don't want to set a password.");
		console.log();
		while (!data.db_host) {
			data.db_host = await ask("Host: ");
		}
		while (!data.db_port) {
			data.db_port = await ask("Port: ");
		}
		while (!data.db_user) {
			data.db_user = await ask("Username: ");
		}
		while (!data.db_pass) {
			data.db_pass = await ask("Password: ");
		}
		while (!data.db_name) {
			data.db_name = await ask("Database name: ");
		}
	} else {
		console.log("SQLite does not use credentials...");
	}

	printTitle("Step 3: Domain setup");
	console.log("Please enter your domain.");
	console.log("You can leave the port field empty if you don't want to set a port.");
	console.log();

	data.domain = await ask("Domain (default=localhost): ");
	if (!data.domain) data.domain = "localhost";
	else data.ssl = /y?/i.test(await ask("SSL/HTTPS (Y/n): "));

	data.port = await ask("Port (default=3001): ");
	if (!data.port) data.port = "3001";

	if (data.db != "sqlite")
		data.env.push(`DATABASE=${data.db}://${data.db_user}:${data.db_pass}@${data.db_host}:${data.db_port}/${data.db_name}`);
	data.env.push(`PORT=${data.port}`);
	data.env.push("THREADS=1");

	printTitle("Step 4: Default rights");
	console.log("Please enter the default rights for new users.");
	console.log("Valid rights are: none, discord, full, custom.");
	console.log();
	let lines = fs.readFileSync(path.join(__dirname, "..", "src", "util", "util", "Rights.ts")).toString();
	let lines2 = lines.split("\n");
	let lines3 = lines2.filter((y) => y.includes(": BitFlag("));
	let lines4 = lines3.map((x) => x.split("//")[0].trim());

	let maxRights = 0n;
	lines4.forEach((x) => {
		maxRights += eval(`rights.${x.replace(":", " = ").replace(",", ";")}`);
	});
	discordRights = maxRights;
	discordRights -= rights.SEND_BACKDATED_EVENTS;
	discordRights -= rights.MANAGE_GUILD_DIRECTORY;
	discordRights -= rights.CREDITABLE;
	discordRights -= rights.BYPASS_RATE_LIMITS;
	discordRights -= rights.ADD_MEMBERS;
	discordRights -= rights.MANAGE_RATE_LIMITS;
	discordRights -= rights.OPERATOR;

	data.default_rights = await ask("Rights (default=none): ");
	if (!data.default_rights || data.defaultRights == "none") data.config.register.defaultRights = "0";
	else if (data.default_rights == "discord") data.config.register.defaultRights = discordRights.toString();
	else if (data.default_rights == "full") data.config.register.defaultRights = maxRights.toString();
	else if (data.default_rights == "custom") data.config.register.defaultRights = (await askRights()).toString();

	if (data.domain != "localhost")
		data.config = {
			cdn: {
				endpointPrivate: `http://localhost:${data.port}`,
				endpointPublic: `${data.ssl ? "https" : "http"}://${data.domain}:${data.port}`
			},
			gateway: {
				endpointPrivate: `ws://localhost:${data.port}`,
				endpointPublic: `${data.ssl ? "wss" : "ws"}://${data.domain}:${data.port}`
			},
			...data.config
		};
	printTitle("Step 5: extra options");

	if (/y?/i.test(await ask("Use fast BCrypt implementation (requires a compiler) (Y/n): "))) data.extra_pkgs.push("bcrypt");
	if (/y?/i.test(await ask("Enable support for widgets (requires compiler, known to fail on some ARM devices.) (Y/n): ")))
		data.extra_pkgs.push("canvas");

	printTitle("Step 6: finalizing...");
	//save
	console.log("==> Writing .env...");
	fs.writeFileSync(".env", data.env.join("\n"));
	console.log("==> Writing initial.json");
	fs.writeFileSync("initial.json", JSON.stringify(data.config, (space = 4)));
	//install packages...
	console.log("==> Installing packages...");
	console.log("  ==> Ensuring yarn is up to date (v3, not v1)...");
	execIn("npx yarn set version stable", process.cwd());
	console.log("  ==> Installing base packages");
	execIn("npx --yes yarn install", process.cwd(), { stdio: "inherit" });
	if (data.extra_pkgs.length > 0) {
		console.log("  ==> Checking dependencies...");
		checkCompilers();
		if (data.extra_pkgs.includes("canvas")) checkCanvasDeps();
		if (data.extra_pkgs.includes("bcrypt")) checkBcryptDeps();

		console.log(`  ==> Installing extra packages: ${data.extra_pkgs.join(", ")}...`);
		execIn(`npx --yes yarn add -O ${data.extra_pkgs.join(" ")}`, process.cwd(), { stdio: "inherit" });
	}

	console.log("==> Building...");
	execIn("npx --yes yarn run build", process.cwd(), { stdio: "inherit" });
	printTitle("Step 6: run your instance!");
	console.log("Installation is complete!");
	console.log("You can now start your instance by running 'npm run start:bundle'!");
	exit(0);
}
main();

async function askRights() {
	let w = 0;
	let brights = { ...eval(`rights`) };
	Object.keys(rights).forEach((x) => {
		brights[x] = false;
		let str = `[x] ${Object.keys(rights).length}: ${x}`;
		if (str.length > w) w = str.length;
	});

	let resp = "";
	let selectedRights = 0n;
	while (resp != "q") {
		selectedRights = 0n;
		Object.keys(brights).forEach((x) => {
			if (brights[x]) selectedRights += rights[x];
		});
		console.clear();
		printTitle("Step 4: Default rights");
		printTitle(`Current rights: ${selectedRights} (0b${selectedRights.toString(2)}, 0x${selectedRights.toString(16)})`);
		let xpos = 0;
		Object.keys(rights).forEach((x) => {
			let str = `[${brights[x] ? "X" : " "}] ${Object.keys(rights).indexOf(x)}: ${x}`.padEnd(w + 1, " ");
			if (xpos + str.length > stdout.columns) {
				console.log();
				xpos = 0;
			}
			stdout.write(str);
			xpos += str.length;
		});

		console.log();
		resp = await ask("Enter an option, or q to exit: ");
		if (/\d{1,}/.test(resp) && resp < Object.keys(rights).length && resp > -1) {
			brights[Object.keys(brights)[parseInt(resp)]] ^= true;
		}
	}
	return selectedRights;
}



function printTitle(input) {
	let width = stdout.columns / 2 - 1; //40
	console.log();
	console.log("-".repeat(width - input.length / 2), input, "-".repeat(width - input.length / 2));
	console.log();
}


function BitFlag(int) {
	return 1n << BigInt(int);
}

function checkCanvasDeps() {
	if (
		!(
			checkDep("pixman", "/usr/include/pixman-1/pixman.h") &&
			checkDep("pixman", "/usr/lib/libpixman-1.so") &&
			checkDep("cairo", "/usr/include/cairo/cairo.h") &&
			checkDep("cairo", "/usr/lib/libcairo.so") &&
			checkDep("pango", "/usr/include/pango-1.0/pango/pangocairo.h") &&
			checkDep("pango", "/usr/lib/libpango-1.0.so") &&
			checkDep("pkgconfig", "/usr/bin/pkg-config")
		)
	) {
		console.log("Canvas requires the following dependencies to be installed: pixman, cairo, pango, pkgconfig");
		exit(1);
	}
}
function checkBcryptDeps() {
	/*if (!(checkDep("bcrypt", "/usr/include/bcrypt.h") && checkDep("bcrypt", "/usr/lib/libbcrypt.so"))) {
		console.log("Bcrypt requires the following dependencies to be installed: bcrypt");
		exit(1);
	}*/
	//TODO: check if required
}

function checkCompilers() {
	//check for gcc, grep, make, python-is-python3
	if (
		!(
			checkDep("gcc", "/usr/bin/gcc") &&
			checkDep("grep", "/usr/bin/grep") &&
			checkDep("make", "/usr/bin/make") &&
			checkDep("python3", "/usr/bin/python3")
		)
	) {
		console.log("Compiler requirements not met. Please install the following: gcc, grep, make, python3");
		exit(1);
	}

	//check if /usr/bin/python is a symlink to /usr/bin/python3
	if (!fs.lstatSync("/usr/bin/python").isSymbolicLink()) {
		console.log("/usr/bin/python is not a symlink. Please make sure it is a symlink to /usr/bin/python3");
		if (fs.existsSync("/usr/bin/python3")) {
			console.log("Hint: sudo ln -s /usr/bin/python3 /usr/bin/python");
		}
		exit(1);
	}
}

function checkDep(name, path, message) {
	if (!fs.existsSync(path)) {
		console.log(`${name} not found at ${path}! Installation of some modules may fail!`);
		console.log(message ?? `Please consult your distro's manual for installation instructions.`);
	}
	return fs.existsSync(path);
}