forked from OpenDroneMap/ClusterODM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admincli.js
259 lines (242 loc) · 12.6 KB
/
admincli.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* ClusterODM - A reverse proxy, load balancer and task tracker for NodeODM
* Copyright (C) 2018-present MasseranoLabs LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const logger = require('./libs/logger');
const net = require('net');
const package_info = require('./package_info');
const nodes = require('./libs/nodes');
const routetable = require('./libs/routetable');
const netutils = require('./libs/netutils');
const asrProvider = require('./libs/asrProvider');
module.exports = {
create: function(options){
logger.info("Starting admin CLI on " + options.port);
if (!options.password){
logger.warn(`No admin CLI password specified, make sure port ${options.port} is secured`);
}
// Start a TCP Server
net.createServer(function (socket) {
let loggedIn = false;
const printCaret = () => {
if (loggedIn) socket.write("#> ");
else socket.write("$> ");
};
const ok = () => {
socket.write("OK\r\n");
};
const invalid = () => {
socket.write("INVALID\r\n");
};
const fail = () => {
socket.write("FAIL\r\n");
};
const reply = (flag) => {
if (flag) ok();
else fail();
};
const printNode = (socket, i, node) => {
const flags = [];
if (node.isLocked()) flags.push("L");
if (node.isAutoSpawned()) flags.push("A");
socket.write(`${(i + 1)}) ${node.toString()} ${node.isOnline() ? '[online]' : '[offline]'} [${node.getTaskQueueCount()}/${node.getMaxParallelTasks()}] <engine: ${node.getEngineInfo()}> <API: ${node.getVersion()}>${flags.length ? ` [${flags.join(",")}]` : ""}\r\n`);
};
const prettyJson = (json) => {
return JSON.stringify(json, null, 2);
};
const jsonResponse = (json) => {
return socket.write(prettyJson(json) + '\r\n');
};
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort;
// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + " " + package_info.name + ":" + package_info.version + "\r\n");
if (options.password){
socket.write("LOGIN <LOGIN> to log-in\r\n");
}else{
loggedIn = true;
socket.write("HELP for help\r\n");
}
socket.write("QUIT to quit\r\n");
printCaret();
// Handle incoming messages from clients.
let lastInput = "";
socket.on('data', async function (data) {
let input = data.toString();
if (input.trim() === "!!") input = lastInput;
else lastInput = input;
const parts = input.split(" ").map(p => p.trim());
const command = parts[0].toLocaleUpperCase();
let args = parts.slice(1, parts.length);
if (command === "QUIT"){
socket.write("Bye!\r\n");
socket.destroy();
return;
}
if (loggedIn){
if (command === "HELP"){
socket.write("NODE ADD <hostname> <port> [token] - Add new node\r\n");
socket.write("NODE DEL <node number> - Remove a node\r\n");
socket.write("NODE INFO <node number> - View node info\r\n");
socket.write("NODE LIST - List nodes\r\n");
socket.write("NODE LOCK <node number> - Stop forwarding tasks to this node\r\n");
socket.write("NODE UNLOCK <node number> - Resume forwarding tasks to this node\r\n");
socket.write("NODE UPDATE - Update all nodes info\r\n");
socket.write("NODE BEST <number of images> - Show best node for the number of images\r\n");
socket.write("ROUTE INFO <taskId> - Find route information for task\r\n");
socket.write("ROUTE LIST [node number] - List routes\r\n");
socket.write("TASK LIST [node number] - List tasks\r\n");
socket.write("TASK INFO <taskId> - View task info\r\n");
socket.write("TASK OUTPUT <taskId> [lines] - View task output\r\n");
socket.write("TASK CANCEL <taskId> - Cancel task\r\n");
socket.write("TASK REMOVE <taskId> - Remove task\r\n");
socket.write("ASR VIEWCMD <number of images> - View command used to create a machine\r\n");
socket.write("!! - Repeat last command\r\n");
}else if (command === "NODE" && args.length > 0){
const subcommand = args[0].toLocaleUpperCase();
args = args.slice(1, args.length);
if (subcommand === "ADD" && args.length >= 2){
const [ hostname, port, token ] = args;
const node = nodes.addUnique(hostname, port, token);
if (node) node.updateInfo();
reply(!!node);
}else if (subcommand === "DEL" && args.length >= 1){
const [ number ] = args;
reply(await netutils.removeAndCleanupNode(nodes.nth(number), asrProvider.get()));
}else if (subcommand === "LOCK" && args.length >= 1){
const [ number ] = args;
reply(nodes.lock(nodes.nth(number)));
}else if (subcommand === "UNLOCK" && args.length >= 1){
const [ number ] = args;
reply(nodes.unlock(nodes.nth(number)));
}else if (subcommand === "LIST"){
nodes.all().forEach((n, i) => {
printNode(socket, i, n);
});
socket.write("\r\n");
}else if (subcommand === "UPDATE"){
nodes.updateInfo().then(() => {
ok(); printCaret();
}).catch(() => {
fail(); printCaret();
});
return;
}else if (subcommand === "INFO" && args.length >= 1){
const [ number ] = args;
const node = nodes.nth(number);
if (node){
jsonResponse(node.nodeData);
}else invalid();
}else if (subcommand === "BEST" && args.length >= 1){
const [ numImages ] = args;
const node = await nodes.findBestAvailableNode(numImages);
if (node){
printNode(socket, 0, node);
}else{
socket.write("No best node available\r\n");
}
}else{
invalid();
}
}else if (command === "ROUTE" && args.length > 0){
const subcommand = args[0].toLocaleUpperCase();
args = args.slice(1, args.length);
if (subcommand === "INFO" && args.length >= 1){
const [ taskId ] = args;
const route = await routetable.lookup(taskId);
if (route){
jsonResponse(route);
}else invalid();
}else if (subcommand === "LIST"){
const [ number ] = args;
let node = null;
if (number !== undefined) node = nodes.nth(number);
if (number !== undefined && !node) invalid();
else{
jsonResponse(await routetable.findByNode(node));
}
}
}else if (command === "TASK" && args.length > 0){
const subcommand = args[0].toLocaleUpperCase();
args = args.slice(1, args.length);
if (subcommand === "LIST"){
const [ number ] = args;
let node = null;
if (number !== undefined) node = nodes.nth(number);
if (number !== undefined && !node) invalid();
else jsonResponse(await netutils.findTasksByNode(node));
}else if (subcommand === "INFO" && args.length >= 1){
const [ taskId ] = args;
const route = await routetable.lookup(taskId);
if (!route) invalid();
else{
const taskInfo = await route.node.taskInfo(taskId);
jsonResponse(taskInfo);
}
}else if (subcommand === "OUTPUT" && args.length >= 1){
const [ taskId, line ] = args;
const route = await routetable.lookup(taskId);
if (!route) invalid();
else{
const taskOutput = await route.node.taskOutput(taskId, line);
if (Array.isArray(taskOutput)){
socket.write(taskOutput.join("\r\n") + '\r\n');
}else{
jsonResponse(taskOutput);
}
}
}else if (["CANCEL", "REMOVE"].indexOf(subcommand) !== -1 && args.length >= 1){
const [ taskId ] = args;
const route = await routetable.lookup(taskId);
const func = subcommand === 'CANCEL' ?
'taskCancel' :
'taskRemove';
if (!route) invalid();
jsonResponse(await (route.node[func])(taskId));
}
}else if (command === "ASR" && args.length > 0){
const asr = asrProvider.get();
if (!asr){
invalid();
return;
}
const subcommand = args[0].toLocaleUpperCase();
args = args.slice(1, args.length);
if (subcommand === "VIEWCMD"){
const [ numImages ] = args;
const cmd = await asr.debugCreateDockerMachineCmd(numImages);
socket.write(`${cmd}\r\n`);
}else{
invalid();
}
}else{
invalid();
}
}else{
if (command === "LOGIN" && args[0] === options.password){
loggedIn = true;
ok();
socket.write("HELP for help\r\n");
}else{
socket.destroy();
return;
}
}
printCaret();
});
}).listen(options.port);
}
};