Plugins: Enable plugin runtime install/uninstall capabilities (#33836)
* add uninstall flow * add install flow * small cleanup * smaller-footprint solution * cleanup + make bp start auto * fix interface contract * improve naming * accept version arg * ensure use of shared logger * make installer a field * add plugin decommissioning * add basic error checking * fix api docs * making initialization idempotent * add mutex * fix comment * fix test * add test for decommission * improve existing test * add more test coverage * more tests * change test func to use read lock * refactoring + adding test asserts * improve purging old install flow * improve dupe checking * change log name * skip over dupe scanned * make test assertion more flexible * remove trailing line * fix pointer receiver name * update comment * add context to API * add config flag * add base http api test + fix update functionality * simplify existing check * clean up test * refactor tests based on feedback * add single quotes to errs * use gcmp in tests + fix logo issue * make plugin list testing more flexible * address feedback * fix API test * fix linter * undo preallocate * Update docs/sources/administration/configuration.md Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Update docs/sources/administration/configuration.md Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * Update docs/sources/administration/configuration.md Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> * fix linting issue in test * add docs placeholder * update install notes * Update docs/sources/plugins/marketplace.md Co-authored-by: Marcus Olsson <marcus.olsson@hey.com> * update access wording * add more placeholder docs * add link to more info * PR feedback - improved errors, refactor, lock fix * improve err details * propagate plugin version errors * don't autostart renderer * add H1 * fix imports Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com> Co-authored-by: Marcus Olsson <marcus.olsson@hey.com>
This commit is contained in:
co-authored by
achatterjee-grafana
Marcus Olsson
parent
1fbadab600
commit
c39d6ad97d
@@ -4,6 +4,7 @@ import (
|
||||
"archive/zip"
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
@@ -40,8 +41,8 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFoundError = errors.New("404 not found error")
|
||||
reGitBuild = regexp.MustCompile("^[a-zA-Z0-9_.-]*/")
|
||||
ErrPluginNotFound = errors.New("plugin not found")
|
||||
reGitBuild = regexp.MustCompile("^[a-zA-Z0-9_.-]*/")
|
||||
)
|
||||
|
||||
type BadRequestError struct {
|
||||
@@ -56,6 +57,35 @@ func (e *BadRequestError) Error() string {
|
||||
return e.Status
|
||||
}
|
||||
|
||||
type ErrVersionUnsupported struct {
|
||||
PluginID string
|
||||
RequestedVersion string
|
||||
RecommendedVersion string
|
||||
}
|
||||
|
||||
func (e ErrVersionUnsupported) Error() string {
|
||||
if len(e.RecommendedVersion) > 0 {
|
||||
return fmt.Sprintf("%s v%s is not supported on your architecture and OS, latest suitable version is %s",
|
||||
e.PluginID, e.RequestedVersion, e.RecommendedVersion)
|
||||
}
|
||||
return fmt.Sprintf("%s v%s is not supported on your architecture and OS", e.PluginID, e.RequestedVersion)
|
||||
}
|
||||
|
||||
type ErrVersionNotFound struct {
|
||||
PluginID string
|
||||
RequestedVersion string
|
||||
RecommendedVersion string
|
||||
}
|
||||
|
||||
func (e ErrVersionNotFound) Error() string {
|
||||
if len(e.RecommendedVersion) > 0 {
|
||||
return fmt.Sprintf("%s v%s is not supported on your architecture and OS, latest suitable version is %s",
|
||||
e.PluginID, e.RequestedVersion, e.RecommendedVersion)
|
||||
}
|
||||
return fmt.Sprintf("could not find a version %s for %s. The latest suitable version is %s", e.RequestedVersion,
|
||||
e.PluginID, e.RecommendedVersion)
|
||||
}
|
||||
|
||||
func New(skipTLSVerify bool, grafanaVersion string, logger plugins.PluginInstallerLogger) *Installer {
|
||||
return &Installer{
|
||||
httpClient: makeHttpClient(skipTLSVerify, 10*time.Second),
|
||||
@@ -67,7 +97,7 @@ func New(skipTLSVerify bool, grafanaVersion string, logger plugins.PluginInstall
|
||||
|
||||
// Install downloads the plugin code as a zip file from specified URL
|
||||
// and then extracts the zip into the provided plugins directory.
|
||||
func (i *Installer) Install(pluginID, version, pluginsDir, pluginZipURL, pluginRepoURL string) error {
|
||||
func (i *Installer) Install(ctx context.Context, pluginID, version, pluginsDir, pluginZipURL, pluginRepoURL string) error {
|
||||
isInternal := false
|
||||
|
||||
var checksum string
|
||||
@@ -140,13 +170,13 @@ func (i *Installer) Install(pluginID, version, pluginsDir, pluginZipURL, pluginR
|
||||
|
||||
res, _ := toPluginDTO(pluginsDir, pluginID)
|
||||
|
||||
i.log.Successf("Installed %s v%s successfully", res.ID, res.Info.Version)
|
||||
i.log.Successf("Downloaded %s v%s zip successfully", res.ID, res.Info.Version)
|
||||
|
||||
// download dependency plugins
|
||||
for _, dep := range res.Dependencies.Plugins {
|
||||
i.log.Infof("Fetching %s dependencies...", res.ID)
|
||||
if err := i.Install(dep.ID, normalizeVersion(dep.Version), pluginsDir, "", pluginRepoURL); err != nil {
|
||||
return errutil.Wrapf(err, "failed to install plugin '%s'", dep.ID)
|
||||
if err := i.Install(ctx, dep.ID, normalizeVersion(dep.Version), pluginsDir, "", pluginRepoURL); err != nil {
|
||||
return errutil.Wrapf(err, "failed to install plugin %s", dep.ID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +184,7 @@ func (i *Installer) Install(pluginID, version, pluginsDir, pluginZipURL, pluginR
|
||||
}
|
||||
|
||||
// Uninstall removes the specified plugin from the provided plugins directory.
|
||||
func (i *Installer) Uninstall(pluginID, pluginPath string) error {
|
||||
func (i *Installer) Uninstall(ctx context.Context, pluginID, pluginPath string) error {
|
||||
pluginDir := filepath.Join(pluginPath, pluginID)
|
||||
|
||||
// verify it's a plugin directory
|
||||
@@ -253,10 +283,9 @@ func (i *Installer) getPluginMetadataFromPluginRepo(pluginID, pluginRepoURL stri
|
||||
i.log.Debugf("Fetching metadata for plugin \"%s\" from repo %s", pluginID, pluginRepoURL)
|
||||
body, err := i.sendRequestGetBytes(pluginRepoURL, "repo", pluginID)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFoundError) {
|
||||
return Plugin{},
|
||||
fmt.Errorf("failed to find plugin \"%s\" in plugin repository. Please check if plugin ID is correct",
|
||||
pluginID)
|
||||
if errors.Is(err, ErrPluginNotFound) {
|
||||
i.log.Errorf("failed to find plugin '%s' in plugin repository. Please check if plugin ID is correct", pluginID)
|
||||
return Plugin{}, err
|
||||
}
|
||||
return Plugin{}, errutil.Wrap("Failed to send request", err)
|
||||
}
|
||||
@@ -335,7 +364,7 @@ func (i *Installer) createRequest(URL string, subPaths ...string) (*http.Request
|
||||
|
||||
func (i *Installer) handleResponse(res *http.Response) (io.ReadCloser, error) {
|
||||
if res.StatusCode == 404 {
|
||||
return nil, ErrNotFoundError
|
||||
return nil, ErrPluginNotFound
|
||||
}
|
||||
|
||||
if res.StatusCode/100 != 2 && res.StatusCode/100 != 4 {
|
||||
@@ -405,7 +434,10 @@ func selectVersion(plugin *Plugin, version string) (*Version, error) {
|
||||
|
||||
latestForArch := latestSupportedVersion(plugin)
|
||||
if latestForArch == nil {
|
||||
return nil, fmt.Errorf("%s is not supported on your architecture and OS", plugin.ID)
|
||||
return nil, ErrVersionUnsupported{
|
||||
PluginID: plugin.ID,
|
||||
RequestedVersion: version,
|
||||
}
|
||||
}
|
||||
|
||||
if version == "" {
|
||||
@@ -419,14 +451,19 @@ func selectVersion(plugin *Plugin, version string) (*Version, error) {
|
||||
}
|
||||
|
||||
if len(ver.Version) == 0 {
|
||||
return nil, fmt.Errorf("could not find a version %s for %s. The latest suitable version is %s",
|
||||
version, plugin.ID, latestForArch.Version)
|
||||
return nil, ErrVersionNotFound{
|
||||
PluginID: plugin.ID,
|
||||
RequestedVersion: version,
|
||||
RecommendedVersion: latestForArch.Version,
|
||||
}
|
||||
}
|
||||
|
||||
if !supportsCurrentArch(&ver) {
|
||||
return nil, fmt.Errorf(
|
||||
"the version you requested is not supported on your architecture and OS, latest suitable version is %s",
|
||||
latestForArch.Version)
|
||||
return nil, ErrVersionUnsupported{
|
||||
PluginID: plugin.ID,
|
||||
RequestedVersion: version,
|
||||
RecommendedVersion: latestForArch.Version,
|
||||
}
|
||||
}
|
||||
|
||||
return &ver, nil
|
||||
|
||||
Reference in New Issue
Block a user