inspect.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.BoolVar(&opts.pretty, "pretty", 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. if service.Spec.TaskTemplate.Placement != nil && len(service.Spec.TaskTemplate.Placement.Constraints) > 0 {
  90. ioutils.FprintfIfNotEmpty(out, " Constraints\t: %s\n", strings.Join(service.Spec.TaskTemplate.Placement.Constraints, ", "))
  91. }
  92. fmt.Fprintf(out, "UpdateConfig:\n")
  93. fmt.Fprintf(out, " Parallelism:\t%d\n", service.Spec.UpdateConfig.Parallelism)
  94. if service.Spec.UpdateConfig.Delay.Nanoseconds() > 0 {
  95. fmt.Fprintf(out, " Delay:\t\t%s\n", service.Spec.UpdateConfig.Delay)
  96. }
  97. fmt.Fprintf(out, "ContainerSpec:\n")
  98. printContainerSpec(out, service.Spec.TaskTemplate.ContainerSpec)
  99. resources := service.Spec.TaskTemplate.Resources
  100. if resources != nil {
  101. fmt.Fprintln(out, "Resources:")
  102. printResources := func(out io.Writer, requirement string, r *swarm.Resources) {
  103. if r == nil || (r.MemoryBytes == 0 && r.NanoCPUs == 0) {
  104. return
  105. }
  106. fmt.Fprintf(out, " %s:\n", requirement)
  107. if r.NanoCPUs != 0 {
  108. fmt.Fprintf(out, " CPU:\t\t%g\n", float64(r.NanoCPUs)/1e9)
  109. }
  110. if r.MemoryBytes != 0 {
  111. fmt.Fprintf(out, " Memory:\t%s\n", units.BytesSize(float64(r.MemoryBytes)))
  112. }
  113. }
  114. printResources(out, "Reservations", resources.Reservations)
  115. printResources(out, "Limits", resources.Limits)
  116. }
  117. if len(service.Spec.Networks) > 0 {
  118. fmt.Fprintf(out, "Networks:")
  119. for _, n := range service.Spec.Networks {
  120. fmt.Fprintf(out, " %s", n.Target)
  121. }
  122. }
  123. if len(service.Endpoint.Ports) > 0 {
  124. fmt.Fprintln(out, "Ports:")
  125. for _, port := range service.Endpoint.Ports {
  126. ioutils.FprintfIfNotEmpty(out, " Name = %s\n", port.Name)
  127. fmt.Fprintf(out, " Protocol = %s\n", port.Protocol)
  128. fmt.Fprintf(out, " TargetPort = %d\n", port.TargetPort)
  129. fmt.Fprintf(out, " PublishedPort = %d\n", port.PublishedPort)
  130. }
  131. }
  132. }
  133. func printContainerSpec(out io.Writer, containerSpec swarm.ContainerSpec) {
  134. fmt.Fprintf(out, " Image:\t\t%s\n", containerSpec.Image)
  135. if len(containerSpec.Args) > 0 {
  136. fmt.Fprintf(out, " Args:\t\t%s\n", strings.Join(containerSpec.Args, " "))
  137. }
  138. if len(containerSpec.Env) > 0 {
  139. fmt.Fprintf(out, " Env:\t\t%s\n", strings.Join(containerSpec.Env, " "))
  140. }
  141. ioutils.FprintfIfNotEmpty(out, " Dir\t\t%s\n", containerSpec.Dir)
  142. ioutils.FprintfIfNotEmpty(out, " User\t\t%s\n", containerSpec.User)
  143. if len(containerSpec.Mounts) > 0 {
  144. fmt.Fprintln(out, " Mounts:")
  145. for _, v := range containerSpec.Mounts {
  146. fmt.Fprintf(out, " Target = %s\n", v.Target)
  147. fmt.Fprintf(out, " Source = %s\n", v.Source)
  148. fmt.Fprintf(out, " ReadOnly = %v\n", v.ReadOnly)
  149. fmt.Fprintf(out, " Type = %v\n", v.Type)
  150. }
  151. }
  152. }