2018-08-13 17:52:59 +00:00
|
|
|
package base
|
2018-08-10 17:50:09 +00:00
|
|
|
|
|
|
|
import (
|
2018-08-17 22:19:44 +00:00
|
|
|
"bytes"
|
2018-08-13 17:52:59 +00:00
|
|
|
"context"
|
2018-08-17 22:19:44 +00:00
|
|
|
"reflect"
|
2018-08-10 17:50:09 +00:00
|
|
|
|
2020-03-18 11:27:32 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2018-08-10 17:50:09 +00:00
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
"github.com/hashicorp/nomad/plugins/base/proto"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-08-13 17:29:29 +00:00
|
|
|
// PluginTypeBase implements the base plugin interface
|
|
|
|
PluginTypeBase = "base"
|
|
|
|
|
2018-08-10 17:50:09 +00:00
|
|
|
// PluginTypeDriver implements the driver plugin interface
|
|
|
|
PluginTypeDriver = "driver"
|
|
|
|
|
|
|
|
// PluginTypeDevice implements the device plugin interface
|
|
|
|
PluginTypeDevice = "device"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Handshake is a common handshake that is shared by all plugins and Nomad.
|
|
|
|
Handshake = plugin.HandshakeConfig{
|
2019-01-17 02:52:31 +00:00
|
|
|
// ProtocolVersion for the executor protocol.
|
|
|
|
// Version 1: pre 0.9 netrpc based executor
|
|
|
|
// Version 2: 0.9+ grpc based executor
|
2019-01-15 22:07:21 +00:00
|
|
|
ProtocolVersion: 2,
|
2018-08-10 17:50:09 +00:00
|
|
|
MagicCookieKey: "NOMAD_PLUGIN_MAGIC_COOKIE",
|
|
|
|
MagicCookieValue: "e4327c2e01eabfd75a8a67adb114fb34a757d57eee7728d857a8cec6e91a7255",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-08-13 17:52:59 +00:00
|
|
|
// PluginBase is wraps a BasePlugin and implements go-plugins GRPCPlugin
|
|
|
|
// interface to expose the interface over gRPC.
|
2018-08-10 17:50:09 +00:00
|
|
|
type PluginBase struct {
|
|
|
|
plugin.NetRPCUnsupportedPlugin
|
2018-08-13 17:29:29 +00:00
|
|
|
Impl BasePlugin
|
2018-08-10 17:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PluginBase) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
|
2018-08-13 18:10:33 +00:00
|
|
|
proto.RegisterBasePluginServer(s, &basePluginServer{
|
2018-08-13 17:29:29 +00:00
|
|
|
impl: p.Impl,
|
2018-08-13 18:10:33 +00:00
|
|
|
broker: broker,
|
|
|
|
})
|
2018-08-10 17:50:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PluginBase) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
|
2018-11-13 01:09:27 +00:00
|
|
|
return &BasePluginClient{
|
|
|
|
Client: proto.NewBasePluginClient(c),
|
|
|
|
DoneCtx: ctx,
|
|
|
|
}, nil
|
2018-08-10 17:50:09 +00:00
|
|
|
}
|
2018-08-17 22:19:44 +00:00
|
|
|
|
|
|
|
// MsgpackHandle is a shared handle for encoding/decoding of structs
|
|
|
|
var MsgpackHandle = func() *codec.MsgpackHandle {
|
2018-10-16 03:03:05 +00:00
|
|
|
h := &codec.MsgpackHandle{}
|
|
|
|
h.RawToString = true
|
2019-05-08 13:04:16 +00:00
|
|
|
|
|
|
|
// maintain binary format from time prior to upgrading latest ugorji
|
|
|
|
h.BasicHandle.TimeNotBuiltin = true
|
|
|
|
|
2018-08-17 22:19:44 +00:00
|
|
|
h.MapType = reflect.TypeOf(map[string]interface{}(nil))
|
|
|
|
return h
|
|
|
|
}()
|
|
|
|
|
|
|
|
// MsgPackDecode is used to decode a MsgPack encoded object
|
|
|
|
func MsgPackDecode(buf []byte, out interface{}) error {
|
|
|
|
return codec.NewDecoder(bytes.NewReader(buf), MsgpackHandle).Decode(out)
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
|
|
|
|
// MsgPackEncode is used to encode an object to MsgPack
|
|
|
|
func MsgPackEncode(b *[]byte, in interface{}) error {
|
|
|
|
return codec.NewEncoderBytes(b, MsgpackHandle).Encode(in)
|
|
|
|
}
|