forked from hexparrot/mineos-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mineos_shutdown.js
76 lines (59 loc) · 1.94 KB
/
mineos_shutdown.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
#!/usr/bin/env node
var getopt = require('node-getopt');
var mineos = require('./mineos');
var fs = require('fs-extra');
function read_ini(filepath) {
var ini = require('ini');
try {
var data = fs.readFileSync(filepath);
return ini.parse(data.toString());
} catch (e) {
return null;
}
}
console.log("Stopping running games");
// List names of running servers
var servers = mineos.server_list_up();
// Read base directory configurations
var mineos_config = read_ini('/etc/mineos.conf') || read_ini('/usr/local/etc/mineos.conf') || {};
var base_directory = '/var/games/minecraft';
if ('base_directory' in mineos_config) {
try {
if (mineos_config['base_directory'].length < 2)
throw new error('Invalid base_directory length.');
base_directory = mineos_config['base_directory'];
fs.ensureDirSync(base_directory);
} catch (e) {
console.error(e.message, 'Aborting shutdown.');
process.exit(2);
}
console.info('base_directory found in mineos.conf, using:', base_directory);
} else {
console.error('base_directory not specified--missing mineos.conf?');
console.error('Aborting startup.');
process.exit(4);
}
// List of running servers
var server_watches=[]
function make_cb(server_watch)
{
return function() {
console.log(" stopped server",server_watch.name);
server_watch.running = false;
for (w of server_watches) {
if (w.running){
console.log(" waiting for",w.name);
}
}
};
}
for (server of servers) {
// obect to track state of server
var server_watch = {name:server, running:true};
server_watches.push(server_watch);
var instance = new mineos.mc(server, base_directory);
console.log("Stopping", server);
cb_stopped = make_cb(server_watch);
instance.stop(cb_stopped);
}
console.log("Waiting for servers to stop");