github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/plugins/drivers/plugin.go (about)

     1  package drivers
     2  
     3  import (
     4  	"context"
     5  
     6  	hclog "github.com/hashicorp/go-hclog"
     7  	plugin "github.com/hashicorp/go-plugin"
     8  	"github.com/hashicorp/nomad/plugins/base"
     9  	baseproto "github.com/hashicorp/nomad/plugins/base/proto"
    10  	"github.com/hashicorp/nomad/plugins/drivers/proto"
    11  	"google.golang.org/grpc"
    12  )
    13  
    14  var _ plugin.GRPCPlugin = &PluginDriver{}
    15  
    16  // PluginDriver wraps a DriverPlugin and implements go-plugins GRPCPlugin
    17  // interface to expose the the interface over gRPC
    18  type PluginDriver struct {
    19  	plugin.NetRPCUnsupportedPlugin
    20  	impl   DriverPlugin
    21  	logger hclog.Logger
    22  }
    23  
    24  func NewDriverPlugin(d DriverPlugin, logger hclog.Logger) *PluginDriver {
    25  	return &PluginDriver{
    26  		impl:   d,
    27  		logger: logger,
    28  	}
    29  }
    30  
    31  func (p *PluginDriver) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
    32  	proto.RegisterDriverServer(s, &driverPluginServer{
    33  		impl:   p.impl,
    34  		broker: broker,
    35  	})
    36  	return nil
    37  }
    38  
    39  func (p *PluginDriver) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
    40  	return &driverPluginClient{
    41  		BasePluginClient: &base.BasePluginClient{
    42  			DoneCtx: ctx,
    43  			Client:  baseproto.NewBasePluginClient(c),
    44  		},
    45  		client:  proto.NewDriverClient(c),
    46  		doneCtx: ctx,
    47  		logger:  p.logger,
    48  	}, nil
    49  }
    50  
    51  // Serve is used to serve a driverplugin
    52  func Serve(d DriverPlugin, logger hclog.Logger) {
    53  	plugin.Serve(&plugin.ServeConfig{
    54  		HandshakeConfig: base.Handshake,
    55  		Plugins: map[string]plugin.Plugin{
    56  			base.PluginTypeBase:   &base.PluginBase{Impl: d},
    57  			base.PluginTypeDriver: &PluginDriver{impl: d, logger: logger},
    58  		},
    59  		GRPCServer: plugin.DefaultGRPCServer,
    60  		Logger:     logger,
    61  	})
    62  }