forked from canonical/lxd
/
list.go
210 lines (179 loc) · 4.3 KB
/
list.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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/gosexy/gettext"
"github.com/lxc/lxd"
"github.com/lxc/lxd/shared"
"github.com/olekukonko/tablewriter"
)
type listCmd struct{}
func (c *listCmd) showByDefault() bool {
return true
}
func (c *listCmd) usage() string {
return gettext.Gettext(
"Lists the available resources.\n" +
"\n" +
"lxc list [resource] [filters]\n" +
"\n" +
"The filters are:\n" +
"* A single keyword like \"web\" which will list any container with \"web\" in its name.\n" +
"* A key/value pair referring to a configuration item. For those, the namespace can be abreviated to the smallest unambiguous identifier:\n" +
"* \"user.blah=abc\" will list all containers with the \"blah\" user property set to \"abc\"\n" +
"* \"u.blah=abc\" will do the same\n" +
"* \"security.privileged=1\" will list all privileged containers\n" +
"* \"s.privileged=1\" will do the same\n")
}
func (c *listCmd) flags() {}
// This seems a little excessive.
func dotPrefixMatch(short string, full string) bool {
fullMembs := strings.Split(full, ".")
shortMembs := strings.Split(short, ".")
if len(fullMembs) != len(shortMembs) {
return false
}
for i, _ := range fullMembs {
if !strings.HasPrefix(fullMembs[i], shortMembs[i]) {
return false
}
}
return true
}
func shouldShow(filters []string, state *shared.ContainerState) bool {
for _, filter := range filters {
if strings.Contains(filter, "=") {
membs := strings.SplitN(filter, "=", 2)
key := membs[0]
var value string
if len(membs) < 2 {
value = ""
} else {
value = membs[1]
}
found := false
for configKey, configValue := range state.Config {
if dotPrefixMatch(key, configKey) {
if value == configValue {
found = true
break
} else {
// the property was found but didn't match
return false
}
}
}
if !found {
return false
}
} else {
if !strings.Contains(state.Name, filter) {
return false
}
}
}
return true
}
func listContainers(client *lxd.Client, cts []string, filters []string, listsnaps bool) error {
var snaps []string
data := [][]string{}
for _, ct := range cts {
// get more information
c, err := client.ContainerStatus(ct)
d := []string{}
if err == nil {
d = []string{ct, c.Status.State}
} else if err == lxd.LXDErrors[http.StatusNotFound] {
continue
} else {
return err
}
if !shouldShow(filters, c) {
continue
}
if c.Status.State == "RUNNING" {
ipv4s := []string{}
ipv6s := []string{}
for _, ip := range c.Status.Ips {
if ip.Interface == "lo" {
continue
}
if ip.Protocol == "IPV6" {
ipv6s = append(ipv6s, ip.Address)
} else {
ipv4s = append(ipv4s, ip.Address)
}
}
ipv4 := strings.Join(ipv4s, ", ")
ipv6 := strings.Join(ipv6s, ", ")
d = append(d, ipv4)
d = append(d, ipv6)
} else {
d = append(d, "")
d = append(d, "")
}
if c.Ephemeral {
d = append(d, "YES")
} else {
d = append(d, "NO")
}
// List snapshots
snaps, err = client.ListSnapshots(ct)
if err != nil {
d = append(d, fmt.Sprintf("(ERR)"))
} else {
d = append(d, fmt.Sprintf("%d", len(snaps)))
}
data = append(data, d)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"NAME", "STATE", "IPV4", "IPV6", "EPHEMERAL", "SNAPSHOTS"})
for _, v := range data {
table.Append(v)
}
table.Render() // Send output
if listsnaps && len(cts) == 1 {
first_snapshot := true
for _, snap := range snaps {
if first_snapshot {
fmt.Printf("Snapshots:\n")
}
fmt.Printf(" %s\n", snap)
first_snapshot = false
}
}
return nil
}
func (c *listCmd) run(config *lxd.Config, args []string) error {
var remote string
name := ""
filters := []string{}
if len(args) == 0 {
remote = config.DefaultRemote
} else {
filters = args
if strings.Contains(args[0], ":") {
remote, name = config.ParseRemoteAndContainer(args[0])
filters = args[1:]
} else if !strings.Contains(args[0], "=") {
remote = config.DefaultRemote
name = args[0]
}
}
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
var cts []string
if name == "" {
cts, err = d.ListContainers()
if err != nil {
return err
}
} else {
cts = []string{name}
}
return listContainers(d, cts, filters, name != "")
}