-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
706 lines (673 loc) · 24.4 KB
/
main.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// Copyright 2018 Oliver Szabo
//
// 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 (
"bytes"
"encoding/json"
"fmt"
"github.com/oleewere/ambarictl/ambari"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
"io/ioutil"
"os"
"os/user"
"strconv"
"strings"
)
// Version that will be generated during the build as a constant
var Version string
// GitRevString that will be generated during the build as a constant - represents git revision value
var GitRevString string
func main() {
app := cli.NewApp()
app.Name = "ambarictl"
app.Usage = "CLI tool for handle Ambari clusters"
app.EnableBashCompletion = true
app.UsageText = "ambarictl command [command options] [arguments...]"
if len(Version) > 0 {
app.Version = Version
} else {
app.Version = "0.1.0"
}
if len(GitRevString) > 0 {
app.Version = app.Version + fmt.Sprintf(" (git short hash: %v)", GitRevString)
}
app.Commands = []cli.Command{}
initCommand := cli.Command{
Name: "init",
Usage: "Initialize Ambari server database",
Action: func(c *cli.Context) error {
ambari.CreateAmbariRegistryDb()
fmt.Println("Ambari registry DB has been initialized.")
return nil
},
}
listCommand := cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "Print all registered Ambari servers",
Action: func(c *cli.Context) error {
ambariServerEntries := ambari.ListAmbariRegistryEntries()
var tableData [][]string
for _, ambariServer := range ambariServerEntries {
activeValue := "false"
if ambariServer.Active {
activeValue = "true"
}
tableData = append(tableData, []string{ambariServer.Name, ambariServer.Hostname, strconv.Itoa(ambariServer.Port), ambariServer.Protocol,
ambariServer.Username, "********", ambariServer.Cluster, ambariServer.ConnectionProfile, activeValue})
}
printTable("AMBARI REGISTRIES:", []string{"Name", "HOSTNAME", "PORT", "PROTOCOL", "USER", "PASSWORD", "CLUSTER", "PROFILE", "ACTIVE"}, tableData, c)
return nil
},
}
profileCommand := cli.Command{
Name: "profiles",
Usage: "Connection profiles related commands",
Subcommands: []cli.Command{
{
Name: "create",
Aliases: []string{"c"},
Usage: "Create new connection profile",
Action: func(c *cli.Context) error {
name := ambari.GetStringFlag(c.String("name"), "", "Enter connection profile name")
connProfileId := ambari.GetConnectionProfileEntryId(name)
if len(connProfileId) > 0 {
fmt.Println("Connection profile entry already exists with id " + name)
os.Exit(1)
}
keyPath := ambari.GetStringFlag(c.String("key_path"), "", "Enter ssh key path")
usr, err := user.Current()
if err != nil {
panic(err)
}
home := usr.HomeDir
keyPath = strings.Replace(keyPath, "~", home, -1)
if len(keyPath) > 0 {
if _, err := os.Stat(keyPath); err != nil {
if os.IsNotExist(err) {
fmt.Println(err)
os.Exit(1)
}
}
}
portStr := ambari.GetStringFlag(c.String("port"), "22", "Enter ssh port")
port, err := strconv.Atoi(portStr)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
userName := ambari.GetStringFlag(c.String("username"), "root", "Enter ssh username")
hostJumpStr := ambari.GetStringFlag(c.String("host_jump"), "n", "Use host jump?")
hostJump := ambari.EvaluateBoolValueFromString(hostJumpStr)
proxyAddress := ""
if hostJump {
proxyAddress = ambari.GetStringFlag(c.String("proxy_address"), "none", "Set a proxy address?")
if proxyAddress == "none" {
proxyAddress = ""
}
}
ambari.RegisterNewConnectionProfile(name, keyPath, port, userName, hostJump, proxyAddress)
fmt.Println("New connection profile entry has been created: " + name)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "Name of the Ambari server entry"},
cli.StringFlag{Name: "key_path", Usage: "Hostname of the Ambari server"},
cli.StringFlag{Name: "port", Usage: "Port for AmbarisServer"},
cli.StringFlag{Name: "username", Usage: "Protocol for Ambar REST API: http/https"},
cli.StringFlag{Name: "host_jump", Usage: "User name for Ambari server"},
cli.StringFlag{Name: "proxy_address", Usage: "Password for Ambari user"},
},
},
{
Name: "list",
Aliases: []string{"ls"},
Usage: "Print all connection profile entries",
Action: func(c *cli.Context) error {
connectionProfiles := ambari.ListConnectionProfileEntries()
var tableData [][]string
for _, profile := range connectionProfiles {
hostJump := "false"
if profile.HostJump {
hostJump = "true"
}
tableData = append(tableData, []string{profile.Name, profile.KeyPath, strconv.Itoa(profile.Port), profile.Username, hostJump, profile.ProxyAddress})
}
printTable("CONNECTION PROFILES:", []string{"NAME", "KEY", "PORT", "USERNAME", "HOST JUMP", "PROXY ADDRESS"}, tableData, c)
return nil
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete a connection profile entry by id",
Action: func(c *cli.Context) error {
if len(c.Args()) == 0 {
fmt.Println("Provide a profile name argument for use command. e.g.: delete vagrant")
os.Exit(1)
}
name := c.Args().First()
profileEntryId := ambari.GetConnectionProfileEntryId(name)
if len(profileEntryId) == 0 {
fmt.Println("Connection profile entry does not exist with id " + name)
os.Exit(1)
}
ambari.DeRegisterConnectionProfile(profileEntryId)
msg := fmt.Sprintf("Connection profile '%s' has been deleted successfully", profileEntryId)
fmt.Println(msg)
return nil
},
},
{
Name: "clear",
Aliases: []string{"cl"},
Usage: "Delete all connection profile entries",
Action: func(c *cli.Context) error {
ambari.DropConnectionProfileRecords()
fmt.Println("All connection profile records has been dropped")
return nil
},
},
},
}
attachCommand := cli.Command{
Name: "attach",
Usage: "Attach a profile to an ambari server entry",
Action: func(c *cli.Context) error {
args := c.Args()
if len(args) == 0 {
fmt.Println("Provide at least 1 argument (<profile>), or 2 (<profile> and <ambariEntry>)")
os.Exit(1)
}
profileId := args.Get(0)
var ambariRegistry ambari.AmbariRegistry
if len(args) == 1 {
ambariRegistry = ambari.GetActiveAmbari()
if len(ambariRegistry.Name) == 0 {
fmt.Println("No active ambari selected")
os.Exit(1)
}
} else {
ambariRegistryId := args.Get(1)
ambari.GetAmbariById(ambariRegistryId)
if len(ambariRegistry.Name) == 0 {
fmt.Println("Cannot find specific ambari server entry")
os.Exit(1)
}
}
profile := ambari.GetConnectionProfileById(profileId)
if len(profile.Name) == 0 {
fmt.Println("Cannot find specific connection profile entry")
os.Exit(1)
}
ambari.SetProfileIdForAmbariEntry(ambariRegistry.Name, profile.Name)
msg := fmt.Sprintf("Attach profile '%s' to '%s'", profile.Name, ambariRegistry.Name)
fmt.Println(msg)
return nil
},
}
listAgentsCommand := cli.Command{
Name: "hosts",
Usage: "Print all registered Ambari agent hosts",
Action: func(c *cli.Context) error {
ambariRegistry := ambari.GetActiveAmbari()
validateActiveAmbari(ambariRegistry)
hosts := ambariRegistry.ListAgents()
var tableData [][]string
for _, host := range hosts {
tableData = append(tableData, []string{host.PublicHostname, host.IP, host.OSType, host.OSArch, strconv.FormatBool(host.UnlimitedJCE), host.HostState})
}
printTable("HOSTS:", []string{"PUBLIC HOSTNAME", "IP", "OS TYPE", "OS ARCH", "UNLIMITED_JCE", "STATE"}, tableData, c)
return nil
},
}
listServicesCommand := cli.Command{
Name: "services",
Usage: "Print all installed Ambari services",
Action: func(c *cli.Context) error {
ambariRegistry := ambari.GetActiveAmbari()
validateActiveAmbari(ambariRegistry)
services := ambariRegistry.ListServices()
var tableData [][]string
for _, service := range services {
tableData = append(tableData, []string{service.ServiceName, service.ServiceState})
}
printTable("SERVICES:", []string{"NAME", "STATE"}, tableData, c)
return nil
},
}
listComponentsCommand := cli.Command{
Name: "components",
Usage: "Print all installed Ambari components",
Action: func(c *cli.Context) error {
ambariRegistry := ambari.GetActiveAmbari()
validateActiveAmbari(ambariRegistry)
components := ambariRegistry.ListComponents()
var tableData [][]string
for _, component := range components {
tableData = append(tableData, []string{component.ComponentName, component.ServiceName, component.ComponentState})
}
printTable("COMPONENTS:", []string{"NAME", "SERVICE", "STATE"}, tableData, c)
return nil
},
}
listHostComponentsCommand := cli.Command{
Name: "hcomponents",
Usage: "Print all installed Ambari host components by component name",
Action: func(c *cli.Context) error {
ambariRegistry := ambari.GetActiveAmbari()
validateActiveAmbari(ambariRegistry)
var param string
useHost := false
if len(c.String("component")) > 0 {
param = c.String("component")
} else if len(c.String("host")) > 0 {
param = c.String("host")
useHost = true
} else {
fmt.Println("Flag '--component' or `--host`with a value is required for 'host-components' action!")
os.Exit(1)
}
components := ambariRegistry.ListHostComponents(param, useHost)
var tableData [][]string
for _, hostComponent := range components {
tableData = append(tableData, []string{hostComponent.HostComponentName, hostComponent.HostComponntHost, hostComponent.HostComponentState})
}
printTable("HOST COMPONENTS: "+param, []string{"NAME", "HOST", "STATE"}, tableData, c)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "component", Usage: "Component filter for host components"},
cli.StringFlag{Name: "host", Usage: "Host name filter for host components"},
},
}
createCommand := cli.Command{
Name: "create",
Usage: "Register new Ambari server entry",
Action: func(c *cli.Context) error {
name := ambari.GetStringFlag(c.String("name"), "", "Enter ambari registry name")
ambariEntryId := ambari.GetAmbariEntryId(name)
if len(ambariEntryId) > 0 {
fmt.Println("Ambari registry entry already exists with id " + name)
os.Exit(1)
}
host := ambari.GetStringFlag(c.String("host"), "", "Enter ambari host name")
portStr := ambari.GetStringFlag(c.String("port"), "8080", "Enter ambari port")
port, err := strconv.Atoi(portStr)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
protocol := strings.ToLower(ambari.GetStringFlag(c.String("protocol"), "http", "Enter ambari protocol"))
if protocol != "http" && protocol != "https" {
fmt.Println("Use 'http' or 'https' value for protocol option")
os.Exit(1)
}
username := strings.ToLower(ambari.GetStringFlag(c.String("username"), "admin", "Enter ambari user"))
password := ambari.GetPassword(c.String("password"), "Enter ambari user password")
cluster := ambari.GetStringFlag(c.String("cluster"), "", "Enter ambari cluster")
ambari.DeactiveAllAmbariRegistry()
ambari.RegisterNewAmbariEntry(name, host, port, protocol,
username, password, cluster)
fmt.Println("New Ambari server entry has been created: " + name)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "Name of the Ambari server entry"},
cli.StringFlag{Name: "host", Usage: "Hostname of the Ambari server"},
cli.StringFlag{Name: "port", Usage: "Port for AmbarisServer"},
cli.StringFlag{Name: "protocol", Usage: "Protocol for Ambar REST API: http/https"},
cli.StringFlag{Name: "username", Usage: "User name for Ambari server"},
cli.StringFlag{Name: "password", Usage: "Password for Ambari user"},
cli.StringFlag{Name: "cluster", Usage: "Cluster name"},
},
}
deleteCommand := cli.Command{
Name: "delete",
Usage: "De-register an existing Ambari server entry",
Action: func(c *cli.Context) error {
if len(c.Args()) == 0 {
fmt.Println("Provide a registry name argument for use command. e.g.: delete vagrant")
os.Exit(1)
}
name := c.Args().First()
ambariEntryId := ambari.GetAmbariEntryId(name)
if len(ambariEntryId) == 0 {
fmt.Println("Ambari registry entry does not exist with id " + name)
os.Exit(1)
}
ambari.DeRegisterAmbariEntry(name)
fmt.Println("Ambari registry de-registered with id: " + name)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "name of the Ambari registry entry"},
},
}
useCommand := cli.Command{
Name: "use",
Usage: "Use selected Ambari server",
Action: func(c *cli.Context) error {
if len(c.Args()) == 0 {
fmt.Println("Provide a server entry name argument for use command. e.g.: use vagrant")
os.Exit(1)
}
name := c.Args().First()
ambariEntryId := ambari.GetAmbariEntryId(name)
if len(ambariEntryId) == 0 {
fmt.Println("Ambari server entry does not exist with id " + name)
os.Exit(1)
}
ambari.DeactiveAllAmbariRegistry()
ambari.ActiveAmbariRegistry(name)
fmt.Println("Ambari server entry selected with id: " + name)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "name of the Ambari registry entry"},
},
}
clearCommand := cli.Command{
Name: "clear",
Usage: "Drop all Ambari server records",
Action: func(c *cli.Context) error {
ambari.DropAmbariRegistryRecords()
fmt.Println("Ambari server entries dropped.")
return nil
},
}
showCommand := cli.Command{
Name: "show",
Usage: "Show active Ambari server details",
Action: func(c *cli.Context) error {
ambariRegistry := ambari.GetActiveAmbari()
var tableData [][]string
if len(ambariRegistry.Name) > 0 {
tableData = append(tableData, []string{ambariRegistry.Name, ambariRegistry.Hostname, strconv.Itoa(ambariRegistry.Port), ambariRegistry.Protocol,
ambariRegistry.Username, "********", ambariRegistry.Cluster, ambariRegistry.ConnectionProfile, "true"})
}
printTable("ACTIVE AMBARI REGISTRY:", []string{"Name", "HOSTNAME", "PORT", "PROTOCOL", "USER", "PASSWORD", "CLUSTER", "PROFILE", "ACTIVE"}, tableData, c)
return nil
},
}
configsCommand := cli.Command{
Name: "configs",
Usage: "Operations with Ambari service configurations",
Subcommands: []cli.Command{
{
Name: "versions",
Usage: "Print all service config types with versions",
Action: func(c *cli.Context) error {
ambariRegistry := ambari.GetActiveAmbari()
validateActiveAmbari(ambariRegistry)
configs := ambariRegistry.ListServiceConfigVersions()
var tableData [][]string
for _, config := range configs {
tableData = append(tableData, []string{config.ServiceConfigType, strconv.FormatFloat(config.ServiceConfigVersion, 'f', -1, 64), config.ServiceConfigTag})
}
printTable("SERVICE_CONFIGS:", []string{"TYPE", "VERSION", "TAG"}, tableData, c)
return nil
},
},
{
Name: "update",
Usage: "Update config value for a specific config key of a config type",
Action: func(c *cli.Context) error {
ambariRegistry := ambari.GetActiveAmbari()
validateActiveAmbari(ambariRegistry)
if len(c.String("config-type")) == 0 {
fmt.Println("Parameter '--config-type' is required")
os.Exit(1)
}
if len(c.String("config-key")) == 0 {
fmt.Println("Parameter '--config-key' is required")
os.Exit(1)
}
if len(c.String("config-value")) == 0 {
fmt.Println("Parameter '--config-value' is required")
os.Exit(1)
}
ambariRegistry.SetConfig(c.String("type"), c.String("key"), c.String("value"))
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "type, t", Usage: "Configuration type"},
cli.StringFlag{Name: "key, k", Usage: "Configuration key"},
cli.StringFlag{Name: "value, v", Usage: "Configuration value"},
},
},
{
Name: "export",
Usage: "Export cluster configuration to a blueprint json",
Action: func(c *cli.Context) error {
ambariRegistry := ambari.GetActiveAmbari()
validateActiveAmbari(ambariRegistry)
var blueprint []byte
if c.Bool("minimal") {
clusterInfo := ambariRegistry.GetClusterInfo()
if len(clusterInfo.ClusterVersion) > 0 {
splittedString := strings.Split(clusterInfo.ClusterVersion, "-")
stackName := splittedString[0]
stackVersion := splittedString[1]
stackDefaults := ambariRegistry.GetStackDefaultConfigs(stackName, stackVersion)
largeBlueprint := ambariRegistry.ExportBlueprintAsMap()
blueprint = ambariRegistry.GetMinimalBlueprint(largeBlueprint, stackDefaults)
if len(c.String("file")) > 0 {
err := ioutil.WriteFile(c.String("file"), formatJson(blueprint).Bytes(), 0644)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
}
} else {
fmt.Println("Cannot find a cluster with a name and version for Ambari servrer")
os.Exit(1)
}
} else {
blueprint = ambariRegistry.ExportBlueprint()
if len(c.String("file")) > 0 {
err := ioutil.WriteFile(c.String("file"), blueprint, 0644)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
}
}
printJson(blueprint)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "file, f", Usage: "File output for the generated JSON"},
cli.BoolFlag{Name: "minimal, m", Usage: "Use minimal configuration"},
},
},
},
}
clusterCommand := cli.Command{
Name: "cluster",
Usage: "Print Ambari managed cluster details",
Action: func(c *cli.Context) error {
ambariRegistry := ambari.GetActiveAmbari()
validateActiveAmbari(ambariRegistry)
clusterInfo := ambariRegistry.GetClusterInfo()
var tableData [][]string
if len(ambariRegistry.Name) > 0 {
tableData = append(tableData, []string{clusterInfo.ClusterName, clusterInfo.ClusterVersion, clusterInfo.ClusterSecurityType, strconv.FormatFloat(clusterInfo.ClusterTotalHosts, 'f', -1, 64)})
}
printTable("CLUSTER INFO:", []string{"Name", "VERSION", "SECURITY", "TOTAL HOSTS"}, tableData, c)
return nil
},
}
runCommand := cli.Command{
Name: "run",
Usage: "Execute commands on all (or specific) hosts",
Action: func(c *cli.Context) error {
ambariServer := ambari.GetActiveAmbari()
validateActiveAmbari(ambariServer)
args := c.Args()
command := ""
for _, arg := range args {
command += arg
}
filter := ambari.CreateFilter(strings.ToUpper(c.String("services")),
strings.ToUpper(c.String("components")), c.String("hosts"), c.Bool("server"))
hosts := ambariServer.GetFilteredHosts(filter)
ambariServer.RunRemoteHostCommand(command, hosts, filter.Server)
return nil
},
Flags: []cli.Flag{
cli.BoolFlag{Name: "server", Usage: "Filter on ambari-server"},
cli.StringFlag{Name: "services, s", Usage: "Filter on services (comma separated)"},
cli.StringFlag{Name: "components, c", Usage: "Filter on components (comma separated)"},
cli.StringFlag{Name: "hosts", Usage: "Filter on hosts (comma separated)"},
},
}
commandCommand := cli.Command{
Name: "command",
Usage: "Execute ambari commands on Ambari server (START/STOP/RESTART/SERVICE_CHECK)",
Action: func(c *cli.Context) error {
ambariServer := ambari.GetActiveAmbari()
validateActiveAmbari(ambariServer)
args := c.Args()
command := ""
for _, arg := range args {
command += arg
}
if len(c.String("services")) == 0 && len(c.String("components")) == 0 {
fmt.Println("It is required to provide --components (-c) or --services (-s) flag")
os.Exit(1)
}
if command == "SERVICE_CHECK" && len(c.String("services")) == 0 {
fmt.Println("Service check can be performed only on services, not components")
os.Exit(1)
}
filter := ambari.CreateFilter(strings.ToUpper(c.String("services")),
strings.ToUpper(c.String("components")), "", false)
ambariServer.RunAmbariServiceCommand(command, filter, len(filter.Services) > 0, len(filter.Components) > 0)
if len(c.String("components")) > 0 {
fmt.Println(fmt.Sprintf("Command %s has been sent to %s (components)", command, c.String("components")))
} else if len(c.String("services")) > 0 {
fmt.Println(fmt.Sprintf("Command %s has been sent to %s (services)", command, c.String("services")))
}
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "services, s", Usage: "Filter on services (comma separated)"},
cli.StringFlag{Name: "components, c", Usage: "Filter on components (comma separated)"},
},
}
playbookCommand := cli.Command{
Name: "playbook",
Usage: "Execute a list of commands defined in playbook file(s)",
Action: func(c *cli.Context) error {
ambariServer := ambari.GetActiveAmbari()
validateActiveAmbari(ambariServer)
if len(c.String("file")) == 0 {
fmt.Println("Provide -f or --file parameter")
os.Exit(1)
}
playbook := ambari.LoadPlaybookFile(c.String("file"), c.String("vars"))
ambariServer.ExecutePlaybook(playbook)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "file, f", Usage: "Playbook file"},
cli.StringFlag{Name: "vars, v", Usage: "Provided extra variables (e.g.: --vars='myvar1=myvalue1 myvar2=myvalue2')"},
},
}
logsCommand := cli.Command{
Name: "logs",
Usage: "Download logs from Ambari agents",
Action: func(c *cli.Context) error {
ambariServer := ambari.GetActiveAmbari()
if len(c.String("destination")) == 0 {
fmt.Println("Provide --destination parameter")
os.Exit(1)
}
filter := ambari.CreateFilter(strings.ToUpper(c.String("services")),
strings.ToUpper(c.String("components")), c.String("hosts"), c.Bool("server"))
ambariServer.DownloadLogs(c.String("destination"), filter)
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "destination, d", Usage: "Download destination"},
cli.BoolFlag{Name: "server", Usage: "Download server logs flag"},
cli.StringFlag{Name: "services, s", Usage: "Filter on services (comma separated)"},
cli.StringFlag{Name: "components, c", Usage: "Filter on components (comma separated)"},
cli.StringFlag{Name: "hosts", Usage: "Filter on hosts (comma separated)"},
},
}
app.Commands = append(app.Commands, initCommand)
app.Commands = append(app.Commands, createCommand)
app.Commands = append(app.Commands, deleteCommand)
app.Commands = append(app.Commands, useCommand)
app.Commands = append(app.Commands, showCommand)
app.Commands = append(app.Commands, runCommand)
app.Commands = append(app.Commands, commandCommand)
app.Commands = append(app.Commands, playbookCommand)
app.Commands = append(app.Commands, profileCommand)
app.Commands = append(app.Commands, attachCommand)
app.Commands = append(app.Commands, listCommand)
app.Commands = append(app.Commands, listAgentsCommand)
app.Commands = append(app.Commands, listServicesCommand)
app.Commands = append(app.Commands, listComponentsCommand)
app.Commands = append(app.Commands, listHostComponentsCommand)
app.Commands = append(app.Commands, configsCommand)
app.Commands = append(app.Commands, clusterCommand)
app.Commands = append(app.Commands, logsCommand)
app.Commands = append(app.Commands, clearCommand)
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func printTable(title string, headers []string, data [][]string, c *cli.Context) {
fmt.Println(title)
if len(data) > 0 {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(headers)
for _, v := range data {
table.Append(v)
}
table.Render()
} else {
for i := 1; i <= len(title); i++ {
fmt.Print("-")
}
fmt.Println()
fmt.Println("NO ENTRIES FOUND!")
}
}
func printJson(b []byte) {
fmt.Println(formatJson(b).String())
}
func formatJson(b []byte) *bytes.Buffer {
var out bytes.Buffer
err := json.Indent(&out, b, "", " ")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return &out
}
func validateActiveAmbari(ambariServer ambari.AmbariRegistry) {
if len(ambariServer.Name) == 0 {
fmt.Println("No active ambari server selected. (see 'use' command)")
os.Exit(1)
}
}