This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
executable file
·269 lines (252 loc) · 10.3 KB
/
install.js
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
#!/bin/sh
':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@"
"use strict";
var sys = require("sys"),
async = require("async"),
mongojs = require("mongojs"),
uuid = require("node-uuid"),
read = require("read"),
crypto = require("crypto"),
fs = require("fs"),
stdin = process.openStdin(),
randomstring = require("randomstring"),
defaultSettings = require("./lib/defaultSettings.json"),
defaultConfig = require("./config.default.json"),
packageJson = require("./package.json"),
// init variables that rely on a config file
config,
mongo,
database;
async.waterfall([
function checkIfConfigFileExists(waterfallDone) {
fs.stat("config.json", function (error) {
if (error) {
if (error.errno === 34) {
return waterfallDone(null, false);
}
return waterfallDone(error)
}
waterfallDone(null, true);
});
},
function writeNewConfigFile(isConfigExisting, waterfallDone) {
if (isConfigExisting) {
return waterfallDone();
}
console.log("Creating new config file, please enter some data.");
defaultConfig.cookie.secret = randomstring.generate(128);
defaultConfig.session.secret = randomstring.generate(128);
async.waterfall([
function inputPort(configDone) {
read({prompt: "Port to use", default: defaultConfig.port}, configDone);
},
function inputMongoUrl(port, isDefault, configDone) {
defaultConfig.port = parseInt(port, 10);
console.log(" ");
console.log("MongoDB data");
console.log("------------------");
read({prompt: "MongoDB url", default: defaultConfig.mongo.url}, configDone);
},
function isRedisSocket(mongoUrl, isDefault, configDone) {
var useRedisSocket;
defaultConfig.mongo.url = mongoUrl;
console.log(" ");
console.log("Redis data");
console.log("------------------");
async.doUntil(function readCorrectInput(untilDone) {
read({prompt: "Use Redis via socket? ", default: "no"}, function (error, input, isDefault) {
if (error) {
return untilDone(error);
}
useRedisSocket = input;
untilDone();
});
}, function test() {
if (["yes", "no", "y", "n", "1", "0"].indexOf(useRedisSocket) >= 0) {
return true;
}
return false;
}, function (error) {
return configDone(error, useRedisSocket);
});
},
function getRedisInput(useRedisSocket, configDone) {
if (["yes", "y", "1"].indexOf(useRedisSocket) >= 0) {
defaultConfig.redis = {
host: "127.0.0.1",
socket: "~/.redis/sock",
database: 0
};
async.waterfall([
function inputRedisHost(redisDone) {
read({prompt: "Host", default: defaultConfig.redis.host}, redisDone);
},
function inputRedisPort(host, isDefault, redisDone) {
defaultConfig.redis.host = host;
read({prompt: "Socket path", default: defaultConfig.redis.socket}, redisDone);
},
function inputRedisDatabase(port, isDefault, redisDone) {
defaultConfig.redis.port = port;
read({prompt: "Database", default: defaultConfig.redis.database}, redisDone);
},
function getRedisDatabase(database, isDefault, redisDone) {
defaultConfig.redis.db = parseInt(database, 10);
redisDone();
}
], function redisDone(error) {
configDone(error);
});
} else {
async.waterfall([
function inputRedisHost(redisDone) {
read({prompt: "Host", default: defaultConfig.redis.host}, redisDone);
},
function inputRedisPort(host, isDefault, redisDone) {
defaultConfig.redis.host = host;
read({prompt: "Port", default: defaultConfig.redis.port}, redisDone);
},
function inputRedisDatabase(port, isDefault, redisDone) {
defaultConfig.redis.port = port;
read({prompt: "Database", default: defaultConfig.redis.db.toString()}, redisDone);
},
function inputRedisPort(database, isDefault, redisDone) {
defaultConfig.redis.db = parseInt(database, 10);
read({prompt: "Password", default: ""}, redisDone);
},
function getRedisPass(pass, isDefault, redisDone) {
defaultConfig.redis.pass = pass.length ? pass : null;
redisDone();
}
], function redisDone(error) {
configDone(error);
});
}
},
function writeConfigFile(configDone) {
var configJson = JSON.stringify(defaultConfig, null, 4);
fs.writeFile("config.json", configJson, function (error) {
configDone(error);
});
}
], function configDone(error) {
waterfallDone(error);
});
},
function requireModules(waterfallDone) {
config = require("./config.json");
mongo = mongojs(config.mongo.url);
database = require("./lib/database")(mongo);
waterfallDone();
},
function initDatabase(waterfallDone) {
console.log(" ");
console.log("------------------");
console.log("Initializing database...");
database.createIndexes(waterfallDone);
},
function createDefaultSettings(waterfallDone) {
console.log(" ");
console.log("------------------");
console.log("Insert default settings...");
var settingsCollection = mongo.collection("settings");
async.each(Object.getOwnPropertyNames(defaultSettings), function (key, eachDone) {
settingsCollection.update({
key: key
}, {
$setOnInsert: {
key: key,
value: defaultSettings[key]
}
}, {
upsert: true
}, function (error) {
eachDone(error);
});
}, function eachDone(error) {
waterfallDone(error);
});
},
function updateVersion(waterfallDone) {
mongo.collection("settings").update({
key: "version"
}, {
key: "version",
value: packageJson.version
}, {
upsert: true
}, function (error) {
waterfallDone(error);
});
},
function checkIfAdminAccountExists(waterfallDone) {
mongo.collection("users").findOne({isAdmin: true}, function (error, user) {
if (error) {
return waterfallDone(error);
}
return waterfallDone(null, user);
});
},
function createAdminAccount(user, waterfallDone) {
if (user) {
return waterfallDone();
}
console.log(" ");
console.log("Admin data");
console.log("------------------");
console.log("Creating admin user account, please enter user data.");
var userName, charName, userEmail;
async.waterfall([
function inputUserName(adminAccDone) {
read({prompt: "Login Name: "}, adminAccDone);
},
function inputCharacterName(name, isDefault, adminAccDone) {
userName = name;
read({prompt: "Character Name: "}, adminAccDone);
},
function inputEmail(name, isDefault, adminAccDone) {
charName = name;
read({prompt: "Email: "}, adminAccDone);
},
function inputPassword(email, isDefault, adminAccDone) {
userEmail = email;
read({prompt: "Enter Password: ", silent: true, replace: "*"}, adminAccDone);
},
function insertUserIntoDatabase(pw, isDefault, adminAccDone) {
var salt = uuid.v4().replace(/-/g, ""),
user = {
login: userName,
smallLogin: userName.toLowerCase(),
salt: salt,
password: "",
email: userEmail.toLowerCase(),
characters: [{
name: charName,
smallName: charName.toLowerCase
}],
isAdmin: true,
dkp: 0
},
hash = crypto.createHash('sha512');
hash.update(JSON.stringify(salt + ":" + pw));
user.password = hash.digest('hex');
mongo.collection("users").insert(user, function (error, user) {
adminAccDone(error, user);
});
}
], function adminAccDone(error) {
if (!error) {
console.log("Account created.");
}
waterfallDone(error);
});
}
], function waterfallDone(error) {
if (error) {
console.log(error);
process.exit(1);
}
console.log(" ");
console.log("------------------");
console.log("Finished installation.");
process.exit();
});