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-20 09:24:49 -06:00
|
|
|
"fmt"
|
2017-08-31 07:05:52 -05:00
|
|
|
"os"
|
2017-12-15 10:17:58 -06:00
|
|
|
"os/exec"
|
|
|
|
"path"
|
2017-08-31 07:05:52 -05:00
|
|
|
"path/filepath"
|
2017-12-20 09:24:49 -06:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2017-12-22 08:40:45 -06:00
|
|
|
"time"
|
2017-12-15 10:17:58 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
"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"
|
2018-01-17 04:37:37 -06:00
|
|
|
"github.com/grafana/grafana_plugin_model/go/datasource"
|
2017-12-15 10:17:58 -06:00
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2017-08-31 07:05:52 -05:00
|
|
|
)
|
2016-01-09 16:34:20 -06:00
|
|
|
|
|
|
|
type DataSourcePlugin struct {
|
|
|
|
FrontendPluginBase
|
2017-09-04 07:09:02 -05:00
|
|
|
Annotations bool `json:"annotations"`
|
|
|
|
Metrics bool `json:"metrics"`
|
|
|
|
Alerting bool `json:"alerting"`
|
|
|
|
QueryOptions map[string]bool `json:"queryOptions,omitempty"`
|
|
|
|
BuiltIn bool `json:"builtIn,omitempty"`
|
|
|
|
Mixed bool `json:"mixed,omitempty"`
|
|
|
|
HasQueryHelp bool `json:"hasQueryHelp,omitempty"`
|
|
|
|
Routes []*AppPluginRoute `json:"routes"`
|
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
|
|
|
|
2017-08-31 07:05:52 -05:00
|
|
|
// look for help markdown
|
2017-09-01 08:57:02 -05:00
|
|
|
helpPath := filepath.Join(p.PluginDir, "QUERY_HELP.md")
|
2017-08-31 07:05:52 -05:00
|
|
|
if _, err := os.Stat(helpPath); os.IsNotExist(err) {
|
2017-09-01 08:57:02 -05:00
|
|
|
helpPath = filepath.Join(p.PluginDir, "query_help.md")
|
2017-08-31 07:05:52 -05:00
|
|
|
}
|
|
|
|
if _, err := os.Stat(helpPath); err == nil {
|
2017-09-01 08:57:02 -05:00
|
|
|
p.HasQueryHelp = true
|
2017-08-31 07:05:52 -05: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-01-10 08:07:04 -06:00
|
|
|
func composeBinaryName(executable, os, arch string) string {
|
|
|
|
var extension string
|
|
|
|
os = strings.ToLower(os)
|
|
|
|
if os == "windows" {
|
|
|
|
extension = ".exe"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s_%s_%s%s", executable, os, strings.ToLower(arch), extension)
|
2017-12-20 09:33:53 -06:00
|
|
|
}
|
|
|
|
|
2017-12-25 06:48:50 -06:00
|
|
|
func (p *DataSourcePlugin) initBackendPlugin(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-01-10 08:07:04 -06:00
|
|
|
cmd := composeBinaryName(p.Executable, runtime.GOOS, runtime.GOARCH)
|
|
|
|
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
|
|
|
}
|