This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
pluginserver.go
315 lines (266 loc) · 6.52 KB
/
pluginserver.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
package main
import (
"fmt"
"os"
"path"
"plugin"
"reflect"
"strings"
"sync"
"time"
)
// --- PluginServer --- //
// Holds the execution status of the plugin server.
type PluginServer struct {
lock sync.RWMutex
pluginsDir string
plugins map[string]*pluginData
instances map[int]*instanceData
events map[int]*eventData
nextInstanceId int
nextEventId int
}
// Create a new server context.
func newServer() *PluginServer {
s := PluginServer{
plugins: map[string]*pluginData{},
instances: map[int]*instanceData{},
events: map[int]*eventData{},
}
if *pluginsDir == "" {
if dir, err := os.Getwd(); err == nil {
s.pluginsDir = dir
}
} else {
s.pluginsDir = *pluginsDir
}
return &s
}
// SetPluginDir tells the server where to find the plugins.
//
// RPC exported method
func (s *PluginServer) SetPluginDir(dir string, reply *string) error {
s.lock.Lock()
s.pluginsDir = dir
s.lock.Unlock()
*reply = "ok"
return nil
}
// --- status --- //
type ServerStatusData struct {
Pid int
Plugins map[string]PluginStatusData
}
func (s *PluginServer) GetStatus(n int, reply *ServerStatusData) error {
s.lock.Lock()
defer s.lock.Unlock()
*reply = ServerStatusData{
Pid: os.Getpid(),
Plugins: make(map[string]PluginStatusData),
}
var err error
for pluginname := range s.plugins {
reply.Plugins[pluginname], err = s.getPluginStatus(pluginname)
if err != nil {
return err
}
}
return nil
}
// --- pluginData --- //
type pluginData struct {
lock sync.Mutex
name string
code *plugin.Plugin
modtime time.Time
loadtime time.Time
constructor func() interface{}
config interface{}
lastStartInstance time.Time
lastCloseInstance time.Time
}
func getModTime(fname string) (modtime time.Time, err error) {
finfo, err := os.Stat(fname)
if err != nil {
return
}
modtime = finfo.ModTime()
return
}
func (s *PluginServer) loadPlugin(name string) (plug *pluginData, err error) {
s.lock.Lock()
defer s.lock.Unlock()
plug, ok := s.plugins[name]
if ok {
return
}
plugFName := path.Join(s.pluginsDir, name+".so")
plugModTime, err := getModTime(plugFName)
if err != nil {
return
}
code, err := plugin.Open(plugFName)
if err != nil {
err = fmt.Errorf("failed to open plugin %s: %w", name, err)
return
}
constructorSymbol, err := code.Lookup("New")
if err != nil {
err = fmt.Errorf("No constructor function on plugin %s: %w", name, err)
return
}
constructor, ok := constructorSymbol.(func() interface{})
if !ok {
err = fmt.Errorf("Wrong constructor signature on plugin %s: %w", name, err)
return
}
plug = &pluginData{
name: name,
code: code,
modtime: plugModTime,
loadtime: time.Now(),
constructor: constructor,
config: constructor(),
}
s.plugins[name] = plug
return
}
type schemaDict map[string]interface{}
func getSchemaDict(t reflect.Type) schemaDict {
switch t.Kind() {
case reflect.String:
return schemaDict{"type": "string"}
case reflect.Bool:
return schemaDict{"type": "boolean"}
case reflect.Int, reflect.Int32:
return schemaDict{"type": "integer"}
case reflect.Uint, reflect.Uint32:
return schemaDict{
"type": "integer",
"between": []int{0, 2147483648},
}
case reflect.Float32, reflect.Float64:
return schemaDict{"type": "number"}
case reflect.Slice:
elemType := getSchemaDict(t.Elem())
if elemType == nil {
break
}
return schemaDict{
"type": "array",
"elements": elemType,
}
case reflect.Map:
kType := getSchemaDict(t.Key())
vType := getSchemaDict(t.Elem())
if kType == nil || vType == nil {
break
}
return schemaDict{
"type": "map",
"keys": kType,
"values": vType,
}
case reflect.Struct:
fieldsArray := []schemaDict{}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
typeDecl := getSchemaDict(field.Type)
if typeDecl == nil {
// ignore unrepresentable types
continue
}
name := field.Tag.Get("json")
if name == "" {
name = strings.ToLower(field.Name)
}
fieldsArray = append(fieldsArray, schemaDict{name: typeDecl})
}
return schemaDict{
"type": "record",
"fields": fieldsArray,
}
}
return nil
}
// Information obtained from a plugin's compiled code.
type PluginInfo struct {
Name string // plugin name
ModTime time.Time `codec:",omitempty"` // plugin file modification time
LoadTime time.Time `codec:",omitempty"` // plugin load time
Phases []string // events it can handle
Version string // version number
Priority int // priority info
Schema schemaDict // JSON representation of the config schema
}
// GetPluginInfo loads and retrieves information from the compiled plugin.
// TODO: reload if the plugin code has been updated.
//
// RPC exported method
func (s *PluginServer) GetPluginInfo(name string, info *PluginInfo) error {
plug, err := s.loadPlugin(name)
if err != nil {
return err
}
*info = PluginInfo{Name: name}
plug.lock.Lock()
defer plug.lock.Unlock()
handlers := getHandlers(plug.config)
info.Phases = make([]string, len(handlers))
var i = 0
for name := range handlers {
info.Phases[i] = name
i++
}
v, _ := plug.code.Lookup("Version")
if v != nil {
info.Version = *v.(*string)
}
prio, _ := plug.code.Lookup("Priority")
if prio != nil {
info.Priority = *prio.(*int)
}
// st, _ := getSchemaDict(reflect.TypeOf(plug.config).Elem())
info.Schema = schemaDict{
"name": name,
"fields": []schemaDict{
schemaDict{"config": getSchemaDict(reflect.TypeOf(plug.config).Elem())},
},
}
return nil
}
type PluginStatusData struct {
Name string
Modtime int64
LoadTime int64
Instances []InstanceStatus
LastStartInstance int64
LastCloseInstance int64
}
func (s *PluginServer) getPluginStatus(name string) (status PluginStatusData, err error) {
plug, ok := s.plugins[name]
if !ok {
err = fmt.Errorf("plugin %#v not loaded", name)
return
}
instances := []InstanceStatus{}
for _, instance := range s.instances {
if instance.plugin == plug {
instances = append(instances, InstanceStatus{
Name: name,
Id: instance.id,
Config: instance.config,
StartTime: instance.startTime.Unix(),
})
}
}
status = PluginStatusData{
Name: name,
Modtime: plug.modtime.Unix(),
LoadTime: plug.loadtime.Unix(),
Instances: instances,
LastStartInstance: plug.lastStartInstance.Unix(),
LastCloseInstance: plug.lastCloseInstance.Unix(),
}
return
}