-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluginator.go
413 lines (362 loc) · 9.81 KB
/
pluginator.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
// Copyright Piero de Salvia.
// All Rights Reserved
// Package pluginator is a low-level plugin manager, working on go source code from the file system or consul
package pluginator
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"plugin"
"strconv"
"strings"
"github.com/fsnotify/fsnotify"
)
// PluginContent is sent on pluginator events. It contains the actual library that was loaded and its source code.
type PluginContent struct {
Lib *plugin.Plugin
Code string
}
// Pluginator is lib's entry point
type Pluginator struct {
pluginDir string
watcher *fsnotify.Watcher
tempDir string
plugins map[string]*PluginContent
scanSubscribers []func(map[string]*PluginContent)
updateSubscribers []func(string, *PluginContent)
removeSubscribers []func(string, *PluginContent)
addSubscribers []func(string, *PluginContent)
consulWatcher *consulWatcher
consulHost string
consulPort int
consulKeyPrefix string
}
// NewPluginatorC instantiates a new Pluginator, watching the subkeys of keyPrefix on the host:port consul instance
func NewPluginatorC(host string, port int, keyPrefix string) (*Pluginator, error) {
err := checkGoToolchain()
if err != nil {
return nil, err
}
PluginDir, err := ioutil.TempDir("", "pluginator-consul")
if err != nil {
return nil, err
}
p := &Pluginator{
pluginDir: PluginDir,
consulHost: host,
consulPort: port,
consulKeyPrefix: keyPrefix,
plugins: make(map[string]*PluginContent),
}
p.tempDir, err = ioutil.TempDir("", "pluginator")
if err != nil {
return nil, err
}
return p, nil
}
// NewPluginatorF instantiates a new Pluginator, watching the PluginDir diretory
func NewPluginatorF(PluginDir string) (*Pluginator, error) {
err := checkGoToolchain()
if err != nil {
return nil, err
}
if strings.HasSuffix(PluginDir, "/") {
return nil, errors.New("Plugin dir must not end with /")
}
f, err := os.Stat(PluginDir)
if err != nil {
return nil, err
}
if !f.Mode().IsDir() {
return nil, errors.New(PluginDir + " is not a directory")
}
p := &Pluginator{
pluginDir: PluginDir,
plugins: make(map[string]*PluginContent),
}
p.tempDir, err = ioutil.TempDir("", "pluginator")
if err != nil {
return nil, err
}
return p, nil
}
func checkGoToolchain() error {
command := exec.Command("go", "version")
out, err := command.Output()
if err != nil {
return err
}
outSplit := strings.Split(string(out), " ")
if len(outSplit) < 4 {
return errors.New("cannot parse output from go version")
}
versionSplit := strings.Split(outSplit[2], ".")
majV := []rune(versionSplit[0])[2]
majVI, err := strconv.Atoi(string(majV))
if err != nil {
return err
}
minV := versionSplit[1]
minVI, err := strconv.Atoi(minV)
if err != nil {
return err
}
osVers := outSplit[3]
if majVI < 1 || minVI < 8 || !strings.HasPrefix(osVers, "linux") {
return errors.New("Bad go version - need 1.8 or higher on linux")
}
return nil
}
// SubscribeScan subscribes its argument to scan events (they happen at start time)
func (p *Pluginator) SubscribeScan(f func(map[string]*PluginContent)) {
p.scanSubscribers = append(p.scanSubscribers, f)
}
// SubscribeUpdate subscribe its argument to update events (changes in plugin code)
func (p *Pluginator) SubscribeUpdate(f func(string, *PluginContent)) {
p.updateSubscribers = append(p.updateSubscribers, f)
}
// SubscribeRemove subscribes its argument to remove events (plugin removal)
func (p *Pluginator) SubscribeRemove(f func(string, *PluginContent)) {
p.removeSubscribers = append(p.removeSubscribers, f)
}
// SubscribeAdd subscribes its argument to add events (plugin adds)
func (p *Pluginator) SubscribeAdd(f func(string, *PluginContent)) {
p.addSubscribers = append(p.addSubscribers, f)
}
// Start start a Pluginator. It will perform a scan of the watched dir/consul key
func (p *Pluginator) Start() error {
var msg string
if p.consulHost != "" {
msg = p.consulHost + ":" + strconv.Itoa(p.consulPort) + ":" + p.consulKeyPrefix
} else {
msg = p.pluginDir
}
log.Println("Watching ", msg)
if p.consulHost != "" {
var err error
p.consulWatcher, err = p.watchConsul()
if err != nil {
return err
}
}
var err error
p.watcher, err = p.watch(p.pluginDir)
if err != nil {
return err
}
p.scan()
return nil
}
// Terminate makes a Pluginator stop watching a directory/consul key
func (p *Pluginator) Terminate() {
if p.consulWatcher != nil {
p.consulWatcher.Terminate()
}
err := p.watcher.Close()
if err != nil {
log.Println(err)
}
}
func (p *Pluginator) watch(fileName string) (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
go func() {
for {
select {
case event := <-watcher.Events:
switch event.Op {
case fsnotify.Write:
fileInfo, err := os.Lstat(event.Name)
if err != nil {
log.Println(err)
break
}
if !p.isCompileUnit(fileInfo) {
break
}
log.Println("Reloading ", fileInfo.Name())
name, pluginLib, err := p.processPlugin(fileInfo)
if err != nil {
log.Println(err)
break
}
for _, subscriber := range p.updateSubscribers {
subscriber(name, pluginLib)
}
case fsnotify.Create:
fileInfo, err := os.Lstat(event.Name)
if err != nil {
log.Println(err)
break
}
if !p.isCompileUnit(fileInfo) {
break
}
log.Println("Discovered ", fileInfo.Name())
name, pluginLib, err := p.processPlugin(fileInfo)
if err != nil {
log.Println(err)
break
}
for _, subscriber := range p.addSubscribers {
subscriber(name, pluginLib)
}
case fsnotify.Rename:
fallthrough
case fsnotify.Remove:
baseName := strings.TrimPrefix(event.Name, p.pluginDir+"/")
baseName = strings.TrimSuffix(baseName, ".go")
if pluginLib, exists := p.plugins[baseName]; exists {
for _, subscriber := range p.removeSubscribers {
subscriber(baseName, pluginLib)
}
delete(p.plugins, baseName)
}
log.Println("Removed ", baseName)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()
err = watcher.Add(fileName)
if err != nil {
return nil, err
}
return watcher, nil
}
/*
scan will scan the whole plugin directory for .go files, compile them, load them and notify scan subscribers.
*/
func (p *Pluginator) scan() {
files, err := ioutil.ReadDir(p.pluginDir)
if err != nil {
log.Println(err)
return
}
for _, file := range files {
if p.isCompileUnit(file) {
log.Println("Discovered ", file.Name())
_, _, err = p.processPlugin(file)
if err != nil {
log.Println(err)
return
}
}
}
for _, scanSubscriber := range p.scanSubscribers {
scanSubscriber(p.plugins)
}
}
func (p *Pluginator) isCompileUnit(file os.FileInfo) bool {
return !file.IsDir() && strings.HasSuffix(file.Name(), ".go")
}
func (p *Pluginator) processPlugin(file os.FileInfo) (string, *PluginContent, error) {
var baseName string
var pluginLib *plugin.Plugin
baseName = strings.TrimSuffix(file.Name(), ".go")
var err error
pluginLib, err = p.compileAndLoad(baseName)
if err != nil {
return "", nil, err
}
code, err := ioutil.ReadFile(p.pluginDir + "/" + file.Name())
if err != nil {
return "", nil, err
}
pc := PluginContent{
Lib: pluginLib,
Code: string(code),
}
p.plugins[baseName] = &pc
return baseName, &pc, nil
}
func (p *Pluginator) compileAndLoad(baseName string) (*plugin.Plugin, error) {
version, err := p.genVersionedName(baseName)
if err != nil {
return nil, err
}
address := fmt.Sprintf("%p", &p)
pPath := "\"" + "-pluginpath=" + address + baseName + version + "\""
command := exec.Command("go", "build", "-ldflags", pPath, "-buildmode=plugin", "-o", p.tempDir+"/"+baseName+"."+version+".so", p.pluginDir+"/"+baseName+".go")
var stdErr bytes.Buffer
command.Stderr = &stdErr
_, err = command.Output()
if err != nil {
return nil, errors.New(stdErr.String())
}
pluginLib, err := plugin.Open(p.tempDir + "/" + baseName + "." + version + ".so")
if err != nil {
return nil, err
}
log.Println("Loaded ", baseName+"."+version+".so")
return pluginLib, nil
}
func (p *Pluginator) genVersionedName(baseName string) (string, error) {
files, err := ioutil.ReadDir(p.tempDir)
if err != nil {
log.Println(err)
return "", err
}
latestVersion := baseName
for _, file := range files {
if !file.IsDir() && strings.HasPrefix(file.Name(), baseName) && strings.HasSuffix(file.Name(), ".so") && file.Name() > latestVersion {
latestVersion = file.Name()
}
}
if latestVersion != baseName {
fSplit := strings.Split(latestVersion, ".")
ver := fSplit[1]
verI, err := strconv.Atoi(ver)
if err != nil {
return "", err
}
verI++
return fmt.Sprintf("%09d", verI), nil
}
return fmt.Sprintf("%09d", 0), nil
}
func (p *Pluginator) watchConsul() (*consulWatcher, error) {
cw, err := newConsulWatcher(p.consulHost, p.consulPort, p.consulKeyPrefix)
if err != nil {
return nil, err
}
go func() {
for {
select {
case event := <-p.consulWatcher.Events:
switch event.Action {
case consulAddAction:
if !strings.HasSuffix(event.Key, ".go") {
log.Println("Bad plugin name must end in .go: ", event.Key)
break
}
p.materializeKV(event.Key, event.Value)
case consulUpdateAction:
p.materializeKV(event.Key, event.Value)
case consulRemoveAction:
p.unMaterializeK(event.Key)
}
}
}
}()
return cw, nil
}
func (p *Pluginator) materializeKV(key, value string) {
key = strings.TrimPrefix(key, p.consulKeyPrefix+".")
if err := ioutil.WriteFile(p.pluginDir+"/"+key, []byte(value), os.ModePerm); err != nil {
log.Println(err)
}
}
func (p *Pluginator) unMaterializeK(key string) {
key = strings.TrimPrefix(key, p.consulKeyPrefix+".")
if err := os.Remove(p.pluginDir + "/" + key); err != nil {
log.Println(err)
}
}