/
list.go
209 lines (185 loc) · 5.69 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
// Copyright 2015 The rkt Authors
//
// 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.
//+build linux
package main
import (
"bytes"
"fmt"
"strings"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/schema/lastditch"
"github.com/coreos/rkt/Godeps/_workspace/src/github.com/spf13/cobra"
common "github.com/coreos/rkt/common"
"github.com/coreos/rkt/networking/netinfo"
)
var (
cmdList = &cobra.Command{
Use: "list",
Short: "List pods",
Run: runWrapper(runList),
}
flagNoLegend bool
flagFullOutput bool
)
func init() {
cmdRkt.AddCommand(cmdList)
cmdList.Flags().BoolVar(&flagNoLegend, "no-legend", false, "suppress a legend with the list")
cmdList.Flags().BoolVar(&flagFullOutput, "full", false, "use long output format")
}
func runList(cmd *cobra.Command, args []string) int {
var errors []error
tabBuffer := new(bytes.Buffer)
tabOut := getTabOutWithWriter(tabBuffer)
if !flagNoLegend {
if flagFullOutput {
fmt.Fprintf(tabOut, "UUID\tAPP\tIMAGE NAME\tIMAGE ID\tSTATE\tNETWORKS\n")
} else {
fmt.Fprintf(tabOut, "UUID\tAPP\tIMAGE NAME\tSTATE\tNETWORKS\n")
}
}
if err := walkPods(includeMostDirs, func(p *pod) {
pm := schema.PodManifest{}
if !p.isPreparing && !p.isAbortedPrepare && !p.isExitedDeleting {
// TODO(vc): we should really hold a shared lock here to prevent gc of the pod
pmf, err := p.readFile(common.PodManifestPath(""))
if err != nil {
errors = append(errors, newPodListReadError(p, err))
return
}
if err := pm.UnmarshalJSON(pmf); err != nil {
errors = append(errors, newPodListLoadError(p, err, pmf))
return
}
if len(pm.Apps) == 0 {
errors = append(errors, newPodListZeroAppsError(p))
return
}
}
type printedApp struct {
uuid string
appName string
imgName string
imgID string
state string
nets string
}
var appsToPrint []printedApp
uuid := p.uuid.String()
state := p.getState()
nets := fmtNets(p.nets)
if !flagFullOutput {
uuid = uuid[:8]
}
for _, app := range pm.Apps {
imageName := "--"
if app.Image.Name != nil {
imageName = app.Image.Name.String()
if version, ok := app.Image.Labels.Get("version"); ok {
imageName = fmt.Sprintf("%s:%s", imageName, version)
}
}
var imageID string
if flagFullOutput {
imageID = app.Image.ID.String()[:19]
}
appsToPrint = append(appsToPrint, printedApp{
uuid: uuid,
appName: app.Name.String(),
imgName: imageName,
imgID: imageID,
state: state,
nets: nets,
})
// clear those variables so they won't be
// printed for another apps in the pod as they
// are actually describing a pod, not an app
uuid = ""
state = ""
nets = ""
}
// if we reached that point, then it means that the
// pod and all its apps are valid, so they can be
// printed
for _, app := range appsToPrint {
if flagFullOutput {
fmt.Fprintf(tabOut, "%s\t%s\t%s\t%s\t%s\t%s\n", app.uuid, app.appName, app.imgName, app.imgID, app.state, app.nets)
} else {
fmt.Fprintf(tabOut, "%s\t%s\t%s\t%s\t%s\n", app.uuid, app.appName, app.imgName, app.state, app.nets)
}
}
}); err != nil {
stderr("Failed to get pod handles: %v", err)
return 1
}
if len(errors) > 0 {
sep := "----------------------------------------"
stderr("%d error(s) encountered when listing pods:", len(errors))
stderr("%s", sep)
for _, err := range errors {
stderr("%s", err.Error())
stderr("%s", sep)
}
stderr("Misc:")
stderr(" rkt's appc version: %s", schema.AppContainerVersion)
stderr("%s", sep)
// make a visible break between errors and the listing
stderr("")
}
tabOut.Flush()
stdout("%s", tabBuffer.String())
return 0
}
func newPodListReadError(p *pod, err error) error {
lines := []string{
fmt.Sprintf("Unable to read pod %s manifest:", p.uuid.String()),
fmt.Sprintf(" %v", err),
}
return fmt.Errorf("%s", strings.Join(lines, "\n"))
}
func newPodListLoadError(p *pod, err error, pmj []byte) error {
lines := []string{
fmt.Sprintf("Unable to load pod %s manifest, because it is invalid:", p.uuid.String()),
fmt.Sprintf(" %v", err),
}
pm := lastditch.PodManifest{}
if err := pm.UnmarshalJSON(pmj); err != nil {
lines = append(lines, " Also, failed to get any information about invalid pod manifest:")
lines = append(lines, fmt.Sprintf(" %v", err))
} else {
if len(pm.Apps) > 0 {
lines = append(lines, "Objects related to this error:")
for _, app := range pm.Apps {
lines = append(lines, fmt.Sprintf(" %s", appLine(app)))
}
} else {
lines = append(lines, "No other objects related to this error")
}
}
return fmt.Errorf("%s", strings.Join(lines, "\n"))
}
func newPodListZeroAppsError(p *pod) error {
return fmt.Errorf("Pod %s contains zero apps", p.uuid.String())
}
func appLine(app lastditch.RuntimeApp) string {
return fmt.Sprintf("App: %q from image %q (%s)",
app.Name, app.Image.Name, app.Image.ID)
}
func fmtNets(nis []netinfo.NetInfo) string {
var parts []string
for _, ni := range nis {
// there will be IPv6 support soon so distinguish between v4 and v6
parts = append(parts, fmt.Sprintf("%v:ip4=%v", ni.NetName, ni.IP))
}
return strings.Join(parts, ", ")
}