-
Notifications
You must be signed in to change notification settings - Fork 649
/
factory.go
79 lines (69 loc) · 1.55 KB
/
factory.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package rpcchainvm
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os/exec"
"github.com/ava-labs/avalanchego/snow"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
)
var (
errWrongVM = errors.New("wrong vm type")
)
// Factory ...
type Factory struct {
Path string
Config string
}
// New ...
func (f *Factory) New(ctx *snow.Context) (interface{}, error) {
// Ignore warning from launching an executable with a variable command
// because the command is a controlled and required input
// #nosec G204
config := &plugin.ClientConfig{
HandshakeConfig: Handshake,
Plugins: PluginMap,
Cmd: exec.Command(f.Path, fmt.Sprintf("--config=%s", f.Config)),
AllowedProtocols: []plugin.Protocol{
plugin.ProtocolNetRPC,
plugin.ProtocolGRPC,
},
}
if ctx != nil {
log.SetOutput(ctx.Log)
config.Stderr = ctx.Log
config.Logger = hclog.New(&hclog.LoggerOptions{
Output: ctx.Log,
Level: hclog.Info,
})
} else {
log.SetOutput(ioutil.Discard)
config.Stderr = ioutil.Discard
config.Logger = hclog.New(&hclog.LoggerOptions{
Output: ioutil.Discard,
})
}
client := plugin.NewClient(config)
rpcClient, err := client.Client()
if err != nil {
client.Kill()
return nil, err
}
raw, err := rpcClient.Dispense("vm")
if err != nil {
client.Kill()
return nil, err
}
vm, ok := raw.(*VMClient)
if !ok {
client.Kill()
return nil, errWrongVM
}
vm.SetProcess(client)
vm.ctx = ctx
return vm, nil
}