github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/plugins/base/plugin.go (about) 1 package base 2 3 import ( 4 "bytes" 5 "context" 6 "reflect" 7 8 plugin "github.com/hashicorp/go-plugin" 9 "github.com/hashicorp/nomad/plugins/base/proto" 10 "github.com/ugorji/go/codec" 11 "google.golang.org/grpc" 12 ) 13 14 const ( 15 // PluginTypeBase implements the base plugin interface 16 PluginTypeBase = "base" 17 18 // PluginTypeDriver implements the driver plugin interface 19 PluginTypeDriver = "driver" 20 21 // PluginTypeDevice implements the device plugin interface 22 PluginTypeDevice = "device" 23 ) 24 25 var ( 26 // Handshake is a common handshake that is shared by all plugins and Nomad. 27 Handshake = plugin.HandshakeConfig{ 28 ProtocolVersion: 1, 29 MagicCookieKey: "NOMAD_PLUGIN_MAGIC_COOKIE", 30 MagicCookieValue: "e4327c2e01eabfd75a8a67adb114fb34a757d57eee7728d857a8cec6e91a7255", 31 } 32 ) 33 34 // PluginBase is wraps a BasePlugin and implements go-plugins GRPCPlugin 35 // interface to expose the interface over gRPC. 36 type PluginBase struct { 37 plugin.NetRPCUnsupportedPlugin 38 Impl BasePlugin 39 } 40 41 func (p *PluginBase) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error { 42 proto.RegisterBasePluginServer(s, &basePluginServer{ 43 impl: p.Impl, 44 broker: broker, 45 }) 46 return nil 47 } 48 49 func (p *PluginBase) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) { 50 return &BasePluginClient{Client: proto.NewBasePluginClient(c)}, nil 51 } 52 53 // MsgpackHandle is a shared handle for encoding/decoding of structs 54 var MsgpackHandle = func() *codec.MsgpackHandle { 55 h := &codec.MsgpackHandle{RawToString: true} 56 h.MapType = reflect.TypeOf(map[string]interface{}(nil)) 57 return h 58 }() 59 60 // MsgPackDecode is used to decode a MsgPack encoded object 61 func MsgPackDecode(buf []byte, out interface{}) error { 62 return codec.NewDecoder(bytes.NewReader(buf), MsgpackHandle).Decode(out) 63 }