/
ucp_containers.go
163 lines (146 loc) · 4.26 KB
/
ucp_containers.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
package cmd
import (
"fmt"
"os"
"strings"
"text/tabwriter"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/joeabbey/diver/pkg/ucp"
)
func init() {
ucpContainerProcesses.Flags().StringVar(&id, "id", "", "Container ID to retrieve processes from")
ucpContainerNetworks.Flags().StringVar(&id, "id", "", "Container ID to retrieve processes from")
ucpContainerGet.AddCommand(ucpContainerProcesses)
ucpContainerGet.AddCommand(ucpContainerNetworks)
// Sub commands
ucpContainer.AddCommand(ucpContainerTop)
ucpContainer.AddCommand(ucpContainerList)
ucpContainer.AddCommand(ucpContainerGet)
UCPRoot.AddCommand(ucpContainer)
}
var ucpContainer = &cobra.Command{
Use: "containers",
Short: "Interact with containers",
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.Level(logLevel))
cmd.Help()
},
}
var ucpContainerTop = &cobra.Command{
Use: "top",
Short: "A list of containers and their CPU usage like the top command on linux",
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.Level(logLevel))
client, err := ucp.ReadToken()
if err != nil {
// Fatal error if can't read the token
log.Fatalf("%v", err)
}
err = client.ContainerTop()
if err != nil {
log.Fatalf("%v", err)
}
return
},
}
var ucpContainerList = &cobra.Command{
Use: "list",
Short: "List all containers across all nodes in UCP",
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.Level(logLevel))
client, err := ucp.ReadToken()
if err != nil {
// Fatal error if can't read the token
log.Fatalf("%v", err)
}
containers, err := client.GetAllContainers()
if err != nil {
log.Fatalf("%v", err)
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, tabPadding, ' ', 0)
fmt.Fprintln(w, "Name\tID\tImage\tNode")
for i := range containers {
// String returned from UCP is /<node>/<container name>
splitUCPName := strings.Split(containers[i].Names[0], "/")
// Image name is image:tag@sha256
splitImageSha := strings.Split(containers[i].Image, "@")
// Shrink the imageID to a manageble size
shrunkID := fmt.Sprintf("%.16s", containers[i].ID)
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", splitUCPName[2], shrunkID, splitImageSha[0], splitUCPName[1])
}
w.Flush()
},
}
var ucpContainerGet = &cobra.Command{
Use: "get",
Short: "Retrieve specific information about a container",
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.Level(logLevel))
cmd.Help()
},
}
var ucpContainerNetworks = &cobra.Command{
Use: "networks",
Short: "List all networks a running container is attached too",
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.Level(logLevel))
client, err := ucp.ReadToken()
if err != nil {
// Fatal error if can't read the token
log.Fatalf("%v", err)
}
if id == "" {
log.Fatalf("No Container ID specified")
}
container, err := client.GetContainerFromID(id)
if err != nil {
log.Fatalf("%v", err)
}
if container.NetworkSettings == nil {
log.Info("No networks")
}
networks := container.NetworkSettings.Networks
w := tabwriter.NewWriter(os.Stdout, 0, 0, tabPadding, ' ', 0)
fmt.Fprintln(w, "Name\tID\tAddress")
for name, network := range networks {
fmt.Fprintf(w, "%s\t%s\t%s\n", name, network.NetworkID, network.IPAddress)
}
w.Flush()
},
}
var ucpContainerProcesses = &cobra.Command{
Use: "processes",
Short: "List all processes in a running container",
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.Level(logLevel))
client, err := ucp.ReadToken()
if err != nil {
// Fatal error if can't read the token
log.Fatalf("%v", err)
}
if id == "" {
log.Fatalf("No Container ID specified")
}
processes, err := client.GetContainerProcesses(id)
if err != nil {
log.Fatalf("%v", err)
}
log.Debugf("Found %d process lines", len(processes.Processes))
w := tabwriter.NewWriter(os.Stdout, 0, 0, tabPadding, ' ', 0)
// Prepare Title headers
for i := range processes.Titles {
fmt.Fprintf(w, "%s\t", processes.Titles[i])
}
// End of line for title
fmt.Fprintf(w, "\n")
// Process lines
for i := range processes.Processes {
for x := range processes.Processes[i] {
fmt.Fprintf(w, "%s\t", processes.Processes[i][x])
}
fmt.Fprintf(w, "\n")
}
w.Flush() // Flush adds a \n
},
}