2022-01-31 09:06:16 -06:00
|
|
|
package updatechecker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-03-28 04:01:06 -05:00
|
|
|
"fmt"
|
2022-08-10 08:37:51 -05:00
|
|
|
"io"
|
2022-01-31 09:06:16 -06:00
|
|
|
"net/http"
|
2023-03-28 04:01:06 -05:00
|
|
|
"net/url"
|
2022-01-31 09:06:16 -06:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-03-28 04:01:06 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
2022-01-31 09:06:16 -06:00
|
|
|
"github.com/hashicorp/go-version"
|
2023-03-28 04:01:06 -05:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
2022-01-31 09:06:16 -06:00
|
|
|
|
2023-03-28 04:01:06 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/httpclient/httpclientprovider"
|
2022-01-31 09:06:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2023-03-28 04:01:06 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2023-09-11 06:59:24 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
2022-01-31 09:06:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PluginsService struct {
|
|
|
|
availableUpdates map[string]string
|
|
|
|
|
|
|
|
enabled bool
|
|
|
|
grafanaVersion string
|
2023-09-11 06:59:24 -05:00
|
|
|
pluginStore pluginstore.Store
|
2022-01-31 09:06:16 -06:00
|
|
|
httpClient httpClient
|
|
|
|
mutex sync.RWMutex
|
|
|
|
log log.Logger
|
2023-03-28 04:01:06 -05:00
|
|
|
tracer tracing.Tracer
|
2024-04-11 05:04:53 -05:00
|
|
|
updateCheckURL *url.URL
|
2022-01-31 09:06:16 -06:00
|
|
|
}
|
|
|
|
|
2023-09-11 06:59:24 -05:00
|
|
|
func ProvidePluginsService(cfg *setting.Cfg, pluginStore pluginstore.Store, tracer tracing.Tracer) (*PluginsService, error) {
|
2023-03-28 04:01:06 -05:00
|
|
|
logger := log.New("plugins.update.checker")
|
|
|
|
cl, err := httpclient.New(httpclient.Options{
|
|
|
|
Middlewares: []httpclient.Middleware{
|
|
|
|
httpclientprovider.TracingMiddleware(logger, tracer),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-11 05:04:53 -05:00
|
|
|
updateCheckURL, err := url.JoinPath(cfg.GrafanaComAPIURL, "plugins", "versioncheck")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedUpdateCheckURL, err := url.Parse(updateCheckURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-31 09:06:16 -06:00
|
|
|
return &PluginsService{
|
2022-04-06 03:50:21 -05:00
|
|
|
enabled: cfg.CheckForPluginUpdates,
|
2022-01-31 09:06:16 -06:00
|
|
|
grafanaVersion: cfg.BuildVersion,
|
2023-03-28 04:01:06 -05:00
|
|
|
httpClient: cl,
|
|
|
|
log: logger,
|
|
|
|
tracer: tracer,
|
2022-01-31 09:06:16 -06:00
|
|
|
pluginStore: pluginStore,
|
|
|
|
availableUpdates: make(map[string]string),
|
2024-04-11 05:04:53 -05:00
|
|
|
updateCheckURL: parsedUpdateCheckURL,
|
2023-03-28 04:01:06 -05:00
|
|
|
}, nil
|
2022-01-31 09:06:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PluginsService) IsDisabled() bool {
|
|
|
|
return !s.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PluginsService) Run(ctx context.Context) error {
|
2023-03-28 04:01:06 -05:00
|
|
|
s.instrumentedCheckForUpdates(ctx)
|
2022-01-31 09:06:16 -06:00
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Minute * 10)
|
|
|
|
run := true
|
|
|
|
|
|
|
|
for run {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2023-03-28 04:01:06 -05:00
|
|
|
s.instrumentedCheckForUpdates(ctx)
|
2022-01-31 09:06:16 -06:00
|
|
|
case <-ctx.Done():
|
|
|
|
run = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PluginsService) HasUpdate(ctx context.Context, pluginID string) (string, bool) {
|
|
|
|
s.mutex.RLock()
|
|
|
|
updateVers, updateAvailable := s.availableUpdates[pluginID]
|
|
|
|
s.mutex.RUnlock()
|
|
|
|
if updateAvailable {
|
|
|
|
// check if plugin has already been updated since the last invocation of `checkForUpdates`
|
|
|
|
plugin, exists := s.pluginStore.Plugin(ctx, pluginID)
|
|
|
|
if !exists {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
if canUpdate(plugin.Info.Version, updateVers) {
|
|
|
|
return updateVers, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2023-03-28 04:01:06 -05:00
|
|
|
func (s *PluginsService) instrumentedCheckForUpdates(ctx context.Context) {
|
|
|
|
start := time.Now()
|
|
|
|
ctx, span := s.tracer.Start(ctx, "updatechecker.PluginsService.checkForUpdates")
|
|
|
|
defer span.End()
|
|
|
|
ctxLogger := s.log.FromContext(ctx)
|
|
|
|
if err := s.checkForUpdates(ctx); err != nil {
|
|
|
|
span.SetStatus(codes.Error, fmt.Sprintf("update check failed: %s", err))
|
|
|
|
span.RecordError(err)
|
|
|
|
ctxLogger.Debug("Update check failed", "error", err, "duration", time.Since(start))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctxLogger.Info("Update check succeeded", "duration", time.Since(start))
|
|
|
|
}
|
2022-01-31 09:06:16 -06:00
|
|
|
|
2023-03-28 04:01:06 -05:00
|
|
|
func (s *PluginsService) checkForUpdates(ctx context.Context) error {
|
|
|
|
ctxLogger := s.log.FromContext(ctx)
|
2024-04-11 05:04:53 -05:00
|
|
|
ctxLogger.Debug("Preparing plugins eligible for version check")
|
2022-01-31 09:06:16 -06:00
|
|
|
localPlugins := s.pluginsEligibleForVersionCheck(ctx)
|
2024-04-11 05:04:53 -05:00
|
|
|
requestURL := s.updateCheckURL
|
|
|
|
requestURLParameters := requestURL.Query()
|
|
|
|
requestURLParameters.Set("slugIn", s.pluginIDsCSV((localPlugins)))
|
|
|
|
requestURLParameters.Set("grafanaVersion", s.grafanaVersion)
|
|
|
|
requestURL.RawQuery = requestURLParameters.Encode()
|
|
|
|
|
|
|
|
ctxLogger.Debug("Checking for plugin updates", "url", requestURL)
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL.String(), nil)
|
2022-01-31 09:06:16 -06:00
|
|
|
if err != nil {
|
2023-03-28 04:01:06 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := s.httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get plugins repo from grafana.com: %w", err)
|
2022-01-31 09:06:16 -06:00
|
|
|
}
|
|
|
|
defer func() {
|
2023-03-28 04:01:06 -05:00
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
ctxLogger.Warn("Failed to close response body", "err", err)
|
2022-01-31 09:06:16 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-08-10 08:37:51 -05:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2022-01-31 09:06:16 -06:00
|
|
|
if err != nil {
|
2023-03-28 04:01:06 -05:00
|
|
|
return fmt.Errorf("failed to read response from grafana.com: %w", err)
|
2022-01-31 09:06:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type gcomPlugin struct {
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
var gcomPlugins []gcomPlugin
|
|
|
|
err = json.Unmarshal(body, &gcomPlugins)
|
|
|
|
if err != nil {
|
2023-03-28 04:01:06 -05:00
|
|
|
return fmt.Errorf("failed to unmarshal plugin repo, reading response from grafana.com: %w", err)
|
2022-01-31 09:06:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
availableUpdates := map[string]string{}
|
|
|
|
for _, gcomP := range gcomPlugins {
|
|
|
|
if localP, exists := localPlugins[gcomP.Slug]; exists {
|
|
|
|
if canUpdate(localP.Info.Version, gcomP.Version) {
|
|
|
|
availableUpdates[localP.ID] = gcomP.Version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(availableUpdates) > 0 {
|
|
|
|
s.mutex.Lock()
|
|
|
|
s.availableUpdates = availableUpdates
|
|
|
|
s.mutex.Unlock()
|
|
|
|
}
|
2023-03-28 04:01:06 -05:00
|
|
|
|
|
|
|
return nil
|
2022-01-31 09:06:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func canUpdate(v1, v2 string) bool {
|
|
|
|
ver1, err1 := version.NewVersion(v1)
|
|
|
|
if err1 != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ver2, err2 := version.NewVersion(v2)
|
|
|
|
if err2 != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return ver1.LessThan(ver2)
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:59:24 -05:00
|
|
|
func (s *PluginsService) pluginIDsCSV(m map[string]pluginstore.Plugin) string {
|
2022-11-22 06:24:36 -06:00
|
|
|
ids := make([]string, 0, len(m))
|
2022-01-31 09:06:16 -06:00
|
|
|
for pluginID := range m {
|
|
|
|
ids = append(ids, pluginID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(ids, ",")
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:59:24 -05:00
|
|
|
func (s *PluginsService) pluginsEligibleForVersionCheck(ctx context.Context) map[string]pluginstore.Plugin {
|
|
|
|
result := make(map[string]pluginstore.Plugin)
|
2022-01-31 09:06:16 -06:00
|
|
|
for _, p := range s.pluginStore.Plugins(ctx) {
|
|
|
|
if p.IsCorePlugin() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result[p.ID] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|