-
Notifications
You must be signed in to change notification settings - Fork 100
/
execPod.js
153 lines (116 loc) · 4.27 KB
/
execPod.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
const CommonService = require('./k8s/service/common/CommonService');
const logger = require("./logger");
const _ = require('lodash');
// function originIsAllowed(origin) {
// // put logic here to detect whether the specified origin is allowed.
// return true;
// }
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
let ExecPod = async (request) => {
let ws = null;
let connection = request.accept('echo-protocol', request.origin);
// console.log(connection);
logger.info('Connection accepted');
connection.on('close', function (reasonCode, description) {
logger.info('peer ' + connection.remoteAddress + ' disconnected.');
if (ws) {
//close pod shell
var buffer = Buffer.from("exit\r\n", 'utf8');
var panddingBuffer = Buffer.concat([Buffer.from('00', 'hex'), buffer]);
ws.send(panddingBuffer);
}
});
let params = request.resourceURL.query;
logger.info('request:', params);
let command = ['bash', '-c'];
let sh;
let pod;
if (params.History == 'true') {
sh = `#!/bin/sh
dir=/usr/local/app/${CommonService.TServerType1}/app_log/${params.PodName}/${params.ServerApp}/${params.ServerName}
if [ -d $dir ]; then
cd $dir
fi
if [ ! -f /bin/bash ]; then
sh
else
bash
fi`;
pod = await CommonService.getDaemonPodByHostIp(params.NodeIP);
// console.log(pod);
if (!pod) {
connection.close();
return;
}
// console.log(pod);
// params.PodName = pod.metadata.name;
} else {
sh = `#!/bin/sh
dir=/usr/local/app/${CommonService.TServerType1}/app_log/${params.ServerApp}/${params.ServerName}
if [ -d $dir ]; then
cd $dir
fi
if [ ! -f /bin/bash ]; then
sh
else
bash
fi`
pod = (await CommonService.getPod(params.PodName)).body;
}
command.push(sh);
try {
logger.info(pod.metadata.name, pod.spec.containers[0].name, pod.metadata.namespace);
ws = await CommonService.connectPodExec(pod.metadata.name, pod.metadata.namespace, command, pod.spec.containers[0].name);
ws.on('close', (code, reason) => {
logger.error('close:', code, reason);
});
ws.on('error', (error) => {
logger.error("error:", error);
});
ws.on('ping', (data) => {
logger.info("ping:", data);
});
ws.on('open', () => {
logger.info('connected to container');
});
ws.on('message', (data) => {
// logger.info("api->nodejs:'" + data.toString() + "'");
let msg = {
operation: "stdout",
data: data.toString()
}
connection.send(JSON.stringify(msg));
});
connection.on('message', function (message) {
if (ws && ws.readyState === 1) {
// logger.info("web->nodejs:", JSON.stringify(message));
let msg = JSON.parse(message.utf8Data);
switch (msg.operation) {
case "stdin":
var buffer = Buffer.from(msg.data, 'utf8');
var panddingBuffer = Buffer.concat([Buffer.from('00', 'hex'), buffer]);
// console.log(msg.data);
ws.send(panddingBuffer);
break;
case "resize":
process.stdout.rows = msg.height;
process.stdout.columns = msg.width;
process.stdout.emit("resize");
break;
case "ping":
let rsp = {
operation: "pong",
data: ""
};
connection.send(JSON.stringify(rsp));
break;
default:
}
}
});
} catch (e) {
logger.error(e);
connection.close();
}
}
module.exports = ExecPod;