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
This commit is contained in:
Will Browne
2022-08-23 11:50:50 +02:00
committed by GitHub
parent cc78486535
commit 26dfdd5af3
36 changed files with 1399 additions and 998 deletions

View File

@@ -11,7 +11,8 @@ import (
"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/grafana/grafana/pkg/plugins/manager/installer"
"github.com/grafana/grafana/pkg/plugins/repo"
"github.com/grafana/grafana/pkg/plugins/storage"
)
func validateInput(c utils.CommandLine, pluginFolder string) error {
@@ -48,16 +49,49 @@ func (cmd Command) installCommand(c utils.CommandLine) error {
pluginID := c.Args().First()
version := c.Args().Get(1)
return InstallPlugin(pluginID, version, c)
return installPlugin(context.Background(), pluginID, version, c)
}
// InstallPlugin downloads the plugin code as a zip file from the Grafana.com API
// and then extracts the zip into the plugins directory.
func InstallPlugin(pluginID, version string, c utils.CommandLine) error {
// installPlugin downloads the plugin code as a zip file from the Grafana.com API
// and then extracts the zip into the plugin's directory.
func installPlugin(ctx context.Context, pluginID, version string, c utils.CommandLine) error {
skipTLSVerify := c.Bool("insecure")
repository := repo.New(skipTLSVerify, c.PluginRepoURL(), services.Logger)
i := installer.New(skipTLSVerify, services.GrafanaVersion, services.Logger)
return i.Install(context.Background(), pluginID, version, c.PluginDirectory(), c.PluginURL(), c.PluginRepoURL())
compatOpts := repo.NewCompatOpts(services.GrafanaVersion, runtime.GOOS, runtime.GOARCH)
var archive *repo.PluginArchive
var err error
pluginZipURL := c.PluginURL()
if pluginZipURL != "" {
if archive, err = repository.GetPluginArchiveByURL(ctx, pluginZipURL, compatOpts); err != nil {
return err
}
} else {
if archive, err = repository.GetPluginArchive(ctx, pluginID, version, compatOpts); err != nil {
return err
}
}
pluginFs := storage.FileSystem(services.Logger, c.PluginDirectory())
extractedArchive, err := pluginFs.Add(ctx, pluginID, archive.File)
if err != nil {
return err
}
for _, dep := range extractedArchive.Dependencies {
services.Logger.Infof("Fetching %s dependency...", dep.ID)
d, err := repository.GetPluginArchive(ctx, dep.ID, dep.Version, compatOpts)
if err != nil {
return fmt.Errorf("%v: %w", fmt.Sprintf("failed to download plugin %s from repository", dep.ID), err)
}
_, err = pluginFs.Add(ctx, dep.ID, d.File)
if err != nil {
return err
}
}
return nil
}
func osAndArchString() string {

View File

@@ -1,6 +1,8 @@
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"
@@ -54,7 +56,7 @@ func (cmd Command) upgradeAllCommand(c utils.CommandLine) error {
return err
}
err = InstallPlugin(p.ID, "", c)
err = installPlugin(context.Background(), p.ID, "", c)
if err != nil {
return err
}

View File

@@ -1,6 +1,7 @@
package commands
import (
"context"
"fmt"
"github.com/fatih/color"
@@ -29,7 +30,7 @@ func (cmd Command) upgradeCommand(c utils.CommandLine) error {
return fmt.Errorf("failed to remove plugin '%s': %w", pluginName, err)
}
return InstallPlugin(pluginName, "", c)
return installPlugin(context.Background(), pluginName, "", c)
}
logger.Infof("%s %s is up to date \n", color.GreenString("✔"), pluginName)

View File

@@ -8,12 +8,12 @@ import (
)
type CLILogger struct {
DebugMode bool
debugMode bool
}
func New(debugMode bool) *CLILogger {
return &CLILogger{
DebugMode: debugMode,
debugMode: debugMode,
}
}
@@ -36,13 +36,13 @@ func (l *CLILogger) Infof(format string, args ...interface{}) {
func (l *CLILogger) Debug(args ...interface{}) {
args = append(args, "\n\n")
if l.DebugMode {
if l.debugMode {
fmt.Print(color.HiBlueString(fmt.Sprint(args...)))
}
}
func (l *CLILogger) Debugf(format string, args ...interface{}) {
if l.DebugMode {
if l.debugMode {
fmt.Print(color.HiBlueString(fmt.Sprintf(addNewlines(format), args...)))
}
}

View File

@@ -37,6 +37,7 @@ import (
"github.com/grafana/grafana/pkg/plugins/manager/loader"
"github.com/grafana/grafana/pkg/plugins/manager/registry"
"github.com/grafana/grafana/pkg/plugins/plugincontext"
"github.com/grafana/grafana/pkg/plugins/repo"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/accesscontrol/ossaccesscontrol"
"github.com/grafana/grafana/pkg/services/alerting"
@@ -171,6 +172,8 @@ var wireSet = wire.NewSet(
uss.ProvideService,
registry.ProvideService,
wire.Bind(new(registry.Service), new(*registry.InMemory)),
repo.ProvideService,
wire.Bind(new(repo.Service), new(*repo.Manager)),
manager.ProvideService,
wire.Bind(new(plugins.Manager), new(*manager.PluginManager)),
wire.Bind(new(plugins.Client), new(*manager.PluginManager)),