2018-05-24 08:26:27 -05:00
|
|
|
package plugins
|
|
|
|
|
2020-01-08 10:43:28 -06:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2020-10-19 10:35:31 -05:00
|
|
|
"path/filepath"
|
2020-01-08 10:43:28 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
2020-06-11 09:14:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/grpcplugin"
|
2020-04-21 09:16:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
2020-01-08 10:43:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
|
|
|
)
|
2018-05-24 08:26:27 -05:00
|
|
|
|
|
|
|
type RendererPlugin struct {
|
2020-04-27 14:15:44 -05:00
|
|
|
FrontendPluginBase
|
2018-05-24 08:26:27 -05:00
|
|
|
|
2020-01-13 13:18:45 -06:00
|
|
|
Executable string `json:"executable,omitempty"`
|
2020-04-21 09:16:41 -05:00
|
|
|
GrpcPluginV2 pluginextensionv2.RendererPlugin
|
2020-01-13 13:18:45 -06:00
|
|
|
backendPluginManager backendplugin.Manager
|
2018-05-24 08:26:27 -05:00
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
func (r *RendererPlugin) Load(decoder *json.Decoder, base *PluginBase,
|
|
|
|
backendPluginManager backendplugin.Manager) (interface{}, error) {
|
2020-01-13 10:13:17 -06:00
|
|
|
if err := decoder.Decode(r); err != nil {
|
2021-03-08 00:02:49 -06:00
|
|
|
return nil, err
|
2018-05-24 08:26:27 -05:00
|
|
|
}
|
|
|
|
|
2020-01-13 13:18:45 -06:00
|
|
|
r.backendPluginManager = backendPluginManager
|
|
|
|
|
2020-06-01 10:11:25 -05:00
|
|
|
cmd := ComposePluginStartCommand("plugin_start")
|
2021-03-08 00:02:49 -06:00
|
|
|
fullpath := filepath.Join(base.PluginDir, cmd)
|
2021-06-03 07:16:58 -05:00
|
|
|
factory := grpcplugin.NewRendererPlugin(r.Id, fullpath, r.onPluginStart)
|
2020-06-11 09:14:05 -05:00
|
|
|
if err := backendPluginManager.Register(r.Id, factory); err != nil {
|
2021-03-08 00:02:49 -06:00
|
|
|
return nil, errutil.Wrapf(err, "failed to register backend plugin")
|
2020-01-08 10:43:28 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
return r, nil
|
2018-05-24 08:26:27 -05:00
|
|
|
}
|
2020-01-08 10:43:28 -06:00
|
|
|
|
|
|
|
func (r *RendererPlugin) Start(ctx context.Context) error {
|
2020-01-13 13:18:45 -06:00
|
|
|
if err := r.backendPluginManager.StartPlugin(ctx, r.Id); err != nil {
|
2020-01-08 10:43:28 -06:00
|
|
|
return errutil.Wrapf(err, "Failed to start renderer plugin")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 07:16:58 -05:00
|
|
|
func (r *RendererPlugin) onPluginStart(pluginID string, renderer pluginextensionv2.RendererPlugin, logger log.Logger) error {
|
|
|
|
r.GrpcPluginV2 = renderer
|
2020-01-08 10:43:28 -06:00
|
|
|
return nil
|
|
|
|
}
|