2018-09-26 17:33:37 +00:00
|
|
|
package drivers
|
|
|
|
|
|
|
|
import (
|
2018-10-16 02:37:58 +00:00
|
|
|
"fmt"
|
2018-09-26 17:33:37 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2018-11-09 04:37:50 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers/proto"
|
2018-11-21 00:30:39 +00:00
|
|
|
dstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-11-14 11:20:35 +00:00
|
|
|
sproto "github.com/hashicorp/nomad/plugins/shared/structs/proto"
|
2018-11-13 01:09:27 +00:00
|
|
|
context "golang.org/x/net/context"
|
2018-11-20 02:51:26 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-09-26 17:33:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type driverPluginServer struct {
|
|
|
|
broker *plugin.GRPCBroker
|
|
|
|
impl DriverPlugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) TaskConfigSchema(ctx context.Context, req *proto.TaskConfigSchemaRequest) (*proto.TaskConfigSchemaResponse, error) {
|
|
|
|
spec, err := b.impl.TaskConfigSchema()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &proto.TaskConfigSchemaResponse{
|
|
|
|
Spec: spec,
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) Capabilities(ctx context.Context, req *proto.CapabilitiesRequest) (*proto.CapabilitiesResponse, error) {
|
|
|
|
caps, err := b.impl.Capabilities()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp := &proto.CapabilitiesResponse{
|
|
|
|
Capabilities: &proto.DriverCapabilities{
|
2019-04-29 17:35:15 +00:00
|
|
|
SendSignals: caps.SendSignals,
|
|
|
|
Exec: caps.Exec,
|
|
|
|
MustCreateNetwork: caps.MustInitiateNetwork,
|
|
|
|
NetworkIsolationModes: []proto.NetworkIsolationSpec_NetworkIsolationMode{},
|
2018-09-26 17:33:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
switch caps.FSIsolation {
|
2019-01-04 21:11:25 +00:00
|
|
|
case FSIsolationNone:
|
2018-09-26 17:33:37 +00:00
|
|
|
resp.Capabilities.FsIsolation = proto.DriverCapabilities_NONE
|
2019-01-04 21:11:25 +00:00
|
|
|
case FSIsolationChroot:
|
2018-09-26 17:33:37 +00:00
|
|
|
resp.Capabilities.FsIsolation = proto.DriverCapabilities_CHROOT
|
2019-01-04 21:11:25 +00:00
|
|
|
case FSIsolationImage:
|
2018-09-26 17:33:37 +00:00
|
|
|
resp.Capabilities.FsIsolation = proto.DriverCapabilities_IMAGE
|
2018-10-04 19:08:20 +00:00
|
|
|
default:
|
|
|
|
resp.Capabilities.FsIsolation = proto.DriverCapabilities_NONE
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
2019-04-29 17:35:15 +00:00
|
|
|
|
|
|
|
for _, mode := range caps.NetIsolationModes {
|
|
|
|
resp.Capabilities.NetworkIsolationModes = append(resp.Capabilities.NetworkIsolationModes, netIsolationModeToProto(mode))
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) Fingerprint(req *proto.FingerprintRequest, srv proto.Driver_FingerprintServer) error {
|
|
|
|
ctx := srv.Context()
|
|
|
|
ch, err := b.impl.Fingerprint(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case f, ok := <-ch:
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resp := &proto.FingerprintResponse{
|
2018-11-21 00:30:39 +00:00
|
|
|
Attributes: dstructs.ConvertStructAttributeMap(f.Attributes),
|
2018-09-26 17:33:37 +00:00
|
|
|
Health: healthStateToProto(f.Health),
|
|
|
|
HealthDescription: f.HealthDescription,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.Send(resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) RecoverTask(ctx context.Context, req *proto.RecoverTaskRequest) (*proto.RecoverTaskResponse, error) {
|
|
|
|
err := b.impl.RecoverTask(taskHandleFromProto(req.Handle))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.RecoverTaskResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) StartTask(ctx context.Context, req *proto.StartTaskRequest) (*proto.StartTaskResponse, error) {
|
2018-10-04 19:08:20 +00:00
|
|
|
handle, net, err := b.impl.StartTask(taskConfigFromProto(req.Task))
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-11-09 04:37:50 +00:00
|
|
|
if rec, ok := err.(structs.Recoverable); ok {
|
|
|
|
st := status.New(codes.FailedPrecondition, rec.Error())
|
2018-11-14 11:20:35 +00:00
|
|
|
st, err := st.WithDetails(&sproto.RecoverableError{Recoverable: rec.IsRecoverable()})
|
2018-11-09 04:37:50 +00:00
|
|
|
if err != nil {
|
|
|
|
// If this error, it will always error
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return nil, st.Err()
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:08:20 +00:00
|
|
|
var pbNet *proto.NetworkOverride
|
|
|
|
if net != nil {
|
|
|
|
pbNet = &proto.NetworkOverride{
|
|
|
|
PortMap: map[string]int32{},
|
|
|
|
Addr: net.IP,
|
|
|
|
AutoAdvertise: net.AutoAdvertise,
|
|
|
|
}
|
|
|
|
for k, v := range net.PortMap {
|
|
|
|
pbNet.PortMap[k] = int32(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
resp := &proto.StartTaskResponse{
|
2018-10-04 19:08:20 +00:00
|
|
|
Handle: taskHandleToProto(handle),
|
|
|
|
NetworkOverride: pbNet,
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) WaitTask(ctx context.Context, req *proto.WaitTaskRequest) (*proto.WaitTaskResponse, error) {
|
|
|
|
ch, err := b.impl.WaitTask(ctx, req.TaskId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-16 02:37:58 +00:00
|
|
|
var ok bool
|
|
|
|
var result *ExitResult
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
case result, ok = <-ch:
|
|
|
|
if !ok {
|
|
|
|
return &proto.WaitTaskResponse{
|
|
|
|
Err: "channel closed",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
var errStr string
|
|
|
|
if result.Err != nil {
|
|
|
|
errStr = result.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &proto.WaitTaskResponse{
|
|
|
|
Err: errStr,
|
|
|
|
Result: &proto.ExitResult{
|
|
|
|
ExitCode: int32(result.ExitCode),
|
|
|
|
Signal: int32(result.Signal),
|
|
|
|
OomKilled: result.OOMKilled,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) StopTask(ctx context.Context, req *proto.StopTaskRequest) (*proto.StopTaskResponse, error) {
|
|
|
|
timeout, err := ptypes.Duration(req.Timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = b.impl.StopTask(req.TaskId, timeout, req.Signal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &proto.StopTaskResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) DestroyTask(ctx context.Context, req *proto.DestroyTaskRequest) (*proto.DestroyTaskResponse, error) {
|
|
|
|
err := b.impl.DestroyTask(req.TaskId, req.Force)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &proto.DestroyTaskResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) InspectTask(ctx context.Context, req *proto.InspectTaskRequest) (*proto.InspectTaskResponse, error) {
|
|
|
|
status, err := b.impl.InspectTask(req.TaskId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
protoStatus, err := taskStatusToProto(status)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:08:20 +00:00
|
|
|
var pbNet *proto.NetworkOverride
|
|
|
|
if status.NetworkOverride != nil {
|
|
|
|
pbNet = &proto.NetworkOverride{
|
|
|
|
PortMap: map[string]int32{},
|
|
|
|
Addr: status.NetworkOverride.IP,
|
|
|
|
AutoAdvertise: status.NetworkOverride.AutoAdvertise,
|
|
|
|
}
|
|
|
|
for k, v := range status.NetworkOverride.PortMap {
|
|
|
|
pbNet.PortMap[k] = int32(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
resp := &proto.InspectTaskResponse{
|
|
|
|
Task: protoStatus,
|
|
|
|
Driver: &proto.TaskDriverStatus{
|
|
|
|
Attributes: status.DriverAttributes,
|
|
|
|
},
|
2018-10-04 19:08:20 +00:00
|
|
|
NetworkOverride: pbNet,
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
func (b *driverPluginServer) TaskStats(req *proto.TaskStatsRequest, srv proto.Driver_TaskStatsServer) error {
|
2019-01-25 03:19:18 +00:00
|
|
|
interval, err := ptypes.Duration(req.CollectionInterval)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse collection interval: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := b.impl.TaskStats(srv.Context(), req.TaskId, interval)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-12-11 20:27:50 +00:00
|
|
|
if rec, ok := err.(structs.Recoverable); ok {
|
|
|
|
st := status.New(codes.FailedPrecondition, rec.Error())
|
|
|
|
st, err := st.WithDetails(&sproto.RecoverableError{Recoverable: rec.IsRecoverable()})
|
|
|
|
if err != nil {
|
|
|
|
// If this error, it will always error
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return st.Err()
|
|
|
|
}
|
|
|
|
return err
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
for stats := range ch {
|
|
|
|
pb, err := TaskStatsToProto(stats)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to encode task stats: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = srv.Send(&proto.TaskStatsResponse{Stats: pb}); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
return nil
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) ExecTask(ctx context.Context, req *proto.ExecTaskRequest) (*proto.ExecTaskResponse, error) {
|
|
|
|
timeout, err := ptypes.Duration(req.Timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := b.impl.ExecTask(req.TaskId, req.Command, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp := &proto.ExecTaskResponse{
|
|
|
|
Stdout: result.Stdout,
|
|
|
|
Stderr: result.Stderr,
|
|
|
|
Result: exitResultToProto(result.ExitResult),
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2019-04-28 21:09:12 +00:00
|
|
|
func (b *driverPluginServer) ExecTaskStreaming(server proto.Driver_ExecTaskStreamingServer) error {
|
|
|
|
msg, err := server.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to receive initial message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Setup == nil {
|
|
|
|
return fmt.Errorf("first message should always be setup")
|
|
|
|
}
|
|
|
|
|
|
|
|
if impl, ok := b.impl.(ExecTaskStreamingRawDriver); ok {
|
|
|
|
return impl.ExecTaskStreamingRaw(server.Context(),
|
|
|
|
msg.Setup.TaskId, msg.Setup.Command, msg.Setup.Tty,
|
|
|
|
server)
|
|
|
|
}
|
|
|
|
|
|
|
|
d, ok := b.impl.(ExecTaskStreamingDriver)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("driver does not support exec")
|
|
|
|
}
|
|
|
|
|
|
|
|
execOpts, errCh := StreamToExecOptions(server.Context(),
|
|
|
|
msg.Setup.Command, msg.Setup.Tty,
|
|
|
|
server)
|
|
|
|
|
|
|
|
result, err := d.ExecTaskStreaming(server.Context(),
|
|
|
|
msg.Setup.TaskId, execOpts)
|
|
|
|
|
|
|
|
execOpts.Stdout.Close()
|
|
|
|
execOpts.Stderr.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for copy to be done
|
|
|
|
select {
|
|
|
|
case err = <-errCh:
|
|
|
|
case <-server.Context().Done():
|
|
|
|
err = fmt.Errorf("exec timed out: %v", server.Context().Err())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
server.Send(&ExecTaskStreamingResponseMsg{
|
|
|
|
Exited: true,
|
|
|
|
Result: exitResultToProto(result),
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
func (b *driverPluginServer) SignalTask(ctx context.Context, req *proto.SignalTaskRequest) (*proto.SignalTaskResponse, error) {
|
|
|
|
err := b.impl.SignalTask(req.TaskId, req.Signal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &proto.SignalTaskResponse{}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) TaskEvents(req *proto.TaskEventsRequest, srv proto.Driver_TaskEventsServer) error {
|
|
|
|
ch, err := b.impl.TaskEvents(srv.Context())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
event := <-ch
|
|
|
|
if event == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
pbTimestamp, err := ptypes.TimestampProto(event.Timestamp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pbEvent := &proto.DriverTaskEvent{
|
|
|
|
TaskId: event.TaskID,
|
2018-12-18 03:36:06 +00:00
|
|
|
AllocId: event.AllocID,
|
|
|
|
TaskName: event.TaskName,
|
2018-09-26 17:33:37 +00:00
|
|
|
Timestamp: pbTimestamp,
|
|
|
|
Message: event.Message,
|
|
|
|
Annotations: event.Annotations,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = srv.Send(pbEvent); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-29 17:35:15 +00:00
|
|
|
|
|
|
|
func (b *driverPluginServer) CreateNetwork(ctx context.Context, req *proto.CreateNetworkRequest) (*proto.CreateNetworkResponse, error) {
|
2019-05-14 01:26:39 +00:00
|
|
|
nm, ok := b.impl.(DriverNetworkManager)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("CreateNetwork RPC not supported by driver")
|
|
|
|
}
|
|
|
|
|
2019-09-18 20:34:57 +00:00
|
|
|
spec, created, err := nm.CreateNetwork(req.AllocId)
|
2019-05-14 01:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.CreateNetworkResponse{
|
|
|
|
IsolationSpec: NetworkIsolationSpecToProto(spec),
|
2019-09-18 20:34:57 +00:00
|
|
|
Created: created,
|
2019-05-14 01:26:39 +00:00
|
|
|
}, nil
|
|
|
|
|
2019-04-29 17:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *driverPluginServer) DestroyNetwork(ctx context.Context, req *proto.DestroyNetworkRequest) (*proto.DestroyNetworkResponse, error) {
|
2019-05-14 01:26:39 +00:00
|
|
|
nm, ok := b.impl.(DriverNetworkManager)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("DestroyNetwork RPC not supported by driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := nm.DestroyNetwork(req.AllocId, NetworkIsolationSpecFromProto(req.IsolationSpec))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.DestroyNetworkResponse{}, nil
|
2019-04-29 17:35:15 +00:00
|
|
|
}
|