-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect.go
188 lines (166 loc) · 5.95 KB
/
inspect.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
package service
import (
"fmt"
"io"
"strings"
"time"
"golang.org/x/net/context"
"github.com/docker/docker/api/client"
"github.com/docker/docker/api/client/inspect"
"github.com/docker/docker/cli"
"github.com/docker/docker/pkg/ioutils"
apiclient "github.com/docker/engine-api/client"
"github.com/docker/engine-api/types/swarm"
"github.com/docker/go-units"
"github.com/spf13/cobra"
)
type inspectOptions struct {
refs []string
format string
pretty bool
}
func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
var opts inspectOptions
cmd := &cobra.Command{
Use: "inspect [OPTIONS] SERVICE [SERVICE...]",
Short: "Display detailed information on one or more services",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.refs = args
if opts.pretty && len(opts.format) > 0 {
return fmt.Errorf("--format is incompatible with human friendly format")
}
return runInspect(dockerCli, opts)
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format.")
return cmd
}
func runInspect(dockerCli *client.DockerCli, opts inspectOptions) error {
client := dockerCli.Client()
ctx := context.Background()
getRef := func(ref string) (interface{}, []byte, error) {
service, _, err := client.ServiceInspectWithRaw(ctx, ref)
if err == nil || !apiclient.IsErrServiceNotFound(err) {
return service, nil, err
}
return nil, nil, fmt.Errorf("Error: no such service: %s", ref)
}
if !opts.pretty {
return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRef)
}
return printHumanFriendly(dockerCli.Out(), opts.refs, getRef)
}
func printHumanFriendly(out io.Writer, refs []string, getRef inspect.GetRefFunc) error {
for idx, ref := range refs {
obj, _, err := getRef(ref)
if err != nil {
return err
}
printService(out, obj.(swarm.Service))
// TODO: better way to do this?
// print extra space between objects, but not after the last one
if idx+1 != len(refs) {
fmt.Fprintf(out, "\n\n")
}
}
return nil
}
// TODO: use a template
func printService(out io.Writer, service swarm.Service) {
fmt.Fprintf(out, "ID:\t\t%s\n", service.ID)
fmt.Fprintf(out, "Name:\t\t%s\n", service.Spec.Name)
if service.Spec.Labels != nil {
fmt.Fprintln(out, "Labels:")
for k, v := range service.Spec.Labels {
fmt.Fprintf(out, " - %s=%s\n", k, v)
}
}
if service.Spec.Mode.Global != nil {
fmt.Fprintln(out, "Mode:\t\tGlobal")
} else {
fmt.Fprintln(out, "Mode:\t\tReplicated")
if service.Spec.Mode.Replicated.Replicas != nil {
fmt.Fprintf(out, " Replicas:\t%d\n", *service.Spec.Mode.Replicated.Replicas)
}
}
if service.UpdateStatus.State != "" {
fmt.Fprintln(out, "Update status:")
fmt.Fprintf(out, " State:\t\t%s\n", service.UpdateStatus.State)
fmt.Fprintf(out, " Started:\t%s ago\n", strings.ToLower(units.HumanDuration(time.Since(service.UpdateStatus.StartedAt))))
if service.UpdateStatus.State == swarm.UpdateStateCompleted {
fmt.Fprintf(out, " Completed:\t%s ago\n", strings.ToLower(units.HumanDuration(time.Since(service.UpdateStatus.CompletedAt))))
}
fmt.Fprintf(out, " Message:\t%s\n", service.UpdateStatus.Message)
}
fmt.Fprintln(out, "Placement:")
if service.Spec.TaskTemplate.Placement != nil && len(service.Spec.TaskTemplate.Placement.Constraints) > 0 {
ioutils.FprintfIfNotEmpty(out, " Constraints\t: %s\n", strings.Join(service.Spec.TaskTemplate.Placement.Constraints, ", "))
}
if service.Spec.UpdateConfig != nil {
fmt.Fprintf(out, "UpdateConfig:\n")
fmt.Fprintf(out, " Parallelism:\t%d\n", service.Spec.UpdateConfig.Parallelism)
if service.Spec.UpdateConfig.Delay.Nanoseconds() > 0 {
fmt.Fprintf(out, " Delay:\t\t%s\n", service.Spec.UpdateConfig.Delay)
}
fmt.Fprintf(out, " On failure:\t%s\n", service.Spec.UpdateConfig.FailureAction)
}
fmt.Fprintf(out, "ContainerSpec:\n")
printContainerSpec(out, service.Spec.TaskTemplate.ContainerSpec)
resources := service.Spec.TaskTemplate.Resources
if resources != nil {
fmt.Fprintln(out, "Resources:")
printResources := func(out io.Writer, requirement string, r *swarm.Resources) {
if r == nil || (r.MemoryBytes == 0 && r.NanoCPUs == 0) {
return
}
fmt.Fprintf(out, " %s:\n", requirement)
if r.NanoCPUs != 0 {
fmt.Fprintf(out, " CPU:\t\t%g\n", float64(r.NanoCPUs)/1e9)
}
if r.MemoryBytes != 0 {
fmt.Fprintf(out, " Memory:\t%s\n", units.BytesSize(float64(r.MemoryBytes)))
}
}
printResources(out, "Reservations", resources.Reservations)
printResources(out, "Limits", resources.Limits)
}
if len(service.Spec.Networks) > 0 {
fmt.Fprintf(out, "Networks:")
for _, n := range service.Spec.Networks {
fmt.Fprintf(out, " %s", n.Target)
}
fmt.Fprintln(out, "")
}
if len(service.Endpoint.Ports) > 0 {
fmt.Fprintln(out, "Ports:")
for _, port := range service.Endpoint.Ports {
ioutils.FprintfIfNotEmpty(out, " Name = %s\n", port.Name)
fmt.Fprintf(out, " Protocol = %s\n", port.Protocol)
fmt.Fprintf(out, " TargetPort = %d\n", port.TargetPort)
fmt.Fprintf(out, " PublishedPort = %d\n", port.PublishedPort)
}
}
}
func printContainerSpec(out io.Writer, containerSpec swarm.ContainerSpec) {
fmt.Fprintf(out, " Image:\t\t%s\n", containerSpec.Image)
if len(containerSpec.Args) > 0 {
fmt.Fprintf(out, " Args:\t\t%s\n", strings.Join(containerSpec.Args, " "))
}
if len(containerSpec.Env) > 0 {
fmt.Fprintf(out, " Env:\t\t%s\n", strings.Join(containerSpec.Env, " "))
}
ioutils.FprintfIfNotEmpty(out, " Dir\t\t%s\n", containerSpec.Dir)
ioutils.FprintfIfNotEmpty(out, " User\t\t%s\n", containerSpec.User)
if len(containerSpec.Mounts) > 0 {
fmt.Fprintln(out, " Mounts:")
for _, v := range containerSpec.Mounts {
fmt.Fprintf(out, " Target = %s\n", v.Target)
fmt.Fprintf(out, " Source = %s\n", v.Source)
fmt.Fprintf(out, " ReadOnly = %v\n", v.ReadOnly)
fmt.Fprintf(out, " Type = %v\n", v.Type)
}
}
}