2018-02-06 00:16:20 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-04-28 21:22:53 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-02-06 00:16:20 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
2020-03-18 11:27:32 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2021-03-22 14:12:42 +00:00
|
|
|
|
2018-02-06 00:16:20 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2019-04-28 21:22:53 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-02-06 00:16:20 +00:00
|
|
|
nstructs "github.com/hashicorp/nomad/nomad/structs"
|
2019-04-28 21:22:53 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-02-06 00:16:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Allocations endpoint is used for interacting with client allocations
|
|
|
|
type Allocations struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
2019-04-28 21:22:53 +00:00
|
|
|
func NewAllocationsEndpoint(c *Client) *Allocations {
|
|
|
|
a := &Allocations{c: c}
|
|
|
|
a.c.streamingRpcs.Register("Allocations.Exec", a.exec)
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:16:20 +00:00
|
|
|
// GarbageCollectAll is used to garbage collect all allocations on a client.
|
2018-02-06 01:20:42 +00:00
|
|
|
func (a *Allocations) GarbageCollectAll(args *nstructs.NodeSpecificRequest, reply *nstructs.GenericResponse) error {
|
2018-02-06 00:16:20 +00:00
|
|
|
defer metrics.MeasureSince([]string{"client", "allocations", "garbage_collect_all"}, time.Now())
|
|
|
|
|
2018-02-13 23:50:51 +00:00
|
|
|
// Check node write permissions
|
2018-02-06 00:16:20 +00:00
|
|
|
if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNodeWrite() {
|
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
a.c.CollectAllAllocs()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GarbageCollect is used to garbage collect an allocation on a client.
|
|
|
|
func (a *Allocations) GarbageCollect(args *nstructs.AllocSpecificRequest, reply *nstructs.GenericResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "allocations", "garbage_collect"}, time.Now())
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := a.c.GetAlloc(args.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check namespace submit job permission.
|
2018-02-06 00:16:20 +00:00
|
|
|
if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
2019-10-01 20:06:24 +00:00
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
2018-02-06 00:16:20 +00:00
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.c.CollectAllocation(args.AllocID) {
|
2021-01-04 16:34:12 +00:00
|
|
|
return fmt.Errorf("No such allocation on client, or allocation not eligible for GC")
|
2018-02-06 00:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 11:37:26 +00:00
|
|
|
// Signal is used to send a signal to an allocation's tasks on a client.
|
2019-04-03 10:46:15 +00:00
|
|
|
func (a *Allocations) Signal(args *nstructs.AllocSignalRequest, reply *nstructs.GenericResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "allocations", "signal"}, time.Now())
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := a.c.GetAlloc(args.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check namespace alloc-lifecycle permission.
|
2019-04-03 10:46:15 +00:00
|
|
|
if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
2019-10-01 20:06:24 +00:00
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityAllocLifecycle) {
|
2019-04-03 10:46:15 +00:00
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.c.SignalAllocation(args.AllocID, args.Task, args.Signal)
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:56:02 +00:00
|
|
|
// Restart is used to trigger a restart of an allocation or a subtask on a client.
|
|
|
|
func (a *Allocations) Restart(args *nstructs.AllocRestartRequest, reply *nstructs.GenericResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "allocations", "restart"}, time.Now())
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := a.c.GetAlloc(args.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check namespace alloc-lifecycle permission.
|
2019-04-01 12:56:02 +00:00
|
|
|
if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
2019-10-01 20:06:24 +00:00
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityAllocLifecycle) {
|
2019-04-01 12:56:02 +00:00
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.c.RestartAllocation(args.AllocID, args.TaskName)
|
|
|
|
}
|
|
|
|
|
2018-02-06 00:16:20 +00:00
|
|
|
// Stats is used to collect allocation statistics
|
|
|
|
func (a *Allocations) Stats(args *cstructs.AllocStatsRequest, reply *cstructs.AllocStatsResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "allocations", "stats"}, time.Now())
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := a.c.GetAlloc(args.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check read-job permission.
|
2018-02-06 00:16:20 +00:00
|
|
|
if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
2019-10-01 20:06:24 +00:00
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityReadJob) {
|
2018-02-06 00:16:20 +00:00
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
clientStats := a.c.StatsReporter()
|
|
|
|
aStats, err := clientStats.GetAllocStats(args.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := aStats.LatestAllocStats(args.Task)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Stats = stats
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
// exec is used to execute command in a running task
|
|
|
|
func (a *Allocations) exec(conn io.ReadWriteCloser) {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "allocations", "exec"}, time.Now())
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
execID := uuid.Generate()
|
|
|
|
decoder := codec.NewDecoder(conn, structs.MsgpackHandle)
|
|
|
|
encoder := codec.NewEncoder(conn, structs.MsgpackHandle)
|
|
|
|
|
|
|
|
code, err := a.execImpl(encoder, decoder, execID)
|
|
|
|
if err != nil {
|
|
|
|
a.c.logger.Info("task exec session ended with an error", "error", err, "code", code)
|
|
|
|
handleStreamResultError(err, code, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.c.logger.Info("task exec session ended", "exec_id", execID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Allocations) execImpl(encoder *codec.Encoder, decoder *codec.Decoder, execID string) (code *int64, err error) {
|
|
|
|
|
|
|
|
// Decode the arguments
|
|
|
|
var req cstructs.AllocExecRequest
|
|
|
|
if err := decoder.Decode(&req); err != nil {
|
|
|
|
return helper.Int64ToPtr(500), err
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:31:39 +00:00
|
|
|
if a.c.GetConfig().DisableRemoteExec {
|
|
|
|
return nil, structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
if req.AllocID == "" {
|
|
|
|
return helper.Int64ToPtr(400), allocIDNotPresentErr
|
|
|
|
}
|
|
|
|
ar, err := a.c.getAllocRunner(req.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
code := helper.Int64ToPtr(500)
|
|
|
|
if structs.IsErrUnknownAllocation(err) {
|
|
|
|
code = helper.Int64ToPtr(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
alloc := ar.Alloc()
|
|
|
|
|
2019-04-28 21:22:53 +00:00
|
|
|
aclObj, token, err := a.c.resolveTokenAndACL(req.QueryOptions.AuthToken)
|
|
|
|
{
|
|
|
|
// log access
|
|
|
|
tokenName, tokenID := "", ""
|
|
|
|
if token != nil {
|
|
|
|
tokenName, tokenID = token.Name, token.AccessorID
|
|
|
|
}
|
|
|
|
|
|
|
|
a.c.logger.Info("task exec session starting",
|
|
|
|
"exec_id", execID,
|
|
|
|
"alloc_id", req.AllocID,
|
|
|
|
"task", req.Task,
|
|
|
|
"command", req.Cmd,
|
|
|
|
"tty", req.Tty,
|
|
|
|
"access_token_name", tokenName,
|
|
|
|
"access_token_id", tokenID,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check alloc-exec permission.
|
2019-04-28 21:22:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-01 20:06:24 +00:00
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityAllocExec) {
|
|
|
|
return nil, structs.ErrPermissionDenied
|
2019-04-28 21:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the arguments
|
|
|
|
if req.Task == "" {
|
|
|
|
return helper.Int64ToPtr(400), taskNotPresentErr
|
|
|
|
}
|
|
|
|
if len(req.Cmd) == 0 {
|
|
|
|
return helper.Int64ToPtr(400), errors.New("command is not present")
|
|
|
|
}
|
|
|
|
|
|
|
|
capabilities, err := ar.GetTaskDriverCapabilities(req.Task)
|
|
|
|
if err != nil {
|
|
|
|
code := helper.Int64ToPtr(500)
|
|
|
|
if structs.IsErrUnknownAllocation(err) {
|
|
|
|
code = helper.Int64ToPtr(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check node access
|
|
|
|
if aclObj != nil && capabilities.FSIsolation == drivers.FSIsolationNone {
|
2019-10-01 20:06:24 +00:00
|
|
|
exec := aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityAllocNodeExec)
|
2019-04-28 21:22:53 +00:00
|
|
|
if !exec {
|
|
|
|
return nil, structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
allocState, err := a.c.GetAllocState(req.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
code := helper.Int64ToPtr(500)
|
|
|
|
if structs.IsErrUnknownAllocation(err) {
|
|
|
|
code = helper.Int64ToPtr(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the task is there
|
|
|
|
taskState := allocState.TaskStates[req.Task]
|
|
|
|
if taskState == nil {
|
|
|
|
return helper.Int64ToPtr(400), fmt.Errorf("unknown task name %q", req.Task)
|
|
|
|
}
|
|
|
|
|
|
|
|
if taskState.StartedAt.IsZero() {
|
|
|
|
return helper.Int64ToPtr(404), fmt.Errorf("task %q not started yet.", req.Task)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
h := ar.GetTaskExecHandler(req.Task)
|
|
|
|
if h == nil {
|
|
|
|
return helper.Int64ToPtr(404), fmt.Errorf("task %q is not running.", req.Task)
|
|
|
|
}
|
|
|
|
|
2019-05-21 12:50:25 +00:00
|
|
|
err = h(ctx, req.Cmd, req.Tty, newExecStream(decoder, encoder))
|
2019-04-28 21:22:53 +00:00
|
|
|
if err != nil {
|
|
|
|
code := helper.Int64ToPtr(500)
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newExecStream returns a new exec stream as expected by drivers that interpolate with RPC streaming format
|
2019-05-21 12:50:25 +00:00
|
|
|
func newExecStream(decoder *codec.Decoder, encoder *codec.Encoder) drivers.ExecTaskStream {
|
2019-04-28 21:22:53 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
return &execStream{
|
2019-05-21 12:50:25 +00:00
|
|
|
decoder: decoder,
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
buf: buf,
|
|
|
|
encoder: encoder,
|
2021-04-02 13:31:10 +00:00
|
|
|
frameCodec: codec.NewEncoder(buf, nstructs.JsonHandle),
|
2019-04-28 21:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type execStream struct {
|
2019-05-21 12:50:25 +00:00
|
|
|
decoder *codec.Decoder
|
2019-04-28 21:22:53 +00:00
|
|
|
|
|
|
|
encoder *codec.Encoder
|
|
|
|
buf *bytes.Buffer
|
|
|
|
frameCodec *codec.Encoder
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send sends driver output response across RPC mechanism using cstructs.StreamErrWrapper
|
|
|
|
func (s *execStream) Send(m *drivers.ExecTaskStreamingResponseMsg) error {
|
|
|
|
s.buf.Reset()
|
|
|
|
s.frameCodec.Reset(s.buf)
|
|
|
|
|
|
|
|
s.frameCodec.MustEncode(m)
|
|
|
|
return s.encoder.Encode(cstructs.StreamErrWrapper{
|
|
|
|
Payload: s.buf.Bytes(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recv returns next exec user input from the RPC to be passed to driver exec handler
|
|
|
|
func (s *execStream) Recv() (*drivers.ExecTaskStreamingRequestMsg, error) {
|
|
|
|
req := drivers.ExecTaskStreamingRequestMsg{}
|
|
|
|
err := s.decoder.Decode(&req)
|
|
|
|
return &req, err
|
|
|
|
}
|