forked from ehazlett/stellar
/
cluster.go
89 lines (78 loc) · 1.97 KB
/
cluster.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
package main
import (
"context"
"fmt"
"os"
"text/tabwriter"
"time"
"github.com/codegangsta/cli"
humanize "github.com/dustin/go-humanize"
clusterapi "github.com/ehazlett/stellar/api/services/cluster/v1"
"github.com/sirupsen/logrus"
)
var clusterCommand = cli.Command{
Name: "cluster",
Usage: "interact with cluster",
Subcommands: []cli.Command{
clusterContainersCommand,
clusterNodesCommand,
},
}
var clusterContainersCommand = cli.Command{
Name: "containers",
Usage: "container management",
Action: func(c *cli.Context) error {
client, err := getClient(c)
if err != nil {
return err
}
defer client.Close()
resp, err := client.ClusterService().Containers(context.Background(), &clusterapi.ContainersRequest{})
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
fmt.Fprintf(w, "ID\tIMAGE\tRUNTIME\tNODE\n")
for _, c := range resp.Containers {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", c.Container.ID, c.Container.Image, c.Container.Runtime, c.Node.Name)
}
w.Flush()
return nil
},
}
var clusterNodesCommand = cli.Command{
Name: "nodes",
Usage: "cluster node management",
Action: func(c *cli.Context) error {
cl, err := getClient(c)
if err != nil {
return err
}
defer cl.Close()
nodes, err := cl.Cluster().Health()
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
fmt.Fprintf(w, "NAME\tADDR\tOS\tUPTIME\tCPUS\tMEMORY (USED)\n")
for _, nodeHealth := range nodes {
node := nodeHealth.Node
health := nodeHealth.Health
started, err := health.Started()
if err != nil {
logrus.Error(err)
continue
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\t%s\n",
node.Name,
node.Addr,
health.OSName+" ("+health.OSVersion+")",
humanize.RelTime(started, time.Now(), "", ""),
health.Cpus,
fmt.Sprintf("%s / %s", humanize.Bytes(uint64(health.MemoryUsed)), humanize.Bytes(uint64(health.MemoryTotal))),
)
}
w.Flush()
return nil
},
}