github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/command/agent/alloc_endpoint.go (about)

     1  package agent
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  )
    10  
    11  const (
    12  	allocNotFoundErr    = "allocation not found"
    13  	resourceNotFoundErr = "resource not found"
    14  )
    15  
    16  func (s *HTTPServer) AllocsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    17  	if req.Method != "GET" {
    18  		return nil, CodedError(405, ErrInvalidMethod)
    19  	}
    20  
    21  	args := structs.AllocListRequest{}
    22  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    23  		return nil, nil
    24  	}
    25  
    26  	var out structs.AllocListResponse
    27  	if err := s.agent.RPC("Alloc.List", &args, &out); err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	setMeta(resp, &out.QueryMeta)
    32  	if out.Allocations == nil {
    33  		out.Allocations = make([]*structs.AllocListStub, 0)
    34  	}
    35  	return out.Allocations, nil
    36  }
    37  
    38  func (s *HTTPServer) AllocSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    39  	allocID := strings.TrimPrefix(req.URL.Path, "/v1/allocation/")
    40  	if req.Method != "GET" {
    41  		return nil, CodedError(405, ErrInvalidMethod)
    42  	}
    43  
    44  	args := structs.AllocSpecificRequest{
    45  		AllocID: allocID,
    46  	}
    47  	if s.parse(resp, req, &args.Region, &args.QueryOptions) {
    48  		return nil, nil
    49  	}
    50  
    51  	var out structs.SingleAllocResponse
    52  	if err := s.agent.RPC("Alloc.GetAlloc", &args, &out); err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	setMeta(resp, &out.QueryMeta)
    57  	if out.Alloc == nil {
    58  		return nil, CodedError(404, "alloc not found")
    59  	}
    60  	return out.Alloc, nil
    61  }
    62  
    63  func (s *HTTPServer) ClientAllocRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    64  	if s.agent.client == nil {
    65  		return nil, clientNotRunning
    66  	}
    67  
    68  	reqSuffix := strings.TrimPrefix(req.URL.Path, "/v1/client/allocation/")
    69  
    70  	// tokenize the suffix of the path to get the alloc id and find the action
    71  	// invoked on the alloc id
    72  	tokens := strings.Split(reqSuffix, "/")
    73  	if len(tokens) != 2 {
    74  		return nil, CodedError(404, resourceNotFoundErr)
    75  	}
    76  	allocID := tokens[0]
    77  	switch tokens[1] {
    78  	case "stats":
    79  		return s.allocStats(allocID, resp, req)
    80  	case "snapshot":
    81  		return s.allocSnapshot(allocID, resp, req)
    82  	}
    83  
    84  	return nil, CodedError(404, resourceNotFoundErr)
    85  }
    86  
    87  func (s *HTTPServer) allocSnapshot(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    88  	allocFS, err := s.agent.Client().GetAllocFS(allocID)
    89  	if err != nil {
    90  		return nil, fmt.Errorf(allocNotFoundErr)
    91  	}
    92  	if err := allocFS.Snapshot(resp); err != nil {
    93  		return nil, fmt.Errorf("error making snapshot: %v", err)
    94  	}
    95  	return nil, nil
    96  }
    97  
    98  func (s *HTTPServer) allocStats(allocID string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
    99  	clientStats := s.agent.client.StatsReporter()
   100  	aStats, err := clientStats.GetAllocStats(allocID)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	task := req.URL.Query().Get("task")
   106  	return aStats.LatestAllocStats(task)
   107  }