github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/plugins/base/server.go (about) 1 package base 2 3 import ( 4 "fmt" 5 6 plugin "github.com/hashicorp/go-plugin" 7 "github.com/hashicorp/nomad/plugins/base/proto" 8 "golang.org/x/net/context" 9 ) 10 11 // basePluginServer wraps a base plugin and exposes it via gRPC. 12 type basePluginServer struct { 13 broker *plugin.GRPCBroker 14 impl BasePlugin 15 } 16 17 func (b *basePluginServer) PluginInfo(context.Context, *proto.PluginInfoRequest) (*proto.PluginInfoResponse, error) { 18 resp, err := b.impl.PluginInfo() 19 if err != nil { 20 return nil, err 21 } 22 23 var ptype proto.PluginType 24 switch resp.Type { 25 case PluginTypeDriver: 26 ptype = proto.PluginType_DRIVER 27 case PluginTypeDevice: 28 ptype = proto.PluginType_DEVICE 29 default: 30 return nil, fmt.Errorf("plugin is of unknown type: %q", resp.Type) 31 } 32 33 presp := &proto.PluginInfoResponse{ 34 Type: ptype, 35 PluginApiVersions: resp.PluginApiVersions, 36 PluginVersion: resp.PluginVersion, 37 Name: resp.Name, 38 } 39 40 return presp, nil 41 } 42 43 func (b *basePluginServer) ConfigSchema(context.Context, *proto.ConfigSchemaRequest) (*proto.ConfigSchemaResponse, error) { 44 spec, err := b.impl.ConfigSchema() 45 if err != nil { 46 return nil, err 47 } 48 49 presp := &proto.ConfigSchemaResponse{ 50 Spec: spec, 51 } 52 53 return presp, nil 54 } 55 56 func (b *basePluginServer) SetConfig(ctx context.Context, req *proto.SetConfigRequest) (*proto.SetConfigResponse, error) { 57 info, err := b.impl.PluginInfo() 58 if err != nil { 59 return nil, err 60 } 61 62 // Client configuration is filtered based on plugin type 63 cfg := nomadConfigFromProto(req.GetNomadConfig()) 64 filteredCfg := new(AgentConfig) 65 66 if cfg != nil { 67 switch info.Type { 68 case PluginTypeDriver: 69 filteredCfg.Driver = cfg.Driver 70 } 71 } 72 73 // Build the config request 74 c := &Config{ 75 ApiVersion: req.GetPluginApiVersion(), 76 PluginConfig: req.GetMsgpackConfig(), 77 AgentConfig: filteredCfg, 78 } 79 80 // Set the config 81 if err := b.impl.SetConfig(c); err != nil { 82 return nil, fmt.Errorf("SetConfig failed: %v", err) 83 } 84 85 return &proto.SetConfigResponse{}, nil 86 }