2018-12-05 16:03:56 +00:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
2019-04-28 21:27:23 +00:00
|
|
|
"fmt"
|
2018-12-15 03:20:01 +00:00
|
|
|
"syscall"
|
2018-12-05 16:03:56 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-12-07 02:13:45 +00:00
|
|
|
"github.com/hashicorp/nomad/drivers/shared/executor/proto"
|
2019-01-10 19:20:18 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-12-05 16:03:56 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2019-01-10 19:20:18 +00:00
|
|
|
sproto "github.com/hashicorp/nomad/plugins/shared/structs/proto"
|
2018-12-05 16:03:56 +00:00
|
|
|
"golang.org/x/net/context"
|
2019-01-10 19:20:18 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-12-05 16:03:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type grpcExecutorServer struct {
|
2018-12-13 19:41:09 +00:00
|
|
|
impl Executor
|
2018-12-05 16:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcExecutorServer) Launch(ctx context.Context, req *proto.LaunchRequest) (*proto.LaunchResponse, error) {
|
2018-12-07 02:13:45 +00:00
|
|
|
ps, err := s.impl.Launch(&ExecCommand{
|
2018-12-05 16:03:56 +00:00
|
|
|
Cmd: req.Cmd,
|
|
|
|
Args: req.Args,
|
2018-12-07 02:22:02 +00:00
|
|
|
Resources: drivers.ResourcesFromProto(req.Resources),
|
2018-12-05 16:03:56 +00:00
|
|
|
StdoutPath: req.StdoutPath,
|
|
|
|
StderrPath: req.StderrPath,
|
|
|
|
Env: req.Env,
|
|
|
|
User: req.User,
|
|
|
|
TaskDir: req.TaskDir,
|
|
|
|
ResourceLimits: req.ResourceLimits,
|
|
|
|
BasicProcessCgroup: req.BasicProcessCgroup,
|
2020-02-13 19:12:46 +00:00
|
|
|
NoPivotRoot: req.NoPivotRoot,
|
2018-12-15 05:08:23 +00:00
|
|
|
Mounts: drivers.MountsFromProto(req.Mounts),
|
|
|
|
Devices: drivers.DevicesFromProto(req.Devices),
|
2019-04-29 17:37:23 +00:00
|
|
|
NetworkIsolation: drivers.NetworkIsolationSpecFromProto(req.NetworkIsolation),
|
2021-02-08 16:36:11 +00:00
|
|
|
ModePID: req.DefaultPidMode,
|
|
|
|
ModeIPC: req.DefaultIpcMode,
|
2018-12-05 16:03:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
process, err := processStateToProto(ps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.LaunchResponse{
|
|
|
|
Process: process,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcExecutorServer) Wait(ctx context.Context, req *proto.WaitRequest) (*proto.WaitResponse, error) {
|
|
|
|
ps, err := s.impl.Wait(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
process, err := processStateToProto(ps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.WaitResponse{
|
|
|
|
Process: process,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcExecutorServer) Shutdown(ctx context.Context, req *proto.ShutdownRequest) (*proto.ShutdownResponse, error) {
|
|
|
|
if err := s.impl.Shutdown(req.Signal, time.Duration(req.GracePeriod)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.ShutdownResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcExecutorServer) UpdateResources(ctx context.Context, req *proto.UpdateResourcesRequest) (*proto.UpdateResourcesResponse, error) {
|
2018-12-07 02:22:02 +00:00
|
|
|
if err := s.impl.UpdateResources(drivers.ResourcesFromProto(req.Resources)); err != nil {
|
2018-12-05 16:03:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.UpdateResourcesResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcExecutorServer) Version(context.Context, *proto.VersionRequest) (*proto.VersionResponse, error) {
|
|
|
|
v, err := s.impl.Version()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.VersionResponse{
|
|
|
|
Version: v.Version,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
func (s *grpcExecutorServer) Stats(req *proto.StatsRequest, stream proto.Executor_StatsServer) error {
|
|
|
|
interval := time.Duration(req.Interval)
|
2019-01-10 19:20:18 +00:00
|
|
|
if interval == 0 {
|
2018-12-11 20:27:50 +00:00
|
|
|
interval = time.Second
|
2018-12-05 16:03:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-12 03:27:23 +00:00
|
|
|
outCh, err := s.impl.Stats(stream.Context(), interval)
|
2018-12-05 16:03:56 +00:00
|
|
|
if err != nil {
|
2019-01-10 19:20:18 +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()})
|
2018-12-11 20:27:50 +00:00
|
|
|
if err != nil {
|
2019-01-10 19:20:18 +00:00
|
|
|
// If this error, it will always error
|
|
|
|
panic(err)
|
2018-12-11 20:27:50 +00:00
|
|
|
}
|
2019-01-10 19:20:18 +00:00
|
|
|
return st.Err()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-12-11 20:27:50 +00:00
|
|
|
|
2019-01-10 19:20:18 +00:00
|
|
|
for resp := range outCh {
|
|
|
|
pbStats, err := drivers.TaskStatsToProto(resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-11 20:27:50 +00:00
|
|
|
|
2019-01-10 19:20:18 +00:00
|
|
|
presp := &proto.StatsResponse{
|
|
|
|
Stats: pbStats,
|
|
|
|
}
|
2018-12-11 20:27:50 +00:00
|
|
|
|
2019-01-10 19:20:18 +00:00
|
|
|
// Send the stats
|
|
|
|
if err := stream.Send(presp); err != nil {
|
|
|
|
return err
|
2018-12-11 20:27:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 19:20:18 +00:00
|
|
|
|
|
|
|
return nil
|
2018-12-05 16:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcExecutorServer) Signal(ctx context.Context, req *proto.SignalRequest) (*proto.SignalResponse, error) {
|
2018-12-15 03:20:01 +00:00
|
|
|
sig := syscall.Signal(req.Signal)
|
|
|
|
if err := s.impl.Signal(sig); err != nil {
|
|
|
|
return nil, err
|
2018-12-05 16:03:56 +00:00
|
|
|
}
|
2018-12-15 03:20:01 +00:00
|
|
|
return &proto.SignalResponse{}, nil
|
2018-12-05 16:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcExecutorServer) Exec(ctx context.Context, req *proto.ExecRequest) (*proto.ExecResponse, error) {
|
|
|
|
deadline, err := ptypes.Timestamp(req.Deadline)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, exit, err := s.impl.Exec(deadline, req.Cmd, req.Args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.ExecResponse{
|
|
|
|
Output: out,
|
|
|
|
ExitCode: int32(exit),
|
|
|
|
}, nil
|
|
|
|
}
|
2019-04-28 21:27:23 +00:00
|
|
|
|
|
|
|
func (s *grpcExecutorServer) ExecStreaming(server proto.Executor_ExecStreamingServer) 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.impl.ExecStreaming(server.Context(),
|
|
|
|
msg.Setup.Command, msg.Setup.Tty,
|
|
|
|
server)
|
|
|
|
}
|