github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/api/nodes.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  	"strconv"
     7  )
     8  
     9  // Nodes is used to query node-related API endpoints
    10  type Nodes struct {
    11  	client *Client
    12  }
    13  
    14  // Nodes returns a handle on the node endpoints.
    15  func (c *Client) Nodes() *Nodes {
    16  	return &Nodes{client: c}
    17  }
    18  
    19  // List is used to list out all of the nodes
    20  func (n *Nodes) List(q *QueryOptions) ([]*NodeListStub, *QueryMeta, error) {
    21  	var resp NodeIndexSort
    22  	qm, err := n.client.query("/v1/nodes", &resp, q)
    23  	if err != nil {
    24  		return nil, nil, err
    25  	}
    26  	sort.Sort(NodeIndexSort(resp))
    27  	return resp, qm, nil
    28  }
    29  
    30  func (n *Nodes) PrefixList(prefix string) ([]*NodeListStub, *QueryMeta, error) {
    31  	return n.List(&QueryOptions{Prefix: prefix})
    32  }
    33  
    34  // Info is used to query a specific node by its ID.
    35  func (n *Nodes) Info(nodeID string, q *QueryOptions) (*Node, *QueryMeta, error) {
    36  	var resp Node
    37  	qm, err := n.client.query("/v1/node/"+nodeID, &resp, q)
    38  	if err != nil {
    39  		return nil, nil, err
    40  	}
    41  	return &resp, qm, nil
    42  }
    43  
    44  // ToggleDrain is used to toggle drain mode on/off for a given node.
    45  func (n *Nodes) ToggleDrain(nodeID string, drain bool, q *WriteOptions) (*WriteMeta, error) {
    46  	drainArg := strconv.FormatBool(drain)
    47  	wm, err := n.client.write("/v1/node/"+nodeID+"/drain?enable="+drainArg, nil, nil, q)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return wm, nil
    52  }
    53  
    54  // Allocations is used to return the allocations associated with a node.
    55  func (n *Nodes) Allocations(nodeID string, q *QueryOptions) ([]*Allocation, *QueryMeta, error) {
    56  	var resp []*Allocation
    57  	qm, err := n.client.query("/v1/node/"+nodeID+"/allocations", &resp, q)
    58  	if err != nil {
    59  		return nil, nil, err
    60  	}
    61  	sort.Sort(AllocationSort(resp))
    62  	return resp, qm, nil
    63  }
    64  
    65  // ForceEvaluate is used to force-evaluate an existing node.
    66  func (n *Nodes) ForceEvaluate(nodeID string, q *WriteOptions) (string, *WriteMeta, error) {
    67  	var resp nodeEvalResponse
    68  	wm, err := n.client.write("/v1/node/"+nodeID+"/evaluate", nil, &resp, q)
    69  	if err != nil {
    70  		return "", nil, err
    71  	}
    72  	return resp.EvalID, wm, nil
    73  }
    74  
    75  func (n *Nodes) Stats(nodeID string, q *QueryOptions) (*HostStats, error) {
    76  	node, _, err := n.client.Nodes().Info(nodeID, q)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	if node.HTTPAddr == "" {
    81  		return nil, fmt.Errorf("http addr of the node %q is running is not advertised", nodeID)
    82  	}
    83  	client, err := NewClient(n.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	var resp HostStats
    88  	if _, err := client.query("/v1/client/stats", &resp, nil); err != nil {
    89  		return nil, err
    90  	}
    91  	return &resp, nil
    92  }
    93  
    94  func (n *Nodes) GC(nodeID string, q *QueryOptions) error {
    95  	node, _, err := n.client.Nodes().Info(nodeID, q)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	if node.HTTPAddr == "" {
   100  		return fmt.Errorf("http addr of the node %q is running is not advertised", nodeID)
   101  	}
   102  	client, err := NewClient(n.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
   103  	if err != nil {
   104  		return err
   105  	}
   106  	var resp struct{}
   107  	_, err = client.query("/v1/client/gc", &resp, nil)
   108  	return err
   109  }
   110  
   111  // Node is used to deserialize a node entry.
   112  type Node struct {
   113  	ID                string
   114  	Datacenter        string
   115  	Name              string
   116  	HTTPAddr          string
   117  	TLSEnabled        bool
   118  	Attributes        map[string]string
   119  	Resources         *Resources
   120  	Reserved          *Resources
   121  	Links             map[string]string
   122  	Meta              map[string]string
   123  	NodeClass         string
   124  	Drain             bool
   125  	Status            string
   126  	StatusDescription string
   127  	StatusUpdatedAt   int64
   128  	CreateIndex       uint64
   129  	ModifyIndex       uint64
   130  }
   131  
   132  // HostStats represents resource usage stats of the host running a Nomad client
   133  type HostStats struct {
   134  	Memory           *HostMemoryStats
   135  	CPU              []*HostCPUStats
   136  	DiskStats        []*HostDiskStats
   137  	Uptime           uint64
   138  	CPUTicksConsumed float64
   139  }
   140  
   141  type HostMemoryStats struct {
   142  	Total     uint64
   143  	Available uint64
   144  	Used      uint64
   145  	Free      uint64
   146  }
   147  
   148  type HostCPUStats struct {
   149  	CPU    string
   150  	User   float64
   151  	System float64
   152  	Idle   float64
   153  }
   154  
   155  type HostDiskStats struct {
   156  	Device            string
   157  	Mountpoint        string
   158  	Size              uint64
   159  	Used              uint64
   160  	Available         uint64
   161  	UsedPercent       float64
   162  	InodesUsedPercent float64
   163  }
   164  
   165  // NodeListStub is a subset of information returned during
   166  // node list operations.
   167  type NodeListStub struct {
   168  	ID                string
   169  	Datacenter        string
   170  	Name              string
   171  	NodeClass         string
   172  	Drain             bool
   173  	Status            string
   174  	StatusDescription string
   175  	CreateIndex       uint64
   176  	ModifyIndex       uint64
   177  }
   178  
   179  // NodeIndexSort reverse sorts nodes by CreateIndex
   180  type NodeIndexSort []*NodeListStub
   181  
   182  func (n NodeIndexSort) Len() int {
   183  	return len(n)
   184  }
   185  
   186  func (n NodeIndexSort) Less(i, j int) bool {
   187  	return n[i].CreateIndex > n[j].CreateIndex
   188  }
   189  
   190  func (n NodeIndexSort) Swap(i, j int) {
   191  	n[i], n[j] = n[j], n[i]
   192  }
   193  
   194  // nodeEvalResponse is used to decode a force-eval.
   195  type nodeEvalResponse struct {
   196  	EvalID string
   197  }
   198  
   199  // AllocationSort reverse sorts allocs by CreateIndex.
   200  type AllocationSort []*Allocation
   201  
   202  func (a AllocationSort) Len() int {
   203  	return len(a)
   204  }
   205  
   206  func (a AllocationSort) Less(i, j int) bool {
   207  	return a[i].CreateIndex > a[j].CreateIndex
   208  }
   209  
   210  func (a AllocationSort) Swap(i, j int) {
   211  	a[i], a[j] = a[j], a[i]
   212  }