mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* allow `Wait()`ing on the supervisor In the event the plugin supervisor shuts down a plugin for crashing too many times, the new `Wait()` interface allows the `ActivatePlugin` to accept a callback function to trigger when `supervisor.Wait()` returns. If the supervisor shuts down normally, this callback is invoked with a nil error, otherwise any error reported by the supervisor is passed along. * improve plugin activation/deactivation logic Avoid triggering activation of previously failed-to-start plugins just becase something in the configuration changed. Now, intelligently compare the global enable bit as well as the each individual plugin's enabled bit. * expose store to manipulate PluginStatuses * expose API to fetch plugin statuses * keep track of whether or not plugin sandboxing is supported * transition plugin statuses * restore error on plugin activation if already active * don't initialize test plugins until successfully loaded * emit websocket events when plugin statuses change * skip pruning if already initialized * MM-8622: maintain plugin statuses in memory Switch away from persisting plugin statuses to the database, and maintain in memory instead. This will be followed by a cluster interface to query the in-memory status of plugin statuses from all cluster nodes. At the same time, rename `cluster_discovery_id` on the `PluginStatus` model object to `cluster_id`. * MM-8622: aggregate plugin statuses across cluster * fetch cluster plugin statuses when emitting websocket notification * address unit test fixes after rebasing * relax (poor) racey unit test re: supervisor.Wait() * make store-mocks
397 lines
11 KiB
Go
397 lines
11 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
// Package pluginenv provides high level functionality for discovering and launching plugins.
|
|
package pluginenv
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
"github.com/mattermost/mattermost-server/plugin"
|
|
)
|
|
|
|
type APIProviderFunc func(*model.Manifest) (plugin.API, error)
|
|
type SupervisorProviderFunc func(*model.BundleInfo) (plugin.Supervisor, error)
|
|
|
|
type ActivePlugin struct {
|
|
BundleInfo *model.BundleInfo
|
|
Supervisor plugin.Supervisor
|
|
}
|
|
|
|
// Environment represents an environment that plugins are discovered and launched in.
|
|
type Environment struct {
|
|
searchPath string
|
|
webappPath string
|
|
apiProvider APIProviderFunc
|
|
supervisorProvider SupervisorProviderFunc
|
|
activePlugins map[string]ActivePlugin
|
|
mutex sync.RWMutex
|
|
}
|
|
|
|
type Option func(*Environment)
|
|
|
|
// Creates a new environment. At a minimum, the APIProvider and SearchPath options are required.
|
|
func New(options ...Option) (*Environment, error) {
|
|
env := &Environment{
|
|
activePlugins: make(map[string]ActivePlugin),
|
|
}
|
|
for _, opt := range options {
|
|
opt(env)
|
|
}
|
|
if env.supervisorProvider == nil {
|
|
env.supervisorProvider = DefaultSupervisorProvider
|
|
}
|
|
if env.searchPath == "" {
|
|
return nil, fmt.Errorf("a search path must be provided")
|
|
}
|
|
return env, nil
|
|
}
|
|
|
|
// Returns the configured webapp path.
|
|
func (env *Environment) WebappPath() string {
|
|
return env.webappPath
|
|
}
|
|
|
|
// Returns the configured search path.
|
|
func (env *Environment) SearchPath() string {
|
|
return env.searchPath
|
|
}
|
|
|
|
// Returns a list of all plugins found within the environment.
|
|
func (env *Environment) Plugins() ([]*model.BundleInfo, error) {
|
|
return ScanSearchPath(env.searchPath)
|
|
}
|
|
|
|
// Returns a list of all currently active plugins within the environment.
|
|
func (env *Environment) ActivePlugins() []*model.BundleInfo {
|
|
env.mutex.RLock()
|
|
defer env.mutex.RUnlock()
|
|
|
|
activePlugins := []*model.BundleInfo{}
|
|
for _, p := range env.activePlugins {
|
|
activePlugins = append(activePlugins, p.BundleInfo)
|
|
}
|
|
|
|
return activePlugins
|
|
}
|
|
|
|
// Returns the ids of the currently active plugins.
|
|
func (env *Environment) ActivePluginIds() (ids []string) {
|
|
env.mutex.RLock()
|
|
defer env.mutex.RUnlock()
|
|
|
|
for id := range env.activePlugins {
|
|
ids = append(ids, id)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Returns true if the plugin is active, false otherwise.
|
|
func (env *Environment) IsPluginActive(pluginId string) bool {
|
|
env.mutex.RLock()
|
|
defer env.mutex.RUnlock()
|
|
|
|
for id := range env.activePlugins {
|
|
if id == pluginId {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// Activates the plugin with the given id.
|
|
func (env *Environment) ActivatePlugin(id string, onError func(error)) error {
|
|
env.mutex.Lock()
|
|
defer env.mutex.Unlock()
|
|
|
|
if !plugin.IsValidId(id) {
|
|
return fmt.Errorf("invalid plugin id: %s", id)
|
|
}
|
|
|
|
if _, ok := env.activePlugins[id]; ok {
|
|
return fmt.Errorf("plugin already active: %v", id)
|
|
}
|
|
plugins, err := ScanSearchPath(env.searchPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var bundle *model.BundleInfo
|
|
for _, p := range plugins {
|
|
if p.Manifest != nil && p.Manifest.Id == id {
|
|
if bundle != nil {
|
|
return fmt.Errorf("multiple plugins found: %v", id)
|
|
}
|
|
bundle = p
|
|
}
|
|
}
|
|
if bundle == nil {
|
|
return fmt.Errorf("plugin not found: %v", id)
|
|
}
|
|
|
|
activePlugin := ActivePlugin{BundleInfo: bundle}
|
|
|
|
var supervisor plugin.Supervisor
|
|
|
|
if bundle.Manifest.Backend != nil {
|
|
if env.apiProvider == nil {
|
|
return fmt.Errorf("env missing api provider, cannot activate plugin: %v", id)
|
|
}
|
|
|
|
supervisor, err = env.supervisorProvider(bundle)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "unable to create supervisor for plugin: %v", id)
|
|
}
|
|
api, err := env.apiProvider(bundle.Manifest)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "unable to get api for plugin: %v", id)
|
|
}
|
|
if err := supervisor.Start(api); err != nil {
|
|
return errors.Wrapf(err, "unable to start plugin: %v", id)
|
|
}
|
|
if onError != nil {
|
|
go func() {
|
|
err := supervisor.Wait()
|
|
if err != nil {
|
|
onError(err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
activePlugin.Supervisor = supervisor
|
|
}
|
|
|
|
if bundle.Manifest.Webapp != nil {
|
|
if env.webappPath == "" {
|
|
if supervisor != nil {
|
|
supervisor.Stop()
|
|
}
|
|
return fmt.Errorf("env missing webapp path, cannot activate plugin: %v", id)
|
|
}
|
|
|
|
bundlePath := filepath.Clean(bundle.Manifest.Webapp.BundlePath)
|
|
if bundlePath == "" || bundlePath[0] == '.' {
|
|
return fmt.Errorf("invalid webapp bundle path")
|
|
}
|
|
bundlePath = filepath.Join(env.searchPath, id, bundlePath)
|
|
|
|
webappBundle, err := ioutil.ReadFile(bundlePath)
|
|
if err != nil {
|
|
// Backwards compatibility for plugins where webapp.bundle_path was ignored. This should
|
|
// be removed eventually.
|
|
if webappBundle2, err2 := ioutil.ReadFile(fmt.Sprintf("%s/%s/webapp/%s_bundle.js", env.searchPath, id, id)); err2 == nil {
|
|
webappBundle = webappBundle2
|
|
} else {
|
|
if supervisor != nil {
|
|
supervisor.Stop()
|
|
}
|
|
return errors.Wrapf(err, "unable to read webapp bundle: %v", id)
|
|
}
|
|
}
|
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/%s_bundle.js", env.webappPath, id), webappBundle, 0644)
|
|
if err != nil {
|
|
if supervisor != nil {
|
|
supervisor.Stop()
|
|
}
|
|
return errors.Wrapf(err, "unable to write webapp bundle: %v", id)
|
|
}
|
|
}
|
|
|
|
env.activePlugins[id] = activePlugin
|
|
return nil
|
|
}
|
|
|
|
// Deactivates the plugin with the given id.
|
|
func (env *Environment) DeactivatePlugin(id string) error {
|
|
env.mutex.Lock()
|
|
defer env.mutex.Unlock()
|
|
|
|
if activePlugin, ok := env.activePlugins[id]; !ok {
|
|
return fmt.Errorf("plugin not active: %v", id)
|
|
} else {
|
|
delete(env.activePlugins, id)
|
|
var err error
|
|
if activePlugin.Supervisor != nil {
|
|
err = activePlugin.Supervisor.Hooks().OnDeactivate()
|
|
if serr := activePlugin.Supervisor.Stop(); err == nil {
|
|
err = serr
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Deactivates all plugins and gracefully shuts down the environment.
|
|
func (env *Environment) Shutdown() (errs []error) {
|
|
env.mutex.Lock()
|
|
defer env.mutex.Unlock()
|
|
|
|
for _, activePlugin := range env.activePlugins {
|
|
if activePlugin.Supervisor != nil {
|
|
if err := activePlugin.Supervisor.Hooks().OnDeactivate(); err != nil {
|
|
errs = append(errs, errors.Wrapf(err, "OnDeactivate() error for %v", activePlugin.BundleInfo.Manifest.Id))
|
|
}
|
|
if err := activePlugin.Supervisor.Stop(); err != nil {
|
|
errs = append(errs, errors.Wrapf(err, "error stopping supervisor for %v", activePlugin.BundleInfo.Manifest.Id))
|
|
}
|
|
}
|
|
}
|
|
env.activePlugins = make(map[string]ActivePlugin)
|
|
return
|
|
}
|
|
|
|
type MultiPluginHooks struct {
|
|
env *Environment
|
|
}
|
|
|
|
type SinglePluginHooks struct {
|
|
env *Environment
|
|
pluginId string
|
|
}
|
|
|
|
func (env *Environment) Hooks() *MultiPluginHooks {
|
|
return &MultiPluginHooks{
|
|
env: env,
|
|
}
|
|
}
|
|
|
|
func (env *Environment) HooksForPlugin(id string) *SinglePluginHooks {
|
|
return &SinglePluginHooks{
|
|
env: env,
|
|
pluginId: id,
|
|
}
|
|
}
|
|
|
|
func (h *MultiPluginHooks) invoke(f func(plugin.Hooks) error) (errs []error) {
|
|
h.env.mutex.RLock()
|
|
defer h.env.mutex.RUnlock()
|
|
|
|
for _, activePlugin := range h.env.activePlugins {
|
|
if activePlugin.Supervisor == nil {
|
|
continue
|
|
}
|
|
if err := f(activePlugin.Supervisor.Hooks()); err != nil {
|
|
errs = append(errs, errors.Wrapf(err, "hook error for %v", activePlugin.BundleInfo.Manifest.Id))
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// OnConfigurationChange invokes the OnConfigurationChange hook for all plugins. Any errors
|
|
// encountered will be returned.
|
|
func (h *MultiPluginHooks) OnConfigurationChange() []error {
|
|
return h.invoke(func(hooks plugin.Hooks) error {
|
|
if err := hooks.OnConfigurationChange(); err != nil {
|
|
return errors.Wrapf(err, "error calling OnConfigurationChange hook")
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// ServeHTTP invokes the ServeHTTP hook for the plugin identified by the request or responds with a
|
|
// 404 not found.
|
|
//
|
|
// It expects the request's context to have a plugin_id set.
|
|
func (h *MultiPluginHooks) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if id := r.Context().Value("plugin_id"); id != nil {
|
|
if idstr, ok := id.(string); ok {
|
|
h.env.mutex.RLock()
|
|
defer h.env.mutex.RUnlock()
|
|
if plugin, ok := h.env.activePlugins[idstr]; ok && plugin.Supervisor != nil {
|
|
plugin.Supervisor.Hooks().ServeHTTP(w, r)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
http.NotFound(w, r)
|
|
}
|
|
|
|
// MessageWillBePosted invokes the MessageWillBePosted hook for all plugins. Ordering
|
|
// is not guaranteed and the next plugin will get the previous one's modifications.
|
|
// if a plugin rejects a post, the rest of the plugins will not know that an attempt was made.
|
|
// Returns the final result post, or nil if the post was rejected and a string with a reason
|
|
// for the user the message was rejected.
|
|
func (h *MultiPluginHooks) MessageWillBePosted(post *model.Post) (*model.Post, string) {
|
|
h.env.mutex.RLock()
|
|
defer h.env.mutex.RUnlock()
|
|
|
|
for _, activePlugin := range h.env.activePlugins {
|
|
if activePlugin.Supervisor == nil {
|
|
continue
|
|
}
|
|
var rejectionReason string
|
|
post, rejectionReason = activePlugin.Supervisor.Hooks().MessageWillBePosted(post)
|
|
if post == nil {
|
|
return nil, rejectionReason
|
|
}
|
|
}
|
|
return post, ""
|
|
}
|
|
|
|
// MessageWillBeUpdated invokes the MessageWillBeUpdated hook for all plugins. Ordering
|
|
// is not guaranteed and the next plugin will get the previous one's modifications.
|
|
// if a plugin rejects a post, the rest of the plugins will not know that an attempt was made.
|
|
// Returns the final result post, or nil if the post was rejected and a string with a reason
|
|
// for the user the message was rejected.
|
|
func (h *MultiPluginHooks) MessageWillBeUpdated(newPost, oldPost *model.Post) (*model.Post, string) {
|
|
h.env.mutex.RLock()
|
|
defer h.env.mutex.RUnlock()
|
|
|
|
post := newPost
|
|
for _, activePlugin := range h.env.activePlugins {
|
|
if activePlugin.Supervisor == nil {
|
|
continue
|
|
}
|
|
var rejectionReason string
|
|
post, rejectionReason = activePlugin.Supervisor.Hooks().MessageWillBeUpdated(post, oldPost)
|
|
if post == nil {
|
|
return nil, rejectionReason
|
|
}
|
|
}
|
|
return post, ""
|
|
}
|
|
|
|
func (h *MultiPluginHooks) MessageHasBeenPosted(post *model.Post) {
|
|
h.invoke(func(hooks plugin.Hooks) error {
|
|
hooks.MessageHasBeenPosted(post)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (h *MultiPluginHooks) MessageHasBeenUpdated(newPost, oldPost *model.Post) {
|
|
h.invoke(func(hooks plugin.Hooks) error {
|
|
hooks.MessageHasBeenUpdated(newPost, oldPost)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (h *SinglePluginHooks) invoke(f func(plugin.Hooks) error) error {
|
|
h.env.mutex.RLock()
|
|
defer h.env.mutex.RUnlock()
|
|
|
|
if activePlugin, ok := h.env.activePlugins[h.pluginId]; ok && activePlugin.Supervisor != nil {
|
|
if err := f(activePlugin.Supervisor.Hooks()); err != nil {
|
|
return errors.Wrapf(err, "hook error for plugin: %v", activePlugin.BundleInfo.Manifest.Id)
|
|
}
|
|
return nil
|
|
}
|
|
return fmt.Errorf("unable to invoke hook for plugin: %v", h.pluginId)
|
|
}
|
|
|
|
// ExecuteCommand invokes the ExecuteCommand hook for the plugin.
|
|
func (h *SinglePluginHooks) ExecuteCommand(args *model.CommandArgs) (resp *model.CommandResponse, appErr *model.AppError, err error) {
|
|
err = h.invoke(func(hooks plugin.Hooks) error {
|
|
resp, appErr = hooks.ExecuteCommand(args)
|
|
return nil
|
|
})
|
|
return
|
|
}
|