-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
116 lines (107 loc) · 3.47 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var spawn = require('child_process').spawn,
fs = require('fs'),
amqp = require('amqp');
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':
create_vhost(message,c);
break;
case 'delete':
delete_vhost(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);
});
}
var create_vhost = function(data,c)
{
if(!data.vm_number || !data.domain || data.domain == '.hycpr.net')
console.log('['+date()+'] Invalid param');
else
{
var file = "server {\n"+
" listen 80;\n"+
" server_name "+data.domain+" *."+data.domain+";\n"+
" access_log /var/log/nginx/access.server0"+data.vm_number+".log;\n"+
" location / {\n"+
" proxy_set_header Host $host;\n"+
" proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n"+
" proxy_set_header X-Real-IP $remote_addr;\n"+
" proxy_pass http://10.10.10."+data.vm_number+";\n"+
" }\n"+
"}";
fs.writeFile("/etc/nginx/sites-enabled/vm"+data.vm_number+".conf", file, function (err) {
if (err) console.log('['+date()+'] stderr : '+err);
var cmd = spawn('/etc/init.d/nginx', ['reload']);
error(cmd);
});
}
};
var delete_vhost = function(data,c)
{
if(!data.vm_number)
console.log('['+date()+'] Invalid param');
else
{
var cmd = spawn('rm',["/etc/nginx/sites-enabled/vm"+data.vm_number+".conf" ]);
error(cmd);
cmd.on('exit', function() {
cmd = spawn('/etc/init.d/nginx', ['reload']);
error(cmd);
});
}
};