summaryrefslogtreecommitdiffstats
path: root/plugin/environment.go
blob: 94e5f2646c1560214c88de311341dd589074c000 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package plugin

import (
	"fmt"
	"hash/fnv"
	"io/ioutil"
	"os"
	"path/filepath"
	"sync"

	"github.com/mattermost/mattermost-server/mlog"
	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/utils"
	"github.com/pkg/errors"
)

type apiImplCreatorFunc func(*model.Manifest) API

// multiPluginHookRunnerFunc is a callback function to invoke as part of RunMultiPluginHook.
//
// Return false to stop the hook from iterating to subsequent plugins.
type multiPluginHookRunnerFunc func(hooks Hooks) bool

type activePlugin struct {
	BundleInfo *model.BundleInfo
	State      int

	supervisor *supervisor
}

// Environment represents the execution environment of active plugins.
//
// It is meant for use by the Mattermost server to manipulate, interact with and report on the set
// of active plugins.
type Environment struct {
	activePlugins   sync.Map
	logger          *mlog.Logger
	newAPIImpl      apiImplCreatorFunc
	pluginDir       string
	webappPluginDir string
}

func NewEnvironment(newAPIImpl apiImplCreatorFunc, pluginDir string, webappPluginDir string, logger *mlog.Logger) (*Environment, error) {
	return &Environment{
		logger:          logger,
		newAPIImpl:      newAPIImpl,
		pluginDir:       pluginDir,
		webappPluginDir: webappPluginDir,
	}, nil
}

// Performs a full scan of the given path.
//
// This function will return info for all subdirectories that appear to be plugins (i.e. all
// subdirectories containing plugin manifest files, regardless of whether they could actually be
// parsed).
//
// Plugins are found non-recursively and paths beginning with a dot are always ignored.
func scanSearchPath(path string) ([]*model.BundleInfo, error) {
	files, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, err
	}
	var ret []*model.BundleInfo
	for _, file := range files {
		if !file.IsDir() || file.Name()[0] == '.' {
			continue
		}
		if info := model.BundleInfoForPath(filepath.Join(path, file.Name())); info.ManifestPath != "" {
			ret = append(ret, info)
		}
	}
	return ret, nil
}

// Returns a list of all plugins within the environment.
func (env *Environment) Available() ([]*model.BundleInfo, error) {
	return scanSearchPath(env.pluginDir)
}

// Returns a list of all currently active plugins within the environment.
func (env *Environment) Active() []*model.BundleInfo {
	activePlugins := []*model.BundleInfo{}
	env.activePlugins.Range(func(key, value interface{}) bool {
		activePlugins = append(activePlugins, value.(activePlugin).BundleInfo)

		return true
	})

	return activePlugins
}

// IsActive returns true if the plugin with the given id is active.
func (env *Environment) IsActive(id string) bool {
	_, ok := env.activePlugins.Load(id)
	return ok
}

// Statuses returns a list of plugin statuses representing the state of every plugin
func (env *Environment) Statuses() (model.PluginStatuses, error) {
	plugins, err := env.Available()
	if err != nil {
		return nil, errors.Wrap(err, "unable to get plugin statuses")
	}

	pluginStatuses := make(model.PluginStatuses, 0, len(plugins))
	for _, plugin := range plugins {
		// For now we don't handle bad manifests, we should
		if plugin.Manifest == nil {
			continue
		}

		pluginState := model.PluginStateNotRunning
		if plugin, ok := env.activePlugins.Load(plugin.Manifest.Id); ok {
			pluginState = plugin.(activePlugin).State
		}

		status := &model.PluginStatus{
			PluginId:    plugin.Manifest.Id,
			PluginPath:  filepath.Dir(plugin.ManifestPath),
			State:       pluginState,
			Name:        plugin.Manifest.Name,
			Description: plugin.Manifest.Description,
			Version:     plugin.Manifest.Version,
		}

		pluginStatuses = append(pluginStatuses, status)
	}

	return pluginStatuses, nil
}

