forked from grafana.jool/grafana-jool
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.2 KiB
84 lines
2.2 KiB
package plugins
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
)
|
|
|
|
// Provision scans a directory for provisioning config files
|
|
// and provisions the app in those files.
|
|
func Provision(ctx context.Context, configDirectory string, pluginStore plugins.Store) error {
|
|
logger := log.New("provisioning.plugins")
|
|
ap := PluginProvisioner{
|
|
log: logger,
|
|
cfgProvider: newConfigReader(logger, pluginStore),
|
|
}
|
|
return ap.applyChanges(ctx, configDirectory)
|
|
}
|
|
|
|
// PluginProvisioner is responsible for provisioning apps based on
|
|
// configuration read by the `configReader`
|
|
type PluginProvisioner struct {
|
|
log log.Logger
|
|
cfgProvider configReader
|
|
}
|
|
|
|
func (ap *PluginProvisioner) apply(ctx context.Context, cfg *pluginsAsConfig) error {
|
|
for _, app := range cfg.Apps {
|
|
if app.OrgID == 0 && app.OrgName != "" {
|
|
getOrgQuery := &models.GetOrgByNameQuery{Name: app.OrgName}
|
|
if err := bus.DispatchCtx(ctx, getOrgQuery); err != nil {
|
|
return err
|
|
}
|
|
app.OrgID = getOrgQuery.Result.Id
|
|
} else if app.OrgID < 0 {
|
|
app.OrgID = 1
|
|
}
|
|
|
|
query := &models.GetPluginSettingByIdQuery{OrgId: app.OrgID, PluginId: app.PluginID}
|
|
err := bus.DispatchCtx(ctx, query)
|
|
if err != nil {
|
|
if !errors.Is(err, models.ErrPluginSettingNotFound) {
|
|
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,
|
|
}
|
|
if err := bus.DispatchCtx(ctx, cmd); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (ap *PluginProvisioner) applyChanges(ctx context.Context, configPath string) error {
|
|
configs, err := ap.cfgProvider.readConfig(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, cfg := range configs {
|
|
if err := ap.apply(ctx, cfg); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|