[WIP] Plugins: Refactoring backend initialization flow (#42247)
* refactoring store interface and init flow * fix import * fix linter * refactor resource calling * load with class * re-order args * fix tests * fix linter * remove old creator * add custom config struct * fix some tests * cleanup * fix linter * add connect failure error * remove unused err * convert test over
This commit is contained in:
@@ -1,104 +1,43 @@
|
||||
package initializer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana-aws-sdk/pkg/awsds"
|
||||
|
||||
"github.com/gosimple/slug"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/grpcplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
type Initializer struct {
|
||||
cfg *setting.Cfg
|
||||
license models.Licensing
|
||||
log log.Logger
|
||||
cfg *plugins.Cfg
|
||||
license models.Licensing
|
||||
backendProvider plugins.BackendFactoryProvider
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func New(cfg *setting.Cfg, license models.Licensing) Initializer {
|
||||
func New(cfg *plugins.Cfg, backendProvider plugins.BackendFactoryProvider, license models.Licensing) Initializer {
|
||||
return Initializer{
|
||||
cfg: cfg,
|
||||
license: license,
|
||||
log: log.New("plugin.initializer"),
|
||||
cfg: cfg,
|
||||
license: license,
|
||||
backendProvider: backendProvider,
|
||||
log: log.New("plugin.initializer"),
|
||||
}
|
||||
}
|
||||
|
||||
func (i *Initializer) Initialize(p *plugins.Plugin) error {
|
||||
if len(p.Dependencies.Plugins) == 0 {
|
||||
p.Dependencies.Plugins = []plugins.Dependency{}
|
||||
}
|
||||
|
||||
if p.Dependencies.GrafanaVersion == "" {
|
||||
p.Dependencies.GrafanaVersion = "*"
|
||||
}
|
||||
|
||||
for _, include := range p.Includes {
|
||||
if include.Role == "" {
|
||||
include.Role = models.ROLE_VIEWER
|
||||
}
|
||||
}
|
||||
|
||||
i.handleModuleDefaults(p)
|
||||
|
||||
p.Info.Logos.Small = pluginLogoURL(p.Type, p.Info.Logos.Small, p.BaseURL)
|
||||
p.Info.Logos.Large = pluginLogoURL(p.Type, p.Info.Logos.Large, p.BaseURL)
|
||||
|
||||
for i := 0; i < len(p.Info.Screenshots); i++ {
|
||||
p.Info.Screenshots[i].Path = evalRelativePluginURLPath(p.Info.Screenshots[i].Path, p.BaseURL, p.Type)
|
||||
}
|
||||
|
||||
if p.IsApp() {
|
||||
for _, child := range p.Children {
|
||||
i.setPathsBasedOnApp(p, child)
|
||||
}
|
||||
|
||||
// slugify pages
|
||||
for _, include := range p.Includes {
|
||||
if include.Slug == "" {
|
||||
include.Slug = slug.Make(include.Name)
|
||||
}
|
||||
if include.Type == "page" && include.DefaultNav {
|
||||
p.DefaultNavURL = i.cfg.AppSubURL + "/plugins/" + p.ID + "/page/" + include.Slug
|
||||
}
|
||||
if include.Type == "dashboard" && include.DefaultNav {
|
||||
p.DefaultNavURL = i.cfg.AppSubURL + "/dashboard/db/" + include.Slug
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pluginLog := i.log.New("pluginID", p.ID)
|
||||
p.SetLogger(pluginLog)
|
||||
|
||||
func (i *Initializer) Initialize(ctx context.Context, p *plugins.Plugin) error {
|
||||
if p.Backend {
|
||||
var backendFactory backendplugin.PluginFactoryFunc
|
||||
if p.IsRenderer() {
|
||||
cmd := plugins.ComposeRendererStartCommand()
|
||||
backendFactory = grpcplugin.NewRendererPlugin(p.ID, filepath.Join(p.PluginDir, cmd),
|
||||
func(pluginID string, renderer pluginextensionv2.RendererPlugin, logger log.Logger) error {
|
||||
p.Renderer = renderer
|
||||
return nil
|
||||
},
|
||||
)
|
||||
} else {
|
||||
cmd := plugins.ComposePluginStartCommand(p.Executable)
|
||||
backendFactory = grpcplugin.NewBackendPlugin(p.ID, filepath.Join(p.PluginDir, cmd))
|
||||
backendFactory := i.backendProvider.BackendFactory(ctx, p)
|
||||
if backendFactory == nil {
|
||||
return fmt.Errorf("could not find backend factory for plugin")
|
||||
}
|
||||
|
||||
if backendClient, err := backendFactory(p.ID, pluginLog, i.envVars(p)); err != nil {
|
||||
if backendClient, err := backendFactory(p.ID, p.Logger(), i.envVars(p)); err != nil {
|
||||
return err
|
||||
} else {
|
||||
p.RegisterClient(backendClient)
|
||||
@@ -109,88 +48,20 @@ func (i *Initializer) Initialize(p *plugins.Plugin) error {
|
||||
}
|
||||
|
||||
func (i *Initializer) InitializeWithFactory(p *plugins.Plugin, factory backendplugin.PluginFactoryFunc) error {
|
||||
err := i.Initialize(p)
|
||||
if factory == nil {
|
||||
return fmt.Errorf("could not initialize plugin %s", p.ID)
|
||||
}
|
||||
|
||||
f, err := factory(p.ID, log.New("pluginID", p.ID), []string{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if factory != nil {
|
||||
var err error
|
||||
|
||||
f, err := factory(p.ID, log.New("pluginID", p.ID), []string{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.RegisterClient(f)
|
||||
} else {
|
||||
i.log.Warn("Could not initialize core plugin process", "pluginID", p.ID)
|
||||
return fmt.Errorf("could not initialize plugin %s", p.ID)
|
||||
}
|
||||
p.RegisterClient(f)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Initializer) handleModuleDefaults(p *plugins.Plugin) {
|
||||
if p.IsCorePlugin() {
|
||||
// Previously there was an assumption that the Core plugins directory
|
||||
// should be public/app/plugins/<plugin type>/<plugin id>
|
||||
// However this can be an issue if the Core plugins directory is renamed
|
||||
baseDir := filepath.Base(p.PluginDir)
|
||||
|
||||
// use path package for the following statements because these are not file paths
|
||||
p.Module = path.Join("app/plugins", string(p.Type), baseDir, "module")
|
||||
p.BaseURL = path.Join("public/app/plugins", string(p.Type), baseDir)
|
||||
return
|
||||
}
|
||||
|
||||
metrics.SetPluginBuildInformation(p.ID, string(p.Type), p.Info.Version, string(p.Signature))
|
||||
|
||||
p.Module = path.Join("plugins", p.ID, "module")
|
||||
p.BaseURL = path.Join("public/plugins", p.ID)
|
||||
}
|
||||
|
||||
func (i *Initializer) setPathsBasedOnApp(parent *plugins.Plugin, child *plugins.Plugin) {
|
||||
appSubPath := strings.ReplaceAll(strings.Replace(child.PluginDir, parent.PluginDir, "", 1), "\\", "/")
|
||||
child.IncludedInAppID = parent.ID
|
||||
child.BaseURL = parent.BaseURL
|
||||
|
||||
if parent.IsCorePlugin() {
|
||||
child.Module = util.JoinURLFragments("app/plugins/app/"+parent.ID, appSubPath) + "/module"
|
||||
} else {
|
||||
child.Module = util.JoinURLFragments("plugins/"+parent.ID, appSubPath) + "/module"
|
||||
}
|
||||
}
|
||||
|
||||
func pluginLogoURL(pluginType plugins.Type, path, baseURL string) string {
|
||||
if path == "" {
|
||||
return defaultLogoPath(pluginType)
|
||||
}
|
||||
|
||||
return evalRelativePluginURLPath(path, baseURL, pluginType)
|
||||
}
|
||||
|
||||
func defaultLogoPath(pluginType plugins.Type) string {
|
||||
return "public/img/icn-" + string(pluginType) + ".svg"
|
||||
}
|
||||
|
||||
func evalRelativePluginURLPath(pathStr, baseURL string, pluginType plugins.Type) string {
|
||||
if pathStr == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
u, _ := url.Parse(pathStr)
|
||||
if u.IsAbs() {
|
||||
return pathStr
|
||||
}
|
||||
|
||||
// is set as default or has already been prefixed with base path
|
||||
if pathStr == defaultLogoPath(pluginType) || strings.HasPrefix(pathStr, baseURL) {
|
||||
return pathStr
|
||||
}
|
||||
|
||||
return path.Join(baseURL, pathStr)
|
||||
}
|
||||
|
||||
func (i *Initializer) envVars(plugin *plugins.Plugin) []string {
|
||||
hostEnv := []string{
|
||||
fmt.Sprintf("GF_VERSION=%s", i.cfg.BuildVersion),
|
||||
@@ -260,7 +131,7 @@ func (ps pluginSettings) asEnvVar(prefix string, hostEnv []string) []string {
|
||||
return env
|
||||
}
|
||||
|
||||
func getPluginSettings(pluginID string, cfg *setting.Cfg) pluginSettings {
|
||||
func getPluginSettings(pluginID string, cfg *plugins.Cfg) pluginSettings {
|
||||
ps := pluginSettings{}
|
||||
for k, v := range cfg.PluginSettings[pluginID] {
|
||||
if k == "path" || strings.ToLower(k) == "id" {
|
||||
|
||||
Reference in New Issue
Block a user