-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (91 loc) · 2.79 KB
/
server.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
var amqp = require('amqp');
var is_creeper = process.env.npm_package_config_creeper || 0;
var server = (is_creeper == 1) ? require(__dirname + '/creeper.js') : require(__dirname + '/chicken.js');
function date() {
var _date = new Date();
return _date.getDate() + "/" + (_date.getMonth()+1) + "/" + _date.getFullYear() + " " + _date.getHours() + ":" + _date.getMinutes();
}
function trim (str) {
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
}
var connection = amqp.createConnection({
host: process.env.npm_package_config_amqp_host,
login : process.env.npm_package_config_amqp_user,
password: process.env.npm_package_config_amqp_pass,
vhost: process.env.npm_package_config_amqp_vhost
});
// Wait for connection to become established.
connection.on('ready', function () {
connection.queue(process.env.npm_package_config_amqp_cons_queue,
{durable: true, autoDelete: false},
function(q){
console.log('queue '+process.env.npm_package_config_amqp_cons_queue+' connected');
// Catch all messages
q.bind('#');
// Receive messages
q.subscribe(function (message) {
console.log(JSON.parse(message.data));
// Print messages to stdout
handle(connection,JSON.parse(message.data));
});
q.on('error', function(err) { console.log(err); });
});
connection.queue(process.env.npm_package_config_amqp_prod_queue,
{durable: true, autoDelete: false},
function(q){
console.log('queue '+process.env.npm_package_config_amqp_prod_queue+' connected');
q.on('error', function(err) { console.log(err); });
});
});
connection.on('error',function(err) {
console.log(err);
});
var handle = function(c, message) {
console.log('['+ date() + '] Command received : '+ message.command);
switch(message.command) {
case 'create':
server.create(message,c);
break;
case 'delete':
server.delete_image(message,c);
break;
case 'start':
server.start(message,c);
break;
case 'stop':
server.stop(message,c);
break;
case 'password':
server.password(message,c);
break;
case 'dedicated_ip':
server.dedicated_ip(message,c);
break;
case 'restart':
if(!is_creeper) {
server.restart(message,c);
break;
}
case 'update':
if(!is_creeper) {
server.update(message,c);
break;
}
default:
console.log('['+date()+'] stderr : Unknown command ' + message.command);
break;
}
};
function error(cmd)
{
cmd.stderr.on('data',function (data) {
console.log('['+date()+'] stderr : '+data);
});
}