-
Notifications
You must be signed in to change notification settings - Fork 8
/
plugins.go
84 lines (73 loc) · 1.99 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
package gripper
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/hashicorp/go-plugin"
"github.com/kennygrant/sanitize"
grpc "google.golang.org/grpc"
)
var Handshake = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "GRIP_PLUGIN_EXTERNAL_RESOURCE",
MagicCookieValue: "gripper",
}
type GripPlugin struct {
plugin.Plugin
Impl GRIPSourceServer
}
var PluginMap = map[string]plugin.Plugin{
"gripper": &GripPlugin{},
}
func (p *GripPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
RegisterGRIPSourceServer(s, p.Impl)
return nil
}
func (p *GripPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return NewGRIPSourceClient(c), nil
}
func LaunchPluginClient(plugindir string, name string, workdir string, params map[string]string) (*plugin.Client, error) {
name = sanitize.BaseName(name)
plugPath, err := filepath.Abs(filepath.Join(plugindir, "gripper-"+name))
if err != nil {
return nil, fmt.Errorf("plugin %s not found", name)
}
if _, err := os.Stat(plugPath); err != nil {
return nil, fmt.Errorf("plugin %s not found", name)
}
confPath := filepath.Join(workdir, "conf.json")
message, err := json.Marshal(params)
if err != nil {
return nil, err
}
err = ioutil.WriteFile(confPath, message, 0644)
if err != nil {
return nil, err
}
client := plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: Handshake,
Plugins: PluginMap,
Cmd: exec.Command(plugPath, confPath), //FIXME
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
})
_, err = client.Start()
return client, err
}
func GetSourceInterface(client *plugin.Client) (GRIPSourceClient, error) {
// Connect via GRPC
rpcClient, err := client.Client()
if err != nil {
return nil, err
}
// Request the plugin
raw, err := rpcClient.Dispense("gripper")
if err != nil {
return nil, err
}
sourceClient := raw.(GRIPSourceClient)
return sourceClient, nil
}