2017-10-25 11:33:19 -05:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2018-06-25 12:33:13 -07:00
|
|
|
// See LICENSE.txt for license information.
|
2017-10-25 11:33:19 -05:00
|
|
|
|
2017-08-16 17:23:38 -05:00
|
|
|
package plugin
|
|
|
|
|
|
2018-06-25 12:33:13 -07:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
2018-07-17 18:47:05 -04:00
|
|
|
"runtime"
|
2018-06-25 12:33:13 -07:00
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-plugin"
|
|
|
|
|
"github.com/mattermost/mattermost-server/mlog"
|
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
|
|
|
)
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
type supervisor struct {
|
2018-06-25 12:33:13 -07:00
|
|
|
client *plugin.Client
|
|
|
|
|
hooks Hooks
|
|
|
|
|
implemented [TotalHooksId]bool
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 11:37:17 -04:00
|
|
|
func newSupervisor(pluginInfo *model.BundleInfo, parentLogger *mlog.Logger, apiImpl API) (retSupervisor *supervisor, retErr error) {
|
2018-07-13 10:29:50 -04:00
|
|
|
supervisor := supervisor{}
|
2018-07-27 11:37:17 -04:00
|
|
|
defer func() {
|
|
|
|
|
if retErr != nil {
|
|
|
|
|
supervisor.Shutdown()
|
|
|
|
|
}
|
|
|
|
|
}()
|
2018-06-25 12:33:13 -07:00
|
|
|
|
|
|
|
|
wrappedLogger := pluginInfo.WrapLogger(parentLogger)
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
hclogAdaptedLogger := &hclogAdapter{
|
2018-07-03 09:58:28 -07:00
|
|
|
wrappedLogger: wrappedLogger.WithCallerSkip(1),
|
2018-06-25 12:33:13 -07:00
|
|
|
extrasKey: "wrapped_extras",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pluginMap := map[string]plugin.Plugin{
|
2018-07-13 10:29:50 -04:00
|
|
|
"hooks": &hooksPlugin{
|
2018-06-25 12:33:13 -07:00
|
|
|
log: wrappedLogger,
|
|
|
|
|
apiImpl: apiImpl,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 18:47:05 -04:00
|
|
|
executable := filepath.Clean(filepath.Join(
|
|
|
|
|
".",
|
|
|
|
|
pluginInfo.Manifest.GetExecutableForRuntime(runtime.GOOS, runtime.GOARCH),
|
|
|
|
|
))
|
2018-06-25 12:33:13 -07:00
|
|
|
if strings.HasPrefix(executable, "..") {
|
|
|
|
|
return nil, fmt.Errorf("invalid backend executable")
|
|
|
|
|
}
|
|
|
|
|
executable = filepath.Join(pluginInfo.Path, executable)
|
|
|
|
|
|
|
|
|
|
supervisor.client = plugin.NewClient(&plugin.ClientConfig{
|
2018-07-13 10:29:50 -04:00
|
|
|
HandshakeConfig: handshake,
|
2018-06-25 12:33:13 -07:00
|
|
|
Plugins: pluginMap,
|
|
|
|
|
Cmd: exec.Command(executable),
|
2018-07-03 09:58:28 -07:00
|
|
|
SyncStdout: wrappedLogger.With(mlog.String("source", "plugin_stdout")).StdLogWriter(),
|
|
|
|
|
SyncStderr: wrappedLogger.With(mlog.String("source", "plugin_stderr")).StdLogWriter(),
|
2018-06-25 12:33:13 -07:00
|
|
|
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 {
|
2018-07-13 10:29:50 -04:00
|
|
|
if hookId, ok := hookNameToId[hookName]; ok {
|
2018-06-25 12:33:13 -07:00
|
|
|
supervisor.implemented[hookId] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = supervisor.Hooks().OnActivate()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &supervisor, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (sup *supervisor) Shutdown() {
|
2018-07-27 11:37:17 -04:00
|
|
|
if sup.client != nil {
|
|
|
|
|
sup.client.Kill()
|
|
|
|
|
}
|
2018-06-25 12:33:13 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (sup *supervisor) Hooks() Hooks {
|
2018-06-25 12:33:13 -07:00
|
|
|
return sup.hooks
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (sup *supervisor) Implements(hookId int) bool {
|
2018-06-25 12:33:13 -07:00
|
|
|
return sup.implemented[hookId]
|
2017-08-16 17:23:38 -05:00
|
|
|
}
|