merge backend-datasource and datasource type

This commit is contained in:
bergquist 2017-12-15 17:17:58 +01:00
parent 8573f73d72
commit 7977d6b669
3 changed files with 61 additions and 13 deletions

View File

@ -3,7 +3,16 @@ package plugins
import ( import (
"encoding/json" "encoding/json"
"os" "os"
"os/exec"
"path"
"path/filepath" "path/filepath"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins/backend"
"github.com/grafana/grafana/pkg/tsdb"
shared "github.com/grafana/grafana/pkg/tsdb/models/proxy"
plugin "github.com/hashicorp/go-plugin"
) )
type DataSourcePlugin struct { type DataSourcePlugin struct {
@ -16,6 +25,12 @@ type DataSourcePlugin struct {
Mixed bool `json:"mixed,omitempty"` Mixed bool `json:"mixed,omitempty"`
HasQueryHelp bool `json:"hasQueryHelp,omitempty"` HasQueryHelp bool `json:"hasQueryHelp,omitempty"`
Routes []*AppPluginRoute `json:"routes"` Routes []*AppPluginRoute `json:"routes"`
Backend bool `json:"backend,omitempty"`
Executable string `json:"executable,omitempty"`
log log.Logger
client *plugin.Client
} }
func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error { func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
@ -39,3 +54,37 @@ func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
DataSources[p.Id] = p DataSources[p.Id] = p
return nil return nil
} }
func (p *DataSourcePlugin) initBackendPlugin(log log.Logger) error {
p.log = log.New("plugin-id", p.Id)
p.client = plugin.NewClient(&plugin.ClientConfig{
HandshakeConfig: handshakeConfig,
Plugins: map[string]plugin.Plugin{p.Id: &shared.TsdbPluginImpl{}},
Cmd: exec.Command(path.Join(p.PluginDir, p.Executable)),
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
Logger: backend.LogWrapper{Logger: p.log},
})
rpcClient, err := p.client.Client()
if err != nil {
return err
}
raw, err := rpcClient.Dispense(p.Id)
if err != nil {
return err
}
plugin := raw.(shared.TsdbPlugin)
tsdb.RegisterTsdbQueryEndpoint(p.Id, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
return &shared.TsdbWrapper{TsdbPlugin: plugin}, nil
})
return nil
}
func (p *DataSourcePlugin) Kill() {
p.client.Kill()
}

View File

@ -41,6 +41,8 @@ type PluginBase struct {
HideFromList bool `json:"hideFromList,omitempty"` HideFromList bool `json:"hideFromList,omitempty"`
State string `json:"state,omitempty"` State string `json:"state,omitempty"`
IncludedInAppId string `json:"-"` IncludedInAppId string `json:"-"`
PluginDir string `json:"-"` PluginDir string `json:"-"`
DefaultNavUrl string `json:"-"` DefaultNavUrl string `json:"-"`

View File

@ -70,10 +70,10 @@ func Init() error {
Plugins = make(map[string]*PluginBase) Plugins = make(map[string]*PluginBase)
BackendDatasources = make(map[string]*BackendDatasource) BackendDatasources = make(map[string]*BackendDatasource)
PluginTypes = map[string]interface{}{ PluginTypes = map[string]interface{}{
"panel": PanelPlugin{}, "panel": PanelPlugin{},
"datasource": DataSourcePlugin{}, "datasource": DataSourcePlugin{},
"app": AppPlugin{}, "app": AppPlugin{},
"backend-datasource": BackendDatasource{}, //"backend-datasource": BackendDatasource{},
} }
plog.Info("Starting plugin search") plog.Info("Starting plugin search")
@ -98,20 +98,17 @@ func Init() error {
for _, panel := range Panels { for _, panel := range Panels {
panel.initFrontendPlugin() panel.initFrontendPlugin()
} }
for _, panel := range DataSources { for _, ds := range DataSources {
panel.initFrontendPlugin() if ds.Backend {
ds.initBackendPlugin(plog)
}
ds.initFrontendPlugin()
} }
for _, app := range Apps { for _, app := range Apps {
app.initApp() app.initApp()
} }
for _, be := range BackendDatasources {
err := be.initBackendPlugin(plog)
if err != nil {
plog.Error("failed to init plugin", "id", be.Id, "error", err)
}
}
go StartPluginUpdateChecker() go StartPluginUpdateChecker()
go updateAppDashboards() go updateAppDashboards()