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
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/hashicorp/go-plugin"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
)
type Supervisor struct {
pluginId string
client *plugin.Client
hooks Hooks
implemented [TotalHooksId]bool
}
func NewSupervisor(pluginInfo *model.BundleInfo, parentLogger *mlog.Logger, apiImpl API) (*Supervisor, error) {
supervisor := Supervisor{}
wrappedLogger := pluginInfo.WrapLogger(parentLogger)
hclogAdaptedLogger := &HclogAdapter{
wrappedLogger: wrappedLogger.WithCallerSkip(1),
extrasKey: "wrapped_extras",
}
pluginMap := map[string]plugin.Plugin{
"hooks": &HooksPlugin{
log: wrappedLogger,
apiImpl: apiImpl,
},
}
executable := filepath.Clean(filepath.Join(".", pluginInfo.Manifest.Backend.Executable))
if strings.HasPrefix(executable, "..") {
return nil, fmt.Errorf("invalid backend executable")
}
executable = filepath.Join(pluginInfo.Path, executable)
supervisor.client = plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: Handshake,
Plugins: pluginMap,
Cmd: exec.Command(executable),
SyncStdout: wrappedLogger.With(mlog.String("source", "plugin_stdout")).StdLogWriter(),
SyncStderr: wrappedLogger.With(mlog.String("source", "plugin_stderr")).StdLogWriter(),
Logger: hclogAdaptedLogger,
StartTimeout: time.Second * 3,
})
rpcClient, err := supervisor.client.Client()
if err != nil {
return nil, err
}
raw, err := rpcClient.Dispense("hooks")
if err != nil {
return nil, err
}
supervisor.hooks = raw.(Hooks)
if impl, err := supervisor.hooks.Implemented(); err != nil {
return nil, err
} else {
for _, hookName := range impl {
if hookId, ok := HookNameToId[hookName]; ok {
supervisor.implemented[hookId] = true
}
}
}
err = supervisor.Hooks().OnActivate()
if err != nil {
return nil, err
}
return &supervisor, nil
}
func (sup *Supervisor) Shutdown() {
sup.client.Kill()
}
func (sup *Supervisor) Hooks() Hooks {
return sup.hooks
}
func (sup *Supervisor) Implements(hookId int) bool {
return sup.implemented[hookId]
}
|