2018-09-26 17:33:37 +00:00
|
|
|
package drivers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2018-09-26 05:18:03 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
baseproto "github.com/hashicorp/nomad/plugins/base/proto"
|
2018-09-26 14:42:02 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers/proto"
|
2018-09-26 17:33:37 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PluginDriver wraps a DriverPlugin and implements go-plugins GRPCPlugin
|
|
|
|
// interface to expose the the interface over gRPC
|
|
|
|
type PluginDriver struct {
|
|
|
|
plugin.NetRPCUnsupportedPlugin
|
|
|
|
impl DriverPlugin
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDriverPlugin(d DriverPlugin, logger hclog.Logger) plugin.GRPCPlugin {
|
|
|
|
return &PluginDriver{
|
|
|
|
impl: d,
|
|
|
|
logger: logger.Named("driver_plugin"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PluginDriver) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
|
|
|
|
proto.RegisterDriverServer(s, &driverPluginServer{
|
|
|
|
impl: p.impl,
|
|
|
|
broker: broker,
|
|
|
|
logger: p.logger,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PluginDriver) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
|
|
|
|
return &driverPluginClient{
|
2018-09-26 05:18:03 +00:00
|
|
|
BasePluginClient: &base.BasePluginClient{
|
2018-11-13 01:09:27 +00:00
|
|
|
DoneCtx: ctx,
|
|
|
|
Client: baseproto.NewBasePluginClient(c),
|
2018-09-26 05:18:03 +00:00
|
|
|
},
|
2018-11-13 01:09:27 +00:00
|
|
|
client: proto.NewDriverClient(c),
|
|
|
|
logger: p.logger,
|
|
|
|
doneCtx: ctx,
|
2018-09-26 17:33:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2018-11-27 19:04:43 +00:00
|
|
|
|
|
|
|
// Serve is used to serve a driverplugin
|
|
|
|
func Serve(d DriverPlugin, logger hclog.Logger) {
|
|
|
|
plugin.Serve(&plugin.ServeConfig{
|
|
|
|
HandshakeConfig: base.Handshake,
|
|
|
|
Plugins: map[string]plugin.Plugin{
|
|
|
|
base.PluginTypeBase: &base.PluginBase{Impl: d},
|
|
|
|
base.PluginTypeDriver: &PluginDriver{impl: d, logger: logger},
|
|
|
|
},
|
|
|
|
GRPCServer: plugin.DefaultGRPCServer,
|
|
|
|
Logger: logger,
|
|
|
|
})
|
|
|
|
}
|