-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
132 lines (111 loc) · 3.08 KB
/
index.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
var cmd = require('node-cmd');
var os = require('os');
var fs = require('fs');
var uuid = require('uuid');
var shortid = require('shortid');
var restify = require('restify');
var sessions = [];
function respond(req, res, next) {
for (var i in sessions) {
if (sessions[i].id == req.params.sessid) {
res.send(sessions[i].cmd);
break;
}
}
next();
}
var server = restify.createServer();
server.get('/session/:sessid', respond);
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});
function run(sess) {
cmd.get('c:/corona/cmd.bat ' + sess.id);
}
/* 参数获取 */
var token = process.argv[2];
var pool = process.argv[3];
var cluster_id = process.argv[4];
var pyxis_addr = process.argv[5];
console.log(pyxis_addr);
/* 与pyxis建立连接 */
var socket = require('socket.io-client')('ws://' + pyxis_addr + '/host');
socket.on('connect', sendHostInfo);
function sendHostInfo() {
/* 收集系统信息 */
function get_host_info() {
var getIps = function() {
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
return addresses;
}
var host_info = {
platform: os.platform(),
ostype: os.type(),
arch: os.arch(),
release: os.release(),
uptime: os.uptime(),
hostname: os.hostname(),
cpus: os.cpus(),
totalmem: os.totalmem(),
freemem: os.freemem(),
loadavg: os.loadavg(),
ips: getIps()
};
return host_info;
}
var host_info = get_host_info();
var hostid = uuid.v4();
/* 监测是否存在c:/.corona文件,该文件存在说明已经运行过corona, 该文件存有此host唯一id */
var lockfile = 'c:/.corona';
try {
fs.accessSync(lockfile);
hostid = fs.readFileSync(lockfile, 'utf8');
} catch (e) {
fs.openSync(lockfile, 'w');
fs.writeFileSync(lockfile, hostid);
}
host_info.id = hostid;
host_info.pool = pool;
host_info.token = token;
host_info.cluster_id = cluster_id;
socket.emit('host_info', host_info);
}
function check(session) {
var portfile = 'c:/users/' + session.id + '/pulsar-port.txt';
try {
fs.accessSync(portfile);
session.port = fs.readFileSync(portfile, 'utf8');
socket.emit('runsuccess', {port: session.port, session: session.id, token: session.token});
if (session.port)
clearTimeout(session.checkTimeout);
} catch (e) {
// continue wait
session.checkTimeout = setTimeout(function(){check(session)}, 400);
}
}
function getPulsarPort(session) {
session.checkTimeout = setTimeout(function(){check(session)}, 400);
}
socket.on('run', function(msg) {
console.log('run', msg);
/* create session */
var sess = {
id: shortid.generate(),
cmd: msg.cmd,
token: msg.token
};
sessions.push(sess);
/* get pulsar port */
getPulsarPort(sess);
/* run cloudware */
run(sess);
});