-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (123 loc) · 3.49 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var net = require('net');
var cp = require('child_process');
var pty = require('pty.js');
var async = require('async');
var ioSocket = net.createConnection('/root/ps-run/io.sock');
var commandSocket = net.createConnection('/root/ps-run/command.sock');
commandSocket.on('error', function(err) {
console.dir(err);
throw err;
});
ioSocket.on('error', function(err) {
console.dir(err);
throw err;
});
connectToServer();
function connectToServer() {
async.series([
function(cb) {
var connected = 0;
ioSocket.on('connect', function() {
connected++;
if(connected == 2) {
cb();
}
});
commandSocket.on('connect', function() {
connected++;
if(connected == 2) {
cb();
}
});
},
processCommands]
);
}
function processCommands() {
commandSocket.on('data', function(data) {
var payload = JSON.parse(data);
var inst;
// handling git push and receive
if(payload.type === 'do' && payload.attached && !payload.pty) {
commandSocket.write('status: starting\n');
inst = spawn(payload, ioSocket, commandSocket);
}
// executing something via rendezvous
if(payload.type === 'do' && payload.attached && payload.pty) {
commandSocket.write('status: starting attached\n');
inst = spawnPty(payload, ioSocket, commandSocket);
}
// normal dyno running
if(payload.type === 'do' && !payload.attached) {
commandSocket.write('status: starting');
inst = spawn(payload, ioSocket, commandSocket);
}
if(payload.type === 'exit') {
if(inst) {
inst.kill('SIGKILL');
process.exit(0);
}
}
});
}
function spawnPty(payload, outputSocket, commandSocket) {
// pty.js doesnt support uid/gid - for running as less priv user
// so we launch a pty and inherit the tty for normal cp spawn
payload.args.unshift(payload.command);
payload.args.unshift('/root/ps-run/runas.js');
var realCommand = '/root/ps-run/node';
// TODO pass over TERM, cols, rows etc..
var term = pty.spawn(realCommand, payload.args || [], {
cols: 80,
rows: 30,
cwd: '/app',
env: {
TERM: 'xterm'
}
});
term.on('data', function(data) {
outputSocket.write(data);
});
outputSocket.on('data', function(data) {
term.write(data);
});
// term.on('exit', function() {
// commandSocket.write('exit - ');
// //socket.destroySoon();
// });
return term;
}
function spawn(payload, outputSocket, commandSocket, bashIt) {
// Use bash to bootstrap for PATH population and PORT interpolation
if(true) {
var origArgs = payload.args;
origArgs.unshift(payload.command);
origArgs.unshift('exec');
payload.command = '/bin/bash';
payload.args = ['-c', origArgs.join(' ')];
}
if(!payload.attached) {
outputSocket.write(JSON.stringify(payload.env_vars));
}
var inst = cp.spawn(payload.command, payload.args,
{
env: payload.env_vars,
cwd: '/app',
uid: 1666,
gid: 666
});
inst.stdout.on('data', function(data) {
outputSocket.write(data);
});
inst.stderr.on('data', function(data) {
outputSocket.write(data);
});
outputSocket.on('data', function(data) {
inst.stdin.write(data);
});
inst.on('close', function(code) {
commandSocket.write('status: exit - ' + code + '\n');
outputSocket.destroySoon();
});
return inst;
}