github.com/manicqin/nomad@v0.9.5/command/agent/alloc_endpoint.go (about)

     1  package agent
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"net"
     9  	"net/http"
    10  	"strconv"
    11  	"strings"
    12  
    13  	"github.com/golang/snappy"
    14  	"github.com/gorilla/websocket"
    15  	cstructs "github.com/hashicorp/nomad/client/structs"
    16  	"github.com/hashicorp/nomad/nomad/structs"
    17  	"github.com/hashicorp/nomad/plugins/drivers"
    18  	"github.com/ugorji/go/codec"
    19  )
    20  
    21  const (
    22  	allocNotFoundErr    = "allocation not found"
    23  	resourceNotFoundErr = "resource not found"
    24  )
    25  
    26  func (s *HTTPServer) AllocsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    27  	if req.Method != "GET" {
    28  		return nil, CodedError(405, ErrInvalidMethod)
    29  	}
    30  
    31  	args := structs.AllocListRequest{}
    32  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    33  		return nil, nil
    34  	}
    35  
    36  	var out structs.AllocListResponse
    37  	if err := s.agent.RPC("Alloc.List", &args, &out); err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	setMeta(resp, &out.QueryMeta)
    42  	if out.Allocations == nil {
    43  		out.Allocations = make([]*structs.AllocListStub, 0)
    44  	}
    45  	for _, alloc := range out.Allocations {
    46  		alloc.SetEventDisplayMessages()
    47  	}
    48  	return out.Allocations, nil
    49  }
    50  
    51  func (s *HTTPServer) AllocSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    52  	reqSuffix := strings.TrimPrefix(req.URL.Path, "/v1/allocation/")
    53  
    54  	// tokenize the suffix of the path to get the alloc id and find the action
    55  	// invoked on the alloc id
    56  	tokens := strings.Split(reqSuffix, "/")
    57  	if len(tokens) > 2 || len(tokens) < 1 {
    58  		return nil, CodedError(404, resourceNotFoundErr)
    59  	}
    60  	allocID := tokens[0]
    61  
    62  	if len(tokens) == 1 {
    63  		return s.allocGet(allocID, resp, req)
    64  	}
    65  
    66  	switch tokens[1] {
    67  	case "stop":
    68  		return s.allocStop(allocID, resp, req)
    69  	}
    70  
    71  	return nil, CodedError(404, resourceNotFoundErr)
    72  }
    73  
    74  func (s *HTTPServer) allocGet(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    75  	if req.Method != "GET" {
    76  		return nil, CodedError(405, ErrInvalidMethod)
    77  	}
    78  
    79  	args := structs.AllocSpecificRequest{
    80  		AllocID: allocID,
    81  	}
    82  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    83  		return nil, nil
    84  	}
    85  
    86  	var out structs.SingleAllocResponse
    87  	if err := s.agent.RPC("Alloc.GetAlloc", &args, &out); err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	setMeta(resp, &out.QueryMeta)
    92  	if out.Alloc == nil {
    93  		return nil, CodedError(404, "alloc not found")
    94  	}
    95  
    96  	// Decode the payload if there is any
    97  	alloc := out.Alloc
    98  	if alloc.Job != nil && len(alloc.Job.Payload) != 0 {
    99  		decoded, err := snappy.Decode(nil, alloc.Job.Payload)
   100  		if err != nil {
   101  			return nil, err
   102  		}
   103  		alloc = alloc.Copy()
   104  		alloc.Job.Payload = decoded
   105  	}
   106  	alloc.SetEventDisplayMessages()
   107  
   108  	return alloc, nil
   109  }
   110  
   111  func (s *HTTPServer) allocStop(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
   112  	if !(req.Method == "POST" || req.Method == "PUT") {
   113  		return nil, CodedError(405, ErrInvalidMethod)
   114  	}
   115  
   116  	sr := &structs.AllocStopRequest{
   117  		AllocID: allocID,
   118  	}
   119  	s.parseWriteRequest(req, &sr.WriteRequest)
   120  
   121  	var out structs.AllocStopResponse
   122  	rpcErr := s.agent.RPC("Alloc.Stop", &sr, &out)
   123  
   124  	if rpcErr != nil {
   125  		if structs.IsErrUnknownAllocation(rpcErr) {
   126  			rpcErr = CodedError(404, allocNotFoundErr)
   127  		}
   128  		return nil, rpcErr
   129  	}
   130  
   131  	setIndex(resp, out.Index)
   132  	return &out, nil
   133  }
   134  
   135  func (s *HTTPServer) ClientAllocRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
   136  	reqSuffix := strings.TrimPrefix(req.URL.Path, "/v1/client/allocation/")
   137  
   138  	// tokenize the suffix of the path to get the alloc id and find the action
   139  	// invoked on the alloc id
   140  	tokens := strings.Split(reqSuffix, "/")
   141  	if len(tokens) != 2 {
   142  		return nil, CodedError(404, resourceNotFoundErr)
   143  	}
   144  	allocID := tokens[0]
   145  	switch tokens[1] {
   146  	case "stats":
   147  		return s.allocStats(allocID, resp, req)
   148  	case "exec":
   149  		return s.allocExec(allocID, resp, req)
   150  	case "snapshot":
   151  		if s.agent.client == nil {
   152  			return nil, clientNotRunning
   153  		}
   154  		return s.allocSnapshot(allocID, resp, req)
   155  	case "restart":
   156  		return s.allocRestart(allocID, resp, req)
   157  	case "gc":
   158  		return s.allocGC(allocID, resp, req)
   159  	case "signal":
   160  		return s.allocSignal(allocID, resp, req)
   161  	}
   162  
   163  	return nil, CodedError(404, resourceNotFoundErr)
   164  }
   165  
   166  func (s *HTTPServer) ClientGCRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
   167  	// Get the requested Node ID
   168  	requestedNode := req.URL.Query().Get("node_id")
   169  
   170  	// Build the request and parse the ACL token
   171  	args := structs.NodeSpecificRequest{
   172  		NodeID: requestedNode,
   173  	}
   174  	s.parse(resp, req, &args.QueryOptions.Region, &args.QueryOptions)
   175  
   176  	// Determine the handler to use
   177  	useLocalClient, useClientRPC, useServerRPC := s.rpcHandlerForNode(requestedNode)
   178  
   179  	// Make the RPC
   180  	var reply structs.GenericResponse
   181  	var rpcErr error
   182  	if useLocalClient {
   183  		rpcErr = s.agent.Client().ClientRPC("Allocations.GarbageCollectAll", &args, &reply)
   184  	} else if useClientRPC {
   185  		rpcErr = s.agent.Client().RPC("ClientAllocations.GarbageCollectAll", &args, &reply)
   186  	} else if useServerRPC {
   187  		rpcErr = s.agent.Server().RPC("ClientAllocations.GarbageCollectAll", &args, &reply)
   188  	} else {
   189  		rpcErr = CodedError(400, "No local Node and node_id not provided")
   190  	}
   191  
   192  	if rpcErr != nil {
   193  		if structs.IsErrNoNodeConn(rpcErr) {
   194  			rpcErr = CodedError(404, rpcErr.Error())
   195  		}
   196  	}
   197  
   198  	return nil, rpcErr
   199  }
   200  
   201  func (s *HTTPServer) allocRestart(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
   202  	// Build the request and parse the ACL token
   203  	args := structs.AllocRestartRequest{
   204  		AllocID:  allocID,
   205  		TaskName: "",
   206  	}
   207  	s.parse(resp, req, &args.QueryOptions.Region, &args.QueryOptions)
   208  
   209  	// Explicitly parse the body separately to disallow overriding AllocID in req Body.
   210  	var reqBody struct {
   211  		TaskName string
   212  	}
   213  	err := json.NewDecoder(req.Body).Decode(&reqBody)
   214  	if err != nil && err != io.EOF {
   215  		return nil, err
   216  	}
   217  	if reqBody.TaskName != "" {
   218  		args.TaskName = reqBody.TaskName
   219  	}
   220  
   221  	// Determine the handler to use
   222  	useLocalClient, useClientRPC, useServerRPC := s.rpcHandlerForAlloc(allocID)
   223  
   224  	// Make the RPC
   225  	var reply structs.GenericResponse
   226  	var rpcErr error
   227  	if useLocalClient {
   228  		rpcErr = s.agent.Client().ClientRPC("Allocations.Restart", &args, &reply)
   229  	} else if useClientRPC {
   230  		rpcErr = s.agent.Client().RPC("ClientAllocations.Restart", &args, &reply)
   231  	} else if useServerRPC {
   232  		rpcErr = s.agent.Server().RPC("ClientAllocations.Restart", &args, &reply)
   233  	} else {
   234  		rpcErr = CodedError(400, "No local Node and node_id not provided")
   235  	}
   236  
   237  	if rpcErr != nil {
   238  		if structs.IsErrNoNodeConn(rpcErr) || structs.IsErrUnknownAllocation(rpcErr) {
   239  			rpcErr = CodedError(404, rpcErr.Error())
   240  		}
   241  	}
   242  
   243  	return reply, rpcErr
   244  }
   245  
   246  func (s *HTTPServer) allocGC(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
   247  	// Build the request and parse the ACL token
   248  	args := structs.AllocSpecificRequest{
   249  		AllocID: allocID,
   250  	}
   251  	s.parse(resp, req, &args.QueryOptions.Region, &args.QueryOptions)
   252  
   253  	// Determine the handler to use
   254  	useLocalClient, useClientRPC, useServerRPC := s.rpcHandlerForAlloc(allocID)
   255  
   256  	// Make the RPC
   257  	var reply structs.GenericResponse
   258  	var rpcErr error
   259  	if useLocalClient {
   260  		rpcErr = s.agent.Client().ClientRPC("Allocations.GarbageCollect", &args, &reply)
   261  	} else if useClientRPC {
   262  		rpcErr = s.agent.Client().RPC("ClientAllocations.GarbageCollect", &args, &reply)
   263  	} else if useServerRPC {
   264  		rpcErr = s.agent.Server().RPC("ClientAllocations.GarbageCollect", &args, &reply)
   265  	} else {
   266  		rpcErr = CodedError(400, "No local Node and node_id not provided")
   267  	}
   268  
   269  	if rpcErr != nil {
   270  		if structs.IsErrNoNodeConn(rpcErr) || structs.IsErrUnknownAllocation(rpcErr) {
   271  			rpcErr = CodedError(404, rpcErr.Error())
   272  		}
   273  	}
   274  
   275  	return nil, rpcErr
   276  }
   277  
   278  func (s *HTTPServer) allocSignal(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
   279  	if !(req.Method == "POST" || req.Method == "PUT") {
   280  		return nil, CodedError(405, ErrInvalidMethod)
   281  	}
   282  
   283  	// Build the request and parse the ACL token
   284  	args := structs.AllocSignalRequest{}
   285  	err := decodeBody(req, &args)
   286  	if err != nil {
   287  		return nil, CodedError(400, fmt.Sprintf("Failed to decode body: %v", err))
   288  	}
   289  	s.parse(resp, req, &args.QueryOptions.Region, &args.QueryOptions)
   290  	args.AllocID = allocID
   291  
   292  	// Determine the handler to use
   293  	useLocalClient, useClientRPC, useServerRPC := s.rpcHandlerForAlloc(allocID)
   294  
   295  	// Make the RPC
   296  	var reply structs.GenericResponse
   297  	var rpcErr error
   298  	if useLocalClient {
   299  		rpcErr = s.agent.Client().ClientRPC("Allocations.Signal", &args, &reply)
   300  	} else if useClientRPC {
   301  		rpcErr = s.agent.Client().RPC("ClientAllocations.Signal", &args, &reply)
   302  	} else if useServerRPC {
   303  		rpcErr = s.agent.Server().RPC("ClientAllocations.Signal", &args, &reply)
   304  	} else {
   305  		rpcErr = CodedError(400, "No local Node and node_id not provided")
   306  	}
   307  
   308  	if rpcErr != nil {
   309  		if structs.IsErrNoNodeConn(rpcErr) || structs.IsErrUnknownAllocation(rpcErr) {
   310  			rpcErr = CodedError(404, rpcErr.Error())
   311  		}
   312  	}
   313  
   314  	return reply, rpcErr
   315  }
   316  
   317  func (s *HTTPServer) allocSnapshot(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
   318  	var secret string
   319  	s.parseToken(req, &secret)
   320  	if !s.agent.Client().ValidateMigrateToken(allocID, secret) {
   321  		return nil, structs.ErrPermissionDenied
   322  	}
   323  
   324  	allocFS, err := s.agent.Client().GetAllocFS(allocID)
   325  	if err != nil {
   326  		return nil, fmt.Errorf(allocNotFoundErr)
   327  	}
   328  	if err := allocFS.Snapshot(resp); err != nil {
   329  		return nil, fmt.Errorf("error making snapshot: %v", err)
   330  	}
   331  	return nil, nil
   332  }
   333  
   334  func (s *HTTPServer) allocStats(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
   335  
   336  	// Build the request and parse the ACL token
   337  	task := req.URL.Query().Get("task")
   338  	args := cstructs.AllocStatsRequest{
   339  		AllocID: allocID,
   340  		Task:    task,
   341  	}
   342  	s.parse(resp, req, &args.QueryOptions.Region, &args.QueryOptions)
   343  
   344  	// Determine the handler to use
   345  	useLocalClient, useClientRPC, useServerRPC := s.rpcHandlerForAlloc(allocID)
   346  
   347  	// Make the RPC
   348  	var reply cstructs.AllocStatsResponse
   349  	var rpcErr error
   350  	if useLocalClient {
   351  		rpcErr = s.agent.Client().ClientRPC("Allocations.Stats", &args, &reply)
   352  	} else if useClientRPC {
   353  		rpcErr = s.agent.Client().RPC("ClientAllocations.Stats", &args, &reply)
   354  	} else if useServerRPC {
   355  		rpcErr = s.agent.Server().RPC("ClientAllocations.Stats", &args, &reply)
   356  	} else {
   357  		rpcErr = CodedError(400, "No local Node and node_id not provided")
   358  	}
   359  
   360  	if rpcErr != nil {
   361  		if structs.IsErrNoNodeConn(rpcErr) || structs.IsErrUnknownAllocation(rpcErr) {
   362  			rpcErr = CodedError(404, rpcErr.Error())
   363  		}
   364  	}
   365  
   366  	return reply.Stats, rpcErr
   367  }
   368  
   369  func (s *HTTPServer) allocExec(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
   370  	// Build the request and parse the ACL token
   371  	task := req.URL.Query().Get("task")
   372  	cmdJsonStr := req.URL.Query().Get("command")
   373  	var command []string
   374  	err := json.Unmarshal([]byte(cmdJsonStr), &command)
   375  	if err != nil {
   376  		// this shouldn't happen, []string is always be serializable to json
   377  		return nil, fmt.Errorf("failed to marshal command into json: %v", err)
   378  	}
   379  
   380  	ttyB := false
   381  	if tty := req.URL.Query().Get("tty"); tty != "" {
   382  		ttyB, err = strconv.ParseBool(tty)
   383  		if err != nil {
   384  			return nil, fmt.Errorf("tty value is not a boolean: %v", err)
   385  		}
   386  	}
   387  
   388  	args := cstructs.AllocExecRequest{
   389  		AllocID: allocID,
   390  		Task:    task,
   391  		Cmd:     command,
   392  		Tty:     ttyB,
   393  	}
   394  	s.parse(resp, req, &args.QueryOptions.Region, &args.QueryOptions)
   395  
   396  	conn, err := s.wsUpgrader.Upgrade(resp, req, nil)
   397  	if err != nil {
   398  		return nil, fmt.Errorf("failed to upgrade connection: %v", err)
   399  	}
   400  
   401  	return s.execStreamImpl(conn, &args)
   402  }
   403  
   404  func (s *HTTPServer) execStreamImpl(ws *websocket.Conn, args *cstructs.AllocExecRequest) (interface{}, error) {
   405  	allocID := args.AllocID
   406  	method := "Allocations.Exec"
   407  
   408  	// Get the correct handler
   409  	localClient, remoteClient, localServer := s.rpcHandlerForAlloc(allocID)
   410  	var handler structs.StreamingRpcHandler
   411  	var handlerErr error
   412  	if localClient {
   413  		handler, handlerErr = s.agent.Client().StreamingRpcHandler(method)
   414  	} else if remoteClient {
   415  		handler, handlerErr = s.agent.Client().RemoteStreamingRpcHandler(method)
   416  	} else if localServer {
   417  		handler, handlerErr = s.agent.Server().StreamingRpcHandler(method)
   418  	}
   419  
   420  	if handlerErr != nil {
   421  		return nil, CodedError(500, handlerErr.Error())
   422  	}
   423  
   424  	// Create a pipe connecting the (possibly remote) handler to the http response
   425  	httpPipe, handlerPipe := net.Pipe()
   426  	decoder := codec.NewDecoder(httpPipe, structs.MsgpackHandle)
   427  	encoder := codec.NewEncoder(httpPipe, structs.MsgpackHandle)
   428  
   429  	// Create a goroutine that closes the pipe if the connection closes.
   430  	ctx, cancel := context.WithCancel(context.Background())
   431  	go func() {
   432  		<-ctx.Done()
   433  		httpPipe.Close()
   434  
   435  		// don't close ws - wait to drain messages
   436  	}()
   437  
   438  	// Create a channel that decodes the results
   439  	errCh := make(chan HTTPCodedError, 2)
   440  
   441  	// stream response
   442  	go func() {
   443  		defer cancel()
   444  
   445  		// Send the request
   446  		if err := encoder.Encode(args); err != nil {
   447  			errCh <- CodedError(500, err.Error())
   448  			return
   449  		}
   450  
   451  		go forwardExecInput(encoder, ws, errCh)
   452  
   453  		for {
   454  			select {
   455  			case <-ctx.Done():
   456  				errCh <- nil
   457  				return
   458  			default:
   459  			}
   460  
   461  			var res cstructs.StreamErrWrapper
   462  			err := decoder.Decode(&res)
   463  			if isClosedError(err) {
   464  				ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
   465  				errCh <- nil
   466  				return
   467  			}
   468  
   469  			if err != nil {
   470  				errCh <- CodedError(500, err.Error())
   471  				return
   472  			}
   473  			decoder.Reset(httpPipe)
   474  
   475  			if err := res.Error; err != nil {
   476  				code := 500
   477  				if err.Code != nil {
   478  					code = int(*err.Code)
   479  				}
   480  				errCh <- CodedError(code, err.Error())
   481  				return
   482  			}
   483  
   484  			if err := ws.WriteMessage(websocket.TextMessage, res.Payload); err != nil {
   485  				errCh <- CodedError(500, err.Error())
   486  				return
   487  			}
   488  		}
   489  	}()
   490  
   491  	// start streaming request to streaming RPC - returns when streaming completes or errors
   492  	handler(handlerPipe)
   493  	// stop streaming background goroutines for streaming - but not websocket activity
   494  	cancel()
   495  	// retreieve any error and/or wait until goroutine stop and close errCh connection before
   496  	// closing websocket connection
   497  	codedErr := <-errCh
   498  
   499  	if isClosedError(codedErr) {
   500  		codedErr = nil
   501  	} else if codedErr != nil {
   502  		ws.WriteMessage(websocket.CloseMessage,
   503  			websocket.FormatCloseMessage(toWsCode(codedErr.Code()), codedErr.Error()))
   504  	}
   505  	ws.Close()
   506  
   507  	return nil, codedErr
   508  }
   509  
   510  func toWsCode(httpCode int) int {
   511  	switch httpCode {
   512  	case 500:
   513  		return websocket.CloseInternalServerErr
   514  	default:
   515  		// placeholder error code
   516  		return websocket.ClosePolicyViolation
   517  	}
   518  }
   519  
   520  func isClosedError(err error) bool {
   521  	if err == nil {
   522  		return false
   523  	}
   524  
   525  	return err == io.EOF ||
   526  		err == io.ErrClosedPipe ||
   527  		strings.Contains(err.Error(), "closed") ||
   528  		strings.Contains(err.Error(), "EOF")
   529  }
   530  
   531  // forwardExecInput forwards exec input (e.g. stdin) from websocket connection
   532  // to the streaming RPC connection to client
   533  func forwardExecInput(encoder *codec.Encoder, ws *websocket.Conn, errCh chan<- HTTPCodedError) {
   534  	for {
   535  		sf := &drivers.ExecTaskStreamingRequestMsg{}
   536  		err := ws.ReadJSON(sf)
   537  		if err == io.EOF {
   538  			return
   539  		}
   540  
   541  		if err != nil {
   542  			errCh <- CodedError(500, err.Error())
   543  			return
   544  		}
   545  
   546  		err = encoder.Encode(sf)
   547  		if err != nil {
   548  			errCh <- CodedError(500, err.Error())
   549  		}
   550  	}
   551  }