forked from boid-com/boidcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·540 lines (474 loc) · 16.2 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
var cmd = require('node-cmd')
var convert = require('xml-js')
var fs = require('fs')
var client = require("./api.js")
const minimist = require('minimist')
const ora = require('ora')
const spinner = ora()
const readline = require('readline')
const { spawn } = require('child_process')
const exec = require('child_process').exec
var form = {
email: '',
password: '',
invitedById: null,
device: null
}
var projectObject = {
deviceId: '',
wcgid: ''
}
module.exports = () => {
const args = minimist(process.argv.slice(2))
var boidcmd = args._[0]
var value = args._[1]
if(args.version || args.v) {
boidcmd = 'version'
}
if(args.help || args.h || args._[0] == undefined) {
boidcmd = 'help'
}
switch (boidcmd) {
case 'setCPU':
setCPU(value)
break
case 'devices':
checkDevices()
break
case 'workunits':
getWorkUnits()
break
case 'setup':
setupBoid()
break
case 'install':
installBoid()
break
case 'run':
runBoid()
break
case 'quit':
quitBoid()
break
case 'resume':
resume()
break
case 'suspend':
suspend()
break
case 'status':
statusBoid()
break
case 'help':
require('./help')
break
case 'version':
require('./version')
break
default:
console.error(`"${boidcmd}" is not a valid command!`)
process.exit(1)
break
}
}
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function isProcessRunning(processName){
const cmd = `ps -A`
return new Promise(function(resolve,reject){
exec(cmd, function(err,stdout,stderr) {
//console.log(stdout.indexOf('boinc'))
resolve(stdout.toLowerCase().indexOf(processName.toLowerCase()) > -1)
})
})
}
async function verifyBoinc(){
let status = await isProcessRunning('boinc')
if(!status){
console.log("boinc process is not running, please run systemctl start boinc-client")
process.exit(-1)
}
}
async function setupBoid(){
await verifyBoinc()
var form = {}
rl.stdoutMuted = false
rl.question('Boid Account Email:', (email) => {
form.email = email
rl.question('Boid Account Password: ', (password) => {
form.password = password
client.send(form,client.endPoint.authenticateUser,function(response){
response = JSON.parse(response)
if (response.invalid){
console.log()
console.log(response.invalid)
return setupBoid()
}
client.setUserData(response)
rl.close()
setupBoinc()
})
})
rl.stdoutMuted = true
})
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted)
rl.output.write("*")
else
rl.output.write(stringToWrite)
}
}
async function checkDevices(value){
var form = {}
rl.stdoutMuted = false
rl.question('Boid Account Email:', (email) => {
form.email = email
rl.question('Boid Account Password: ', (password) => {
form.password = password
client.send(form,client.endPoint.authenticateUser,function(response){
response = JSON.parse(response)
if (response.invalid){
console.log()
console.log(response.invalid)
return checkDevices(value)
}
client.setUserData(response)
rl.close()
client.send({"id":response.id},client.endPoint.getUser,function(obj){
//console.log(obj)
var json = JSON.parse(obj)
if(value == "csv"){
console.log("\nUsername: ", json.username )
console.log("User Boid Power: ", json.dPower)
console.log("Registered devices (CSV format):")
console.log("ID,STATUS,TYPE,POWER,PENDING,NAME")
for(count in json.devices){
console.log(json.devices[count].id+","+json.devices[count].status+","+json.devices[count].type+","+json.devices[count].power+","+json.devices[count].pending+","+json.devices[count].name)
}
}
else {
console.log(json)
}
})
})
})
rl.stdoutMuted = true
// rl.history = rl.history.slice(1)
})
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted)
rl.output.write("*")
else
rl.output.write(stringToWrite)
}
}
async function getWorkUnits(){
var form = {}
rl.stdoutMuted = false
rl.question('Boid Account Email:', (email) => {
form.email = email
rl.question('Boid Account Password: ', (password) => {
form.password = password
client.send(form,client.endPoint.authenticateUser,function(response){
response = JSON.parse(response)
if (response.invalid){
console.log()
console.log(response.invalid)
return getWorkUnits()
}
client.setUserData(response)
rl.close()
// Find out the local hostId (world grid community host id)
fs.readFile( '/var/lib/boinc-client/client_state.xml', function(err, data) {
var result1 = convert.xml2json(data, {compact: true, spaces: 4});
var object = JSON.parse(result1);
var project = object.client_state.project
if (project == undefined){
console.error("ERROR: you have no project attached, the boinc daemon is likely not installed or running.")
process.exit(-1)
}
if (project.length > 1){
console.error("ERROR: you have more than 1 project already configured, boidcmd requires you to only be attached to www.worldcommunitygrid.org")
process.exit(-1)
}
var wcgid = project.hostid._text
console.log("\nquerying boid for workunits for hostid: "+wcgid)
client.send({"wcgid": wcgid, "valid": true},client.endPoint.getWorkUnits,function(obj){
//console.log(obj)
var json = JSON.parse(obj)
if (json.length > 0){
console.log(json)
}
else console.log("this device has not submitted any workunits recently.")
})
})
})
})
rl.stdoutMuted = true
// rl.history = rl.history.slice(1)
})
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted)
rl.output.write("*")
else
rl.output.write(stringToWrite)
}
}
function runBoid(){
const subprocess = spawn('boinc',['--dir', '/var/lib/boinc-client/', '--daemon', '--allow_remote_gui_rpc'], {
detached: true,
stdio: 'ignore'
});
subprocess.unref();
}
function quitBoid(){
const subprocess = spawn('boinccmd',['--quit'], {
detached: true,
stdio: 'ignore'
});
subprocess.unref();
console.log("boinc stopped..")
process.exit(0)
}
async function installBoid() {
await verifyBoinc()
cmd.get(
`
boinccmd --project_attach http://www.worldcommunitygrid.org/ 1061556_a0c611b081f8692b7ef0c11d39e6105c
`,
function(err, data, stderr){
if (!err) {
console.log("\nwaiting for project information")
spinner.start()
setTimeout(projectCheck,3000)
} else {
console.log('error', err)
}
}
);
}
function projectCheck(){
fs.readFile( '/var/lib/boinc-client/client_state.xml', function(err, data) {
var result1 = convert.xml2json(data, {compact: true, spaces: 4});
var object = JSON.parse(result1);
var hostInfo = object.client_state.host_info
var project = object.client_state.project
var name = hostInfo.domain_name._text
var cpid = hostInfo.host_cpid._text
var threads = hostInfo.p_ncpus._text
var model = hostInfo.p_model._text
if (project == undefined){
console.error("ERROR: you have no project attached, the boinc daemon is likely not installed or running.")
process.exit(-1)
}
if (project.length > 1){
console.error("ERROR: you have more than 1 project already configured, boidcmd requires you to only be attached to www.worldcommunitygrid.org")
process.exit(-1)
}
var wcgid = project.hostid._text
if(wcgid == "0"){
setTimeout(projectCheck,3000)
console.log("Timeout while checking project status.")
}else{
spinner.stop()
console.log("Success!")
}
});
}
function valBetween(v, min, max) {
return Math.min(max, Math.max(min, v))
}
async function setCPU(value){
await verifyBoinc()
fs.readFile( '/var/lib/boinc-client/global_prefs_override.xml', function(err, data) {
var result1 = convert.xml2json(data, {compact: true, spaces: 4});
var object = JSON.parse(result1);
object.global_preferences.cpu_usage_limit = valBetween(value,0,100)
var options = {compact:true, ignoreComment:true, spaces:4};
var xmlResult = convert.json2xml(object,options);
console.log(object)
console.log(xmlResult)
fs.writeFile('/var/lib/boinc-client/global_prefs_override.xml',xmlResult,function(err){
if (err) throw err;
readGlobalPrefsOverride()
})
});
}
async function readGlobalPrefsOverride(){
await verifyBoinc()
isProcessRunning("boinc", (status) => {
if(!status){
console.log("boinc process is not running, please run systemctl start boinc-client")
process.exit(-1)
}
})
spinner.start()
cmd.get(
`
boinccmd --read_global_prefs_override
`,
function(err, data, stderr){
if (!err) {
console.log("Sucessfully set cpu limit")
spinner.stop()
console.log(data)
console.log(stderr)
process.exit(0);
} else {
console.log('error', err)
}
}
);
}
async function setupBoinc(){
await verifyBoinc()
cmd.get(
`
boinccmd --project_attach http://www.worldcommunitygrid.org/ 1061556_a0c611b081f8692b7ef0c11d39e6105c
`,
function(err, data, stderr){
if (!err) {
console.log("\nwaiting for project information")
spinner.start()
setTimeout(readFiles,3000)
} else {
// if (error.search(''))
console.log('error', err)
setTimeout(readFiles,3000)
}
}
);
}
async function resume() {
await verifyBoinc()
cmd.get(
`
boinccmd --project http://www.worldcommunitygrid.org/ resume
`,
function(err, data, stderr){
if (!err) {
console.log(data)
console.log(stderr)
process.exit(0);
} else {
console.log('error', err)
}
}
);
}
async function suspend() {
await verifyBoinc()
cmd.get(
`
boinccmd --project http://www.worldcommunitygrid.org/ suspend
`,
function(err, data, stderr){
if (!err) {
console.log(data)
console.log(stderr)
process.exit(0);
} else {
console.log('error', err)
}
}
);
}
async function statusBoid() {
await verifyBoinc()
var pass = "n/a"
fs.readFile('/var/lib/boinc-client/gui_rpc_auth.cfg', function(err, data) {
console.log(data)
pass = data
console.log(err)
})
console.log(pass)
cmd.get(
`
boinccmd --get_simple_gui_info
`,
function(err, data, stderr){
if (!err) {
console.log(data)
console.log(stderr)
process.exit(0);
} else {
console.log('error', err)
}
}
);
}
function readFiles(callback) {
fs.readFile( '/var/lib/boinc-client/client_state.xml', function(err, data) {
var result1 = convert.xml2json(data, {compact: true, spaces: 4});
var object = JSON.parse(result1);
var hostInfo = object.client_state.host_info
var name = hostInfo.domain_name._text
var cpid = hostInfo.host_cpid._text
var threads = hostInfo.p_ncpus._text
var model = hostInfo.p_model._text
var project = object.client_state.project
if (project == undefined){
console.error("ERROR: you have no project attached, the boinc daemon is likely not installed or running.")
process.exit(-1)
}
if (project.length > 1){
console.error("ERROR: you have more than 1 project already configured, boidcmd requires you to only be attached to www.worldcommunitygrid.org")
process.exit(-1)
}
var wcgid = project.hostid._text
if(wcgid == "0"){
setTimeout(readFiles,3000)
}else{
spinner.stop()
projectObject.wcgid = wcgid
var device = {
cpid:cpid,
//cpid:makeid(),
name:name,
type:"LINUX"
}
console.log("Adding device to boid account")
client.send(device,client.endPoint.addDevice,function(obj){
var json = JSON.parse(obj)
if(json.error != undefined) {
console.log("error adding Device")
}else{
console.log("Added device to boid account")
var deviceId = json.id
client.send({"id":deviceId},client.endPoint.getDevice,function(obj){
var json = JSON.parse(obj)
console.log(json)
projectObject.deviceId = json.id
client.send(projectObject,client.endPoint.updateDevice,function(obj){
var json = JSON.parse(obj)
console.log(json)
console.log("Setting up project")
if (json.error == undefined) {
//console.log(json.error)
}else{
client.send({"id":projectObject.deviceId},client.endPoint.getDevice,function(obj){
var json = JSON.parse(obj)
if(json.wcgid != null){
if(projectObject.wcgid == json.wcgid){
console.log("Project setup Success!")
}else {
console.log(" Device is using a different wcgid wcgid:"+json.wcgid)
}
}else{
//some other issue caused a problem
}
console.log('Setup complete. Run "boidcmd status" to view project status')
process.exit(0);
})
}
})
})
}
})
}
});
}
// sudo aptitude install boinc-client
// sudo aptitude purge boinc-client