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"
|
2020-01-24 09:00:35 +05:30
|
|
|
"sync"
|
2018-06-25 12:33:13 -07:00
|
|
|
"time"
|
|
|
|
|
|
2019-01-30 18:55:24 +01:00
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2021-01-07 22:42:43 +05:30
|
|
|
|
2020-02-14 15:47:43 -05:00
|
|
|
"github.com/mattermost/mattermost-server/v5/einterfaces"
|
2019-11-28 14:39:38 +01:00
|
|
|
"github.com/mattermost/mattermost-server/v5/mlog"
|
|
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
2018-06-25 12:33:13 -07:00
|
|
|
)
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
type supervisor struct {
|
2020-01-24 09:00:35 +05:30
|
|
|
lock sync.RWMutex
|
2018-06-25 12:33:13 -07:00
|
|
|
client *plugin.Client
|
|
|
|
|
hooks Hooks
|
|
|
|
|
implemented [TotalHooksId]bool
|
2019-05-09 16:08:31 -04:00
|
|
|
pid int
|
2018-06-25 12:33:13 -07:00
|
|
|
}
|
|
|
|
|
|
2020-02-14 15:47:43 -05:00
|
|
|
func newSupervisor(pluginInfo *model.BundleInfo, apiImpl API, parentLogger *mlog.Logger, metrics einterfaces.MetricsInterface) (retSupervisor *supervisor, retErr error) {
|
2019-01-30 18:55:24 +01:00
|
|
|
sup := supervisor{}
|
2018-07-27 11:37:17 -04:00
|
|
|
defer func() {
|
|
|
|
|
if retErr != nil {
|
2019-01-30 18:55:24 +01:00
|
|
|
sup.Shutdown()
|
2018-07-27 11:37:17 -04:00
|
|
|
}
|
|
|
|
|
}()
|
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,
|
2020-02-14 15:47:43 -05:00
|
|
|
apiImpl: &apiTimerLayer{pluginInfo.Manifest.Id, apiImpl, metrics},
|
2018-06-25 12:33:13 -07:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
|
2019-05-09 16:08:31 -04:00
|
|
|
cmd := exec.Command(executable)
|
|
|
|
|
|
2019-01-30 18:55:24 +01:00
|
|
|
sup.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,
|
2019-05-09 16:08:31 -04:00
|
|
|
Cmd: cmd,
|
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,
|
|
|
|
|
})
|
|
|
|
|
|
2019-01-30 18:55:24 +01:00
|
|
|
rpcClient, err := sup.client.Client()
|
2018-06-25 12:33:13 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 16:08:31 -04:00
|
|
|
sup.pid = cmd.Process.Pid
|
|
|
|
|
|
2018-06-25 12:33:13 -07:00
|
|
|
raw, err := rpcClient.Dispense("hooks")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 15:47:43 -05:00
|
|
|
sup.hooks = &hooksTimerLayer{pluginInfo.Manifest.Id, raw.(Hooks), metrics}
|
2018-06-25 12:33:13 -07:00
|
|
|
|
2019-01-30 18:55:24 +01:00
|
|
|
impl, err := sup.hooks.Implemented()
|
|
|
|
|
if err != nil {
|
2018-06-25 12:33:13 -07:00
|
|
|
return nil, err
|
2019-01-30 18:55:24 +01:00
|
|
|
}
|
|
|
|
|
for _, hookName := range impl {
|
|
|
|
|
if hookId, ok := hookNameToId[hookName]; ok {
|
|
|
|
|
sup.implemented[hookId] = true
|
2018-06-25 12:33:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 18:55:24 +01:00
|
|
|
return &sup, nil
|
2018-06-25 12:33:13 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (sup *supervisor) Shutdown() {
|
2020-01-24 09:00:35 +05:30
|
|
|
sup.lock.RLock()
|
|
|
|
|
defer sup.lock.RUnlock()
|
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 {
|
2020-01-24 09:00:35 +05:30
|
|
|
sup.lock.RLock()
|
|
|
|
|
defer sup.lock.RUnlock()
|
2018-06-25 12:33:13 -07:00
|
|
|
return sup.hooks
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 18:38:25 -03:00
|
|
|
// PerformHealthCheck checks the plugin through an an RPC ping.
|
2019-05-09 16:08:31 -04:00
|
|
|
func (sup *supervisor) PerformHealthCheck() error {
|
2020-01-24 09:00:35 +05:30
|
|
|
// No need for a lock here because Ping is read-locked.
|
2019-05-09 16:08:31 -04:00
|
|
|
if pingErr := sup.Ping(); pingErr != nil {
|
2021-01-04 11:32:29 +05:30
|
|
|
for pingFails := 1; pingFails < HealthCheckPingFailLimit; pingFails++ {
|
2019-05-09 16:08:31 -04:00
|
|
|
pingErr = sup.Ping()
|
|
|
|
|
if pingErr == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if pingErr != nil {
|
2019-09-12 15:31:09 +02:00
|
|
|
mlog.Debug("Error pinging plugin", mlog.Err(pingErr))
|
2019-05-09 16:08:31 -04:00
|
|
|
return fmt.Errorf("Plugin RPC connection is not responding")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ping checks that the RPC connection with the plugin is alive and healthy.
|
|
|
|
|
func (sup *supervisor) Ping() error {
|
2020-01-24 09:00:35 +05:30
|
|
|
sup.lock.RLock()
|
|
|
|
|
defer sup.lock.RUnlock()
|
2019-05-09 16:08:31 -04:00
|
|
|
client, err := sup.client.Client()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return client.Ping()
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (sup *supervisor) Implements(hookId int) bool {
|
2020-01-24 09:00:35 +05:30
|
|
|
sup.lock.RLock()
|
|
|
|
|
defer sup.lock.RUnlock()
|
2018-06-25 12:33:13 -07:00
|
|
|
return sup.implemented[hookId]
|
2017-08-16 17:23:38 -05:00
|
|
|
}
|