-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (73 loc) · 1.75 KB
/
main.go
File metadata and controls
87 lines (73 loc) · 1.75 KB
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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/hashicorp/go-hclog"
gop "github.com/hashicorp/go-plugin"
"github.com/pkg/errors"
"github.com/pipego/scheduler/plugin"
)
type config struct {
name string
path string
}
var (
configs = []config{
// Plugin: LocalHost
{
name: "LocalHost",
path: "./plugin/fetch-localhost",
},
// Plugin: MetalFlow
{
name: "MetalFlow",
path: "./plugin/fetch-metalflow",
},
}
handshake = gop.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "plugin",
MagicCookieValue: "plugin",
}
logger = hclog.New(&hclog.LoggerOptions{
Name: "plugin",
Output: os.Stderr,
Level: hclog.Error,
})
)
func main() {
for _, item := range configs {
p, _ := filepath.Abs(item.path)
if result, err := helper(p, item.name); err == nil {
fmt.Println(result.AllocatableResource.MilliCPU, result.AllocatableResource.Memory, result.AllocatableResource.Storage)
fmt.Println(result.RequestedResource.MilliCPU, result.RequestedResource.Memory, result.RequestedResource.Storage)
} else {
fmt.Println(err.Error())
}
}
}
func helper(path, name string) (plugin.FetchResult, error) {
plugins := map[string]gop.Plugin{
name: &plugin.Fetch{},
}
client := gop.NewClient(&gop.ClientConfig{
Cmd: exec.Command(path),
HandshakeConfig: handshake,
Logger: logger,
Plugins: plugins,
})
defer client.Kill()
rpcClient, err := client.Client()
if err != nil {
return plugin.FetchResult{}, errors.Wrap(err, "failed to init client")
}
raw, err := rpcClient.Dispense(name)
if err != nil {
return plugin.FetchResult{}, errors.Wrap(err, "failed to dispense instance")
}
n := raw.(plugin.FetchImpl)
result := n.Run("127.0.0.1")
return result, nil
}