plugins.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package plugins
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "golang.org/x/net/context"
  7. "github.com/grafana/grafana/pkg/log"
  8. "github.com/grafana/grafana/pkg/tsdb/plugins/proto"
  9. "github.com/grafana/grafana/pkg/tsdb/plugins/shared"
  10. plugin "github.com/hashicorp/go-plugin"
  11. )
  12. func Init() (*plugin.Client, error) {
  13. /*
  14. Setup
  15. go get -u google.golang.org/grpc \
  16. go get -u github.com/golang/protobuf/{proto,protoc-gen-go} \
  17. go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway \
  18. */
  19. /*
  20. Generate
  21. sudo protoc --go_out=. *.proto
  22. protoc --go_out=plugins=grpc:. *.proto
  23. */
  24. // initial goal: pass a string object back and forth over grpc.
  25. // simplify tsdb req/res message/service
  26. //lets be silly
  27. //plugin path
  28. client := plugin.NewClient(&plugin.ClientConfig{
  29. HandshakeConfig: shared.Handshake,
  30. Plugins: shared.PluginMap,
  31. Cmd: exec.Command("sh", "-c", "/home/carl/go/src/github.com/grafana/simple-plugin/simple-plugin"),
  32. AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
  33. })
  34. // Connect via RPC
  35. rpcClient, err := client.Client()
  36. if err != nil {
  37. fmt.Println("Error:", err.Error())
  38. os.Exit(1)
  39. }
  40. // Request the plugin
  41. raw, err := rpcClient.Dispense("kv")
  42. if err != nil {
  43. fmt.Println("Error:", err.Error())
  44. //os.Exit(1)
  45. //client.Kill()
  46. return nil, err
  47. }
  48. plugin := raw.(shared.TsdbPlugin)
  49. response, err := plugin.Get(context.Background(), &proto.TsdbRequest{})
  50. log.Error2("got response from plugin. ", "response", response)
  51. return client, nil
  52. }