/
stack.go
436 lines (380 loc) · 12.7 KB
/
stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//: ----------------------------------------------------------------------------
//: Copyright (C) 2017 Verizon. All Rights Reserved.
//:
//: Licensed under the Apache License, Version 2.0 (the "License");
//: you may not use this file except in compliance with the License.
//: You may obtain a copy of the License at
//:
//: http://www.apache.org/licenses/LICENSE-2.0
//:
//: Unless required by applicable law or agreed to in writing, software
//: distributed under the License is distributed on an "AS IS" BASIS,
//: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//: See the License for the specific language governing permissions and
//: limitations under the License.
//:
//: ----------------------------------------------------------------------------
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/fatih/color"
"github.com/parnurzeal/gorequest"
"strconv"
)
/////////////////// MANUAL DEPLOYMENT ///////////////////
/*
* {
* "datacenter": "perryman",
* "namespace": "stage",
* "serviceType": "cassandra",
* "version": "1.2.3,
* "hash": "abcd1234",
* "description": "a cassandra for great good",
* "port": 1234
* }
*/
type ManualDeploymentRequest struct {
Datacenter string `json:"datacenter"`
Namespace string `json:"namespace"`
ServiceType string `json:"service_type"`
Version string `json:"version"`
Hash string `json:"hash"`
Port int64 `json:"port"`
Description string `json:"description"`
}
func RegisterManualDeployment(
req ManualDeploymentRequest,
http *gorequest.SuperAgent, cfg *Config) (string, []error) {
r, body, errs := AugmentRequest(
http.Post(cfg.Endpoint+"/v1/deployments"), cfg).Send(req).EndBytes()
if errs != nil {
return "", errs
}
if r.StatusCode/100 != 3 {
resp := string(body[:])
errs = append(errs, errors.New("Unexpected response from Nelson server"))
return resp, errs
} else {
return "Manual stack has been registered.", errs
}
}
/////////////////// INSPECT ///////////////////
/*
* {
* "timestamp": "2016-06-28T20:02:34.449Z",
* "message": "instructing perryman's chronos to handle job container",
* "status": "deploying"
* }
*/
type StackStatus struct {
Timestamp string `json:"timestamp"`
Message string `json:"message"`
Status string `json:"status"`
}
/*
* "dependencies": {
* "outbound": [
* ...
* ],
* "inbound": []
* },
*/
type StackDependencies struct {
Outbound []Stack `json:"outbound"`
Inbound []Stack `json:"inbound"`
}
/*
* {
* "workflow": "pulsar",
* "guid": "e4184c271bb9",
* "statuses": [
* {
* "timestamp": "2016-07-14T22:30:22.358Z",
* "message": "inventory-inventory deployed to perryman",
* "status": "ready"
* },
* ...
* ],
* "stack_name": "inventory-inventory--2-0-11--8gufie2b",
* "deployed_at": 1468535384221,
* "unit": "inventory-inventory",
* "plan": "service",
* "expiration": 1469928212871,
* "dependencies": {
* "outbound": [
* {
* "workflow": "manual",
* "guid": "1a69395e919d",
* "stack_name": "dev-iptv-cass-dev--4-8-4--mtq2odqzndc0mg",
* "deployed_at": 1468518896093,
* "unit": "dev-iptv-cass-dev",
* "plan": "service"
* }
* ],
* "inbound": []
* },
* "namespace": "dev"
* }
*/
type StackSummary struct {
Workflow string `json:"workflow"`
Guid string `json:"guid"`
StackName string `json:"stack_name"`
DeployedAt int64 `json:"deployed_at"`
UnitName string `json:"unit"`
Plan string `json:"plan"`
NamespaceRef string `json:"namespace"`
Expiration int64 `json:"expiration"`
Statuses []StackStatus `json:"statuses"`
Dependencies StackDependencies `json:"dependencies"`
Resources []string `json:"resources"`
}
func InspectStack(guid string, http *gorequest.SuperAgent, cfg *Config) (result StackSummary, err []error) {
r, bytes, errs := AugmentRequest(
http.Get(cfg.Endpoint+"/v1/deployments/"+guid), cfg).EndBytes()
if errs != nil {
return StackSummary{}, errs
}
if r.StatusCode/100 != 2 {
errs = append(errs, errors.New("Bad response from Nelson server"))
// zomg, constructor just nulls *all* the fields, because who cares about correctness!
return StackSummary{}, errs
} else {
var result StackSummary
if err := json.Unmarshal(bytes, &result); err != nil {
panic(err)
}
return result, errs
}
}
func PrintInspectStack(s StackSummary) {
//>>>>>>>>>>> status history
var tabulized = [][]string{}
tabulized = append(tabulized, []string{"GUID:", s.Guid})
tabulized = append(tabulized, []string{"STACK NAME:", s.StackName})
tabulized = append(tabulized, []string{"NAMESPACE:", s.NamespaceRef})
tabulized = append(tabulized, []string{"PLAN:", s.Plan})
tabulized = append(tabulized, []string{"WORKFLOW:", s.Workflow})
tabulized = append(tabulized, []string{"DEPLOYED AT:", JavaEpochToDateStr(s.DeployedAt)})
tabulized = append(tabulized, []string{"EXPIRES AT:", JavaEpochToDateStr(s.Expiration)})
if len(s.Resources) != 0 {
resources := ""
for i, r := range s.Resources {
if i == 0 {
resources = r
} else {
resources = resources + ", " + r
}
}
tabulized = append(tabulized, []string{"RESOURCES:", resources})
}
fmt.Println("===>> Stack Information")
RenderTableToStdout([]string{"Paramater", "Value"}, tabulized)
//>>>>>>>>>>> dependency information
green := color.New(color.FgGreen).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
if (len(s.Dependencies.Outbound) + len(s.Dependencies.Inbound)) != 0 {
fmt.Println("") // give us a new line for spacing
fmt.Println("===>> Dependencies")
var dependencies = [][]string{}
if len(s.Dependencies.Outbound) != 0 {
for _, w := range s.Dependencies.Outbound {
dependencies = append(dependencies, []string{w.Guid, w.StackName, w.Type, JavaEpochToDateStr(w.DeployedAt), yellow("OUTBOUND"), strconv.FormatInt(w.Weight, 10)})
}
}
if len(s.Dependencies.Inbound) != 0 {
for _, w := range s.Dependencies.Inbound {
dependencies = append(dependencies, []string{w.Guid, w.StackName, w.Type, JavaEpochToDateStr(w.DeployedAt), green("INBOUND"), strconv.FormatInt(w.Weight, 10)})
}
}
RenderTableToStdout([]string{"GUID", "Stack", "Type", "Deployed At", "Direction", "Weight"}, dependencies)
}
//>>>>>>>>>>> status history
fmt.Println("") // give us a new line for spacing
fmt.Println("===>> Status History")
var statuslines = [][]string{}
for _, o := range s.Statuses {
statuslines = append(statuslines, []string{o.Status, o.Timestamp, o.Message})
}
RenderTableToStdout([]string{"Status", "Timestamp", "Message"}, statuslines)
}
/////////////////// REDEPLOYMENT ///////////////////
func Redeploy(guid string, http *gorequest.SuperAgent, cfg *Config) (str string, err []error) {
r, body, errs := AugmentRequest(
http.Post(cfg.Endpoint+"/v1/deployments/"+guid+"/redeploy"), cfg).EndBytes()
if errs != nil {
return "", errs
}
if r.StatusCode/100 != 2 {
resp := string(body[:])
errs = append(errs, errors.New("bad response from Nelson server"))
return resp, errs
} else {
return "Redeployment requested.", errs
}
}
/////////////////// LISTING STACKS ///////////////////
/**
* {
* "workflow": "quasar",
* "guid": "67e04d28d6ab",
* "stack_name": "blobstore-testsuite--0-1-55--kbqg9nff",
* "deployed_at": 1467225866870,
* "unit": "blobstore-testsuite",
* "plan": "fooo",
* "namespace": "dev"
* }
*/
type Stack struct {
Workflow string `json:"workflow"`
Guid string `json:"guid"`
StackName string `json:"stack_name"`
DeployedAt int64 `json:"deployed_at"`
UnitName string `json:"unit"`
Plan string `json:"plan"`
Type string `json:"type,omitempty"`
NamespaceRef string `json:"namespace,omitempty"`
Status string `json:"status"`
Weight int64 `json:"weight,omitempty"`
}
func ListStacks(delimitedDcs string, delimitedNamespaces string, delimitedStatuses string, http *gorequest.SuperAgent, cfg *Config) (list []Stack, err []error) {
uri := "/v1/deployments?"
// set the datacenters if specified
if isValidCommaDelimitedList(delimitedDcs) {
uri = uri + "dc=" + delimitedDcs + "&"
}
if isValidCommaDelimitedList(delimitedStatuses) {
uri = uri + "status=" + delimitedStatuses + "&"
} else {
// if the user didnt specify statuses, they probally want all the stacks except historical terminated ones.
uri = uri + "status=pending,deploying,warming,ready,deprecated,failed&"
}
if isValidCommaDelimitedList(delimitedNamespaces) {
uri = uri + "ns=" + delimitedNamespaces
} else {
uri = uri + "ns=dev,qa,prod"
}
r, bytes, errs := AugmentRequest(
http.Get(cfg.Endpoint+uri), cfg).EndBytes()
if errs != nil {
return nil, errs
}
if r.StatusCode/100 != 2 {
errs = append(errs, errors.New("Bad response from Nelson server"))
return nil, errs
} else {
var list []Stack
if err := json.Unmarshal(bytes, &list); err != nil {
panic(err)
}
return list, errs
}
}
func PrintListStacks(stacks []Stack) {
var tabulized = [][]string{}
for _, s := range stacks {
tabulized = append(tabulized, []string{s.Guid, s.NamespaceRef, truncateString(s.StackName, 55), s.Status, s.Plan, s.Workflow, javaEpochToHumanizedTime(s.DeployedAt)})
}
RenderTableToStdout([]string{"GUID", "Namespace", "Stack", "Status", "Plan", "Workflow", "Deployed At"}, tabulized)
}
/////////////////// DEPLOYMENT LOG ///////////////////
type StackLog struct {
Content []string `json:"content"`
Offset int `json:"offset"`
}
// v1/deployments/:id/log
func GetDeploymentLog(guid string, http *gorequest.SuperAgent, cfg *Config) {
_, bytes, errs := AugmentRequest(
http.Get(cfg.Endpoint+"/v1/deployments/"+guid+"/log"), cfg).EndBytes()
if len(errs) > 0 {
panic(errs)
}
var logs StackLog
if err := json.Unmarshal(bytes, &logs); err != nil {
panic(err)
}
fmt.Println("===>> logs for stack " + guid)
for _, l := range logs.Content {
fmt.Println(l)
}
}
/////////////////// RUNTIME INSPECT ///////////////////
/*
* {
* "consul_health": [{
* "check_id": "64c0a2b5bd2972c9521fb7313b00db7dad58c04c",
* "node": "ip-10-113-128-190",
* "status": "passing",
* "name": "service: default howdy-http--1-0-344--9uuu1mp2 check"
* }],
* "scheduler": {
* "failed": 0,
* "completed": 0,
* "pending": 0,
* "running": 1
* },
* "current_status": "ready",
* "expires_at" : 10101333
* }
*/
type StackRuntime struct {
CurrentStatus string `json:"current_status"`
ExpiresAt int64 `json:"expires_at"`
Scheduler StackRuntimeScheduler `json:"scheduler"`
ConsulHealth []StackRuntimeHealth `json:"consul_health"`
}
type StackRuntimeHealth struct {
CheckId string `json:"check_id"`
Node string `json:"node"`
Status string `json:"status"`
Name string `json:"name"`
}
type StackRuntimeScheduler struct {
Failed int `json:"failed"`
Completed int `json:"completed"`
Pending int `json:"pending"`
Running int `json:"running"`
}
func GetStackRuntime(guid string, http *gorequest.SuperAgent, cfg *Config) (runtime StackRuntime, err []error) {
r, bytes, errs := AugmentRequest(
http.Get(cfg.Endpoint+"/v1/deployments/"+guid+"/runtime"), cfg).EndBytes()
if errs != nil {
return StackRuntime{}, errs
}
if r.StatusCode/100 != 2 {
errs = append(errs, errors.New("bad response from Nelson server"))
return runtime, errs
} else {
if err := json.Unmarshal(bytes, &runtime); err != nil {
panic(err)
}
return runtime, errs
}
}
func PrintStackRuntime(r StackRuntime) {
fmt.Println("")
fmt.Println("==>> Stack Status")
var tabulized = [][]string{}
tabulized = append(tabulized, []string{"STATUS:", r.CurrentStatus})
tabulized = append(tabulized, []string{"EXPIRES:", JavaEpochToDateStr(r.ExpiresAt)})
RenderTableToStdout([]string{"Paramater", "Value"}, tabulized)
fmt.Println("")
// >>>>>>>>> Scheudler Summary
tabulized = [][]string{}
tabulized = append(tabulized, []string{"PENDING:", strconv.Itoa(r.Scheduler.Pending)})
tabulized = append(tabulized, []string{"RUNNING:", strconv.Itoa(r.Scheduler.Running)})
tabulized = append(tabulized, []string{"COMPLETED:", strconv.Itoa(r.Scheduler.Completed)})
tabulized = append(tabulized, []string{"FAILED:", strconv.Itoa(r.Scheduler.Failed)})
fmt.Println("==>> Scheudler")
RenderTableToStdout([]string{"Paramater", "Value"}, tabulized)
// >>>>>>>>> Consul Health
tabulized = [][]string{}
for _, s := range r.ConsulHealth {
tabulized = append(tabulized, []string{s.CheckId, s.Node, s.Status, s.Name})
}
fmt.Println("")
fmt.Println("==>> Health Checks")
RenderTableToStdout([]string{"ID", "Node", "Status", "Details"}, tabulized)
}