mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
add core plugins thru store (#43085)
This commit is contained in:
@@ -2,8 +2,11 @@ package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func ComposePluginStartCommand(executable string) string {
|
||||
@@ -29,3 +32,14 @@ func ComposeRendererStartCommand() string {
|
||||
|
||||
return fmt.Sprintf("%s_%s_%s%s", "plugin_start", os, strings.ToLower(arch), extension)
|
||||
}
|
||||
|
||||
func CoreDataSourcePathResolver(cfg *setting.Cfg, pluginRootDirName string) PluginPathResolver {
|
||||
return func() (string, error) {
|
||||
// override mismatch cloud monitoring plugin
|
||||
if pluginRootDirName == "stackdriver" {
|
||||
pluginRootDirName = "cloud-monitoring"
|
||||
}
|
||||
|
||||
return filepath.Join(cfg.StaticRootPath, "app/plugins/datasource", pluginRootDirName), nil
|
||||
}
|
||||
}
|
||||
|
@@ -18,11 +18,15 @@ type Store interface {
|
||||
Plugins(ctx context.Context, pluginTypes ...Type) []PluginDTO
|
||||
|
||||
// Add adds a plugin to the store.
|
||||
Add(ctx context.Context, pluginID, version string, opts AddOpts) error
|
||||
Add(ctx context.Context, pluginID, version string) error
|
||||
// AddWithFactory adds a plugin to the store.
|
||||
AddWithFactory(ctx context.Context, pluginID string, factory backendplugin.PluginFactoryFunc, resolver PluginPathResolver) error
|
||||
// Remove removes a plugin from the store.
|
||||
Remove(ctx context.Context, pluginID string) error
|
||||
}
|
||||
|
||||
type PluginPathResolver func() (string, error)
|
||||
|
||||
type AddOpts struct {
|
||||
PluginInstallDir, PluginZipURL, PluginRepoURL string
|
||||
}
|
||||
@@ -66,11 +70,6 @@ type RendererManager interface {
|
||||
Renderer() *Plugin
|
||||
}
|
||||
|
||||
type CoreBackendRegistrar interface {
|
||||
// LoadAndRegister loads and registers a Core backend plugin
|
||||
LoadAndRegister(pluginID string, factory backendplugin.PluginFactoryFunc) error
|
||||
}
|
||||
|
||||
type StaticRouteResolver interface {
|
||||
Routes() []*StaticRoute
|
||||
}
|
||||
|
@@ -32,7 +32,6 @@ var _ plugins.Client = (*PluginManager)(nil)
|
||||
var _ plugins.Store = (*PluginManager)(nil)
|
||||
var _ plugins.PluginDashboardManager = (*PluginManager)(nil)
|
||||
var _ plugins.StaticRouteResolver = (*PluginManager)(nil)
|
||||
var _ plugins.CoreBackendRegistrar = (*PluginManager)(nil)
|
||||
var _ plugins.RendererManager = (*PluginManager)(nil)
|
||||
|
||||
type PluginManager struct {
|
||||
@@ -358,31 +357,6 @@ func (m *PluginManager) isRegistered(pluginID string) bool {
|
||||
return !p.IsDecommissioned()
|
||||
}
|
||||
|
||||
func (m *PluginManager) LoadAndRegister(pluginID string, factory backendplugin.PluginFactoryFunc) error {
|
||||
if m.isRegistered(pluginID) {
|
||||
return fmt.Errorf("backend plugin %s already registered", pluginID)
|
||||
}
|
||||
|
||||
pluginRootDir := pluginID
|
||||
if pluginID == "stackdriver" {
|
||||
pluginRootDir = "cloud-monitoring"
|
||||
}
|
||||
|
||||
path := filepath.Join(m.cfg.StaticRootPath, "app/plugins/datasource", pluginRootDir)
|
||||
|
||||
p, err := m.pluginLoader.LoadWithFactory(path, factory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.register(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *PluginManager) Routes() []*plugins.StaticRoute {
|
||||
staticRoutes := make([]*plugins.StaticRoute, 0)
|
||||
|
||||
|
@@ -173,7 +173,7 @@ func TestPluginManager_Installer(t *testing.T) {
|
||||
pm.pluginLoader = l
|
||||
})
|
||||
|
||||
err := pm.Add(context.Background(), testPluginID, "1.0.0", plugins.AddOpts{})
|
||||
err := pm.Add(context.Background(), testPluginID, "1.0.0")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 1, i.installCount)
|
||||
@@ -196,7 +196,7 @@ func TestPluginManager_Installer(t *testing.T) {
|
||||
assert.Len(t, pm.Plugins(context.Background()), 1)
|
||||
|
||||
t.Run("Won't install if already installed", func(t *testing.T) {
|
||||
err := pm.Add(context.Background(), testPluginID, "1.0.0", plugins.AddOpts{})
|
||||
err := pm.Add(context.Background(), testPluginID, "1.0.0")
|
||||
assert.Equal(t, plugins.DuplicateError{
|
||||
PluginID: p.ID,
|
||||
ExistingPluginDir: p.PluginDir,
|
||||
@@ -211,7 +211,7 @@ func TestPluginManager_Installer(t *testing.T) {
|
||||
}
|
||||
pm.pluginLoader = l
|
||||
|
||||
err = pm.Add(context.Background(), testPluginID, "1.2.0", plugins.AddOpts{})
|
||||
err = pm.Add(context.Background(), testPluginID, "1.2.0")
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, 2, i.installCount)
|
||||
@@ -272,7 +272,7 @@ func TestPluginManager_Installer(t *testing.T) {
|
||||
|
||||
verifyNoPluginErrors(t, pm)
|
||||
|
||||
err = pm.Add(context.Background(), testPluginID, "", plugins.AddOpts{})
|
||||
err = pm.Add(context.Background(), testPluginID, "")
|
||||
assert.Equal(t, plugins.ErrInstallCorePlugin, err)
|
||||
|
||||
t.Run("Can't uninstall core plugin", func(t *testing.T) {
|
||||
@@ -306,7 +306,7 @@ func TestPluginManager_Installer(t *testing.T) {
|
||||
|
||||
verifyNoPluginErrors(t, pm)
|
||||
|
||||
err = pm.Add(context.Background(), testPluginID, "", plugins.AddOpts{})
|
||||
err = pm.Add(context.Background(), testPluginID, "")
|
||||
assert.Equal(t, plugins.ErrInstallCorePlugin, err)
|
||||
|
||||
t.Run("Can't uninstall bundled plugin", func(t *testing.T) {
|
||||
|
@@ -2,10 +2,12 @@ package manager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
)
|
||||
|
||||
func (m *PluginManager) Plugin(_ context.Context, pluginID string) (plugins.PluginDTO, bool) {
|
||||
@@ -38,13 +40,9 @@ func (m *PluginManager) Plugins(_ context.Context, pluginTypes ...plugins.Type)
|
||||
return pluginsList
|
||||
}
|
||||
|
||||
func (m *PluginManager) Add(ctx context.Context, pluginID, version string, opts plugins.AddOpts) error {
|
||||
func (m *PluginManager) Add(ctx context.Context, pluginID, version string) error {
|
||||
var pluginZipURL string
|
||||
|
||||
if opts.PluginRepoURL == "" {
|
||||
opts.PluginRepoURL = grafanaComURL
|
||||
}
|
||||
|
||||
if plugin, exists := m.plugin(pluginID); exists {
|
||||
if !plugin.IsExternalPlugin() {
|
||||
return plugins.ErrInstallCorePlugin
|
||||
@@ -58,7 +56,7 @@ func (m *PluginManager) Add(ctx context.Context, pluginID, version string, opts
|
||||
}
|
||||
|
||||
// get plugin update information to confirm if upgrading is possible
|
||||
updateInfo, err := m.pluginInstaller.GetUpdateInfo(ctx, pluginID, version, opts.PluginRepoURL)
|
||||
updateInfo, err := m.pluginInstaller.GetUpdateInfo(ctx, pluginID, version, grafanaComURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -72,20 +70,12 @@ func (m *PluginManager) Add(ctx context.Context, pluginID, version string, opts
|
||||
}
|
||||
}
|
||||
|
||||
if opts.PluginInstallDir == "" {
|
||||
opts.PluginInstallDir = m.cfg.PluginsPath
|
||||
}
|
||||
|
||||
if opts.PluginZipURL == "" {
|
||||
opts.PluginZipURL = pluginZipURL
|
||||
}
|
||||
|
||||
err := m.pluginInstaller.Install(ctx, pluginID, version, opts.PluginInstallDir, opts.PluginZipURL, opts.PluginRepoURL)
|
||||
err := m.pluginInstaller.Install(ctx, pluginID, version, m.cfg.PluginsPath, pluginZipURL, grafanaComURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.loadPlugins(opts.PluginInstallDir)
|
||||
err = m.loadPlugins(m.cfg.PluginsPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -93,6 +83,28 @@ func (m *PluginManager) Add(ctx context.Context, pluginID, version string, opts
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *PluginManager) AddWithFactory(_ context.Context, pluginID string, factory backendplugin.PluginFactoryFunc,
|
||||
pathResolver plugins.PluginPathResolver) error {
|
||||
if m.isRegistered(pluginID) {
|
||||
return fmt.Errorf("plugin %s is already registered", pluginID)
|
||||
}
|
||||
|
||||
path, err := pathResolver()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p, err := m.pluginLoader.LoadWithFactory(path, factory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = m.register(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *PluginManager) Remove(ctx context.Context, pluginID string) error {
|
||||
plugin, exists := m.plugin(pluginID)
|
||||
if !exists {
|
||||
|
Reference in New Issue
Block a user