-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
1225 lines (1083 loc) · 43.3 KB
/
server.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
# The MIT License (MIT) #
# #
# Copyright (c) 2020 Nils Haustein #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal#
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,#
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE#
# SOFTWARE. #
################################################################################
#
# General
# -------
# Name: eeapi - Tape Archive REST API for IBM Spectrum Archive EE
# Author: Nils Haustein - haustein(at)de.ibm.com
# Contributor: Khanh V Ngo - khanhn(at)us.ibm.com
# Contributor: Achim Christ - achim(dot)christ(at)gmail(dot)com
# Version: 0.95
# Dependencies:
# - nodejs version 10
# - IBM Spectrum Archive EE version 1.3.0 and above and IBM Spectrum Scale
# - optional: remote access to IBM Spectrum Archive EE node using ssh with pre-shared keys
# Repository: https://github.ibm.com/nils-haustein/eeapi
#
################################################################################
#
# Description
# ------------
# The Tape archive REST API facilitates controlling migration and recalls of files
# managed by IBM Spectrum Archive Enterprise Edition. It also allows to obtain
# component status for IBM Spectrum Archive system. The Tape archive API is a
# REST API that provides http calls to manage files and obtain status information.
#
# The Tape archive REST API is based on Node JavaScript (Node JS) and can be deployed
# directly on a Spectrum Archive server or on a remote system that uses
# password-less SSH to communicate with the Spectrum Archive server. For defining
# the deployment and communication parameters environmental variables are available.
#
################################################################################
#
# Disclaimer:
# -----------
# This sample is provided 'as is', without any warranty or support.
# It is provided solely for demonstrative purposes - the end user must test and
# modify this sample to suit his or her particular environment. This code is
# provided for your convenience, only - though being tested, there's no
# guarantee that it doesn't seriously break things in your environment! If you
# decide to run it, you do so on your own risk!
#
################################################################################
#
# change history:
# ---------------
# 08/16/19 first implementation
# 08/20/19 implement test function, streamline packages and package.json
# 08/20/19 added strictHostKeyCheck=no as ssh option
# 10/15/20 use node ssh instead of ssh command executed with spawn /usr/bin/ssh
# 10/18/20 change endpoint /filestate to provide file names in request body
# 10/22/20 added new endpoint /runpolicy
# 10/23/20 streamline code and messaging
################################################################################
#
# to be done:
# -----------
# catch wrong endpoint
# add json output to all encpoints (test, migrate, recall)
# put routes in separate files
#
******************************************************************************/
/* define required modules */
const express = require("express");
const https = require("https");
const spawn = require("child_process").spawn
const shortid = require("shortid");
const fs = require("fs");
const bodyParser = require("body-parser");
const sprintf = require("sprintf-js").sprintf;
const morgan = require("morgan");
const process = require("process");
const node_ssh = require('node-ssh')
/* assign environment variables or defaults */
// http port to be used by the API
const httpPort = process.env.EEAPI_PORT || 80;
// when using SSH then the API does not run on eenode
const useSSH = process.env.EEAPI_USESSH || "true";
// ssh Port
const sshPort = process.env.EEAPI_SSHPORT || 22;
// ssh and scp user
const sshUser = process.env.EEAPI_SSHUSER || "root";
// ssh and scp host address or name
const sshHost = process.env.EEAPI_SSHHOST || "localhost";
// name of the key file used for ssh
const sshKey = process.env.EEAPI_KEYFILE || "/root/.ssh/id_rsa";
// directory and file name prefix for recall filelists on EE node
const recallFileSpec = process.env.EEAPI_RECALLFILE || "/tmp/recall-list";
// directory and file name prefix for migrate filelists on EE node
const migrateFileSpec = process.env.EEAPI_MIGRATEFILE || "/tmp/migrate-list";
// use sudo or not
const useSudo = process.env.EEAPI_USESUDO || "false";
// NEW directory and file name prefix for policy file name on EE node
const policyFileSpec = process.env.EEAPI_POLICYFILE || "/tmp/policy-file";
/* define global constants */
// define version
const ver = "0.92"
// common ssh and scp option
const sshOpts ="-p "+sshPort+" -o BatchMode=yes -o StrictHostKeyChecking=no -i "+sshKey+" "+sshUser+"@"+sshHost+""
const scpOpts ="-P "+sshPort+" -o BatchMode=yes -o StrictHostKeyChecking=no -i "+sshKey+""
// define bytes
var bytes = 1024*1024*1024;
// instantiate ssh object
const ssh = new node_ssh()
/*******************************************************************************
MAIN Code
*******************************************************************************/
try {
// instantiate express object
var app = express();
var adminRoutes = express.Router();
app.use(morgan("common"));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
// start web server
app.listen(httpPort)
// print welcome */
console.log("Tape Archive REST API version "+ver+" started on HTTP port "+httpPort);
} catch(err) {
console.log("ERROR: something when wrong in the webserver"+err);
};
/*****************************************************************************************
Endpoint: /about
Prints the available endpoints
Example: curl -X GET http://localhost/about
*****************************************************************************************/
app.get("/about", function(req, res) {
let format = req.query.format || "text";
let count = 0
console.log("DEBUG: Route started: "+req.route.path+"");
res.write("Welcome to the Tape Archive REST API!\n");
app._router.stack.forEach(function(r){
if (r.route && r.route.path && ! r.route.path.includes("*")){
if (format === "json") {
let routePath = r.route.path.split('/');
let name = routePath[1];
if (count == 0) {
res.write("{\"Response\": { \n");
}
count++
res.write(sprintf(" \"%s\" : \"%3s http://%s:%s/%s\",\n", name, Object.keys(r.route.methods).toString().toUpperCase(), sshHost, httpPort, name));
}
else {
res.write(sprintf("%4s http://%s:%s%s\n", Object.keys(r.route.methods).toString().toUpperCase(), sshHost, httpPort, r.route.path));
}
}
});
if (format === "json") res.write(" }\n} \n");
res.end();
console.log("DEBUG: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Endpoint: /test
Performs the following tests and return status
- print environment
- if ssh = true then
test ssh & scp using the environment
check if eeadm is installed
- if ssh = false then test if eeadm is present
Should be used after starting the server to check if the comminication works
Example: curl -X GET http://localhost/test
TODO:
- produce json output
*****************************************************************************************/
app.get("/test", async function(req, res) {
let format = req.query.format || "text";
let output = "";
let errNo = 0;
let result;
let dummyFile = "/tmp/localfile."+shortid.generate()+"";
let destFile = "/tmp/remotefile."+shortid.generate()+"";
const eeadmCmd = "/opt/ibm/ltfsee/bin/eeadm";
console.log("DEBUG: Route started: "+req.route.path+"");
output = ("INFO Environment:\n EEAPI_PORT="+httpPort+"\n EEAPI_USESSH="+useSSH+"\n EEAPI_SSHPORT="+sshPort+"\n EEAPI_SSHKEY="+sshKey+"\n EEAPI_SSHUSER="+sshUser+"\n EEAPI_SSHHOST="+sshHost+"\n EEAPI_USESUDO="+useSudo+"\n EEAPI_RECALLFILE="+recallFileSpec+"\n EEAPI_MIGRATEFILE="+migrateFileSpec+"\n EEAPI_POLICYFILE="+policyFileSpec+"\n");
console.log(output);
if (useSSH == "true") {
// test ssh
try {
console.log("INFO: Checking ssh connection to "+sshUser+"@"+sshHost+" Port "+sshPort+" keyfile "+sshKey+"");
result = await runCommand("exit 0", "text");
if ( result.code === 0 ) {
output = ""+output+"INFO: ssh works\n";
console.log("INFO: ssh works, Message: " +result.msg);
} else {
output = ""+output+"ERROR: ssh failed with return code "+result.code+" message: "+result.msg+"\n";
console.log("ERROR: ssh failed with return code "+result.code+" message: "+result.msg+"");
errNo += 1;
};
} catch(err) {
console.log("ERROR: Checking ssh connection to "+sshUser+"@"+sshHost+" failed. Error message: " +err);
res.status(500).send("ERROR: Checking ssh connection to "+sshUser+"@"+sshHost+" failed. Error message: "+err+" \nStatus: "+output+"");
return;
};
// test scp, create a dummy file and runCopy
try {
fs.writeFileSync(dummyFile, "scp test\n");
console.log("INFO: calling runCopy "+dummyFile+" to "+sshUser+"@"+sshHost+":"+destFile+" Port "+sshPort+" keyfile "+sshKey+"");
result = await runCopy(dummyFile, destFile)
fs.unlink(dummyFile, function(err) {
if (err) {
console.log("WARNING: unlink "+dummyFile+" failed with err.message \n");
}
});
// delete destFile
delResult = await runCommand("/usr/bin/rm -f "+destFile, format);
if ( delResult.code > 0 ) {
console.log("WARNING: unable to delete destinatin file ("+destFile+"). Return code "+delResult.code+".\nMessage: "+delResult.msg+"");
};
if ( result.code === 0 ) {
output = ""+output+"INFO: scp works\n";
console.log("INFO: scp works. Message: " +result.msg);
} else {
output = ""+output+"ERROR: scp failed with return code "+result.code+" message: "+result.msg+"\n";
console.log("ERROR: scp failed with return code "+result.code+" message: "+result.msg+"");
errNo += 1;
};
} catch(err) {
console.log("ERROR: Failed to create and copy file to remote host. Error message: " +err);
res.status(500).send("ERROR: Failed to create and copy file to remote host. Error message: "+err+" \nStatus: "+output+"");
return;
};
// check if eeadm exists
try {
console.log("INFO: checking if "+eeadmCmd+" exists on "+sshUser+"@"+sshHost+" Port "+sshPort+" keyfile "+sshKey+"");
result = await runCommand("/usr/bin/ls /opt/ibm/ltfsee/bin/eeadm", "text");
if ( result.code === 0 ) {
output = ""+output+"INFO: "+eeadmCmd+" exists\n";
console.log("INFO: "+eeadmCmd+" exists. Message: " +result.msg);
} else {
output = ""+output+"ERROR: checking for "+eeadmCmd+" failed, return code "+result.code+" message: "+result.msg+"";
console.log("ERROR: checking for "+eeadmCmd+" failed, return code "+result.code+" message: "+result.msg+"");
errNo += 1;
};
} catch(err) {
console.log("ERROR: check for "+eeadmCmd+" failed. Error message: " +err);
res.status(500).send("ERROR: check for "+eeadmCmd+" failed. Error message: "+err+" \nStatus: "+output+"");
return;
};
// send status and output
if ( errNo == 0 ) {
res.status(200).send(output);
return;
} else {
res.status(500).send(output);
return;
};
} else {
console.log("INFO: checking if "+eeadmCmd+" exists on this server");
try {
fs.accessSync(eeadmCmd);
output = ""+output+"INFO: eeadm exists.\n";
console.log("INFO: eeadm exists.\n");
res.status(200).send(output);
return;
} catch(err) {
output = "ERROR: eeadm does not exists, message: "+err.message+".\n"
console.log(output);
res.status(500).send(output);
return;
};
};
console.log("INFO: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Endpoint: /status
Check Spectrum Archive EE status by running eeadm node list
Returns output of eeadm node list
Example: curl -X GET http://localhost/status
*****************************************************************************************/
app.get("/status", async function(req, res) {
let format = req.query.format || "text";
let result;
const cmd = "/opt/ibm/ltfsee/bin/eeadm node list";
console.log("DEBUG: Route started: "+req.route.path+"");
try {
result = await runCommand(cmd, format);
if ( result.code === 0 ) {
console.log("INFO: command "+cmd+" sucessful.");
res.status(200).send(result.msg+"\n");
return;
} else {
console.log("ERROR: command "+cmd+" failed with return code "+result.code+" ("+result.msg+")");
res.status(500).send("ERROR: command "+cmd+" failed with return code "+result.code+" ("+result.msg+")\n");
return;
};
} catch(err) {
console.log("ERROR: function runCommand("+cmd+") failed , error message: " +err);
res.status(500).send("ERROR: function runCommand("+cmd+") failed , error message: "+err+" \n");
return;
};
console.log("DEBUG: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Endpoint: /info/tape|drive|node|pool|library
Modifier: format = json | text
Runs command: eeadm <comp> list
Provide eeadm info command output
Example: curl -X GET http://localhost/info/<cmd>
*****************************************************************************************/
app.get("/info/:cmd(tape|drive|node|pool|library)", async function(req, res) {
let format = req.query.format || "text";
let comp = req.params.cmd;
let result;
const cmd = "/opt/ibm/ltfsee/bin/eeadm "+comp+" list";
console.log("INFO: Route started: "+req.route.path+"");
// run command
try {
result = await runCommand(cmd, format);
if ( result.code === 0 ) {
console.log("INFO: command "+cmd+" sucessful.");
res.status(200).send(result.msg+"\n");
return;
} else {
console.log("ERROR: command "+cmd+" failed with return code "+result.code+" ("+result.msg+")");
res.status(500).send("ERROR: command "+cmd+" failed with return code "+result.code+" ("+result.msg+")\n");
return;
};
} catch(err) {
console.log("ERROR: function runCommand("+cmd+") failed , error message: " +err);
res.status(500).send("ERROR: function runCommand("+cmd+") failed , error message: "+err+" \n");
return;
};
console.log("DEBUG: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Endpoint: /tasks/:cmd(active|complete)
Modifier: format = json | text
filter = <recall|migrate|..>
Runs command: eeadm task list [-c]
Provide eeadm task list [-c] output
Example: curl -X GET http://localhost/tasks/:cmd(active|all)
*****************************************************************************************/
app.get("/tasks/:cmd(active|all)", async function(req, res) {
let format = req.query.format || "text";
let filter = req.query.filter;
let scope = req.params.cmd;
let opt = "";
let result;
let cmd = "/opt/ibm/ltfsee/bin/eeadm task list";
console.log("INFO: Route started: "+req.route.path+", scope="+cmd+"");
if (scope === "all") {
opt = "-c";
}
if (filter) {
cmd = ""+cmd+" "+opt+" | grep "+filter+"";
}
else {
cmd = ""+cmd+" "+opt+"";
}
// run command
try {
result = await runCommand(cmd, format);
if ( result.code === 0 ) {
console.log("INFO: command "+cmd+" sucessful.");
res.status(200).send(result.msg+"\n");
return;
} else {
console.log("ERROR: command "+cmd+" failed with return code "+result.code+" ("+result.msg+")");
res.status(500).send("ERROR: command "+cmd+" failed with return code "+result.code+" ("+result.msg+")\n");
return;
};
} catch(err) {
console.log("ERROR: function runCommand("+cmd+") failed , error message: " +err);
res.status(500).send("ERROR: function runCommand("+cmd+") failed , error message: "+err+" \n");
return;
};
console.log("INFO: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Endpoint: /taskshow/:taskid
Modifier: format = json | text
Runs command: eeadm task show task-ID
Provide eeadm task show task-id
Example: curl -X GET http://localhost/taskshow/:task-id
*****************************************************************************************/
app.get("/taskshow/:taskid", async function(req, res) {
let format = req.query.format || "text";
let taskid = req.params.taskid;
let execCmd = "";
let result;
const cmd = "/opt/ibm/ltfsee/bin/eeadm task show ";
console.log("INFO: Route started: "+req.route.path+" task-id="+taskid+"");
if (!isNaN(taskid)) {
try {
execCmd = cmd+taskid;
result = await runCommand(execCmd, format);
if ( result.code === 0 ) {
console.log("INFO: command "+execCmd+" sucessful.");
res.status(200).send(result.msg+"\n");
return;
} else {
console.log("ERROR: command "+execCmd+" failed with return code "+result.code+" ("+result.msg+")");
res.status(500).send("ERROR: command "+execCmd+" failed with return code "+result.code+" ("+result.msg+")\n");
return;
}
} catch(err) {
console.log("ERROR: function runCommand("+execCmd+") failed , error message: " +err);
res.status(500).send("ERROR: function runCommand("+execCmd+") failed , error message: "+err+" \n");
return;
};
}
else {
/* return error */
console.log("Error: Invalid task-ID ("+taskid+"), must be a integer number");
res.status(412).send("Error: Invalid task-ID, must be a integer number\n");
return;
}
console.log("INFO: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Endpoint: /filestate
Modifier: format = json | text
Body: list of fully qualified path and file names
Processing:
Runs command: eeadm file state on given file
Provide eeadm file state output
Example: curl -X GET http://localhost/filestate -d "<list of file names>"
TODO:
- accept wildcards
- produce json
*****************************************************************************************/
// the trick here is to run function(req,res) as async which allows us to wait on runCommand
app.get("/filestate", async function(req, res) {
let format = req.query.format || "text";
let cmd = "/opt/ibm/ltfsee/bin/eeadm file state ";
let output = "";
let file = "";
let execCmd = "";
let num = 0;
let fileList;
let result;
console.log("INFO: Route started: "+req.route.path+"");
// assign request body to object array and try to trim and split the file list now, if it is empty, we will catch the error,
// expected output [ 'line1', 'line2', '', 'line3' ]
try {
fileList = Object.keys(req.body)[0];
fileList=fileList.trim().split("\n");
} catch (err) {
console.log("ERROR: request body cannot be processed, message: "+err);
res.status(412).send("ERROR: request body cannot be processed. You have to provide one file name per line in the request body.\n");
return;
};
for (let file of Object.values(fileList)) {
try {
file = file.trimLeft();
} catch (err) {
console.log ("WARNING: file name ("+file+") is not valid. Message: "+err);
continue;
};
if ( file === "" || file.includes(";") ) {
console.log("WARNING: file name ("+file+") is empty or not valid, continuing with next file.");
output += "Name: "+file+"\nState: invalid_file_name\n\n";
continue;
} else {
// await runCommand to get it serialized.
try {
execCmd = cmd+"'"+file+"'";
console.log("INFO: runing command: "+execCmd+"");
result = await runCommand(execCmd, "");
if ( result.code === 0 ) {
output += result.msg+"\n\n";
num += 1
} else {
output = output+"ERROR: command "+execCmd+" failed with return code "+result.code+" ("+result.msg+")\n";
console.log("ERROR: command "+execCmd+" failed with return code "+result.code+" ("+result.msg+")");
};
} catch(err) {
console.log("ERROR: async function runCommand("+execCmd+") failed , error message: " +err);
res.status(500).send("ERROR: async function runCommand("+execCmd+") failed , error message: "+err+" \n");
};
};
};
if ( output === "" ) {
output = "ERROR: No file names provided in request body.\n";
console.log(output);
res.status(500).send(output);
return;
} else {
if ( num == 0 ) {
output = output+"ERROR: Unable to determine the status of any given files.\n";
console.log(output);
res.status(500).send(output);
return;
} else {
res.status(200).send(output);
};
};
console.log("INFO: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Endpoint: /recall
Modifier: format = json | text
Body: list of fully qualified file names to be recalled.
Processing:
Obtains file list from request.body
Copies filelist to EE host
Runs command: eeadm recall filelist
Returns result of the operation
Example: curl -X PUT http://localhost/recall -d "filelist"
TODO:
- produce json
*****************************************************************************************/
app.put("/recall", async function(req, res) {
let format = req.query.format || "text";
let fileList;
let execCmd = "";
let result;
let delResult;
const cmd = "/opt/ibm/ltfsee/bin/eeadm recall ";
const tmpFile = "/tmp/ee-restapi-reclist."+shortid.generate();
// cannot be const because it may get tmpFile assigned
let destFile = ""+recallFileSpec+"."+shortid.generate()+"";
console.log("INFO: Route started: "+req.route.path+"");
// console.log("DEBUG: request body sting: "+fileList);
// write fileList in file tmpFile
try {
fileList = Object.keys(req.body)[0];
fileList = fileList.trim();
fs.writeFileSync(tmpFile, fileList);
} catch (err) {
console.log("ERROR: creating recall file-list from request body. Message: "+err);
res.status(500).send("ERROR: creating recall-file list from request body. Message: "+err);
return;
};
if ( useSSH == "true" ) {
// send tmpFile to eenode as file destFile, return 500 (internal server error) if it fails
try {
console.log("INFO: copying file "+tmpFile+" to file "+destFile+"");
result = await runCopy(tmpFile, destFile)
fs.unlink(tmpFile, function(err) {
if (err) {
console.log("WARNING: unlink "+tmpFile+" failed with err.message \n");
}
});
if ( result.code === 0 ) {
console.log("INFO: Copy done.");
} else {
console.log("ERROR: runCopy failed with return code "+result.code+" message: "+result.msg+"");
res.status(500).send("ERROR: runCopy failed with return code "+result.code+" message: "+result.msg+"\n");
return;
};
} catch(err) {
console.log("ERROR: runCopy failed, error message: " +err);
res.status(500).send("ERROR: runcopy1 failed, error message: "+err+"\n");
return;
};
} else {
destFile = tmpFile;
};
// run recall
try {
execCmd = cmd+destFile;
console.log("INFO: running command "+execCmd+"");
result = await runCommand(execCmd, format);
// delete destFile
delResult = await runCommand("/usr/bin/rm -f "+destFile, format);
if ( delResult.code > 0 ) {
console.log("WARNING: unable to delete recall list file ("+destFile+"). Return code "+delResult.code+".\nMessage: "+delResult.msg+"");
};
if ( result.code === 0 ) {
console.log("INFO: command "+execCmd+" successful.");
res.status(200).send(result.msg+"\n");
return;
} else {
console.log("ERROR: command "+execCmd+" failed with return code "+result.code+" ("+result.msg+")");
res.status(500).send("ERROR: command "+execCmd+" failed with return code "+result.code+" ("+result.msg+")\n");
return;
};
} catch(err) {
console.log("ERROR: function runCommand("+execCmd+") failed , error message: " +err);
res.status(500).send("ERROR: function runCommand("+execCmd+") failed , error message: "+err+" \n");
return;
};
console.log("INFO: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Endpoint: /migrate
Modifier: format = json | text
pool = poolname (required)
pool1-pool3 = poolname (obtional)
Body: list of fully qualified file names to be recalled.
Processing:
Obtains file list from request.body and pool name from modifier ?pool1=name?po
Copies filelist to EE host
Runs command: eeadm migrate filelist -p pool
Returns result of the operation
Example: curl -X PUT http://localhost/migrate?pool1=pool1@lib1&pool2=pool2@lib1 -d "filelist"
TODO:
- produce json
*****************************************************************************************/
app.put("/migrate", async function(req, res) {
// get format from URL modifier ?format=, default is text
let format = req.query.format || "text";
// extract the file names from req.body key field 0
let fileList;
let execCmd = "";
let result;
let delResult;
const tmpFile = "/tmp/ee-restapi-miglist."+shortid.generate();
const cmd = "/opt/ibm/ltfsee/bin/eeadm migrate ";
// cannot be const because it may get tmpFile assigned.
let destFile = ""+migrateFileSpec+"."+shortid.generate()+"";
// get pool name from URL modiers
let pool = [];
pool[0] = req.query.pool || undefined;
pool[1] = req.query.pool1 || undefined;
pool[2] = req.query.pool2 || undefined;
pool[3] = req.query.pool3 || undefined;
let pools = "";
let poolCount = 0;
// build the pools string
for (let i=0; i<=3; i++) {
if (pool[i]) {
if (pools === "") {
pools = pool[i];
} else {
pools = ""+pools+","+pool[i]+"";
}
poolCount++;
}
}
console.log("INFO: Route started: "+req.route.path+", pools="+pools+"");
// bail out if we do not have a pool
if (pools === "") {
console.log("ERROR: no migration destination pool specified.");
res.status(412).send("ERROR: no migration destination pool specified, use modifier ?pool=pool \n");
// return to prevent continuing this function
return;
}
// bail out if pool count is greate than 3
if (poolCount > 3) {
console.log("ERROR: Number of pools ("+poolCount+") exceeds the maximum (3).");
res.status(412).send("ERROR: Number of pools ("+poolCount+") exceeds the maximum (3).\n");
// return to prevent continuing this function
return;
}
// write fileList in file tmpFile
try {
fileList = Object.keys(req.body)[0];
fileList = fileList.trim();
fs.writeFileSync(tmpFile, fileList);
} catch (err) {
console.log("ERROR: creating migrate file-list from request body. Message: "+err.message+"");
res.status(500).send("ERROR: creating migrate file-list from request body. Message: "+err.message+"\n");
return;
};
if ( useSSH == "true" ) {
// send tmpFile to eenode as file destFile, return 500 (internal server error) if it fails
try {
console.log("INFO: copying file "+tmpFile+" to file "+destFile+"");
result = await runCopy(tmpFile, destFile)
fs.unlink(tmpFile, function(err) {
if (err) {
console.log("WARNING: unlink "+tmpFile+" failed with err.message \n");
}
});
if ( result.code === 0 ) {
console.log("INFO: Copy done.");
} else {
console.log("ERROR: runCopy failed with return code "+result.code+" message: "+result.msg+"");
res.status(500).send("ERROR: runCopy failed with return code "+result.code+" message: "+result.msg+"\n");
return;
};
} catch(err) {
console.log("ERROR: runCopy failed, error message: " +err);
res.status(500).send("ERROR: runcopy1 failed, error message: "+err+"\n");
return;
};
} else {
destFile = tmpFile;
}
// run migrate
try {
execCmd = cmd+destFile+" -p "+pools+"";
console.log("DEBUG: running command "+execCmd+"");
result = await runCommand(execCmd, format);
// delete destFile
delResult = await runCommand("/usr/bin/rm -f "+destFile, format);
if ( delResult.code > 0 ) {
console.log("WARNING: unable to delete migrate list file ("+destFile+"). Return code "+delResult.code+".\nMessage: "+delResult.msg+"");
};
if ( result.code === 0 ) {
console.log("INFO: command "+execCmd+" sucessful.");
res.status(200).send(result.msg+"\n");
return;
} else {
console.log("ERROR: command "+execCmd+" failed with return code "+result.code+" ("+result.msg+")");
res.status(500).send("ERROR: command "+execCmd+" failed with return code "+result.code+" ("+result.msg+")\n");
return;
};
} catch(err) {
console.log("ERROR: function runCommand("+execCmd+") failed , error message: " +err);
res.status(500).send("ERROR: function runCommand("+execCmd+") failed , error message: "+err+" \n");
return;
};
console.log("INFO: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Endpoint: /runpolicy
Modifier: format = json | text
Body: Path, options and policy to run in the following format:
Options: options (must all be in one line)
Path: path for the policy run (must all be in one line)
Policy: policy rules (can cross over multiple lines)
Note, all lines which do not have any of the token above are interpreted as policy
Processing:
Runs command: mmapplypolicy path -P policyfile [opts]
Provide eeadm file state output
Example: curl -X PUT http://localhost/runpolicy -d "<Path: scope-for-policy \nOptions: [opts] \nPolicy: \n"
TODO:
- produce json
*****************************************************************************************/
// the trick here is to run function(req,res) as async which allows us to wait on runCommand
app.put("/runpolicy", async function(req, res) {
let format = req.query.format || "text";
let reqBody;
let path = "";
let opts = "";
let policy = "";
let execCmd = "";
let result;
let delResult;
const cmd = "/usr/lpp/mmfs/bin/mmapplypolicy ";
const tmpFile = "/tmp/ee-restapi-polfile."+shortid.generate();
// cannot be const because it may get tmpFile assigned.
let destFile = ""+policyFileSpec+"."+shortid.generate()+"";
console.log("INFO: Route started: "+req.route.path+"");
// assign request body to object array and try to trim and split the file list now, if it is empty, we will catch the error,
// expected output [ 'line1', 'line2', '', 'line3' ]
try {
reqBody = Object.keys(req.body)[0];
reqBody=reqBody.trim().split("\n");
// console.log("DEBUG: reqBody:");
// console.log(reqBody);
} catch (err) {
console.log("ERROR: request body cannot be processed, message: "+err);
res.status(412).send("ERROR: request body cannot be processed. You have to provide one file name per line in the request body.\n");
return;
};
for (let line of Object.values(reqBody)) {
try {
line = line.trimLeft();
} catch (err) {
console.log ("WARNING: Line ("+line+") is not valid. Message: "+err);
continue;
};
// catch empty lines and escapes
if ( line === "" || line.includes(";")) {
console.log("WARNING: Line ("+line+") is empty or not valid, continuing with next line.");
continue;
}
if ( line.includes("Path:") ) {
path = line.split(":")[1];
path = path.trimLeft();
continue;
};
if ( line.includes("Options:") ) {
opts += line.split(":")[1];
opts = opts.trimLeft();
continue;
};
if ( line.includes("Policy:") ) {
policy = line.split(":")[1];
policy = policy.trimLeft();
if ( policy === "" ) {
policy = "";
} else {
policy += "\n";
}
} else {
policy += line+"\n"
};
};
if ( path === "" || opts == "invalid" ) {
console.log("ERROR Path ("+path+") or Options ("+opts+") not specified or not valid.\n");
res.status(500).send("ERROR Path or Options not specified or not valid.\n");
return;
} else {
if ( policy == "" ) {
console.log("ERROR Policy not specified or invalid.\n");
res.status(500).send("ERROR Policy not specified.\n");
return;
} else {
// write policy in file tmpFile
console.log("DeBUG: policy:");
console.log(policy);
try {
await fs.writeFileSync(tmpFile, policy);
} catch (err) {
console.log("ERROR: creating policy file. Message: "+err);
res.status(500).send("ERROR: creating policy file. Message: "+err+"\n");
return;
};
if ( useSSH == "true" ) {
// send tmpFile to eenode as file destFile, return 500 (internal server error) if it fails
try {
console.log("INFO: copying file "+tmpFile+" to file "+destFile+"");
result = await runCopy(tmpFile, destFile)
fs.unlink(tmpFile, function(err) {
if (err) {
console.log("WARNING: unlink "+tmpFile+" failed: "+err);
}
});
if ( result.code === 0 ) {
console.log("INFO: Copy done.");
} else {
console.log("ERROR: runCopy failed with return code "+result.code+" message: "+result.msg+"");
res.status(500).send("ERROR: runCopy failed with return code "+result.code+" message: "+result.msg+"");
return;
};
} catch (err) {
console.log("ERROR: runCopy failed, error message: " +err);
res.status(500).send("ERROR: runcopy failed, error message: "+err+"\n");
return;
};
} else {
destFile = tmpFile;
};
// run policy
try {
execCmd = ""+cmd+""+path+" -P "+destFile+" "+opts+"";
console.log("INFO: running command "+execCmd+"");
result = await runCommand(execCmd, format);
// delete destFile
delResult = await runCommand("/usr/bin/rm -f "+destFile, format);
if ( delResult.code > 0 ) {
console.log("WARNING: unable to delete policy file ("+destFile+"). Return code "+delResult.code+".\nMessage: "+delResult.msg+"");
};
if ( result.code === 0 ) {
console.log("INFO: command "+execCmd+" sucessful.");
res.status(200).send(result.msg+"\n");
return;
} else {
console.log("ERROR: command "+execCmd+" failed with return code "+result.code+".\nMessage: "+result.msg+"");
res.status(500).send("ERROR: command "+execCmd+" failed with return code "+result.code+".\nMessage: "+result.msg+"\n");
return;
};
} catch(err) {
console.log("ERROR: function runCommand("+execCmd+") failed , error message: " +err);
res.status(500).send("ERROR: function runCommand("+execCmd+") failed , error message: "+err+" \n");
return;
};
};
};
console.log("INFO: Route finished: "+req.route.path+"");
});
/*****************************************************************************************
Default Endpoint: (catch wrong GET endpoints)
Processing:
send default error message
Example: curl -X get http://localhost/something
TODO:
- produce json
*****************************************************************************************/
app.get('*', function(req, res, next) {
const output = "ERROR: 404 - Endpoint not found. For available endpoints see: GET http://localhost/about\n";
console.log(output);
res.status(404).send(output);
});
/*****************************************************************************************