-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.go
1035 lines (934 loc) · 32.7 KB
/
plugin.go
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
// Copyright (c) 2024 Veritas Technologies LLC. All rights reserved. IP63-2828-7171-04-15-9
// Package pm defines Plugin Manager (PM) functions like executing
// all plugins of a particular plugin type.
package pm
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"log/syslog"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/VeritasOS/plugin-manager/config"
"github.com/VeritasOS/plugin-manager/graph"
"github.com/VeritasOS/plugin-manager/types"
"github.com/VeritasOS/plugin-manager/types/status"
logger "github.com/VeritasOS/plugin-manager/utils/log"
osutils "github.com/VeritasOS/plugin-manager/utils/os"
"github.com/VeritasOS/plugin-manager/utils/output"
"gopkg.in/yaml.v3"
)
var (
// Version of the Plugin Manager (PM).
version = "5.0"
)
// Plugin is of type types.Plugin
type Plugin = types.Plugin
// Plugins is of type types.Plugins
type Plugins = types.Plugins
// getPluginFiles retrieves the plugin files under each component matching
// the specified pluginType.
func getPluginFiles(pluginType, library string) ([]string, error) {
logger.Debug.Println("Entering getPluginFiles")
defer logger.Debug.Println("Exiting getPluginFiles")
var pluginFiles []string
if _, err := os.Stat(library); os.IsNotExist(err) {
return pluginFiles, logger.ConsoleError.PrintNReturnError("Library '%s' doesn't exist. "+
"A valid plugins library path must be specified.", library)
}
var files []string
dirs, err := ioutil.ReadDir(library)
if err != nil {
logger.Error.Printf("Failed to call ioutil.ReadDir(%s), err=%s", library, err.Error())
return pluginFiles, logger.ConsoleError.PrintNReturnError("Failed to get contents of %s plugins library.", library)
}
for _, dir := range dirs {
compPluginDir := filepath.FromSlash(library + "/" + dir.Name())
fi, err := os.Stat(compPluginDir)
if err != nil {
logger.Error.Printf("Unable to stat on %s directory, err=%s", dir, err.Error())
continue
}
if !fi.IsDir() {
logger.Error.Printf("%s is not a directory.", compPluginDir)
continue
}
tfiles, err := ioutil.ReadDir(compPluginDir)
if err != nil {
logger.Error.Printf("Unable to read contents of %s directory, err=%s", compPluginDir, err.Error())
}
for _, tf := range tfiles {
files = append(files, filepath.FromSlash(dir.Name()+"/"+tf.Name()))
}
}
for _, file := range files {
matched, err := regexp.MatchString("[.]"+pluginType+"$", file)
if err != nil {
logger.Error.Printf("Failed to call regexp.MatchString(%s, %s), err=%s", "[.]"+pluginType, file, err.Error())
continue
}
if matched == true {
pluginFiles = append(pluginFiles, file)
}
}
return pluginFiles, nil
}
// getPluginType returns the plugin type of the specified plugin file.
func getPluginType(file string) string {
return strings.Replace(path.Ext(file), ".", ``, -1)
}
func getPluginsInfoFromJSONStrOrFile(strOrFile string) (Plugin, error) {
var err error
var pluginsInfo Plugin
rawData := strOrFile
jsonFormat := true
// If Plugins information is in file...
fi, err := os.Stat(strOrFile)
if err != nil {
logger.Debug.Printf("Specified input is not a file. Err: %s",
err.Error())
} else {
if fi.IsDir() {
return pluginsInfo,
logger.ConsoleError.PrintNReturnError(
"Specified path %s is directory. Plugins info should be specified either as a json string or in a json file.",
strOrFile)
}
pluginsFile := strOrFile
fh, err := os.Open(pluginsFile)
if err != nil {
logger.ConsoleError.PrintNReturnError("%s", err)
return pluginsInfo, err
}
defer fh.Close()
rawData, err = readFile(filepath.FromSlash(pluginsFile))
if err != nil {
return pluginsInfo,
logger.ConsoleError.PrintNReturnError(err.Error())
}
logger.Debug.Printf("Plugins file %v has ext %v", pluginsFile, path.Ext(pluginsFile))
if path.Ext(pluginsFile) == ".yaml" || path.Ext(pluginsFile) == ".yml" {
jsonFormat = false
}
}
// INFO: Use Plugin to unmarshal to keep input consistent with current
// output json, so that rerun failed could be done using result json.
var pluginsData Plugin
if jsonFormat {
err = json.Unmarshal([]byte(rawData), &pluginsData)
} else {
err = yaml.Unmarshal([]byte(rawData), &pluginsData)
}
if err != nil {
logger.Error.Printf("Failed to call Unmarshal(%s, %v); err=%#v",
rawData, &pluginsInfo, err)
return pluginsInfo,
logger.ConsoleError.PrintNReturnError(
"Plugins is not in expected format. Error: %s", err.Error())
}
return pluginsData, nil
}
func getPluginsInfoFromLibrary(pluginType, library string) (Plugins, error) {
var pluginsInfo Plugins
pluginFiles, err := getPluginFiles(pluginType, library)
if err != nil {
return pluginsInfo, err
}
for file := range pluginFiles {
fContents, rerr := readFile(filepath.FromSlash(
library + pluginFiles[file]))
if rerr != nil {
return pluginsInfo, logger.ConsoleError.PrintNReturnError(rerr.Error())
}
logger.Debug.Printf("Plugin file %s contents: \n%s\n",
pluginFiles[file], fContents)
pInfo, perr := parseUnitFile(fContents)
if perr != nil {
return pluginsInfo, perr
}
logger.Info.Printf("Plugin %s info: %+v", pluginFiles[file], pInfo)
pInfo.Name = pluginFiles[file]
pluginsInfo = append(pluginsInfo, &pInfo)
}
return pluginsInfo, nil
}
func normalizePluginsInfo(pluginsInfo Plugins) Plugins {
logger.Debug.Printf("Entering normalizePluginsInfo(%+v)...", pluginsInfo)
defer logger.Debug.Println("Exiting normalizePluginsInfo")
nPInfo := make(Plugins, len(pluginsInfo))
pluginIndexes := make(map[string]int, len(pluginsInfo))
for pIdx, pInfo := range pluginsInfo {
pluginIndexes[pInfo.Name] = pIdx
nPInfo[pIdx] = &Plugin{
Name: pInfo.Name,
Description: pInfo.Description,
ExecStart: pInfo.ExecStart,
Plugins: pInfo.Plugins,
Library: pInfo.Library,
}
nPInfo[pIdx].RequiredBy = append(nPInfo[pIdx].Requires, pInfo.RequiredBy...)
nPInfo[pIdx].Requires = append(nPInfo[pIdx].Requires, pInfo.Requires...)
logger.Debug.Printf("%s plugin dependencies: %v", nPInfo[pIdx].Name, nPInfo[pIdx])
}
for pIdx, pInfo := range nPInfo {
p := pInfo.Name
logger.Debug.Printf("nPInfo key(%v): %v", p, nPInfo[pIdx])
for _, rs := range nPInfo[pIdx].Requires {
// Check whether it's already marked as RequiredBy dependency in `Requires` plugin.
// logger.Info.Printf("Check whether `in` (%s) already marked as RequiredBy dependency in `Requires`(%s) plugin: %v",
// p, rs, nPInfo[rs])
present := false
// If dependencies are missing, then pluginIndexes[rs] value will not be defined.
if rsIdx, ok := pluginIndexes[rs]; ok {
logger.Debug.Printf("PluginInfo for %s is present: %v", rs, nPInfo[rsIdx])
for _, rby := range nPInfo[rsIdx].RequiredBy {
logger.Debug.Printf("p(%s) == rby(%s)? %v", p, rby, p == rby)
if p == rby {
present = true
break
}
}
if !present {
nPInfo[rsIdx].RequiredBy = append(nPInfo[rsIdx].RequiredBy, p)
logger.Info.Printf("Added %s as RequiredBy dependency of %s: %+v", p, rs, nPInfo[rsIdx])
}
}
}
// Check whether RequiredBy dependencies are also marked as Requires dependency on other plugin.
logger.Info.Println("Check whether RequiredBy dependencies are also marked as Requires dependency on other plugin.")
for _, rby := range nPInfo[pIdx].RequiredBy {
rbyIdx := pluginIndexes[rby]
logger.Debug.Printf("RequiredBy of %s: %s", p, rby)
logger.Debug.Printf("nPInfo of %s: %+v", rby, nPInfo[rbyIdx])
// INFO: If one plugin type is added as dependent on another by
// any chance, then skip checking its contents as the other
// plugin type files were not parsed.
if _, ok := pluginIndexes[rby]; !ok {
// NOTE: Add the missing plugin in Requires, So that the issue
// gets caught during validation.
nPInfo[pIdx].Requires = append(nPInfo[pIdx].Requires, rby)
continue
}
present := false
for _, rs := range nPInfo[rbyIdx].Requires {
if p == rs {
present = true
break
}
}
if !present {
nPInfo[rbyIdx].Requires = append(nPInfo[rbyIdx].Requires, p)
logger.Debug.Printf("Added %s as Requires dependency of %s: %+v", p, rby, nPInfo[rbyIdx])
}
}
}
logger.Debug.Printf("Plugins info after normalizing: \n%+v\n", nPInfo)
return nPInfo
}
// parseUnitFile parses the plugin file contents.
func parseUnitFile(fileContents string) (Plugin, error) {
logger.Debug.Println("Entering parseUnitFile")
defer logger.Debug.Println("Exiting parseUnitFile")
pluginInfo := Plugin{}
if len(fileContents) == 0 {
return pluginInfo, nil
}
lines := strings.Split(fileContents, "\n")
for l := range lines {
line := strings.TrimSpace(lines[l])
logger.Debug.Println("line...", line)
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
if strings.HasPrefix(line, "#") {
// No need to parse comments.
logger.Debug.Println("Skipping comment line...", line)
continue
}
fields := strings.Split(line, "=")
if len(fields) == 0 {
continue
}
key := strings.TrimSpace(fields[0])
val := strings.TrimSpace(strings.Join(fields[1:], "="))
switch key {
case "Description":
pluginInfo.Description = val
break
case "ExecStart":
pluginInfo.ExecStart = val
break
case "RequiredBy":
pluginInfo.RequiredBy = strings.Split(val, " ")
break
case "Requires":
pluginInfo.Requires = strings.Split(val, " ")
break
default:
logger.Debug.Printf("Non-standard line found: %s", line)
break
}
}
return pluginInfo, nil
}
func validateDependencies(nPInfo Plugins) ([]string, error) {
logger.Debug.Println("Entering validateDependencies")
defer logger.Debug.Println("Exiting validateDependencies")
var pluginOrder []string
notPlacedPlugins := []string{}
dependencyMet := map[string]bool{}
pluginIndexes := make(map[string]int)
for pIdx, pInfo := range nPInfo {
pluginIndexes[pInfo.Name] = pIdx
}
for pNameIndex := range nPInfo {
pName := nPInfo[pNameIndex].Name
pContents := nPInfo[pNameIndex]
logger.Debug.Printf("\nPlugin: %s \n%+v \n\n", pName, pContents)
if len(pContents.Requires) == 0 {
dependencyMet[pName] = true
pluginOrder = append(pluginOrder, pName)
} else {
dependencyMet[pName] = false
notPlacedPlugins = append(notPlacedPlugins, pName)
}
}
curLen := len(notPlacedPlugins)
// elementsLeft to process in the notPlacedPlugins queue!
elementsLeft := curLen
prevLen := curLen
// INFO:
// When all the elements are processed in the queue
// (i.e., `elementsLeft` becomes 0), check whether at least one of the
// plugin's dependency has been met (i.e., prevLen != curLen). If not,
// then there is a circular dependency, or plugins are missing dependencies.
for curLen != 0 {
pName := notPlacedPlugins[0]
notPlacedPlugins = notPlacedPlugins[1:]
pIdx := pluginIndexes[pName]
pDependencies := nPInfo[pIdx].Requires
logger.Info.Printf("Plugin %s dependencies: %+v", pName, pDependencies)
dependencyMet[pName] = true
for w := range pDependencies {
val := dependencyMet[pDependencies[w]]
if false == val {
// If dependency met is false, then process it later again after all dependencies are met.
dependencyMet[pName] = false
logger.Warning.Printf("Adding %s back to list %s to process as %s plugin dependency is not met.",
pName, notPlacedPlugins, pDependencies[w])
notPlacedPlugins = append(notPlacedPlugins, pName)
break
}
}
// If dependency met is not set to false, then it means all
// dependencies are met. So, add it to pluginOrder
if false != dependencyMet[pName] {
logger.Info.Printf("Dependency met for %s: %v.", pName, dependencyMet[pName])
pluginOrder = append(pluginOrder, pName)
}
elementsLeft--
if elementsLeft == 0 {
logger.Debug.Printf("PrevLen: %d; CurLen: %d.", prevLen, curLen)
curLen = len(notPlacedPlugins)
if prevLen == curLen {
// INFO: Clear out the pluginOrder as we cannot run all the
// plugins either due to missing dependencies or having
// circular dependency.
return []string{}, logger.ConsoleError.PrintNReturnError(
"There is either a circular dependency between plugins, "+
"or some dependencies are missing in these plugins: %+v",
notPlacedPlugins)
}
prevLen = curLen
elementsLeft = curLen
}
}
return pluginOrder, nil
}
func executePluginCmd(statusCh chan<- map[string]*Plugin, pInfo Plugin, failedDependency bool, env map[string]string) {
p := pInfo.Name
logger.Debug.Printf("Channel: Plugin %s info: \n%+v", p, pInfo)
graph.UpdateGraph(getPluginType(p), p, status.Start, "")
logger.ConsoleInfo.Printf("%s: %s", pInfo.Description, status.Start)
pluginLogFile := ""
var chLog *log.Logger
if !logger.IsFileLogger() {
var logTag string
// Set log tag for
logTag = logger.SyslogTagPrefix + "pm-" + logger.GetLogTag()
logger.Debug.Printf("logTag = %s", logTag)
syslogHandle, err := syslog.New(syslog.LOG_LOCAL0|syslog.LOG_INFO, logTag)
if err != nil {
logger.Error.Printf("Failed to call syslog.New, err=%s", err.Error())
}
defer syslogHandle.Close()
chLog = log.New(syslogHandle, "", 0)
} else {
// Get relative path to plugins log file from PM log dir, so that linking
// in plugin graph works even when the logs are copied to another system.
pluginLogFile = strings.Replace(config.GetPluginsLogDir(), config.GetPMLogDir(), "", -1) +
strings.Replace(p, string(os.PathSeparator), ":", -1) +
"." + time.Now().Format(time.RFC3339Nano) + ".log"
logFile := config.GetPMLogDir() + pluginLogFile
fh, openerr := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if openerr != nil {
logger.Error.Printf("Failed to call os.OpenFile(%s), err=%s", logFile, openerr.Error())
// Ignore error and continue as plugin log file creation is not fatal.
}
defer fh.Close()
// chLog is a channel logger
chLog = log.New(fh, "", log.LstdFlags)
chLog.SetOutput(fh)
}
chLog.Println("INFO: Plugin file:", p)
// If already marked as failed/skipped due to dependency fail,
// then just return that status.
myStatus := ""
myStatusMsg := ""
if failedDependency {
myStatusMsg = "Skipping as its dependency failed."
myStatus = status.Skip
} else if len(pInfo.Plugins) != 0 {
execStatus := executePlugins(&pInfo.Plugins, false, env)
if !execStatus {
myStatus = status.Fail
graph.UpdateGraph(getPluginType(p), p, myStatus, "")
err := fmt.Errorf("Running %s plugins: %s", p, myStatus)
statusCh <- map[string]*Plugin{p: {Status: myStatus, StdOutErr: []string{err.Error()}}}
return
}
myStatus = status.Ok
} else if pInfo.ExecStart == "" {
myStatusMsg = "Passing as ExecStart value is empty!"
myStatus = status.Ok
}
if myStatus != "" {
chLog.Println("INFO: ", myStatusMsg)
logger.Info.Printf("Plugin(%s): %s", p, myStatusMsg)
graph.UpdateGraph(getPluginType(p), p, myStatus, "")
logger.ConsoleInfo.Printf("%s: %s", pInfo.Description, myStatus)
statusCh <- map[string]*Plugin{p: {Status: myStatus}}
return
}
// INFO: First initialize with existing OS env, and then overwrite any
// existing keys with user specified values. I.e., Even if PM_LIBRARY
// env is set in shell, it'll be overwritten by Library parameter passed
// by user.
envList := osutils.OsEnviron()
envMap := osutils.EnvMap()
for envKey, envValue := range env {
envList = append(envList, envKey+"="+envValue)
envMap[envKey] = envValue
}
getEnvVal := func(name string) string {
// logger.Debug.Printf("In getEnvVal(%v)...", name)
// logger.Debug.Printf("env: %+v", envMap)
if val, ok := envMap[name]; ok {
// logger.Debug.Printf("Key:%v = Value:%v", name, val)
return val
}
return ""
}
logger.Info.Printf("Executing command, cmd=%s", pInfo.ExecStart)
// INFO: Expand environment values like "PM_LIBRARY" so that ...
// 1. Binaries or scripts placed in the same directory as that of plugins
// can be accessed as ${PM_LIBRARY}/<binary|script> path.
// 2. Envs that are set by caller of calling plugin manager gets expanded.
cmdParam := strings.Split(os.Expand(pInfo.ExecStart, getEnvVal), " ")
cmdStr := cmdParam[0]
cmdParams := cmdParam[1:]
cmd := exec.Command(cmdStr, cmdParams...)
cmd.Env = envList
iostdout, err := cmd.StdoutPipe()
if err != nil {
pInfo.Status = status.Fail
logger.Error.Printf("Failed to execute plugin %s. Error: %s\n", pInfo.Name, err.Error())
pInfo.StdOutErr = []string{err.Error()}
logger.ConsoleInfo.Printf("%s: %s\n", pInfo.Description, pInfo.Status)
statusCh <- map[string]*Plugin{p: &pInfo}
return
}
cmd.Stderr = cmd.Stdout
chLog.Println("Executing command:", pInfo.ExecStart)
err = cmd.Start()
var stdOutErr []string
if err == nil {
scanner := bufio.NewScanner(iostdout)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
iobytes := scanner.Text()
chLog.Println(string(iobytes))
stdOutErr = append(stdOutErr, iobytes)
}
err = cmd.Wait()
// chLog.Printf("command exited with code: %+v", err)
}
func() {
chLog.Printf("INFO: Plugin(%s): Executing command: %s", p, pInfo.ExecStart)
if err != nil {
chLog.Printf("ERROR: Plugin(%s): Failed to execute command, err=%s", p, err.Error())
graph.UpdateGraph(getPluginType(p), p, status.Fail, pluginLogFile)
} else {
chLog.Printf("INFO: Plugin(%s): Stdout & Stderr: %v", p, stdOutErr)
graph.UpdateGraph(getPluginType(p), p, status.Ok, pluginLogFile)
}
}()
logger.Debug.Println("Stdout & Stderr:", stdOutErr)
pStatus := Plugin{StdOutErr: stdOutErr}
if err != nil {
pStatus.Status = status.Fail
logger.Error.Printf("Failed to execute plugin %s. err=%s\n", p, err.Error())
logger.ConsoleError.Printf("%s: %s\n", pInfo.Description, status.Fail)
statusCh <- map[string]*Plugin{p: &pStatus}
return
}
pStatus.Status = status.Ok
logger.ConsoleInfo.Printf("%s: %s\n", pInfo.Description, status.Ok)
statusCh <- map[string]*Plugin{p: &pStatus}
}
func executePlugins(psStatus *Plugins, sequential bool, env map[string]string) bool {
logger.Debug.Printf("Entering executePlugins(%+v, %v, %+v)...",
psStatus, sequential, env)
defer logger.Debug.Println("Exiting executePlugins")
retStatus := true
nPInfo := normalizePluginsInfo(*psStatus)
_, err := validateDependencies(nPInfo)
if err != nil {
return false
}
waitCount := map[string]int{}
for pIdx, pInfo := range nPInfo {
p := pInfo.Name
waitCount[p] = len(nPInfo[pIdx].Requires)
logger.Debug.Printf("%s plugin dependencies: %+v", p, nPInfo[pIdx])
}
pluginIndexes := make(map[string]int)
for pIdx, pInfo := range *psStatus {
pluginIndexes[pInfo.Name] = pIdx
}
executingCnt := 0
exeCh := make(chan map[string]*Plugin)
failedDependency := make(map[string]bool)
for len(pluginIndexes) > 0 || executingCnt != 0 {
for _, pInfo := range nPInfo {
p := pInfo.Name
// INFO: When all dependencies are met, plugin waitCount would be 0.
// When sequential execution is enforced, even if a plugin is ready
// to run, make sure that only one plugin is running at time, by
// checking executing count is 0.
// When sequential execution is not enforced, run plugins that are ready.
if waitCount[p] == 0 && ((sequential == false) ||
(sequential == true && executingCnt == 0)) {
logger.Info.Printf("Plugin %s is ready for execution: %v.", p, pInfo)
waitCount[p]--
pIdx := pluginIndexes[p]
ps := *psStatus
ps[pIdx].RunTime.StartTime = time.Now()
go executePluginCmd(exeCh, *pInfo, failedDependency[p], env)
executingCnt++
}
}
// start other dependent ones as soon as one of the plugin completes.
exeStatus := <-exeCh
executingCnt--
for plugin, pStatus := range exeStatus {
logger.Info.Printf("%s status: %v", plugin, pStatus.Status)
pIdx := pluginIndexes[plugin]
ps := *psStatus
ps[pIdx].RunTime.EndTime = time.Now()
ps[pIdx].RunTime.Duration = ps[pIdx].RunTime.EndTime.Sub(ps[pIdx].RunTime.StartTime)
ps[pIdx].Status = pStatus.Status
ps[pIdx].StdOutErr = pStatus.StdOutErr
if pStatus.Status == status.Fail {
retStatus = false
}
for _, rby := range nPInfo[pIdx].RequiredBy {
if pStatus.Status == status.Fail ||
pStatus.Status == status.Skip {
// TODO: When "Wants" and "WantedBy" options are supported similar to
// "Requires" and "RequiredBy", the failedDependency flag should be
// checked in conjunction with if its required dependency is failed,
// and not the wanted dependency.
failedDependency[rby] = true
}
waitCount[rby]--
}
delete(pluginIndexes, plugin)
}
}
return retStatus
}
// CmdOptions contains subcommands and parameters of the pm command.
var CmdOptions struct {
RunCmd *flag.FlagSet
ListCmd *flag.FlagSet
versionCmd *flag.FlagSet
versionPtr *bool
// sequential enforces execution of plugins in sequence mode.
// (If sequential is disabled, plugins whose dependencies are met would be executed in parallel).
sequential *bool
// pluginsPtr specifies plugins Name and its Description, ExecStart and any dependencies (Requires, RequiredBy).
// For input format, check 'Plugins' struct.
pluginsPtr *string
// pluginTypePtr indicates type of the plugin to run.
pluginTypePtr *string
// libraryPtr indicates the path of the plugins library.
libraryPtr *string
// pluginDirPtr indicates the location of the plugins.
// NOTE: `pluginDir` is deprecated, use `library` instead.
pluginDirPtr *string
}
// ListOptions are optional parameters related to list function.
type ListOptions struct {
Type string
}
// RunOptions are optional parameters related to run function.
type RunOptions struct {
Library string
Type string
Sequential bool
}
// ListFromLibrary lists the plugin and its dependencies from the plugins
// library path.
func ListFromLibrary(pluginType, library string) error {
pluginsInfo, err := getPluginsInfoFromLibrary(pluginType, library)
if err != nil {
return err
}
listOptions := ListOptions{
Type: pluginType,
}
return list(pluginsInfo, listOptions)
}
// ListFromJSONStrOrFile lists the plugin and its dependencies from a json
// string or a json file.
func ListFromJSONStrOrFile(jsonStrOrFile string, listOptions ListOptions) error {
pluginsInfo, err := getPluginsInfoFromJSONStrOrFile(jsonStrOrFile)
if err != nil {
return err
}
return list(pluginsInfo.Plugins, listOptions)
}
// List the plugin and its dependencies.
func list(pluginsInfo Plugins, listOptions ListOptions) error {
pluginType := listOptions.Type
var err error
err = graph.InitGraph(pluginType, pluginsInfo)
if err != nil {
return err
}
logger.ConsoleInfo.Printf("The list of plugins are mapped in %s", graph.GetImagePath())
return nil
}
func readFile(filePath string) (string, error) {
bFileContents, err := ioutil.ReadFile(filePath)
if err != nil {
message := "Failed to read " + filePath + " file."
err = errors.New(message)
return "", err
}
return string(bFileContents), nil
}
// RegisterCommandOptions registers the command options that are supported
func RegisterCommandOptions(progname string) {
logger.Debug.Println("Entering RegisterCommandOptions")
defer logger.Debug.Println("Exiting RegisterCommandOptions")
CmdOptions.versionCmd = flag.NewFlagSet(progname+" version", flag.ContinueOnError)
CmdOptions.versionPtr = CmdOptions.versionCmd.Bool("version", false, "print Plugin Manager (PM) version.")
CmdOptions.RunCmd = flag.NewFlagSet(progname+" run", flag.PanicOnError)
CmdOptions.pluginsPtr = CmdOptions.RunCmd.String(
"plugins",
"",
"Plugins and its dependencies in json format as a string or in a file (Ex: './plugins.json').\nWhen specified, plugin files are not looked up in specified -library path.",
)
CmdOptions.pluginTypePtr = CmdOptions.RunCmd.String(
"type",
"",
"Type of plugin.",
)
CmdOptions.libraryPtr = CmdOptions.RunCmd.String(
"library",
"",
"Path of the plugins library.\nSets PM_LIBRARY env value.\n"+
"When '-plugins' is specified, only PM_LIBRARY env value is set. "+
"The plugin files are not read from library path.",
)
CmdOptions.sequential = CmdOptions.RunCmd.Bool(
"sequential",
false,
"Enforce running plugins in sequential.",
)
logger.RegisterCommandOptions(CmdOptions.RunCmd, map[string]string{
"log-dir": config.GetLogDir(),
"log-file": config.GetLogFile(),
"log-level": config.GetLogLevel(),
})
output.RegisterCommandOptions(CmdOptions.RunCmd, map[string]string{})
CmdOptions.ListCmd = flag.NewFlagSet(progname+" list", flag.PanicOnError)
CmdOptions.ListCmd.StringVar(
CmdOptions.pluginsPtr,
"plugins",
"",
"Plugins and its dependencies in json format as a string or in a file (Ex: './plugins.json')",
)
CmdOptions.ListCmd.StringVar(
CmdOptions.pluginTypePtr,
"type",
"",
"Type of plugin.",
)
CmdOptions.ListCmd.StringVar(
CmdOptions.libraryPtr,
"library",
"",
"Path of the plugins library.",
)
logger.RegisterCommandOptions(CmdOptions.ListCmd, map[string]string{
"log-dir": config.GetLogDir(),
"log-file": config.GetLogFile(),
"log-level": config.GetLogLevel(),
})
}
// RunFromJSONStrOrFile runs the plugins based on dependencies specified in a
// json string or a json/yaml file.
func RunFromJSONStrOrFile(result *Plugin, jsonStrOrFile string, runOptions RunOptions) error {
pluginsInfo, err := getPluginsInfoFromJSONStrOrFile(jsonStrOrFile)
if err != nil {
result.Status = status.Fail
result.StdOutErr = append(result.StdOutErr, err.Error())
return err
}
result.Name = pluginsInfo.Name
result.Library = pluginsInfo.Library
result.Plugins = pluginsInfo.Plugins
// INFO: Override values of json/file with explicitly passed cmdline parameter. Else, set runOptions type from json/file.
if runOptions.Type != "" {
result.Name = runOptions.Type
} else {
runOptions.Type = pluginsInfo.Name
}
if runOptions.Library != "" {
result.Library = runOptions.Library
} else {
runOptions.Library = pluginsInfo.Library
}
return run(result, runOptions)
}
// RunFromLibrary runs the specified plugin type plugins from the library.
func RunFromLibrary(result *Plugin, pluginType string, runOptions RunOptions) error {
result.Name = pluginType
var pluginsInfo, err = getPluginsInfoFromLibrary(pluginType, runOptions.Library)
if err != nil {
result.Status = status.Fail
result.StdOutErr = append(result.StdOutErr, err.Error())
return err
}
result.Plugins = pluginsInfo
runOptions.Type = pluginType
return run(result, runOptions)
}
// run the specified plugins.
func run(result *Plugin, runOptions RunOptions) error {
logger.Debug.Printf("Entering run(%+v, %+v)...", result, runOptions)
defer logger.Debug.Println("Exiting run")
pluginType := runOptions.Type
sequential := runOptions.Sequential
result.RunTime.StartTime = time.Now()
defer func() {
result.RunTime.EndTime = time.Now()
result.RunTime.Duration = result.RunTime.EndTime.Sub(result.RunTime.StartTime)
}()
if err := osutils.OsMkdirAll(config.GetPluginsLogDir(), 0755); nil != err {
err = logger.ConsoleError.PrintNReturnError(
"Failed to create the plugins logs directory: %s. "+
"Error: %s", config.GetPluginsLogDir(), err.Error())
result.Status = status.Fail
result.StdOutErr = append(result.StdOutErr, err.Error())
return err
}
graph.InitGraph(pluginType, result.Plugins)
env := map[string]string{}
if runOptions.Library != "" {
env["PM_LIBRARY"] = runOptions.Library
}
execStatus := executePlugins(&result.Plugins, sequential, env)
if execStatus != true {
result.Status = status.Fail
err := fmt.Errorf("Running %s plugins: %s", pluginType, status.Fail)
result.StdOutErr = append(result.StdOutErr, err.Error())
logger.ConsoleError.Printf("%s\n", err.Error())
return err
}
result.Status = status.Ok
logger.ConsoleInfo.Printf("Running %s plugins: %s\n", pluginType, status.Ok)
return nil
}
// ScanCommandOptions scans for the command line options and makes appropriate
// function call.
// Input:
// 1. map[string]interface{}
// where, the options could be following:
// "progname": Name of the program along with any cmds (ex: asum pm)
// "cmd-index": Index to the cmd (ex: run)
func ScanCommandOptions(options map[string]interface{}) error {
logger.Debug.Printf("Entering ScanCommandOptions(%+v)...", options)
defer logger.Debug.Println("Exiting ScanCommandOptions")
progname := filepath.Base(os.Args[0])
cmdIndex := 1
if valI, ok := options["progname"]; ok {
progname = valI.(string)
}
if valI, ok := options["cmd-index"]; ok {
cmdIndex = valI.(int)
}
cmd := os.Args[cmdIndex]
logger.Debug.Println("progname:", progname, "cmd with arguments: ", os.Args[cmdIndex:])
switch cmd {
case "version":
logger.ConsoleInfo.Printf("Plugin Manager (PM) version %s", version)
case "list":
err := CmdOptions.ListCmd.Parse(os.Args[cmdIndex+1:])
if err != nil {
logger.Error.Printf("Command arguments parse error, cmd=%s, err=%s", cmd, err.Error())
}
case "run":
err := CmdOptions.RunCmd.Parse(os.Args[cmdIndex+1:])
if err != nil {
logger.Error.Printf("Command arguments parse error, cmd=%s, err=%s", cmd, err.Error())
}
case "help":
subcmd := ""
if len(os.Args) == cmdIndex+2 {
subcmd = os.Args[cmdIndex+1]
} else if len(os.Args) > cmdIndex+2 {
fmt.Fprintf(os.Stderr, "usage: %s help command\n\nToo many arguments (%d) given.\n", progname, len(os.Args))
os.Exit(2)
}
usage(progname, subcmd)
default:
fmt.Fprintf(os.Stderr, "%s: unknown command \"%s\"\n", progname, os.Args[1])
fmt.Fprintf(os.Stderr, "Run '%s help [command]' for usage.\n", progname)
os.Exit(2)
}
// Override `pm.config.yaml` value with command-line arguments.
if *CmdOptions.libraryPtr != "" {
config.SetPluginsLibrary(*CmdOptions.libraryPtr)
}
myLogFile := logger.DefaultLogDir
if logger.GetLogDir() != "" {
config.SetPMLogDir(logger.GetLogDir())
myLogFile = config.GetPMLogDir()
}
// Info: Call set PM log-dir to clean extra slashes, and to append path
// separator at the end.
config.SetPMLogDir(config.GetPMLogDir())
// Reinit logging if required.
if logger.GetLogTag() != "" {
// Use Syslog whenever logTag is specified.
err := logger.InitSysLogger(logger.GetLogTag(), logger.GetLogLevel())
if err != nil {
fmt.Printf("Failed to initialize SysLog [%v]. Exiting...\n", err)
os.Exit(-1)
}
} else {
tLogFile := logger.DefaultLogFile
if logger.GetLogFile() != "" {
tLogFile = logger.GetLogFile()
}
// NOTE: Even when no log file is specified, and we're using default log
// file name, we still need to call SetPMLogFile() as SVG image file name
// is based on this. Otherwise image and dot files will not have any names
// but only extensions (i.e., they get created as hidden files).
config.SetPMLogFile(tLogFile)
myLogFile += config.GetPMLogFile()
if myLogFile != logger.DefaultLogPath {
myLogFile := filepath.Clean(myLogFile)
logger.Info.Println("Logging to specified log file:", myLogFile)
errList := logger.DeInitLogger()
if len(errList) > 0 {
fmt.Printf("Failed to deinitialize logger, err=[%v]", errList)
os.Exit(-1)
}
err := logger.InitFileLogger(myLogFile, logger.GetLogLevel())
if err != nil {
fmt.Printf("Failed to initialize logger, err=[%v]", err)
os.Exit(-1)
}
}
}
var err error
pluginType := *CmdOptions.pluginTypePtr
if *CmdOptions.pluginsPtr != "" {
jsonStrOrFile := *CmdOptions.pluginsPtr
switch cmd {
case "list":
err = ListFromJSONStrOrFile(jsonStrOrFile,
ListOptions{Type: pluginType})
case "run":
pmstatus := Plugin{}
runOptions := RunOptions{
Type: pluginType,
Sequential: *CmdOptions.sequential,
}
// NOTE: When '-plugins' info is passed as str or file, don't use
// Library from config.
// The config file is expected to have some library path, and that
// may not be applicable for this set of inputs. So, set/use
// "Library" value only if it's passed as cmdline argument.
if *CmdOptions.libraryPtr != "" {
runOptions.Library = config.GetPluginsLibrary()
}
err = RunFromJSONStrOrFile(&pmstatus, jsonStrOrFile, runOptions)
output.Write(pmstatus)
}
} else if pluginType != "" {
switch cmd {
case "list":
err = ListFromLibrary(pluginType, config.GetPluginsLibrary())
case "run":
pmstatus := Plugin{}
err = RunFromLibrary(&pmstatus, pluginType,
RunOptions{Library: config.GetPluginsLibrary(),
Sequential: *CmdOptions.sequential})
output.Write(pmstatus)
}
}
return err