mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Perform plugin update check prior to uninstall (#36909)
* only uninstall if update is possible * add . * update lingo for clarity
This commit is contained in:
parent
8ebba0a93d
commit
08a73a5291
@ -74,11 +74,12 @@ type DataRequestHandler interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PluginInstaller interface {
|
type PluginInstaller interface {
|
||||||
// Install finds the plugin given the provided information
|
// Install finds the plugin given the provided information and installs in the provided plugins directory.
|
||||||
// and installs in the provided plugins directory.
|
|
||||||
Install(ctx context.Context, pluginID, version, pluginsDirectory, pluginZipURL, pluginRepoURL string) error
|
Install(ctx context.Context, pluginID, version, pluginsDirectory, pluginZipURL, pluginRepoURL string) error
|
||||||
// Uninstall removes the specified plugin from the provided plugins directory.
|
// Uninstall removes the specified plugin from the provided plugins directory.
|
||||||
Uninstall(ctx context.Context, pluginPath string) error
|
Uninstall(ctx context.Context, pluginPath string) error
|
||||||
|
// GetUpdateInfo returns update information if the requested plugin is supported on the running system.
|
||||||
|
GetUpdateInfo(pluginID, version, pluginRepoURL string) (UpdateInfo, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type PluginInstallerLogger interface {
|
type PluginInstallerLogger interface {
|
||||||
|
@ -410,9 +410,28 @@ func normalizeVersion(version string) string {
|
|||||||
return normalized
|
return normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
// selectVersion returns latest version if none is specified or the specified version. If the version string is not
|
func (i *Installer) GetUpdateInfo(pluginID, version, pluginRepoURL string) (plugins.UpdateInfo, error) {
|
||||||
// matched to existing version it errors out. It also errors out if version that is matched is not available for current
|
plugin, err := i.getPluginMetadataFromPluginRepo(pluginID, pluginRepoURL)
|
||||||
// os and platform. It expects plugin.Versions to be sorted so the newest version is first.
|
if err != nil {
|
||||||
|
return plugins.UpdateInfo{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := i.selectVersion(&plugin, version)
|
||||||
|
if err != nil {
|
||||||
|
return plugins.UpdateInfo{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return plugins.UpdateInfo{
|
||||||
|
PluginZipURL: fmt.Sprintf("%s/%s/versions/%s/download", pluginRepoURL, pluginID, v.Version),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectVersion selects the most appropriate plugin version
|
||||||
|
// returns the specified version if supported.
|
||||||
|
// returns latest version if no specific version is specified.
|
||||||
|
// returns error if the supplied version does not exist.
|
||||||
|
// returns error if supplied version exists but is not supported.
|
||||||
|
// NOTE: It expects plugin.Versions to be sorted so the newest version is first.
|
||||||
func (i *Installer) selectVersion(plugin *Plugin, version string) (*Version, error) {
|
func (i *Installer) selectVersion(plugin *Plugin, version string) (*Version, error) {
|
||||||
var ver Version
|
var ver Version
|
||||||
|
|
||||||
|
@ -726,6 +726,8 @@ func (pm *PluginManager) StaticRoutes() []*plugins.PluginStaticRoute {
|
|||||||
|
|
||||||
func (pm *PluginManager) Install(ctx context.Context, pluginID, version string) error {
|
func (pm *PluginManager) Install(ctx context.Context, pluginID, version string) error {
|
||||||
plugin := pm.GetPlugin(pluginID)
|
plugin := pm.GetPlugin(pluginID)
|
||||||
|
|
||||||
|
var pluginZipURL string
|
||||||
if plugin != nil {
|
if plugin != nil {
|
||||||
if plugin.IsCorePlugin {
|
if plugin.IsCorePlugin {
|
||||||
return plugins.ErrInstallCorePlugin
|
return plugins.ErrInstallCorePlugin
|
||||||
@ -738,14 +740,22 @@ func (pm *PluginManager) Install(ctx context.Context, pluginID, version string)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get plugin update information to confirm if upgrading is possible
|
||||||
|
updateInfo, err := pm.pluginInstaller.GetUpdateInfo(pluginID, version, grafanaComURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
pluginZipURL = updateInfo.PluginZipURL
|
||||||
|
|
||||||
// remove existing installation of plugin
|
// remove existing installation of plugin
|
||||||
err := pm.Uninstall(context.Background(), plugin.Id)
|
err = pm.Uninstall(context.Background(), plugin.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := pm.pluginInstaller.Install(ctx, pluginID, version, pm.Cfg.PluginsPath, "", grafanaComURL)
|
err := pm.pluginInstaller.Install(ctx, pluginID, version, pm.Cfg.PluginsPath, pluginZipURL, grafanaComURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -702,6 +702,10 @@ func (f *fakePluginInstaller) Uninstall(ctx context.Context, pluginPath string)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *fakePluginInstaller) GetUpdateInfo(pluginID, version, pluginRepoURL string) (plugins.UpdateInfo, error) {
|
||||||
|
return plugins.UpdateInfo{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func createManager(t *testing.T, cbs ...func(*PluginManager)) *PluginManager {
|
func createManager(t *testing.T, cbs ...func(*PluginManager)) *PluginManager {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
@ -158,3 +158,7 @@ type EnabledPlugins struct {
|
|||||||
DataSources map[string]*DataSourcePlugin
|
DataSources map[string]*DataSourcePlugin
|
||||||
Apps []*AppPlugin
|
Apps []*AppPlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateInfo struct {
|
||||||
|
PluginZipURL string
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user