forked from distribworks/dkron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
89 lines (75 loc) · 2.22 KB
/
plugins.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
package main
import (
"os/exec"
"path/filepath"
"strings"
"bitbucket.org/kardianos/osext"
"github.com/Sirupsen/logrus"
"github.com/hashicorp/go-plugin"
"github.com/victorcoder/dkron/dkron"
dkplugin "github.com/victorcoder/dkron/plugin"
)
type Plugins struct {
Processors map[string]dkron.ExecutionProcessor
}
// Discover plugins located on disk
//
// We look in the following places for plugins:
//
// 1. Dkron configuration path
// 2. Path where Dkron is installed
//
// Whichever file is discoverd LAST wins.
func (p *Plugins) DiscoverPlugins() error {
p.Processors = make(map[string]dkron.ExecutionProcessor)
// Look in /etc/dkron/plugins
processors, err := plugin.Discover("dkron-processor-*", filepath.Join("/etc", "dkron", "plugins"))
if err != nil {
return err
}
// Next, look in the same directory as the Dkron executable, usually
// /usr/local/bin. If found, this replaces what we found in the config path.
exePath, err := osext.Executable()
if err != nil {
logrus.WithError(err).Error("Error loading exe directory")
} else {
processors, err = plugin.Discover("dkron-processor-*", filepath.Dir(exePath))
if err != nil {
return err
}
}
for _, file := range processors {
// If the filename has a ".", trim up to there
// if idx := strings.Index(file, "."); idx >= 0 {
// file = file[:idx]
// }
// Look for foo-bar-baz. The plugin name is "baz"
parts := strings.SplitN(file, "-", 3)
if len(parts) != 3 {
continue
}
processor, _ := p.processorFactory(file)
p.Processors[parts[2]] = processor
}
return nil
}
func (p *Plugins) processorFactory(path string) (dkron.ExecutionProcessor, error) {
// Build the plugin client configuration and init the plugin
var config plugin.ClientConfig
config.Cmd = exec.Command(path)
config.HandshakeConfig = dkplugin.Handshake
config.Managed = true
config.Plugins = dkplugin.PluginMap
client := plugin.NewClient(&config)
// Request the RPC client so we can get the provider
// so we can build the actual RPC-implemented provider.
rpcClient, err := client.Client()
if err != nil {
return nil, err
}
raw, err := rpcClient.Dispense(dkplugin.ProcessorPluginName)
if err != nil {
return nil, err
}
return raw.(dkron.ExecutionProcessor), nil
}