/
applications.go
568 lines (505 loc) · 14.1 KB
/
applications.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package main
import (
"context"
"fmt"
"io"
"math"
"os"
"os/exec"
"strings"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/chef/automate/api/external/applications"
"github.com/chef/automate/components/automate-cli/pkg/client/apiclient"
"github.com/chef/automate/components/automate-cli/pkg/docs"
"github.com/chef/automate/components/automate-deployment/pkg/cli"
)
const (
SHOW_COMMAND = "chef-automate applications show-svcs"
)
type IExecutor interface {
execCommand(string) (string, error)
runCommandOnSingleAutomateNode(cmd *cobra.Command, args []string) (string, error)
}
type Executor struct{}
func init() {
appsSubcmd := newApplicationsRootSubcmd()
appsSubcmd.AddCommand(newApplicationsShowSvcsCmd())
appsSubcmd.AddCommand(newApplicationsRemoveSvcsCmd())
RootCmd.AddCommand(appsSubcmd)
}
func newApplicationsRootSubcmd() *cobra.Command {
return &cobra.Command{
Use: "applications COMMAND",
Short: "Manage applications observability features",
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
}
type applicationsServiceFilters struct {
disconnected bool
origin string
serviceName string
version string
channel string
application string
environment string
site string
buildTimestamp string
groupName string
}
// FilterApplied returns true if any member of applicationsServiceFilters is
// not the default value, i.e., the user has applied at least one filter
// criterion.
func (a applicationsServiceFilters) FilterApplied() bool {
stringVals := []string{
a.origin,
a.serviceName,
a.version,
a.channel,
a.application,
a.environment,
a.site,
a.buildTimestamp,
a.groupName,
}
for _, value := range stringVals {
if value != "" {
return true
}
}
if a.disconnected {
return true
}
return false
}
var applicationsServiceFiltersFlags = applicationsServiceFilters{}
func addFilteringFlagsToCmd(cmd *cobra.Command) {
cmd.PersistentFlags().BoolVarP(
&applicationsServiceFiltersFlags.disconnected,
"disconnected",
"D",
false,
"Select only services that are disconnected",
)
cmd.PersistentFlags().StringVarP(
&applicationsServiceFiltersFlags.origin,
"origin",
"o",
"",
"Select only services where the origin matches the given pattern",
)
cmd.PersistentFlags().StringVarP(
&applicationsServiceFiltersFlags.serviceName,
"service-name",
"n",
"",
"Select only services where the name matches the given pattern",
)
cmd.PersistentFlags().StringVarP(
&applicationsServiceFiltersFlags.version,
"version",
"v",
"",
"Select only services where the package version matches the given pattern",
)
cmd.PersistentFlags().StringVarP(
&applicationsServiceFiltersFlags.channel,
"channel",
"c",
"",
"Select only services where the subscribed channel matches the given pattern",
)
cmd.PersistentFlags().StringVarP(
&applicationsServiceFiltersFlags.application,
"application",
"a",
"",
"Select only services where the application name matches the given pattern",
)
cmd.PersistentFlags().StringVarP(
&applicationsServiceFiltersFlags.environment,
"environment",
"e",
"",
"Select only services where the application environment matches the given pattern",
)
cmd.PersistentFlags().StringVarP(
&applicationsServiceFiltersFlags.site,
"site",
"s",
"",
"Select only services where the site matches the given pattern",
)
cmd.PersistentFlags().StringVarP(
&applicationsServiceFiltersFlags.buildTimestamp,
"buildstamp",
"b",
"",
"Select only services where the buildstamp matches the given pattern",
)
cmd.PersistentFlags().StringVarP(
&applicationsServiceFiltersFlags.groupName,
"group",
"g",
"",
"Select only services where the group name (suffix) matches the given pattern",
)
}
func newApplicationsShowSvcsCmd() *cobra.Command {
c := &cobra.Command{
Use: "show-svcs",
Short: "Show services in the applications database",
Long: `
Display a list of the habitat services stored in the applications database.
`,
RunE: runApplicationsShowSvcsCmd,
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
addFilteringFlagsToCmd(c)
return c
}
type removeSvcsOptions struct {
all bool
yes bool
}
var removeSvcsFlags = removeSvcsOptions{}
func newApplicationsRemoveSvcsCmd() *cobra.Command {
c := &cobra.Command{
Use: "remove-svcs",
Short: "Remove services from the applications database",
Long: `
Remove services from the applications database.
You must fully decommission services by retiring physical hardware, terminating
the VM or container, or by using 'hab svc unload', before using the
'remove-svcs' command. Services that are incompletely decommissioned will send
a health-check at the appointed time and Automate will re-add them to the
services database.
`,
RunE: runApplicationsRemoveSvcsCmd,
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
addFilteringFlagsToCmd(c)
c.PersistentFlags().BoolVar(
&removeSvcsFlags.all,
"all",
false,
"Delete all services in the database. This flag must be given if no other filter is given.",
)
c.PersistentFlags().BoolVarP(
&removeSvcsFlags.yes,
"yes",
"y",
false,
"Delete the services without a confirmation prompt",
)
return c
}
func makeServicesReqWithFilters() *applications.ServicesReq {
req := &applications.ServicesReq{}
flags := applicationsServiceFiltersFlags
// disconnected bool
if flags.disconnected {
req.Filter = append(req.Filter, "status:disconnected")
}
// origin string
if flags.origin != "" {
req.Filter = append(req.Filter, fmt.Sprintf("origin:%s", flags.origin))
}
// serviceName string
if flags.serviceName != "" {
req.Filter = append(req.Filter, fmt.Sprintf("service:%s", flags.serviceName))
}
// version string
if flags.version != "" {
req.Filter = append(req.Filter, fmt.Sprintf("version:%s", flags.version))
}
// channel string
if flags.channel != "" {
req.Filter = append(req.Filter, fmt.Sprintf("channel:%s", flags.channel))
}
// application string
if flags.application != "" {
req.Filter = append(req.Filter, fmt.Sprintf("application:%s", flags.application))
}
// environment string
if flags.environment != "" {
req.Filter = append(req.Filter, fmt.Sprintf("environment:%s", flags.environment))
}
// site string
if flags.site != "" {
req.Filter = append(req.Filter, fmt.Sprintf("site:%s", flags.site))
}
// buildTimestamp string
if flags.buildTimestamp != "" {
req.Filter = append(req.Filter, fmt.Sprintf("buildstamp:%s", flags.buildTimestamp))
}
// groupName string
if flags.groupName != "" {
req.Filter = append(req.Filter, fmt.Sprintf("group:%s", flags.groupName))
}
return req
}
func showApplicationsHA(cmd *cobra.Command, args []string, e IExecutor) error {
output, err := e.runCommandOnSingleAutomateNode(cmd, args)
if err != nil {
return err
}
writer.Print(output)
return nil
}
func showApplicationsStandalone() error {
s := &serviceSet{
applicationsServiceFilters: applicationsServiceFiltersFlags,
}
err := s.Connect()
if err != nil {
return err
}
err = s.Load()
if err != nil {
return err
}
err = s.PrintTSV()
if err != nil {
return err
}
return nil
}
func runApplicationsShowSvcsCmd(cmd *cobra.Command, args []string) error {
if isA2HARBFileExist() {
if err := showApplicationsHA(cmd, args, Executor{}); err != nil {
return err
}
} else {
if err := showApplicationsStandalone(); err != nil {
return err
}
}
return nil
}
func showAndPrompt(cmd *cobra.Command, e IExecutor, w *cli.Writer) ([]string, bool, error) {
var splittedString []string
flags := GetEnabledFlags(cmd, map[string]int{"all": 1})
showCmd := SHOW_COMMAND + flags
output, err := e.execCommand(showCmd)
if err != nil {
return splittedString, false, err
}
splittedString = strings.Split(output, "\n")
// splittedString always contains 2 lines no matter services matches the criteria or not
// first line is table header and last line is empty line.
// >2 means it's containing the services which matches the criteria.
if len(splittedString) > 2 {
for i := 0; i < len(splittedString); i++ {
fmt.Println(splittedString[i])
}
prompt := fmt.Sprintf("The above %d services will be deleted. Do you wish to continue?", len(splittedString)-2)
proceed, err := w.Confirm(prompt)
if err != nil {
return splittedString, false, err
}
if !proceed {
return splittedString, false, nil
}
}
return splittedString, true, nil
}
func removeApplicationsHA(cmd *cobra.Command, args []string, e IExecutor, w *cli.Writer) error {
var splittedString []string
// If user is not passing -y flag, then first we need to show the list with given conditions and then
// give confirmation prompt
if !removeSvcsFlags.yes {
var proceed bool
var err error
splittedString, proceed, err = showAndPrompt(cmd, e, w)
if err != nil {
return err
}
if !proceed {
return nil
}
}
args = append(args, "-y")
output, err := e.runCommandOnSingleAutomateNode(cmd, args)
if err != nil {
return err
}
// len(splittedString) == 2 means list is empty it's just containing table header and empty line which means we haven't
// got any services to remove
if removeSvcsFlags.yes || len(splittedString) == 2 {
writer.Println(output)
} else {
writer.Println(fmt.Sprintf("Removed %d services", len(splittedString)-2))
}
return nil
}
func removeApplicationsStandalone() error {
s := &serviceSet{
applicationsServiceFilters: applicationsServiceFiltersFlags,
}
err := s.Connect()
if err != nil {
return err
}
err = s.Load()
if err != nil {
return err
}
if len(s.services) < 1 {
fmt.Fprintln(os.Stderr, "No services match the criteria, nothing to delete")
return nil
}
err = s.PrintTSV()
if err != nil {
return err
}
if !removeSvcsFlags.yes {
fmt.Println("")
prompt := fmt.Sprintf("The above %d services will be deleted. Do you wish to continue?", len(s.services))
proceed, err := writer.Confirm(prompt)
if err != nil {
return err
}
if !proceed {
return nil
}
}
err = s.RemoveSet()
if err != nil {
return err
}
return nil
}
func runApplicationsRemoveSvcsCmd(cmd *cobra.Command, args []string) error {
if !applicationsServiceFiltersFlags.FilterApplied() && !removeSvcsFlags.all {
return errors.New("You must filter the services to be deleted or pass the --all flag to delete all services")
}
if isA2HARBFileExist() {
if err := removeApplicationsHA(cmd, args, Executor{}, writer); err != nil {
return err
}
} else {
if err := removeApplicationsStandalone(); err != nil {
return err
}
}
return nil
}
type serviceSet struct {
applicationsServiceFilters
appsClient applications.ApplicationsServiceClient
services []*applications.Service
ctx context.Context
}
func (e Executor) execCommand(cmd string) (string, error) {
output, err := exec.Command("/bin/sh", "-c", cmd).Output()
if err != nil {
return "", err
}
return string(output), nil
}
func (e Executor) runCommandOnSingleAutomateNode(cmd *cobra.Command, args []string) (string, error) {
return RunCmdOnSingleAutomateNode(cmd, args)
}
func (s *serviceSet) Connect() error {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
apiClient, err := apiclient.OpenConnection(ctx)
if err != nil {
return err
}
s.ctx = ctx
s.appsClient = apiClient.ApplicationsClient()
return nil
}
func (s *serviceSet) Load() error {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
req := makeServicesReqWithFilters()
streamIn, err := s.appsClient.FindServices(ctx, req)
if err != nil {
return err
}
for {
svc, err := streamIn.Recv()
if err == io.EOF {
break
}
if err != nil {
return err
}
s.services = append(s.services, svc)
}
return nil
}
// PrintTSV prints a plain text table of the services
// - Header: match the widths to the first row so it looks ok
// - Header: print it to stderr so you can do easy shell redirection/pipes for text
// processing
// - Print table rows as TSV for easier processing, even though the columns
// won't align all the time.
func (s *serviceSet) PrintTSV() error {
maxIDLength, maxGroupLength, maxReleaseLength, maxFqdnLength, maxApplicationLength, maxEnvironmentLength, maxStatusLabelLength := 0, 0, 0, 0, 0, 0, 0
for _, t := range s.services {
maxIDLength = int(math.Max(float64(maxIDLength), float64(len(t.Id))))
maxGroupLength = int(math.Max(float64(maxGroupLength), float64(len(t.Group))))
maxReleaseLength = int(math.Max(float64(maxReleaseLength), float64(len(t.Release))))
maxFqdnLength = int(math.Max(float64(maxFqdnLength), float64(len(t.Fqdn))))
maxApplicationLength = int(math.Max(float64(maxApplicationLength), float64(len(t.Application))))
maxEnvironmentLength = int(math.Max(float64(maxEnvironmentLength), float64(len(t.Environment))))
maxStatusLabelLength = int(math.Max(float64(maxStatusLabelLength), float64(len(s.statusLabelFor(t)))))
}
// calculate a format string with fixed width, right padded strings for each
// of the columns
fmtString := fmt.Sprintf("%%-%ds\t%%-%ds\t%%-%ds\t%%-%ds\t%%-%ds\t%%-%ds\n",
maxIDLength,
maxGroupLength,
maxReleaseLength,
maxFqdnLength,
maxApplicationLength+maxEnvironmentLength+1,
maxStatusLabelLength,
)
fmt.Printf(fmtString, "id", "svc.group", "release", "FQDN", "app:env", "status")
for _, svc := range s.services {
fmt.Printf(fmtString,
svc.Id,
svc.Group,
svc.Release,
svc.Fqdn,
svc.Application+":"+svc.Environment,
s.statusLabelFor(svc),
)
}
return nil
}
func (s *serviceSet) RemoveSet() error {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
toRemove := make([]string, len(s.services))
for i, svc := range s.services {
toRemove[i] = svc.Id
}
req := &applications.DeleteServicesByIDReq{
Ids: toRemove,
}
res, err := s.appsClient.DeleteServicesByID(ctx, req)
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "Removed %d services\n", len(res.GetServices()))
return nil
}
func (s *serviceSet) statusLabelFor(svc *applications.Service) string {
if svc.Disconnected {
return "DISCONNECTED"
} else {
return svc.HealthCheck.String()
}
}