github.com/anuvu/nomad@v0.8.7-atom1/api/deployments.go (about)

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