Files
grafana/pkg/cmd/grafana-cli/commands/upgrade_all_command.go
Will Browne 26dfdd5af3 Plugins: Refactor plugin download/installation (#43046)
* installer -> repo

* add semver format checking

* add plugin callbacks in test

* remove newline

* post install only scans new directories

* remove unused stuff

* everything in own package

* add missing cli params

* make grafana version part of the API

* resolve conflicts

* tidy up logger

* fix cli and tidy log statements

* rename log package

* update struct name

* fix linter issue

* fs -> filestore

* reorder imports

* alias import

* fix test

* fix test

* inline var

* revert jsonc file

* make repo dep of manager

* actually inject the thing

* accept all args for compatability checks

* accept compat from store

* pass os + arch vals

* don't inject fs

* tidy up

* tidy up

* merge with main and tidy fs storage

* fix test

* fix packages

* fix comment + field name

* update fs naming

* fixed wire

* remove unused func

* fix mocks

* fix storage test

* renaming

* fix log line

* fix test

* re-order field

* tidying

* add test for update with same version

* fix wire for CLI

* remove use of ioutil

* don't pass field

* small tidy

* ignore code scanning warn

* fix testdata link

* update lgtm code
2022-08-23 11:50:50 +02:00

67 lines
1.5 KiB
Go

package commands
import (
"context"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
"github.com/hashicorp/go-version"
)
func shouldUpgrade(installed string, remote *models.Plugin) bool {
installedVersion, err := version.NewVersion(installed)
if err != nil {
return false
}
latest := latestSupportedVersion(remote)
latestVersion, err := version.NewVersion(latest.Version)
if err != nil {
return false
}
return installedVersion.LessThan(latestVersion)
}
func (cmd Command) upgradeAllCommand(c utils.CommandLine) error {
pluginsDir := c.PluginDirectory()
localPlugins := services.GetLocalPlugins(pluginsDir)
remotePlugins, err := cmd.Client.ListAllPlugins(c.String("repo"))
if err != nil {
return err
}
pluginsToUpgrade := make([]models.InstalledPlugin, 0)
for _, localPlugin := range localPlugins {
for _, p := range remotePlugins.Plugins {
remotePlugin := p
if localPlugin.ID != remotePlugin.ID {
continue
}
if shouldUpgrade(localPlugin.Info.Version, &remotePlugin) {
pluginsToUpgrade = append(pluginsToUpgrade, localPlugin)
}
}
}
for _, p := range pluginsToUpgrade {
logger.Infof("Updating %v \n", p.ID)
err := services.RemoveInstalledPlugin(pluginsDir, p.ID)
if err != nil {
return err
}
err = installPlugin(context.Background(), p.ID, "", c)
if err != nil {
return err
}
}
return nil
}