2018-08-13 17:52:59 +00:00
|
|
|
package base
|
2018-08-10 17:50:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
"github.com/hashicorp/nomad/plugins/base/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
// basePluginServer wraps a base plugin and exposes it via gRPC.
|
|
|
|
type basePluginServer struct {
|
|
|
|
broker *plugin.GRPCBroker
|
|
|
|
impl BasePlugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *basePluginServer) PluginInfo(context.Context, *proto.PluginInfoRequest) (*proto.PluginInfoResponse, error) {
|
|
|
|
resp, err := b.impl.PluginInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ptype proto.PluginType
|
|
|
|
switch resp.Type {
|
|
|
|
case PluginTypeDriver:
|
|
|
|
ptype = proto.PluginType_DRIVER
|
|
|
|
case PluginTypeDevice:
|
|
|
|
ptype = proto.PluginType_DEVICE
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("plugin is of unknown type: %q", resp.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
presp := &proto.PluginInfoResponse{
|
|
|
|
Type: ptype,
|
|
|
|
PluginApiVersion: resp.PluginApiVersion,
|
|
|
|
PluginVersion: resp.PluginVersion,
|
|
|
|
Name: resp.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
return presp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *basePluginServer) ConfigSchema(context.Context, *proto.ConfigSchemaRequest) (*proto.ConfigSchemaResponse, error) {
|
|
|
|
spec, err := b.impl.ConfigSchema()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
presp := &proto.ConfigSchemaResponse{
|
|
|
|
Spec: spec,
|
|
|
|
}
|
|
|
|
|
|
|
|
return presp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *basePluginServer) SetConfig(ctx context.Context, req *proto.SetConfigRequest) (*proto.SetConfigResponse, error) {
|
2018-10-19 03:31:01 +00:00
|
|
|
info, err := b.impl.PluginInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client configuration is filtered based on plugin type
|
|
|
|
cfg := nomadConfigFromProto(req.GetNomadConfig())
|
2018-10-30 01:34:34 +00:00
|
|
|
filteredCfg := new(ClientAgentConfig)
|
2018-10-19 03:31:01 +00:00
|
|
|
|
2018-10-31 00:14:27 +00:00
|
|
|
if cfg != nil {
|
|
|
|
switch info.Type {
|
|
|
|
case PluginTypeDriver:
|
|
|
|
filteredCfg.Driver = cfg.Driver
|
|
|
|
}
|
2018-10-19 03:31:01 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 17:50:09 +00:00
|
|
|
// Set the config
|
2018-10-19 03:31:01 +00:00
|
|
|
if err := b.impl.SetConfig(req.GetMsgpackConfig(), filteredCfg); err != nil {
|
2018-08-10 17:50:09 +00:00
|
|
|
return nil, fmt.Errorf("SetConfig failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.SetConfigResponse{}, nil
|
|
|
|
}
|