-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmanager.go
152 lines (117 loc) · 3.25 KB
/
manager.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
package plugin
import (
"errors"
"log"
"os/exec"
"path/filepath"
"strings"
"sync"
plugin "github.com/hashicorp/go-plugin"
)
type PluginInfo struct {
ID string
Path string
Client *plugin.Client
}
func NewManager(ptype, glob, dir string, pluginImpl plugin.Plugin) *Manager {
manager := &Manager{
Type: ptype,
Glob: glob,
Path: dir,
Plugins: map[string]*PluginInfo{},
pluginImpl: pluginImpl,
}
return manager
}
// Manager handles lifecycle of plugin mgmt for different plugin types. In This
// example we have two plugin types: greeter and clubber, both of which have
// multiple implementations.
type Manager struct {
Type string // id for types of plugins this manager deals with
Glob string // glob match for plugin filenames
Path string // path for plugins
Plugins map[string]*PluginInfo // Info for foudn plugins
initialized bool // has been initialized
pluginImpl plugin.Plugin // Plugin implementation dummy interface
}
func (m *Manager) Init() error {
// discover plugin abs paths
plugins, err := plugin.Discover(m.Glob, m.Path)
if err != nil {
return err
}
// grab all PluginInfos
for _, plugin := range plugins {
// use glob trailing * to get us a friendly plugin id name
_, file := filepath.Split(plugin)
globAsterix := strings.LastIndex(m.Glob, "*")
trim := m.Glob[0:globAsterix]
id := strings.TrimPrefix(file, trim)
// add to our slice of plugin info
m.Plugins[id] = &PluginInfo{
ID: id,
Path: plugin,
}
}
m.initialized = true
return nil
}
func (m *Manager) Launch() error {
for id, info := range m.Plugins {
log.Printf("Registering plugin client for type=%s, id=%s, impl=%s", m.Type, id, info.Path)
// create new client
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: HandshakeConfig,
Plugins: m.pluginMap(id),
Cmd: exec.Command(info.Path),
})
if _, ok := m.Plugins[id]; !ok {
// if not found, ignore?
continue
}
pinfo := m.Plugins[id]
pinfo.Client = client
}
return nil
}
func (m *Manager) Dispose() {
var wg sync.WaitGroup
for _, pinfo := range m.Plugins {
wg.Add(1)
go func(client *plugin.Client) {
client.Kill()
wg.Done()
}(pinfo.Client)
}
wg.Wait()
}
func (m *Manager) GetInterface(id string) (interface{}, error) {
if _, ok := m.Plugins[id]; !ok {
return nil, errors.New("Plugin ID not found in registered plugins!")
}
// Grab registerd plugin.Client
client := m.Plugins[id].Client
// Connect via RPC
rpcClient, err := client.Client()
if err != nil {
return nil, err
}
// Request the plugin
raw, err := rpcClient.Dispense(id)
if err != nil {
return nil, err
}
return raw, nil
}
// pluginMap should be used by clients for the map of plugins.
// This maps types of plugins to interface implementations. Since each
// plugin.Client we have registers one binary, we set a 1:1 mapping between
// plugin type (here treated as a name/id) to its implementation.
func (m *Manager) pluginMap(id string) map[string]plugin.Plugin {
pmap := map[string]plugin.Plugin{}
// for _, pinfo := range m.Plugins {
// pmap[pinfo.ID] = m.pluginImpl
// }
pmap[id] = m.pluginImpl
return pmap
}