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 (
"encoding/json"
"os"
"os/exec"
"path"
"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 {
@ -16,6 +25,12 @@ type DataSourcePlugin struct {
Mixed bool `json:"mixed,omitempty"`
HasQueryHelp bool `json:"hasQueryHelp,omitempty"`
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 {
@ -39,3 +54,37 @@ func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
DataSources[p.Id] = p
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"`
State string `json:"state,omitempty"`
IncludedInAppId string `json:"-"`
PluginDir string `json:"-"`
DefaultNavUrl string `json:"-"`

View File

@ -70,10 +70,10 @@ func Init() error {
Plugins = make(map[string]*PluginBase)
BackendDatasources = make(map[string]*BackendDatasource)
PluginTypes = map[string]interface{}{
"panel": PanelPlugin{},
"datasource": DataSourcePlugin{},
"app": AppPlugin{},
"backend-datasource": BackendDatasource{},
"panel": PanelPlugin{},
"datasource": DataSourcePlugin{},
"app": AppPlugin{},
//"backend-datasource": BackendDatasource{},
}
plog.Info("Starting plugin search")
@ -98,20 +98,17 @@ func Init() error {
for _, panel := range Panels {
panel.initFrontendPlugin()
}
for _, panel := range DataSources {
panel.initFrontendPlugin()
for _, ds := range DataSources {
if ds.Backend {
ds.initBackendPlugin(plog)
}
ds.initFrontendPlugin()
}
for _, app := range Apps {
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 updateAppDashboards()