Preinstall: Allow to set a download URL (#96535)
This commit is contained in:
@@ -21,12 +21,12 @@ import (
|
||||
)
|
||||
|
||||
type FakePluginInstaller struct {
|
||||
AddFunc func(ctx context.Context, pluginID, version string, opts plugins.CompatOpts) error
|
||||
AddFunc func(ctx context.Context, pluginID, version string, opts plugins.AddOpts) error
|
||||
// Remove removes a plugin from the store.
|
||||
RemoveFunc func(ctx context.Context, pluginID, version string) error
|
||||
}
|
||||
|
||||
func (i *FakePluginInstaller) Add(ctx context.Context, pluginID, version string, opts plugins.CompatOpts) error {
|
||||
func (i *FakePluginInstaller) Add(ctx context.Context, pluginID, version string, opts plugins.AddOpts) error {
|
||||
if i.AddFunc != nil {
|
||||
return i.AddFunc(ctx, pluginID, version, opts)
|
||||
}
|
||||
|
||||
@@ -51,12 +51,7 @@ func New(pluginRegistry registry.Service, pluginLoader loader.Service, pluginRep
|
||||
}
|
||||
}
|
||||
|
||||
func (m *PluginInstaller) Add(ctx context.Context, pluginID, version string, opts plugins.CompatOpts) error {
|
||||
compatOpts, err := RepoCompatOpts(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *PluginInstaller) Add(ctx context.Context, pluginID, version string, opts plugins.AddOpts) error {
|
||||
if ok, _ := m.installing.Load(pluginID); ok != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -65,7 +60,7 @@ func (m *PluginInstaller) Add(ctx context.Context, pluginID, version string, opt
|
||||
m.installing.Delete(pluginID)
|
||||
}()
|
||||
|
||||
archive, err := m.install(ctx, pluginID, version, compatOpts)
|
||||
archive, err := m.install(ctx, pluginID, version, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -93,8 +88,12 @@ func (m *PluginInstaller) Add(ctx context.Context, pluginID, version string, opt
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *PluginInstaller) install(ctx context.Context, pluginID, version string, compatOpts repo.CompatOpts) (*storage.ExtractedPluginArchive, error) {
|
||||
func (m *PluginInstaller) install(ctx context.Context, pluginID, version string, opts plugins.AddOpts) (*storage.ExtractedPluginArchive, error) {
|
||||
var pluginArchive *repo.PluginArchive
|
||||
compatOpts, err := RepoCompatOpts(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if plugin, exists := m.plugin(ctx, pluginID, version); exists {
|
||||
if plugin.IsCorePlugin() || plugin.IsBundledPlugin() {
|
||||
return nil, plugins.ErrInstallCorePlugin
|
||||
@@ -105,46 +104,21 @@ func (m *PluginInstaller) install(ctx context.Context, pluginID, version string,
|
||||
PluginID: plugin.ID,
|
||||
}
|
||||
}
|
||||
|
||||
// get plugin update information to confirm if target update is possible
|
||||
pluginArchiveInfo, err := m.pluginRepo.GetPluginArchiveInfo(ctx, pluginID, version, compatOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m.log.Info("Updating plugin", "pluginId", pluginID, "from", plugin.Info.Version, "to", pluginArchiveInfo.Version)
|
||||
|
||||
// if existing plugin version is the same as the target update version
|
||||
if pluginArchiveInfo.Version == plugin.Info.Version {
|
||||
return nil, plugins.DuplicateError{
|
||||
PluginID: plugin.ID,
|
||||
}
|
||||
}
|
||||
|
||||
if pluginArchiveInfo.URL == "" && pluginArchiveInfo.Version == "" {
|
||||
return nil, fmt.Errorf("could not determine update options for %s", pluginID)
|
||||
}
|
||||
|
||||
// remove existing installation of plugin
|
||||
err = m.Remove(ctx, plugin.ID, plugin.Info.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if pluginArchiveInfo.URL != "" {
|
||||
pluginArchive, err = m.pluginRepo.GetPluginArchiveByURL(ctx, pluginArchiveInfo.URL, compatOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if opts.URL() != "" {
|
||||
pluginArchive, err = m.updateFromURL(ctx, plugin, opts.URL(), compatOpts)
|
||||
} else {
|
||||
pluginArchive, err = m.pluginRepo.GetPluginArchive(ctx, pluginID, pluginArchiveInfo.Version, compatOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pluginArchive, err = m.updateFromCatalog(ctx, plugin, version, compatOpts)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
pluginArchive, err = m.pluginRepo.GetPluginArchive(ctx, pluginID, version, compatOpts)
|
||||
if opts.URL() != "" {
|
||||
pluginArchive, err = m.pluginRepo.GetPluginArchiveByURL(ctx, opts.URL(), compatOpts)
|
||||
} else {
|
||||
pluginArchive, err = m.pluginRepo.GetPluginArchive(ctx, pluginID, version, compatOpts)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -156,9 +130,64 @@ func (m *PluginInstaller) install(ctx context.Context, pluginID, version string,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check that the extracted plugin archive has the expected ID and version
|
||||
// but avoid a hard error for backwards compatibility with older plugins
|
||||
// and because in the case of an update, the previous version has been already uninstalled
|
||||
if extractedArchive.ID != pluginID {
|
||||
m.log.Error("Installed plugin ID mismatch", "expected", pluginID, "got", extractedArchive.ID)
|
||||
}
|
||||
if version != "" && extractedArchive.Version != version {
|
||||
m.log.Error("Installed plugin version mismatch", "expected", version, "got", extractedArchive.Version)
|
||||
}
|
||||
|
||||
return extractedArchive, nil
|
||||
}
|
||||
|
||||
func (m *PluginInstaller) updateFromURL(ctx context.Context, plugin *plugins.Plugin, url string, compatOpts repo.CompatOpts) (*repo.PluginArchive, error) {
|
||||
m.log.Info("Updating plugin", "pluginId", plugin.ID, "from", plugin.Info.Version, "url", url)
|
||||
|
||||
// remove existing installation of plugin
|
||||
err := m.Remove(ctx, plugin.ID, plugin.Info.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m.pluginRepo.GetPluginArchiveByURL(ctx, url, compatOpts)
|
||||
}
|
||||
|
||||
func (m *PluginInstaller) updateFromCatalog(ctx context.Context, plugin *plugins.Plugin, version string, compatOpts repo.CompatOpts) (*repo.PluginArchive, error) {
|
||||
// get plugin update information to confirm if target update is possible
|
||||
pluginArchiveInfo, err := m.pluginRepo.GetPluginArchiveInfo(ctx, plugin.ID, version, compatOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m.log.Info("Updating plugin", "pluginId", plugin.ID, "from", plugin.Info.Version, "to", pluginArchiveInfo.Version)
|
||||
|
||||
// if existing plugin version is the same as the target update version
|
||||
if pluginArchiveInfo.Version == plugin.Info.Version {
|
||||
return nil, plugins.DuplicateError{
|
||||
PluginID: plugin.ID,
|
||||
}
|
||||
}
|
||||
|
||||
if pluginArchiveInfo.URL == "" && pluginArchiveInfo.Version == "" {
|
||||
return nil, fmt.Errorf("could not determine update options for %s", plugin.ID)
|
||||
}
|
||||
|
||||
// remove existing installation of plugin
|
||||
err = m.Remove(ctx, plugin.ID, plugin.Info.Version)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if pluginArchiveInfo.URL != "" {
|
||||
return m.pluginRepo.GetPluginArchiveByURL(ctx, pluginArchiveInfo.URL, compatOpts)
|
||||
} else {
|
||||
return m.pluginRepo.GetPluginArchive(ctx, plugin.ID, pluginArchiveInfo.Version, compatOpts)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *PluginInstaller) Remove(ctx context.Context, pluginID, version string) error {
|
||||
plugin, exists := m.plugin(ctx, pluginID, version)
|
||||
if !exists {
|
||||
@@ -197,7 +226,7 @@ func (m *PluginInstaller) plugin(ctx context.Context, pluginID, pluginVersion st
|
||||
return p, true
|
||||
}
|
||||
|
||||
func RepoCompatOpts(opts plugins.CompatOpts) (repo.CompatOpts, error) {
|
||||
func RepoCompatOpts(opts plugins.AddOpts) (repo.CompatOpts, error) {
|
||||
os := opts.OS()
|
||||
arch := opts.Arch()
|
||||
if len(os) == 0 || len(arch) == 0 {
|
||||
|
||||
@@ -3,6 +3,7 @@ package manager
|
||||
import (
|
||||
"archive/zip"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"testing"
|
||||
@@ -62,7 +63,9 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
require.Equal(t, pluginID, id)
|
||||
require.Equal(t, mockZipV1, z)
|
||||
return &storage.ExtractedPluginArchive{
|
||||
Path: zipNameV1,
|
||||
ID: pluginID,
|
||||
Version: v1,
|
||||
Path: zipNameV1,
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
@@ -84,6 +87,22 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
}, err)
|
||||
})
|
||||
|
||||
t.Run("Add from URL", func(t *testing.T) {
|
||||
url := "https://grafanaplugins.com"
|
||||
pluginRepo := &fakes.FakePluginRepo{
|
||||
GetPluginArchiveByURLFunc: func(_ context.Context, archiveURL string, _ repo.CompatOpts) (*repo.PluginArchive, error) {
|
||||
require.Equal(t, pluginID, pluginID)
|
||||
require.Equal(t, url, archiveURL)
|
||||
return &repo.PluginArchive{
|
||||
File: mockZipV1,
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
inst := New(fakes.NewFakePluginRegistry(), loader, pluginRepo, fs, storage.SimpleDirNameGeneratorFunc, &fakes.FakeAuthService{})
|
||||
err := inst.Add(context.Background(), pluginID, v1, plugins.NewAddOpts(v1, runtime.GOOS, runtime.GOARCH, url))
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Update plugin to different version", func(t *testing.T) {
|
||||
// mock a plugin to be returned automatically by the plugin loader
|
||||
pluginV2 := createPlugin(t, pluginID, plugins.ClassExternal, true, true, func(plugin *plugins.Plugin) {
|
||||
@@ -113,7 +132,9 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
require.Equal(t, pluginV1.ID, pluginID)
|
||||
require.Equal(t, mockZipV2, z)
|
||||
return &storage.ExtractedPluginArchive{
|
||||
Path: zipNameV2,
|
||||
ID: pluginID,
|
||||
Version: v2,
|
||||
Path: zipNameV2,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -121,6 +142,47 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Update plugin from url", func(t *testing.T) {
|
||||
url := "https://grafanaplugins.com"
|
||||
// mock a plugin to be returned automatically by the plugin loader
|
||||
pluginV2 := createPlugin(t, pluginID, plugins.ClassExternal, true, true, func(plugin *plugins.Plugin) {
|
||||
plugin.Info.Version = v2
|
||||
})
|
||||
|
||||
mockZipV2 := &zip.ReadCloser{Reader: zip.Reader{File: []*zip.File{{
|
||||
FileHeader: zip.FileHeader{Name: zipNameV2},
|
||||
}}}}
|
||||
loader.LoadFunc = func(ctx context.Context, src plugins.PluginSource) ([]*plugins.Plugin, error) {
|
||||
require.Equal(t, plugins.ClassExternal, src.PluginClass(ctx))
|
||||
require.Equal(t, []string{zipNameV2}, src.PluginURIs(ctx))
|
||||
return []*plugins.Plugin{pluginV2}, nil
|
||||
}
|
||||
pluginRepo.GetPluginArchiveInfoFunc = func(_ context.Context, _, _ string, _ repo.CompatOpts) (*repo.PluginArchiveInfo, error) {
|
||||
return nil, errors.New("shouldn't be called")
|
||||
}
|
||||
getPluginArchiveByURLCalled := false
|
||||
pluginRepo.GetPluginArchiveByURLFunc = func(_ context.Context, pluginZipURL string, _ repo.CompatOpts) (*repo.PluginArchive, error) {
|
||||
require.Equal(t, url, pluginZipURL)
|
||||
getPluginArchiveByURLCalled = true
|
||||
return &repo.PluginArchive{
|
||||
File: mockZipV2,
|
||||
}, nil
|
||||
}
|
||||
fs.ExtractFunc = func(_ context.Context, pluginID string, _ storage.DirNameGeneratorFunc, z *zip.ReadCloser) (*storage.ExtractedPluginArchive, error) {
|
||||
require.Equal(t, pluginV1.ID, pluginID)
|
||||
require.Equal(t, mockZipV2, z)
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: pluginID,
|
||||
Version: v2,
|
||||
Path: zipNameV2,
|
||||
}, nil
|
||||
}
|
||||
|
||||
err = inst.Add(context.Background(), pluginID, v2, plugins.NewAddOpts(v2, runtime.GOOS, runtime.GOARCH, url))
|
||||
require.NoError(t, err)
|
||||
require.True(t, getPluginArchiveByURLCalled)
|
||||
})
|
||||
|
||||
t.Run("Removing an existing plugin", func(t *testing.T) {
|
||||
inst.pluginRegistry = &fakes.FakePluginRegistry{
|
||||
Store: map[string]*plugins.Plugin{
|
||||
@@ -210,14 +272,19 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
ExtractFunc: func(_ context.Context, id string, _ storage.DirNameGeneratorFunc, z *zip.ReadCloser) (*storage.ExtractedPluginArchive, error) {
|
||||
switch id {
|
||||
case p1:
|
||||
return &storage.ExtractedPluginArchive{Path: p1Zip}, nil
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p1,
|
||||
Path: p1Zip,
|
||||
}, nil
|
||||
case p2:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p2,
|
||||
Dependencies: []*storage.Dependency{{ID: p1}},
|
||||
Path: p2Zip,
|
||||
}, nil
|
||||
case p3:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p3,
|
||||
Dependencies: []*storage.Dependency{{ID: p2}},
|
||||
Path: p3Zip,
|
||||
}, nil
|
||||
@@ -260,11 +327,13 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
switch id {
|
||||
case p1:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p1,
|
||||
Dependencies: []*storage.Dependency{{ID: p2}},
|
||||
Path: p1Zip,
|
||||
}, nil
|
||||
case p2:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p2,
|
||||
Dependencies: []*storage.Dependency{{ID: p1}},
|
||||
Path: p2Zip,
|
||||
}, nil
|
||||
@@ -309,6 +378,7 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
switch id {
|
||||
case testPluginID:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: testPluginID,
|
||||
Dependencies: []*storage.Dependency{{ID: pluginDependencyID}},
|
||||
Path: "test-plugin.zip",
|
||||
}, nil
|
||||
@@ -352,6 +422,6 @@ func createPlugin(t *testing.T, pluginID string, class plugins.Class, managed, b
|
||||
return p
|
||||
}
|
||||
|
||||
func testCompatOpts() plugins.CompatOpts {
|
||||
return plugins.NewCompatOpts("10.0.0", runtime.GOOS, runtime.GOARCH)
|
||||
func testCompatOpts() plugins.AddOpts {
|
||||
return plugins.NewAddOpts("10.0.0", runtime.GOOS, runtime.GOARCH, "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user