This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionCommandExecutor.java
858 lines (737 loc) · 32.9 KB
/
FunctionCommandExecutor.java
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
package cmd.functionality_commands;
import cmd.CommandExecutor;
import cmd.docker_daemon_utility.DockerException;
import cmd.docker_daemon_utility.DockerExecutor;
import cmd.StreamGobbler;
import cmd.functionality_commands.output_parsing.URLFinder;
import cmd.functionality_commands.output_parsing.ReplyCollector;
import databases.mysql.CloudEntityData;
import databases.mysql.daos.CompositionsRepositoryDAO;
import databases.mysql.daos.FunctionsRepositoryDAO;
import utility.PropertiesManager;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Utility for CLI serverless function related command execution
*/
@SuppressWarnings({"DuplicatedCode", "SameParameterValue"})
public class FunctionCommandExecutor extends CommandExecutor {
/**
* Deploys a composition handler to Google Cloud Functions and persists on DB
* @param functionName name of the handler
* @param runtime runtime of the handler
* @param entryPoint handler entry point path
* @param timeout handler timeout in seconds
* @param memory handler memory amount in megabytes
* @param region handler region of deployment
* @param directoryAbsolutePath path of the directory containing handler implementation
*/
public static void deployGoogleCloudHandlerFunction(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String region,
String directoryAbsolutePath) {
deployOnGoogleCloudFunctions(functionName, runtime, entryPoint, timeout, memory, region,
directoryAbsolutePath, 0);
}
/**
* Deploys a function to Google Cloud Functions and persists on DB
* @param functionName name of the function
* @param runtime runtime of the function
* @param entryPoint function entry point path
* @param timeout function timeout in seconds
* @param memory function memory amount in megabytes
* @param region function region of deployment
* @param directoryAbsolutePath path of the directory containing function implementation
*/
public static void deployOnGoogleCloudFunction(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String region,
String directoryAbsolutePath) {
deployOnGoogleCloudFunctions(functionName, runtime, entryPoint, timeout, memory, region,
directoryAbsolutePath, 1);
}
/**
* Deploys a function to Google Cloud Functions without persistence on DB
* @param functionName name of the function
* @param runtime runtime of the function
* @param entryPoint function entry point path
* @param timeout function timeout in seconds
* @param memory function memory amount in megabytes
* @param region function region of deployment
* @param directoryAbsolutePath path of the directory containing function implementation
* @return function URL
*/
protected static String deployOnGoogleCloudCompositionFunction(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String region,
String directoryAbsolutePath) {
return deployOnGoogleCloudFunctions(functionName, runtime, entryPoint, timeout, memory, region,
directoryAbsolutePath, 2);
}
/**
* Deploys a generic function to Google Cloud Functions
* @param functionName name of the function
* @param runtime runtime of the function
* @param entryPoint function entry point path
* @param timeout function timeout in seconds
* @param memory function memory amount in megabytes
* @param region function region of deployment
* @param directoryAbsolutePath path of the directory containing function implementation
* @param functionality 0 for handler deployment and persistence, 1 for function deployment and persistence,
* 2 for deployment only
* @return function URL
*/
private static String deployOnGoogleCloudFunctions(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String region,
String directoryAbsolutePath, Integer functionality) {
assert functionality == 0 || functionality == 1 || functionality == 2;
try {
functionName = GoogleCommandUtility.applyRuntimeId(functionName, runtime);
DockerExecutor.checkDocker();
} catch (IllegalNameException | DockerException e) {
System.err.println("Could not deploy function '" + functionName + "' to Google Cloud Functions: " +
e.getMessage());
return "";
}
if (functionality != 2) {
System.out.println("\n" + "\u001B[33m" +
"Deploying \"" + functionName + "\" to Google Cloud Platform..." +
"\u001B[0m" + "\n");
}
// build command
String cmd = GoogleCommandUtility.buildGoogleCloudFunctionsDeployCommand(functionName, runtime, entryPoint,
timeout, memory, region, directoryAbsolutePath);
// start executors
ExecutorService executorServiceOut = Executors.newSingleThreadExecutor();
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
try {
// start process execution
Process process = buildCommand(cmd).start();
// create, execute and submit output gobblers
URLFinder urlFinder = new URLFinder();
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(),
urlFinder::findGoogleCloudFunctionsUrl);
// google deploying progresses are on the error stream
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), System.out::println);
executorServiceOut.submit(outputGobbler);
executorServiceErr.submit(errorGobbler);
// wait for completion and free environment
if (process.waitFor() != 0) {
System.err.println("Could not deploy function '" + functionName + "'");
process.destroy();
return "";
}
String url = urlFinder.getResult();
if (functionality != 2) {
System.out.println("\u001B[32m" + "Deployed function to: " + url + "\u001B[0m");
}
process.destroy();
switch (functionality) {
case 0:
// handler
CompositionsRepositoryDAO.persistGoogleHandler(functionName, url, region);
break;
case 1:
// function to persist
FunctionsRepositoryDAO.persistGoogle(functionName, url, region);
break;
default:
break;
}
return url;
} catch (InterruptedException | IOException e) {
System.err.println("Could not deploy function '" + functionName + "': " + e.getMessage());
return "";
} finally {
executorServiceOut.shutdown();
executorServiceErr.shutdown();
}
}
/**
* Deploys a composition handler to Amazon Lambda, creates the API Gateway associated API and persists on DB
* @param functionName name of the handler
* @param runtime runtime of the handler
* @param entryPoint handler entry point path
* @param timeout handler timeout in seconds
* @param memory handler memory amount in megabytes
* @param region handler region of deployment
* @param zipFolderAbsolutePath path of the folder containing handler zipped implementation
* @param zipFileName file name of the zipped implementation
*/
protected static void deployAmazonRESTHandlerFunction(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String region,
String zipFolderAbsolutePath, String zipFileName) {
deployOnAmazonRESTFunctions(functionName, runtime, entryPoint, timeout, memory, region, zipFolderAbsolutePath,
zipFileName, true);
}
/**
* Deploys a function to Amazon Lambda, creates the API Gateway associated API and persists on DB
* @param functionName name of the function
* @param runtime runtime of the function
* @param entryPoint function entry point path
* @param timeout function timeout in seconds
* @param memory function memory amount in megabytes
* @param region function region of deployment
* @param zipFolderAbsolutePath path of the folder containing function zipped implementation
* @param zipFileName file name of the zipped implementation
*/
public static void deployOnAmazonRESTFunction(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String region,
String zipFolderAbsolutePath, String zipFileName) {
deployOnAmazonRESTFunctions(functionName, runtime, entryPoint, timeout, memory, region, zipFolderAbsolutePath,
zipFileName, false);
}
/**
* Deploys a generic function to Amazon Lambda
* @param functionName name of the function
* @param runtime runtime of the function
* @param entryPoint function entry point path
* @param timeout function timeout in seconds
* @param memory function memory amount in megabytes
* @param region function region of deployment
* @param zipFolderAbsolutePath path of the folder containing function zipped implementation
* @param zipFileName file name of the zipped implementation
* @return ARN of the deployed function
* @throws IOException exception related to directory position or process execution
* @throws InterruptedException exception related to Thread management
*/
protected static String deployOnAmazonLambdaFunctions(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String region,
String zipFolderAbsolutePath, String zipFileName)
throws IOException, InterruptedException {
Process process;
StreamGobbler outputGobbler;
StreamGobbler errorGobbler;
ExecutorService executorServiceOut = Executors.newSingleThreadExecutor();
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
// deploy function
String cmdDeploy = AmazonCommandUtility.buildLambdaFunctionDeployCommand(functionName, runtime, entryPoint,
timeout, memory, region, zipFolderAbsolutePath, zipFileName);
process = buildCommand(cmdDeploy).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not deploy '" + functionName + "' on AWS Lambda");
executorServiceOut.shutdown();
executorServiceErr.shutdown();
process.destroy();
return "";
}
process.destroy();
System.out.println("'" + functionName + "' deploy on AWS Lambda completed");
// get lambda arn
String cmdArnGetter = AmazonCommandUtility.buildLambdaArnGetterCommand(functionName, region);
process = buildCommand(cmdArnGetter).start();
ReplyCollector lambdaArnReplyCollector = new ReplyCollector();
outputGobbler = new StreamGobbler(process.getInputStream(), lambdaArnReplyCollector::collectResult);
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceOut.submit(outputGobbler);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not get AWS Lambda arn for '" + functionName + "'");
executorServiceOut.shutdown();
executorServiceErr.shutdown();
process.destroy();
return "";
}
String lambdaARN = lambdaArnReplyCollector.getResult();
process.destroy();
System.out.println("Get AWS Lambda arn completed for '" + functionName + "'");
executorServiceOut.shutdown();
executorServiceErr.shutdown();
return lambdaARN;
}
/**
* Deploys a generic function to Amazon Lambda, creates the API Gateway associated API and persists on DB
* @param functionName name of the function
* @param runtime runtime of the function
* @param entryPoint function entry point path
* @param timeout function timeout in seconds
* @param memory function memory amount in megabytes
* @param region function region of deployment
* @param zipFolderAbsolutePath path of the folder containing function zipped implementation
* @param zipFileName file name of the zipped implementation
* @param handler true if an handler is being deployed, false for functions
*/
private static void deployOnAmazonRESTFunctions(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String region,
String zipFolderAbsolutePath, String zipFileName, boolean handler) {
try {
functionName = AmazonCommandUtility.applyRuntimeId(functionName, runtime);
DockerExecutor.checkDocker();
} catch (IllegalNameException | DockerException e) {
System.err.println("Could not deploy function '" + functionName + "' to AWS Lambda: " +
e.getMessage());
return;
}
System.out.println("\n" + "\u001B[33m" +
"Deploying \"" + functionName + "\" to Amazon Web Services..." +
"\u001B[0m" + "\n");
ExecutorService executorServiceOut = Executors.newSingleThreadExecutor();
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
try {
String lambdaARN = deployOnAmazonLambdaFunctions(functionName, runtime, entryPoint, timeout, memory, region,
zipFolderAbsolutePath, zipFileName);
if (lambdaARN.equals("")) {
return;
}
Process process;
StreamGobbler outputGobbler;
StreamGobbler errorGobbler;
// create api
String cmdApiCreation = AmazonCommandUtility.buildGatewayApiCreationCommand(functionName,
functionName + " function API", region);
process = buildCommand(cmdApiCreation).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not create api on API Gateway for '" + functionName + "'");
process.destroy();
return;
}
process.destroy();
System.out.println("Create api on API Gateway completed for '" + functionName + "'");
// get api id
String cmdApiIdGetter = AmazonCommandUtility.buildGatewayApiIdGetterCommand(functionName, region);
process = buildCommand(cmdApiIdGetter).start();
ReplyCollector apiIdReplyCollector = new ReplyCollector();
outputGobbler = new StreamGobbler(process.getInputStream(),
apiIdReplyCollector::collectResult);
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceOut.submit(outputGobbler);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not get api id for '" + functionName + "'");
process.destroy();
return;
}
String apiId = apiIdReplyCollector.getResult();
if (apiId.contains("\t")) {
System.err.println("Too many APIs with the same name ('" + functionName +
"'), could not continue execution");
process.destroy();
return;
}
process.destroy();
System.out.println("Get api id completed for '" + functionName + "'");
// get api parent id
String cmdApiParentIdGetter = AmazonCommandUtility.buildGatewayApiParentIdGetterCommand(apiId, region);
process = buildCommand(cmdApiParentIdGetter).start();
ReplyCollector apiParentIdReplyCollector = new ReplyCollector();
outputGobbler = new StreamGobbler(process.getInputStream(), apiParentIdReplyCollector::collectResult);
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceOut.submit(outputGobbler);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not get api parent id for '" + functionName + "'");
process.destroy();
return;
}
String apiParentId = apiParentIdReplyCollector.getResult();
process.destroy();
System.out.println("Get api parent id completed for '" + functionName + "'");
// create resource on api
String cmdResourceApiCreation = AmazonCommandUtility.buildGatewayResourceApiCreationCommand(functionName, apiId,
apiParentId, region);
process = buildCommand(cmdResourceApiCreation).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not create resource on api for '" + functionName + "'");
process.destroy();
return;
}
process.destroy();
System.out.println("Create resource on api completed for '" + functionName + "'");
// get api resource id
String cmdResourceApiIdGetter = AmazonCommandUtility.buildGatewayResourceApiIdGetterCommand(functionName, apiId,
region);
process = buildCommand(cmdResourceApiIdGetter).start();
ReplyCollector apiResourceIdReplyCollector = new ReplyCollector();
outputGobbler = new StreamGobbler(process.getInputStream(), apiResourceIdReplyCollector::collectResult);
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceOut.submit(outputGobbler);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not get api resource id for '" + functionName + "'");
process.destroy();
return;
}
String apiResourceId = apiResourceIdReplyCollector.getResult();
process.destroy();
System.out.println("Get api resource id completed for '" + functionName + "'");
// create api method
String cmdApiMethodCreation = AmazonCommandUtility.buildGatewayApiMethodOnResourceCreationCommand(apiId,
apiResourceId, region);
process = buildCommand(cmdApiMethodCreation).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not create api method for '" + functionName + "'");
process.destroy();
return;
}
process.destroy();
System.out.println("Create api method completed for '" + functionName + "'");
// link api method and lambda function
String cmdApiLinkage = AmazonCommandUtility.buildGatewayLambdaLinkageCommand(apiId, apiResourceId, lambdaARN,
region);
process = buildCommand(cmdApiLinkage).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not link api method and lambda function '" + functionName + "'");
process.destroy();
return;
}
process.destroy();
System.out.println("Link api method and lambda function '" + functionName + "' completed");
// deploy api
String cmdApiDeploy = AmazonCommandUtility.buildGatewayDeploymentCreationCommand(apiId, "benchmark",
region);
process = buildCommand(cmdApiDeploy).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not deploy api for '" + functionName + "'");
process.destroy();
return;
}
process.destroy();
System.out.println("Deploy api completed for '" + functionName + "'");
// grant gateway permission for lambda function execution
String cmdApiLambdaAuth = AmazonCommandUtility.buildGatewayLambdaAuthCommand(functionName, apiId, lambdaARN,
region);
process = buildCommand(cmdApiLambdaAuth).start();
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not authorize api gateway for '" + functionName + "' execution");
process.destroy();
return;
}
process.destroy();
System.out.println("Authorize api gateway for '" + functionName + "' execution completed");
// noinspection SpellCheckingInspection
String url = "https://" + apiId + ".execute-api." + region + ".amazonaws.com/benchmark/" + functionName;
System.out.println("\u001B[32m" + "Deployed function to: " + url + "\u001B[0m");
process.destroy();
if (handler) {
CompositionsRepositoryDAO.persistAmazonHandler(functionName, url, apiId, region);
} else {
FunctionsRepositoryDAO.persistAmazon(functionName, url, apiId, region);
}
} catch (InterruptedException | IOException e) {
System.err.println("\"" + functionName + "\" function deploy failed: " + e.getMessage());
} finally {
executorServiceOut.shutdown();
executorServiceErr.shutdown();
}
}
/**
*
* @param functionName name of the function
* @param runtime runtime of the function
* @param entryPoint function entry point path
* @param timeout function timeout in seconds
* @param memory function memory amount in megabytes
* @param zipFolderAbsolutePath path of the folder containing function zipped implementation
* @param zipFileName file name of the zipped implementation
*/
public static void deployOnOpenWhisk(String functionName, String runtime, String entryPoint, Integer timeout,
Integer memory, String zipFolderAbsolutePath, String zipFileName) {
deployOnOpenWhisk(functionName, runtime, entryPoint, timeout, memory, zipFolderAbsolutePath, zipFileName,
Boolean.valueOf(PropertiesManager.getInstance().getProperty(PropertiesManager.OPENWHISK_SSL_IGNORE)),
1);
}
/**
*
* @param functionName name of the function
* @param runtime runtime of the function
* @param entryPoint function entry point path
* @param timeout function timeout in seconds
* @param memory function memory amount in megabytes
* @param zipFolderAbsolutePath path of the folder containing function zipped implementation
* @param zipFileName file name of the zipped implementation
* @return function URL
*/
protected static String deployOnOpenWhiskCompositions(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory,
String zipFolderAbsolutePath, String zipFileName) {
return deployOnOpenWhisk(functionName, runtime, entryPoint, timeout, memory, zipFolderAbsolutePath, zipFileName,
Boolean.valueOf(PropertiesManager.getInstance().getProperty(PropertiesManager.OPENWHISK_SSL_IGNORE)),
2);
}
/**
* Deploys a generic function to OpenWhisk
* @param functionName name of the function
* @param runtime runtime of the function
* @param entryPoint function entry point path
* @param timeout function timeout in seconds
* @param memory function memory amount in megabytes
* @param zipFolderAbsolutePath path of the folder containing function zipped implementation
* @param zipFileName file name of the zipped implementation
* @param functionality 0 for handler deployment and persistence, 1 for function deployment and persistence,
* 2 for deployment only
* @return function URL
*/
private static String deployOnOpenWhisk(String functionName, String runtime, String entryPoint,
Integer timeout, Integer memory, String zipFolderAbsolutePath,
String zipFileName, Boolean ignoreSSL, Integer functionality) {
assert functionality == 1 || functionality == 2;
boolean webDeploy = functionality == 1;
try {
functionName = OpenWhiskCommandUtility.applyRuntimeId(functionName, runtime);
DockerExecutor.checkDocker();
} catch (IllegalNameException | DockerException e) {
System.err.println("Could not deploy function '" + functionName + "' to OpenWhisk: " +
e.getMessage());
return "";
}
if (webDeploy) {
System.out.println("\n" + "\u001B[33m" +
"Deploying \"" + functionName + "\" to OpenWhisk..." +
"\u001B[0m" + "\n");
}
// build deploy commands
String deployCmd = OpenWhiskCommandUtility.buildActionDeployCommand(functionName, runtime, entryPoint, timeout,
memory, zipFolderAbsolutePath, zipFileName, webDeploy);
String urlGetterCmd = OpenWhiskCommandUtility.buildActionUrlGetterCommand(functionName);
// start executors
ExecutorService executorServiceOut = Executors.newSingleThreadExecutor();
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
try {
// start function deploy process execution
Process process = buildCommand(deployCmd).start();
// create, execute and submit output gobblers
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
// wait for completion
if (process.waitFor() != 0) {
System.err.println("Could not deploy function '" + functionName + "'");
process.destroy();
return "";
}
process.destroy();
if (webDeploy) {
// start function URL retrieval process execution
process = buildCommand(urlGetterCmd).start();
// create, execute and submit output gobblers
URLFinder urlFinder = new URLFinder(ignoreSSL);
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), urlFinder::findOpenWhiskUrl);
errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceOut.submit(outputGobbler);
executorServiceErr.submit(errorGobbler);
// wait for completion
if (process.waitFor() != 0) {
System.err.println("Could not deploy function '" + functionName + "'");
process.destroy();
return "";
}
String url = urlFinder.getResult();
System.out.println("\u001B[32m" + "Deployed function to: " + url + "\u001B[0m");
process.destroy();
// function to persist
FunctionsRepositoryDAO.persistOpenWhisk(functionName, url);
}
return functionName;
} catch (InterruptedException | IOException e) {
System.err.println("Could not deploy function '" + functionName + "': " + e.getMessage());
return "";
} finally {
executorServiceOut.shutdown();
executorServiceErr.shutdown();
}
}
/**
* Removes a function from Google Cloud Functions
* @param functionName name of the function to remove
* @param region function to remove deployment region
* @throws IOException exception related to process execution
* @exception InterruptedException exception related to Thread management
*/
protected static void removeGoogleFunction(String functionName, String region)
throws IOException, InterruptedException {
String cmd = GoogleCommandUtility.buildGoogleCloudFunctionsRemoveCommand(functionName, region);
ExecutorService executorServiceOut = Executors.newSingleThreadExecutor();
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
Process process = buildCommand(cmd).start();
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), System.out::println);
StreamGobbler errorGobbler = new StreamGobbler(process.getInputStream(), System.err::println);
executorServiceOut.submit(outputGobbler);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not delete Google function '" + functionName + "'");
} else {
System.out.println("'" + functionName + "' function removed!");
}
process.destroy();
executorServiceOut.shutdown();
executorServiceErr.shutdown();
}
/**
* Removes every function from Google Cloud Platform
*/
public static void cleanupGoogleCloudFunctions() {
try {
DockerExecutor.checkDocker();
} catch (DockerException e) {
System.err.println("Could not cleanup Google Cloud Functions: " + e.getMessage());
return;
}
System.out.println("\n" + "\u001B[33m" +
"Cleaning up Google functions environment..." +
"\u001B[0m" + "\n");
List<CloudEntityData> toRemove = FunctionsRepositoryDAO.getGoogles();
if (toRemove == null) {
return;
}
for (CloudEntityData elem : toRemove) {
try {
removeGoogleFunction(elem.getEntityName(), elem.getRegion());
} catch (IOException | InterruptedException e) {
System.err.println("Could not delete '" + elem.getEntityName() + "': " + e.getMessage());
}
}
System.out.println("\u001B[32m" + "\nGoogle cleanup completed!\n" + "\u001B[0m");
FunctionsRepositoryDAO.dropGoogle();
}
/**
* Removes a function from AWS Lambda
* @param functionName name of the function to remove
* @param region function to remove region of deployment
* @throws IOException exception related to process execution
* @throws InterruptedException exception related to Thread management
*/
protected static void removeLambdaFunction(String functionName, String region)
throws IOException, InterruptedException {
String cmd = AmazonCommandUtility.buildLambdaDropCommand(functionName, region);
ExecutorService executorServiceOut = Executors.newSingleThreadExecutor();
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
Process process = buildCommand(cmd).start();
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), System.out::println);
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceOut.submit(outputGobbler);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not delete Lambda function '" + functionName + "'");
} else {
System.out.println("'" + functionName + "' function removed!");
}
process.destroy();
executorServiceOut.shutdown();
executorServiceErr.shutdown();
}
/**
* Removes an API from API Gateway
* @param functionName name of the function associated to the API to remove
* @param apiId id of the API to remove
* @param region API region of deployment
* @throws IOException exception related to process execution
* @throws InterruptedException exception related to Thread management
*/
protected static void removeGatewayApi(String functionName, String apiId, String region)
throws IOException, InterruptedException {
String cmd = AmazonCommandUtility.buildGatewayDropCommand(apiId, region);
ExecutorService executorServiceOut = Executors.newSingleThreadExecutor();
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
Process process = buildCommand(cmd).start();
StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), System.out::println);
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceOut.submit(outputGobbler);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
process.destroy();
executorServiceOut.shutdown();
executorServiceErr.shutdown();
System.err.println("Could not delete Gateway api '" + functionName + "'");
} else {
process.destroy();
executorServiceOut.shutdown();
executorServiceErr.shutdown();
// let AWS API Gateway remote environment complete the cleanup operation
waitFor("Cleanup", 30);
System.out.println("'" + functionName + "' api removed!");
}
}
/**
* Removes every function and API from AWS
*/
public static void cleanupAmazonRESTFunctions() {
try {
DockerExecutor.checkDocker();
} catch (DockerException e) {
System.err.println("Could not cleanup Amazon function environment: " + e.getMessage());
return;
}
System.out.println("\n" + "\u001B[33m" +
"Cleaning up Amazon functions environment..." +
"\u001B[0m" + "\n");
List<CloudEntityData> toRemove = FunctionsRepositoryDAO.getAmazons();
if (toRemove == null) {
return;
}
for (CloudEntityData elem : toRemove) {
try {
removeLambdaFunction(elem.getEntityName(), elem.getRegion());
} catch (InterruptedException | IOException e) {
System.err.println("Could not delete Lambda function '" + elem.getEntityName() + "': " +
e.getMessage());
}
try {
removeGatewayApi(elem.getEntityName(), elem.getId(), elem.getRegion());
} catch (InterruptedException | IOException e) {
System.err.println("Could not delete Gateway api '" + elem.getEntityName() + "': " +
e.getMessage());
}
}
System.out.println("\u001B[32m" + "\nAmazon cleanup completed!\n" + "\u001B[0m");
FunctionsRepositoryDAO.dropAmazon();
}
/**
* Removes an action from OpenWhisk (function or composition)
* @param actionName name of the action to remove
* @throws IOException exception related to process execution
* @throws InterruptedException exception related to Thread management
*/
protected static void removeOpenWhiskAction(String actionName) throws IOException, InterruptedException {
String cmd = OpenWhiskCommandUtility.buildActionDeletionCommand(actionName);
ExecutorService executorServiceErr = Executors.newSingleThreadExecutor();
Process process = buildCommand(cmd).start();
StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), System.err::println);
executorServiceErr.submit(errorGobbler);
if (process.waitFor() != 0) {
System.err.println("Could not delete OpenWhisk action '" + actionName + "'");
} else {
System.out.println("'" + actionName + "' action removed!");
}
process.destroy();
executorServiceErr.shutdown();
}
/**
* Removes every function from OpenWhisk
*/
public static void cleanupOpenWhiskFunctions() {
try {
DockerExecutor.checkDocker();
} catch (DockerException e) {
System.err.println("Could not cleanup OpenWhisk Functions: " + e.getMessage());
return;
}
System.out.println("\n" + "\u001B[33m" +
"Cleaning up OpenWhisk functions environment..." +
"\u001B[0m" + "\n");
List<CloudEntityData> toRemove = FunctionsRepositoryDAO.getOpenWhisks();
if (toRemove == null) {
return;
}
for (CloudEntityData elem : toRemove) {
try {
removeOpenWhiskAction(elem.getEntityName());
} catch (IOException | InterruptedException e) {
System.err.println("Could not delete '" + elem.getEntityName() + "': " + e.getMessage());
}
}
System.out.println("\u001B[32m" + "\nOpenWhisk cleanup completed!\n" + "\u001B[0m");
FunctionsRepositoryDAO.dropOpenWhisk();
}
}