github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/plugins/device/plugin.go (about)

     1  package device
     2  
     3  import (
     4  	"context"
     5  
     6  	log "github.com/hashicorp/go-hclog"
     7  	plugin "github.com/hashicorp/go-plugin"
     8  	"github.com/hashicorp/nomad/plugins/base"
     9  	bproto "github.com/hashicorp/nomad/plugins/base/proto"
    10  	"github.com/hashicorp/nomad/plugins/device/proto"
    11  	"google.golang.org/grpc"
    12  )
    13  
    14  // PluginDevice is wraps a DevicePlugin and implements go-plugins GRPCPlugin
    15  // interface to expose the interface over gRPC.
    16  type PluginDevice struct {
    17  	plugin.NetRPCUnsupportedPlugin
    18  	Impl DevicePlugin
    19  }
    20  
    21  func (p *PluginDevice) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
    22  	proto.RegisterDevicePluginServer(s, &devicePluginServer{
    23  		impl:   p.Impl,
    24  		broker: broker,
    25  	})
    26  	return nil
    27  }
    28  
    29  func (p *PluginDevice) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
    30  	return &devicePluginClient{
    31  		client: proto.NewDevicePluginClient(c),
    32  		BasePluginClient: &base.BasePluginClient{
    33  			Client: bproto.NewBasePluginClient(c),
    34  		},
    35  	}, nil
    36  }
    37  
    38  // Serve is used to serve a device plugin
    39  func Serve(dev DevicePlugin, logger log.Logger) {
    40  	plugin.Serve(&plugin.ServeConfig{
    41  		HandshakeConfig: base.Handshake,
    42  		Plugins: map[string]plugin.Plugin{
    43  			base.PluginTypeBase:   &base.PluginBase{Impl: dev},
    44  			base.PluginTypeDevice: &PluginDevice{Impl: dev},
    45  		},
    46  		GRPCServer: plugin.DefaultGRPCServer,
    47  		Logger:     logger,
    48  	})
    49  }