github.com/jrxfive/nomad@v0.6.1-0.20170802162750-1fef470e89bf/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 evaluations. 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 evaluation 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 JobID string 129 JobVersion uint64 130 JobModifyIndex uint64 131 JobCreateIndex uint64 132 TaskGroups map[string]*DeploymentState 133 Status string 134 StatusDescription string 135 CreateIndex uint64 136 ModifyIndex uint64 137 } 138 139 // DeploymentState tracks the state of a deployment for a given task group. 140 type DeploymentState struct { 141 PlacedCanaries []string 142 AutoRevert bool 143 Promoted bool 144 DesiredCanaries int 145 DesiredTotal int 146 PlacedAllocs int 147 HealthyAllocs int 148 UnhealthyAllocs int 149 } 150 151 // DeploymentIndexSort is a wrapper to sort deployments by CreateIndex. We 152 // reverse the test so that we get the highest index first. 153 type DeploymentIndexSort []*Deployment 154 155 func (d DeploymentIndexSort) Len() int { 156 return len(d) 157 } 158 159 func (d DeploymentIndexSort) Less(i, j int) bool { 160 return d[i].CreateIndex > d[j].CreateIndex 161 } 162 163 func (d DeploymentIndexSort) Swap(i, j int) { 164 d[i], d[j] = d[j], d[i] 165 } 166 167 // DeploymentUpdateResponse is used to respond to a deployment change. The 168 // response will include the modify index of the deployment as well as details 169 // of any triggered evaluation. 170 type DeploymentUpdateResponse struct { 171 EvalID string 172 EvalCreateIndex uint64 173 DeploymentModifyIndex uint64 174 RevertedJobVersion *uint64 175 WriteMeta 176 } 177 178 // DeploymentAllocHealthRequest is used to set the health of a set of 179 // allocations as part of a deployment. 180 type DeploymentAllocHealthRequest struct { 181 DeploymentID string 182 183 // Marks these allocations as healthy, allow further allocations 184 // to be rolled. 185 HealthyAllocationIDs []string 186 187 // Any unhealthy allocations fail the deployment 188 UnhealthyAllocationIDs []string 189 190 WriteRequest 191 } 192 193 // DeploymentPromoteRequest is used to promote task groups in a deployment 194 type DeploymentPromoteRequest struct { 195 DeploymentID string 196 197 // All is to promote all task groups 198 All bool 199 200 // Groups is used to set the promotion status per task group 201 Groups []string 202 203 WriteRequest 204 } 205 206 // DeploymentPauseRequest is used to pause a deployment 207 type DeploymentPauseRequest struct { 208 DeploymentID string 209 210 // Pause sets the pause status 211 Pause bool 212 213 WriteRequest 214 } 215 216 // DeploymentSpecificRequest is used to make a request specific to a particular 217 // deployment 218 type DeploymentSpecificRequest struct { 219 DeploymentID string 220 QueryOptions 221 } 222 223 // DeploymentFailRequest is used to fail a particular deployment 224 type DeploymentFailRequest struct { 225 DeploymentID string 226 WriteRequest 227 } 228 229 // SingleDeploymentResponse is used to respond with a single deployment 230 type SingleDeploymentResponse struct { 231 Deployment *Deployment 232 QueryMeta 233 }