inspect.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package service
  2. import (
  3. "fmt"
  4. "io"
  5. "strings"
  6. "golang.org/x/net/context"
  7. "github.com/docker/docker/api/client"
  8. "github.com/docker/docker/api/client/inspect"
  9. "github.com/docker/docker/cli"
  10. "github.com/docker/docker/pkg/ioutils"
  11. apiclient "github.com/docker/engine-api/client"
  12. "github.com/docker/engine-api/types/swarm"
  13. "github.com/docker/go-units"
  14. "github.com/spf13/cobra"
  15. )
  16. type inspectOptions struct {
  17. refs []string
  18. format string
  19. pretty bool
  20. }
  21. func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
  22. var opts inspectOptions
  23. cmd := &cobra.Command{
  24. Use: "inspect [OPTIONS] SERVICE [SERVICE...]",
  25. Short: "Display detailed information on one or more services",
  26. Args: cli.RequiresMinArgs(1),
  27. RunE: func(cmd *cobra.Command, args []string) error {
  28. opts.refs = args
  29. if opts.pretty && len(opts.format) > 0 {
  30. return fmt.Errorf("--format is incompatible with human friendly format")
  31. }
  32. return runInspect(dockerCli, opts)
  33. },
  34. }
  35. flags := cmd.Flags()
  36. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
  37. flags.BoolVarP(&opts.pretty, "pretty", "p", false, "Print the information in a human friendly format.")
  38. return cmd
  39. }
  40. func runInspect(dockerCli *client.DockerCli, opts inspectOptions) error {
  41. client := dockerCli.Client()
  42. ctx := context.Background()
  43. getRef := func(ref string) (interface{}, []byte, error) {
  44. service, _, err := client.ServiceInspectWithRaw(ctx, ref)
  45. if err == nil || !apiclient.IsErrServiceNotFound(err) {
  46. return service, nil, err
  47. }
  48. return nil, nil, fmt.Errorf("Error: no such service: %s", ref)
  49. }
  50. if !opts.pretty {
  51. return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRef)
  52. }
  53. return printHumanFriendly(dockerCli.Out(), opts.refs, getRef)
  54. }
  55. func printHumanFriendly(out io.Writer, refs []string, getRef inspect.GetRefFunc) error {
  56. for idx, ref := range refs {
  57. obj, _, err := getRef(ref)
  58. if err != nil {
  59. return err
  60. }
  61. printService(out, obj.(swarm.Service))
  62. // TODO: better way to do this?
  63. // print extra space between objects, but not after the last one
  64. if idx+1 != len(refs) {
  65. fmt.Fprintf(out, "\n\n")
  66. }
  67. }
  68. return nil
  69. }
  70. // TODO: use a template
  71. func printService(out io.Writer, service swarm.Service) {
  72. fmt.Fprintf(out, "ID:\t\t%s\n", service.ID)
  73. fmt.Fprintf(out, "Name:\t\t%s\n", service.Spec.Name)
  74. if service.Spec.Labels != nil {
  75. fmt.Fprintln(out, "Labels:")
  76. for k, v := range service.Spec.Labels {
  77. fmt.Fprintf(out, " - %s=%s\n", k, v)
  78. }
  79. }
  80. if service.Spec.Mode.Global != nil {
  81. fmt.Fprintln(out, "Mode:\t\tGlobal")
  82. } else {
  83. fmt.Fprintln(out, "Mode:\t\tReplicated")
  84. if service.Spec.Mode.Replicated.Replicas != nil {
  85. fmt.Fprintf(out, " Replicas:\t%d\n", *service.Spec.Mode.Replicated.Replicas)
  86. }
  87. }
  88. fmt.Fprintln(out, "Placement:")
  89. fmt.Fprintln(out, " Strategy:\tSpread")
  90. if service.Spec.TaskTemplate.Placement != nil && len(service.Spec.TaskTemplate.Placement.Constraints) > 0 {
  91. ioutils.FprintfIfNotEmpty(out, " Constraints\t: %s\n", strings.Join(service.Spec.TaskTemplate.Placement.Constraints, ", "))
  92. }
  93. fmt.Fprintf(out, "UpdateConfig:\n")
  94. fmt.Fprintf(out, " Parallelism:\t%d\n", service.Spec.UpdateConfig.Parallelism)
  95. if service.Spec.UpdateConfig.Delay.Nanoseconds() > 0 {
  96. fmt.Fprintf(out, " Delay:\t\t%s\n", service.Spec.UpdateConfig.Delay)
  97. }
  98. fmt.Fprintf(out, "ContainerSpec:\n")
  99. printContainerSpec(out, service.Spec.TaskTemplate.ContainerSpec)
  100. if service.Spec.TaskTemplate.Resources != nil {
  101. fmt.Fprintln(out, "Resources:")
  102. printResources := func(out io.Writer, r *swarm.Resources) {
  103. if r.NanoCPUs != 0 {
  104. fmt.Fprintf(out, " CPU:\t\t%g\n", float64(r.NanoCPUs)/1e9)
  105. }
  106. if r.MemoryBytes != 0 {
  107. fmt.Fprintf(out, " Memory:\t\t%s\n", units.BytesSize(float64(r.MemoryBytes)))
  108. }
  109. }
  110. if service.Spec.TaskTemplate.Resources.Reservations != nil {
  111. fmt.Fprintln(out, "Reservations:")
  112. printResources(out, service.Spec.TaskTemplate.Resources.Reservations)
  113. }
  114. if service.Spec.TaskTemplate.Resources.Limits != nil {
  115. fmt.Fprintln(out, "Limits:")
  116. printResources(out, service.Spec.TaskTemplate.Resources.Limits)
  117. }
  118. }
  119. if len(service.Spec.Networks) > 0 {
  120. fmt.Fprintf(out, "Networks:")
  121. for _, n := range service.Spec.Networks {
  122. fmt.Fprintf(out, " %s", n.Target)
  123. }
  124. }
  125. if len(service.Endpoint.Ports) > 0 {
  126. fmt.Fprintln(out, "Ports:")
  127. for _, port := range service.Endpoint.Ports {
  128. fmt.Fprintf(out, " Name = %s\n", port.Name)
  129. fmt.Fprintf(out, " Protocol = %s\n", port.Protocol)
  130. fmt.Fprintf(out, " TargetPort = %d\n", port.TargetPort)
  131. fmt.Fprintf(out, " PublishedPort = %d\n", port.PublishedPort)
  132. }
  133. }
  134. }
  135. func printContainerSpec(out io.Writer, containerSpec swarm.ContainerSpec) {
  136. fmt.Fprintf(out, " Image:\t\t%s\n", containerSpec.Image)
  137. if len(containerSpec.Command) > 0 {
  138. fmt.Fprintf(out, " Command:\t%s\n", strings.Join(containerSpec.Command, " "))
  139. }
  140. if len(containerSpec.Args) > 0 {
  141. fmt.Fprintf(out, " Args:\t\t%s\n", strings.Join(containerSpec.Args, " "))
  142. }
  143. if len(containerSpec.Env) > 0 {
  144. fmt.Fprintf(out, " Env:\t\t%s\n", strings.Join(containerSpec.Env, " "))
  145. }
  146. ioutils.FprintfIfNotEmpty(out, " Dir\t\t%s\n", containerSpec.Dir)
  147. ioutils.FprintfIfNotEmpty(out, " User\t\t%s\n", containerSpec.User)
  148. if len(containerSpec.Mounts) > 0 {
  149. fmt.Fprintln(out, " Mounts:")
  150. for _, v := range containerSpec.Mounts {
  151. fmt.Fprintf(out, " Target = %s\n", v.Target)
  152. fmt.Fprintf(out, " Source = %s\n", v.Source)
  153. fmt.Fprintf(out, " ReadOnly = %v\n", v.ReadOnly)
  154. fmt.Fprintf(out, " Type = %v\n", v.Type)
  155. }
  156. }
  157. }