func (env *Environment) Activate(id string) (manifest *model.Manifest, activated bool, reterr error) {
	// Check if we are already active
	if _, ok := env.activePlugins.Load(id); ok {
		return nil, false, nil
	}

	plugins, err := env.Available()
	if err != nil {
		return nil, false, err
	}
	var pluginInfo *model.BundleInfo
	for _, p := range plugins {
		if p.Manifest != nil && p.Manifest.Id == id {
			if pluginInfo != nil {
				return nil, false, fmt.Errorf("multiple plugins found: %v", id)
			}
			pluginInfo = p
		}
	}
	if pluginInfo == nil {
		return nil, false, fmt.Errorf("plugin not found: %v", id)
	}

	activePlugin := activePlugin{BundleInfo: pluginInfo}
	defer func() {
		if reterr == nil {
			activePlugin.State = model.PluginStateRunning
		} else {
			activePlugin.State = model.PluginStateFailedToStart
		}
		env.activePlugins.Store(pluginInfo.Manifest.Id, activePlugin)
	}()

	componentActivated := false

	if pluginInfo.Manifest.HasWebapp() {
		bundlePath := filepath.Clean(pluginInfo.Manifest.Webapp.BundlePath)
		if bundlePath == "" || bundlePath[0] == '.' {
			return nil, false, fmt.Errorf("invalid webapp bundle path")
		}
		bundlePath = filepath.Join(env.pluginDir, id, bundlePath)
		destinationPath := filepath.Join(env.webappPluginDir, id)

		if err := os.RemoveAll(destinationPath); err != nil {
			return nil, false, errors.Wrapf(err, "unable to remove old webapp bundle directory: %v", destinationPath)
		}

		if err := utils.CopyDir(filepath.Dir(bundlePath), destinationPath); err != nil {
			return nil, false, errors.Wrapf(err, "unable to copy webapp bundle directory: %v", id)
		}

		sourceBundleFilepath := filepath.Join(destinationPath, filepath.Base(bundlePath))

		sourceBundleFileContents, err := ioutil.ReadFile(sourceBundleFilepath)
		if err != nil {
			return nil, false, errors.Wrapf(err, "unable to read webapp bundle: %v", id)
		}

		hash := fnv.New64a()
		hash.Write(sourceBundleFileContents)
		pluginInfo.Manifest.Webapp.BundleHash = hash.Sum([]byte{})

		if err := os.Rename(
			sourceBundleFilepath,
			filepath.Join(destinationPath, fmt.Sprintf("%s_%x_bundle.js", id, pluginInfo.Manifest.Webapp.BundleHash)),
		); err != nil {
			return nil, false, errors.Wrapf(err, "unable to rename webapp bundle: %v", id)
		}

		componentActivated = true
	}

	if pluginInfo.Manifest.HasServer() {
		supervisor, err := newSupervisor(pluginInfo, env.logger, env.newAPIImpl(pluginInfo.Manifest))
		if err != nil {
			return nil, false, errors.Wrapf(err, "unable to start plugin: %v", id)
		}
		activePlugin.supervisor = supervisor

		componentActivated = true
	}

	if !componentActivated {
		return nil, false, fmt.Errorf("unable to start plugin: must at least have a web app or server component")
	}

	return pluginInfo.Manifest, true, nil
}

// Deactivates the plugin with the given id.
func (env *Environment) Deactivate(id string) bool {
	p, ok := env.activePlugins.Load(id)
	if !ok {
		return false
	}

	env.activePlugins.Delete(id)

	activePlugin := p.(activePlugin)
	if activePlugin.supervisor != nil {
		if err := activePlugin.supervisor.Hooks().OnDeactivate(); err != nil {
			env.logger.Error("Plugin OnDeactivate() error", mlog.String("plugin_id", activePlugin.BundleInfo.Manifest.Id), mlog.Err(err))
		}
		activePlugin.supervisor.Shutdown()
	}

	return true
}

// Shutdown deactivates all plugins and gracefully shuts down the environment.
func (env *Environment) Shutdown() {
	env.activePlugins.Range(func(key, value interface{}) bool {
		activePlugin := value.(activePlugin)

		if activePlugin.supervisor != nil {
			if err := activePlugin.supervisor.Hooks().OnDeactivate(); err != nil {
				env.logger.Error("Plugin OnDeactivate() error", mlog.String("plugin_id", activePlugin.BundleInfo.Manifest.Id), mlog.Err(err))
			}
			activePlugin.supervisor.Shutdown()
		}

		env.activePlugins.Delete(key)

		return true
	})
}

// HooksForPlugin returns the hooks API for the plugin with the given id.
//
// Consider using RunMultiPluginHook instead.
func (env *Environment) HooksForPlugin(id string) (Hooks, error) {
	if p, ok := env.activePlugins.Load(id); ok {
		activePlugin := p.(activePlugin)
		if activePlugin.supervisor != nil {
			return activePlugin.supervisor.Hooks(), nil
		}
	}

	return nil, fmt.Errorf("plugin not found: %v", id)
}

// RunMultiPluginHook invokes hookRunnerFunc for each plugin that implements the given hookId.
//
// If hookRunnerFunc returns false, iteration will not continue. The iteration order among active
// plugins is not specified.
func (env *Environment) RunMultiPluginHook(hookRunnerFunc multiPluginHookRunnerFunc, hookId int) {
	env.activePlugins.Range(func(key, value interface{}) bool {
		activePlugin := value.(activePlugin)

		if activePlugin.supervisor == nil || !activePlugin.supervisor.Implements(hookId) {
			return true
		}
		if !hookRunnerFunc(activePlugin.supervisor.Hooks()) {
			return false
		}

		return true
	})
}