2020-06-22 23:49:13 +08:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
2021-12-03 01:08:59 +08:00
|
|
|
"context"
|
2020-11-19 21:47:17 +08:00
|
|
|
"errors"
|
|
|
|
|
2020-06-22 23:49:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-03-18 20:53:01 +08:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2020-06-22 23:49:13 +08:00
|
|
|
)
|
|
|
|
|
2022-02-23 18:12:37 +08:00
|
|
|
type Store interface {
|
|
|
|
GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error
|
|
|
|
GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error
|
|
|
|
UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error
|
|
|
|
}
|
|
|
|
|
2020-06-22 23:49:13 +08:00
|
|
|
// Provision scans a directory for provisioning config files
|
|
|
|
// and provisions the app in those files.
|
2022-02-23 18:12:37 +08:00
|
|
|
func Provision(ctx context.Context, configDirectory string, store Store, pluginStore plugins.Store) error {
|
2021-03-18 20:53:01 +08:00
|
|
|
logger := log.New("provisioning.plugins")
|
|
|
|
ap := PluginProvisioner{
|
|
|
|
log: logger,
|
2021-11-01 17:53:33 +08:00
|
|
|
cfgProvider: newConfigReader(logger, pluginStore),
|
2022-02-23 18:12:37 +08:00
|
|
|
store: store,
|
2021-03-18 20:53:01 +08:00
|
|
|
}
|
2021-12-03 01:08:59 +08:00
|
|
|
return ap.applyChanges(ctx, configDirectory)
|
2020-06-22 23:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// PluginProvisioner is responsible for provisioning apps based on
|
|
|
|
// configuration read by the `configReader`
|
|
|
|
type PluginProvisioner struct {
|
|
|
|
log log.Logger
|
|
|
|
cfgProvider configReader
|
2022-02-23 18:12:37 +08:00
|
|
|
store Store
|
2020-06-22 23:49:13 +08:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:08:59 +08:00
|
|
|
func (ap *PluginProvisioner) apply(ctx context.Context, cfg *pluginsAsConfig) error {
|
2020-06-22 23:49:13 +08:00
|
|
|
for _, app := range cfg.Apps {
|
|
|
|
if app.OrgID == 0 && app.OrgName != "" {
|
|
|
|
getOrgQuery := &models.GetOrgByNameQuery{Name: app.OrgName}
|
2022-02-23 18:12:37 +08:00
|
|
|
if err := ap.store.GetOrgByNameHandler(ctx, getOrgQuery); err != nil {
|
2020-06-22 23:49:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
app.OrgID = getOrgQuery.Result.Id
|
|
|
|
} else if app.OrgID < 0 {
|
|
|
|
app.OrgID = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
query := &models.GetPluginSettingByIdQuery{OrgId: app.OrgID, PluginId: app.PluginID}
|
2022-02-23 18:12:37 +08:00
|
|
|
err := ap.store.GetPluginSettingById(ctx, query)
|
2020-06-22 23:49:13 +08:00
|
|
|
if err != nil {
|
2020-11-19 21:47:17 +08:00
|
|
|
if !errors.Is(err, models.ErrPluginSettingNotFound) {
|
2020-06-22 23:49:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app.PluginVersion = query.Result.PluginVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
ap.log.Info("Updating app from configuration ", "type", app.PluginID, "enabled", app.Enabled)
|
|
|
|
cmd := &models.UpdatePluginSettingCmd{
|
|
|
|
OrgId: app.OrgID,
|
|
|
|
PluginId: app.PluginID,
|
|
|
|
Enabled: app.Enabled,
|
|
|
|
Pinned: app.Pinned,
|
|
|
|
JsonData: app.JSONData,
|
|
|
|
SecureJsonData: app.SecureJSONData,
|
|
|
|
PluginVersion: app.PluginVersion,
|
|
|
|
}
|
2022-02-23 18:12:37 +08:00
|
|
|
if err := ap.store.UpdatePluginSetting(ctx, cmd); err != nil {
|
2020-06-22 23:49:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-03 01:08:59 +08:00
|
|
|
func (ap *PluginProvisioner) applyChanges(ctx context.Context, configPath string) error {
|
2021-12-22 18:02:42 +08:00
|
|
|
configs, err := ap.cfgProvider.readConfig(ctx, configPath)
|
2020-06-22 23:49:13 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cfg := range configs {
|
2021-12-03 01:08:59 +08:00
|
|
|
if err := ap.apply(ctx, cfg); err != nil {
|
2020-06-22 23:49:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|