-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlavajs.js
More file actions
218 lines (195 loc) · 7.73 KB
/
lavajs.js
File metadata and controls
218 lines (195 loc) · 7.73 KB
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
/*
* Copyright (C) 2015-2016 Quantum HPC Inc.
*
* 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 <http://www.gnu.org/licenses/>.
*/
var cproc = require('child_process');
var spawn = cproc.spawnSync;
var fs = require("fs");
var path = require("path");
var nodeStatus = {'ok' : 'OK', 'unavail' : 'Unavailable', 'unreach' : 'Unreachable', 'closed' : 'Closed'};
// General command dictionnary keeping track of implemented features
var cmdDict = {
"queue" : "",
"queues" : "",
"job" : "",
"jobs" : "",
"node" : "",
"nodes" : "bhosts -l",
"submit" : "",
"delete" : "",
"setting" : "",
"settings" : ""
};
// Parse the command and return stdout of the process depending on the method
/*
spawnCmd : shell command / [file, destinationDir],
spawnType : shell / copy,
spawnDirection : null / send || retrieve,
lava_config
*/
// TODO: treat errors
function spawnProcess(spawnCmd, spawnType, spawnDirection, lava_config){
var spawnExec;
switch (spawnType){
case "shell":
switch (lava_config.method){
case "ssh":
spawnExec = lava_config.ssh_exec;
spawnCmd = [lava_config.username + "@" + lava_config.serverName,"-o","StrictHostKeyChecking=no","-i",lava_config.secretAccessKey].concat(spawnCmd.split(" "));
break;
case "local":
spawnExec = lava_config.local_shell;
spawnCmd = spawnCmd.split(" ");
break;
}
break;
//Copy the files according to the spawnCmd array : 0 is the file, 1 is the destination dir
case "copy":
// Special case if we can use a shared file system
if (lava_config.useSharedDir){
spawnExec = lava_config.local_copy;
}else{
switch (lava_config.method){
// Build the scp command
case "ssh":
spawnExec = lava_config.scp_exec;
var file;
var destDir;
switch (spawnDirection){
case "send":
file = spawnCmd[0];
destDir = lava_config.username + "@" + lava_config.serverName + ":" + spawnCmd[1];
break;
case "retrieve":
file = lava_config.username + "@" + lava_config.serverName + ":" + spawnCmd[0];
destDir = spawnCmd[1];
break;
}
spawnCmd = ["-o","StrictHostKeyChecking=no","-i",lava_config.secretAccessKey,file,destDir];
break;
case "local":
spawnExec = lava_config.local_copy;
break;
}
}
break;
}
return spawn(spawnExec, spawnCmd, { encoding : 'utf8' });
}
function createUID()
{
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
function createJobWorkDir(lava_config){
// Get configuration working directory and Generate a UID for the working dir
var jobWorkingDir = path.join(lava_config.working_dir,createUID());
//Create workdir
spawnProcess("[ -d "+jobWorkingDir+" ] || mkdir "+jobWorkingDir,"shell", null, lava_config);
//TODO:handles error
return jobWorkingDir;
}
function jsonifyBhosts(output){
var results={};
// Store node name
results.name = output.shift().trim();
// Look for properties, first line is labels
// Second line is Status
// for (var i = 1; i < output.length; i++) {
var data = output[1].split(/\s+/g);
results.status = nodeStatus[data[0]];
results.cpuf = (data[1] === '-' ? null : data[1]);
results.jlu = (data[2] === '-' ? null : data[2]);
results.max = (data[3] === '-' ? null : data[3]);
results.njobs = (data[4] === '-' ? null : data[4]);
results.run = (data[5] === '-' ? null : data[5]);
results.ssusp = (data[6] === '-' ? null : data[6]);
results.ususp = (data[7] === '-' ? null : data[7]);
results.rsv = (data[8] === '-' ? null : data[8]);
results.dispatch = (data[9] === '-' ? null : data[9]);
// Get the load if node is available
if (results.status !== 'Unavailable'){
//5th line is Total and 6th is Reserved load
//9th and 10th are threshold
var lines = {
"totalLoad" : 4,
"reservedLoad" : 5,
"loadSched" : 8,
"loadStop" : 9
};
var load;
for (var j in lines){
data = output[lines[j]].trim().split(/\s+/g);
load = {};
load.r15s = (data[1] === '-' ? null : data[1]);
load.r1m = (data[2] === '-' ? null : data[2]);
load.r15m = (data[3] === '-' ? null : data[3]);
load.ut = (data[4] === '-' ? null : data[4]);
load.pg = (data[5] === '-' ? null : data[5]);
load.io = (data[6] === '-' ? null : data[6]);
load.ls = (data[7] === '-' ? null : data[7]);
load.it = (data[8] === '-' ? null : data[8]);
load.tmp = (data[9] === '-' ? null : data[9]);
load.swp = (data[10] === '-' ? null : data[10]);
load.mem = (data[11] === '-' ? null : data[11]);
results[j] = load;
}
}
return results;
}
// Return the list of nodes
function lavanodes_js(lava_config, nodeName, callback){
// JobId is optionnal so we test on the number of args
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// first argument is the config file
lava_config = args.shift();
// last argument is the callback function
callback = args.pop();
var remote_cmd = lava_config.binaries_dir;
// Info on a specific node
if (args.length == 1){
nodeName = args.pop();
remote_cmd += cmdDict.node + nodeName;
}else{
remote_cmd += cmdDict.nodes;
}
var output = spawnProcess(remote_cmd,"shell",null,lava_config);
// Transmit the error if any
if (output.stderr){
return callback(new Error(output.stderr));
}
var nodes = [];
//Detect empty values
//output = output.stdout.replace(/=,/g,"=null,");
//Separate each node
output = output.stdout.split(/HOST\s+/g);
//Loop on each node
for (var j = 0; j < output.length; j++) {
if (output[j].length>1){
//Split at lign breaks
output[j] = output[j].trim().split(/\n+/);
nodes.push(jsonifyBhosts(output[j]));
}
}
return callback(null, nodes);
}
module.exports = {
lavanodes_js : lavanodes_js,
};