github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/api/deployments.go (about)

     1  package api
     2  
     3  import (
     4  	"sort"
     5  )
     6  
     7  // Deployments is used to query the deployments endpoints.
     8  type Deployments struct {
     9  	client *Client
    10  }
    11  
    12  // Deployments returns a new handle on the deployments.
    13  func (c *Client) Deployments() *Deployments {
    14  	return &Deployments{client: c}
    15  }
    16  
    17  // List is used to dump all of the deployments.
    18  func (d *Deployments) List(q *QueryOptions) ([]*Deployment, *QueryMeta, error) {
    19  	var resp []*Deployment
    20  	qm, err := d.client.query("/v1/deployments", &resp, q)
    21  	if err != nil {
    22  		return nil, nil, err
    23  	}
    24  	sort.Sort(DeploymentIndexSort(resp))
    25  	return resp, qm, nil
    26  }
    27  
    28  func (d *Deployments) PrefixList(prefix string) ([]*Deployment, *QueryMeta, error) {
    29  	return d.List(&QueryOptions{Prefix: prefix})
    30  }
    31  
    32  // Info is used to query a single deployment by its ID.
    33  func (d *Deployments) Info(deploymentID string, q *QueryOptions) (*Deployment, *QueryMeta, error) {
    34  	var resp Deployment
    35  	qm, err := d.client.query("/v1/deployment/"+deploymentID, &resp, q)
    36  	if err != nil {
    37  		return nil, nil, err
    38  	}
    39  	return &resp, qm, nil
    40  }
    41  
    42  // Allocations is used to retrieve a set of allocations that are part of the
    43  // deployment
    44  func (d *Deployments) Allocations(deploymentID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) {
    45  	var resp []*AllocationListStub
    46  	qm, err := d.client.query("/v1/deployment/allocations/"+deploymentID, &resp, q)
    47  	if err != nil {
    48  		return nil, nil, err
    49  	}
    50  	sort.Sort(AllocIndexSort(resp))
    51  	return resp, qm, nil
    52  }
    53  
    54  // Fail is used to fail the given deployment.
    55  func (d *Deployments) Fail(deploymentID string, q *WriteOptions) (*DeploymentUpdateResponse, *WriteMeta, error) {
    56  	var resp DeploymentUpdateResponse
    57  	req := &DeploymentFailRequest{
    58  		DeploymentID: deploymentID,
    59  	}
    60  	wm, err := d.client.write("/v1/deployment/fail/"+deploymentID, req, &resp, q)
    61  	if err != nil {
    62  		return nil, nil, err
    63  	}
    64  	return &resp, wm, nil
    65  }
    66  
    67  // Pause is used to pause or unpause the given deployment.
    68  func (d *Deployments) Pause(deploymentID string, pause bool, q *WriteOptions) (*DeploymentUpdateResponse, *WriteMeta, error) {
    69  	var resp DeploymentUpdateResponse
    70  	req := &DeploymentPauseRequest{
    71  		DeploymentID: deploymentID,
    72  		Pause:        pause,
    73  	}
    74  	wm, err := d.client.write("/v1/deployment/pause/"+deploymentID, req, &resp, q)
    75  	if err != nil {
    76  		return nil, nil, err
    77  	}
    78  	return &resp, wm, nil
    79  }
    80  
    81  // PromoteAll is used to promote all canaries in the given deployment
    82  func (d *Deployments) PromoteAll(deploymentID string, q *WriteOptions) (*DeploymentUpdateResponse, *WriteMeta, error) {
    83  	var resp DeploymentUpdateResponse
    84  	req := &DeploymentPromoteRequest{
    85  		DeploymentID: deploymentID,
    86  		All:          true,
    87  	}
    88  	wm, err := d.client.write("/v1/deployment/promote/"+deploymentID, req, &resp, q)
    89  	if err != nil {
    90  		return nil, nil, err
    91  	}
    92  	return &resp, wm, nil
    93  }
    94  
    95  // PromoteGroups is used to promote canaries in the passed groups in the given deployment
    96  func (d *Deployments) PromoteGroups(deploymentID string, groups []string, q *WriteOptions) (*DeploymentUpdateResponse, *WriteMeta, error) {
    97  	var resp DeploymentUpdateResponse
    98  	req := &DeploymentPromoteRequest{
    99  		DeploymentID: deploymentID,
   100  		Groups:       groups,
   101  	}
   102  	wm, err := d.client.write("/v1/deployment/promote/"+deploymentID, req, &resp, q)
   103  	if err != nil {
   104  		return nil, nil, err
   105  	}
   106  	return &resp, wm, nil
   107  }
   108  
   109  // SetAllocHealth is used to set allocation health for allocs that are part of
   110  // the given deployment
   111  func (d *Deployments) SetAllocHealth(deploymentID string, healthy, unhealthy []string, q *WriteOptions) (*DeploymentUpdateResponse, *WriteMeta, error) {
   112  	var resp DeploymentUpdateResponse
   113  	req := &DeploymentAllocHealthRequest{
   114  		DeploymentID:           deploymentID,
   115  		HealthyAllocationIDs:   healthy,
   116  		UnhealthyAllocationIDs: unhealthy,
   117  	}
   118  	wm, err := d.client.write("/v1/deployment/allocation-health/"+deploymentID, req, &resp, q)
   119  	if err != nil {
   120  		return nil, nil, err
   121  	}
   122  	return &resp, wm, nil
   123  }
   124  
   125  // Deployment is used to serialize an deployment.
   126  type Deployment struct {
   127  	ID                string
   128  	Namespace         string
   129  	JobID             string
   130  	JobVersion        uint64
   131  	JobModifyIndex    uint64
   132  	JobCreateIndex    uint64
   133  	TaskGroups        map[string]*DeploymentState
   134  	Status            string
   135  	StatusDescription string
   136  	CreateIndex       uint64
   137  	ModifyIndex       uint64
   138  }
   139  
   140  // DeploymentState tracks the state of a deployment for a given task group.
   141  type DeploymentState struct {
   142  	PlacedCanaries  []string
   143  	AutoRevert      bool
   144  	Promoted        bool
   145  	DesiredCanaries int
   146  	DesiredTotal    int
   147  	PlacedAllocs    int
   148  	HealthyAllocs   int
   149  	UnhealthyAllocs int
   150  }
   151  
   152  // DeploymentIndexSort is a wrapper to sort deployments by CreateIndex. We
   153  // reverse the test so that we get the highest index first.
   154  type DeploymentIndexSort []*Deployment
   155  
   156  func (d DeploymentIndexSort) Len() int {
   157  	return len(d)
   158  }
   159  
   160  func (d DeploymentIndexSort) Less(i, j int) bool {
   161  	return d[i].CreateIndex > d[j].CreateIndex
   162  }
   163  
   164  func (d DeploymentIndexSort) Swap(i, j int) {
   165  	d[i], d[j] = d[j], d[i]
   166  }
   167  
   168  // DeploymentUpdateResponse is used to respond to a deployment change. The
   169  // response will include the modify index of the deployment as well as details
   170  // of any triggered evaluation.
   171  type DeploymentUpdateResponse struct {
   172  	EvalID                string
   173  	EvalCreateIndex       uint64
   174  	DeploymentModifyIndex uint64
   175  	RevertedJobVersion    *uint64
   176  	WriteMeta
   177  }
   178  
   179  // DeploymentAllocHealthRequest is used to set the health of a set of
   180  // allocations as part of a deployment.
   181  type DeploymentAllocHealthRequest struct {
   182  	DeploymentID string
   183  
   184  	// Marks these allocations as healthy, allow further allocations
   185  	// to be rolled.
   186  	HealthyAllocationIDs []string
   187  
   188  	// Any unhealthy allocations fail the deployment
   189  	UnhealthyAllocationIDs []string
   190  
   191  	WriteRequest
   192  }
   193  
   194  // DeploymentPromoteRequest is used to promote task groups in a deployment
   195  type DeploymentPromoteRequest struct {
   196  	DeploymentID string
   197  
   198  	// All is to promote all task groups
   199  	All bool
   200  
   201  	// Groups is used to set the promotion status per task group
   202  	Groups []string
   203  
   204  	WriteRequest
   205  }
   206  
   207  // DeploymentPauseRequest is used to pause a deployment
   208  type DeploymentPauseRequest struct {
   209  	DeploymentID string
   210  
   211  	// Pause sets the pause status
   212  	Pause bool
   213  
   214  	WriteRequest
   215  }
   216  
   217  // DeploymentSpecificRequest is used to make a request specific to a particular
   218  // deployment
   219  type DeploymentSpecificRequest struct {
   220  	DeploymentID string
   221  	QueryOptions
   222  }
   223  
   224  // DeploymentFailRequest is used to fail a particular deployment
   225  type DeploymentFailRequest struct {
   226  	DeploymentID string
   227  	WriteRequest
   228  }
   229  
   230  // SingleDeploymentResponse is used to respond with a single deployment
   231  type SingleDeploymentResponse struct {
   232  	Deployment *Deployment
   233  	QueryMeta
   234  }