2016-01-09 16:34:20 -06:00
|
|
|
package plugins
|
|
|
|
|
2017-08-31 07:05:52 -05:00
|
|
|
import (
|
2017-12-25 06:48:50 -06:00
|
|
|
"context"
|
2017-08-31 07:05:52 -05:00
|
|
|
"encoding/json"
|
2017-12-15 10:17:58 -06:00
|
|
|
"os/exec"
|
|
|
|
"path"
|
2017-12-22 08:40:45 -06:00
|
|
|
"time"
|
2017-12-15 10:17:58 -06:00
|
|
|
|
2018-05-24 08:26:27 -05:00
|
|
|
"github.com/grafana/grafana-plugin-model/go/datasource"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2017-12-15 10:17:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2018-01-17 04:37:37 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/datasource/wrapper"
|
2017-12-15 10:17:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2017-08-31 07:05:52 -05:00
|
|
|
)
|
2016-01-09 16:34:20 -06:00
|
|
|
|
2018-07-20 10:07:17 -05:00
|
|
|
// DataSourcePlugin contains all metadata about a datasource plugin
|
2016-01-09 16:34:20 -06:00
|
|
|
type DataSourcePlugin struct {
|
|
|
|
FrontendPluginBase
|
2019-05-21 06:46:19 -05:00
|
|
|
Annotations bool `json:"annotations"`
|
|
|
|
Metrics bool `json:"metrics"`
|
|
|
|
Alerting bool `json:"alerting"`
|
|
|
|
Explore bool `json:"explore"`
|
|
|
|
Table bool `json:"tables"`
|
|
|
|
HiddenQueries bool `json:"hiddenQueries"`
|
|
|
|
Logs bool `json:"logs"`
|
|
|
|
QueryOptions map[string]bool `json:"queryOptions,omitempty"`
|
|
|
|
BuiltIn bool `json:"builtIn,omitempty"`
|
|
|
|
Mixed bool `json:"mixed,omitempty"`
|
|
|
|
Routes []*AppPluginRoute `json:"routes"`
|
2019-06-03 07:54:32 -05:00
|
|
|
Streaming bool `json:"streaming"`
|
2017-12-15 10:17:58 -06:00
|
|
|
|
|
|
|
Backend bool `json:"backend,omitempty"`
|
|
|
|
Executable string `json:"executable,omitempty"`
|
|
|
|
|
|
|
|
log log.Logger
|
|
|
|
client *plugin.Client
|
2016-01-09 16:34:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
|
|
|
|
if err := decoder.Decode(&p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-10 04:03:12 -06:00
|
|
|
if err := p.registerPlugin(pluginDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-09 16:34:20 -06:00
|
|
|
|
2016-02-10 04:03:12 -06:00
|
|
|
DataSources[p.Id] = p
|
2016-01-09 16:34:20 -06:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-15 10:17:58 -06:00
|
|
|
|
2017-12-20 09:24:49 -06:00
|
|
|
var handshakeConfig = plugin.HandshakeConfig{
|
|
|
|
ProtocolVersion: 1,
|
2018-01-09 08:04:59 -06:00
|
|
|
MagicCookieKey: "grafana_plugin_type",
|
|
|
|
MagicCookieValue: "datasource",
|
2017-12-20 09:24:49 -06:00
|
|
|
}
|
|
|
|
|
2018-04-27 08:11:55 -05:00
|
|
|
func (p *DataSourcePlugin) startBackendPlugin(ctx context.Context, log log.Logger) error {
|
2017-12-15 10:17:58 -06:00
|
|
|
p.log = log.New("plugin-id", p.Id)
|
|
|
|
|
2018-01-09 07:56:23 -06:00
|
|
|
err := p.spawnSubProcess()
|
|
|
|
if err == nil {
|
|
|
|
go p.restartKilledProcess(ctx)
|
|
|
|
}
|
2017-12-22 08:40:45 -06:00
|
|
|
|
2018-01-09 07:56:23 -06:00
|
|
|
return err
|
2017-12-22 08:40:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DataSourcePlugin) spawnSubProcess() error {
|
2018-05-24 08:26:27 -05:00
|
|
|
cmd := ComposePluginStartCommmand(p.Executable)
|
2018-01-10 08:07:04 -06:00
|
|
|
fullpath := path.Join(p.PluginDir, cmd)
|
2017-12-20 09:24:49 -06:00
|
|
|
|
2017-12-15 10:17:58 -06:00
|
|
|
p.client = plugin.NewClient(&plugin.ClientConfig{
|
|
|
|
HandshakeConfig: handshakeConfig,
|
2018-01-17 04:37:37 -06:00
|
|
|
Plugins: map[string]plugin.Plugin{p.Id: &datasource.DatasourcePluginImpl{}},
|
2018-01-10 08:07:04 -06:00
|
|
|
Cmd: exec.Command(fullpath),
|
2017-12-15 10:17:58 -06:00
|
|
|
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
|
2018-01-10 05:11:57 -06:00
|
|
|
Logger: LogWrapper{Logger: p.log},
|
2017-12-15 10:17:58 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
rpcClient, err := p.client.Client()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := rpcClient.Dispense(p.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-17 04:37:37 -06:00
|
|
|
plugin := raw.(datasource.DatasourcePlugin)
|
2017-12-15 10:17:58 -06:00
|
|
|
|
|
|
|
tsdb.RegisterTsdbQueryEndpoint(p.Id, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
|
2018-01-17 04:37:37 -06:00
|
|
|
return wrapper.NewDatasourcePluginWrapper(p.log, plugin), nil
|
2017-12-15 10:17:58 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-09 07:56:23 -06:00
|
|
|
func (p *DataSourcePlugin) restartKilledProcess(ctx context.Context) error {
|
2017-12-22 08:40:45 -06:00
|
|
|
ticker := time.NewTicker(time.Second * 1)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2017-12-25 06:48:50 -06:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2017-12-22 08:40:45 -06:00
|
|
|
case <-ticker.C:
|
|
|
|
if p.client.Exited() {
|
|
|
|
err := p.spawnSubProcess()
|
2017-12-25 06:48:50 -06:00
|
|
|
p.log.Debug("Spawning new sub process", "name", p.Name, "id", p.Id)
|
2017-12-22 08:40:45 -06:00
|
|
|
if err != nil {
|
|
|
|
p.log.Error("Failed to spawn subprocess")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 10:17:58 -06:00
|
|
|
func (p *DataSourcePlugin) Kill() {
|
2017-12-20 09:24:49 -06:00
|
|
|
if p.client != nil {
|
2017-12-25 06:48:50 -06:00
|
|
|
p.log.Debug("Killing subprocess ", "name", p.Name)
|
2017-12-20 09:24:49 -06:00
|
|
|
p.client.Kill()
|
|
|
|
}
|
2017-12-15 10:17:58 -06:00
|
|
|
}
|