mirror of
https://github.com/grafana/grafana.git
synced 2026-07-30 08:18:10 -05: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:
@@ -410,9 +410,28 @@ func normalizeVersion(version string) string {
|
||||
return normalized
|
||||
}
|
||||
|
||||
// selectVersion returns latest version if none is specified or the specified version. If the version string is not
|
||||
// matched to existing version it errors out. It also errors out if version that is matched is not available for current
|
||||
// os and platform. It expects plugin.Versions to be sorted so the newest version is first.
|
||||
func (i *Installer) GetUpdateInfo(pluginID, version, pluginRepoURL string) (plugins.UpdateInfo, error) {
|
||||
plugin, err := i.getPluginMetadataFromPluginRepo(pluginID, pluginRepoURL)
|
||||
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) {
|
||||
var ver Version
|
||||
|
||||
|
||||
@@ -726,6 +726,8 @@ func (pm *PluginManager) StaticRoutes() []*plugins.PluginStaticRoute {
|
||||
|
||||
func (pm *PluginManager) Install(ctx context.Context, pluginID, version string) error {
|
||||
plugin := pm.GetPlugin(pluginID)
|
||||
|
||||
var pluginZipURL string
|
||||
if plugin != nil {
|
||||
if plugin.IsCorePlugin {
|
||||
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
|
||||
err := pm.Uninstall(context.Background(), plugin.Id)
|
||||
err = pm.Uninstall(context.Background(), plugin.Id)
|
||||
if err != nil {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -702,6 +702,10 @@ func (f *fakePluginInstaller) Uninstall(ctx context.Context, pluginPath string)
|
||||
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 {
|
||||
t.Helper()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user