inspect.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/spf13/cobra"
  14. )
  15. type inspectOptions struct {
  16. refs []string
  17. format string
  18. pretty bool
  19. }
  20. func newInspectCommand(dockerCli *client.DockerCli) *cobra.Command {
  21. var opts inspectOptions
  22. cmd := &cobra.Command{
  23. Use: "inspect [OPTIONS] SERVICE [SERVICE...]",
  24. Short: "Inspect a service",
  25. Args: cli.RequiresMinArgs(1),
  26. RunE: func(cmd *cobra.Command, args []string) error {
  27. opts.refs = args
  28. if opts.pretty && len(opts.format) > 0 {
  29. return fmt.Errorf("--format is incompatible with human friendly format")
  30. }
  31. return runInspect(dockerCli, opts)
  32. },
  33. }
  34. flags := cmd.Flags()
  35. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template")
  36. flags.BoolVarP(&opts.pretty, "pretty", "p", false, "Print the information in a human friendly format.")
  37. return cmd
  38. }
  39. func runInspect(dockerCli *client.DockerCli, opts inspectOptions) error {
  40. client := dockerCli.Client()
  41. ctx := context.Background()
  42. getRef := func(ref string) (interface{}, []byte, error) {
  43. service, err := client.ServiceInspect(ctx, ref)
  44. if err == nil || !apiclient.IsErrServiceNotFound(err) {
  45. return service, nil, err
  46. }
  47. return nil, nil, fmt.Errorf("Error: no such service: %s", ref)
  48. }
  49. if !opts.pretty {
  50. return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRef)
  51. }
  52. return printHumanFriendly(dockerCli.Out(), opts.refs, getRef)
  53. }
  54. func printHumanFriendly(out io.Writer, refs []string, getRef inspect.GetRefFunc) error {
  55. for idx, ref := range refs {
  56. obj, _, err := getRef(ref)
  57. if err != nil {
  58. return err
  59. }
  60. printService(out, obj.(swarm.Service))
  61. // TODO: better way to do this?
  62. // print extra space between objects, but not after the last one
  63. if idx+1 != len(refs) {
  64. fmt.Fprintf(out, "\n\n")
  65. }
  66. }
  67. return nil
  68. }
  69. // TODO: use a template
  70. func printService(out io.Writer, service swarm.Service) {
  71. fmt.Fprintf(out, "ID:\t\t%s\n", service.ID)
  72. fmt.Fprintf(out, "Name:\t\t%s\n", service.Spec.Name)
  73. if service.Spec.Labels != nil {
  74. fmt.Fprintln(out, "Labels:")
  75. for k, v := range service.Spec.Labels {
  76. fmt.Fprintf(out, " - %s=%s\n", k, v)
  77. }
  78. }
  79. if service.Spec.Mode.Global != nil {
  80. fmt.Fprintln(out, "Mode:\t\tGLOBAL")
  81. } else {
  82. fmt.Fprintln(out, "Mode:\t\tREPLICATED")
  83. if service.Spec.Mode.Replicated.Replicas != nil {
  84. fmt.Fprintf(out, " Replicas:\t\t%d\n", *service.Spec.Mode.Replicated.Replicas)
  85. }
  86. }
  87. fmt.Fprintln(out, "Placement:")
  88. fmt.Fprintln(out, " Strategy:\tSPREAD")
  89. fmt.Fprintf(out, "UpdateConfig:\n")
  90. fmt.Fprintf(out, " Parallelism:\t%d\n", service.Spec.UpdateConfig.Parallelism)
  91. if service.Spec.UpdateConfig.Delay.Nanoseconds() > 0 {
  92. fmt.Fprintf(out, " Delay:\t\t%s\n", service.Spec.UpdateConfig.Delay)
  93. }
  94. fmt.Fprintf(out, "ContainerSpec:\n")
  95. printContainerSpec(out, service.Spec.TaskTemplate.ContainerSpec)
  96. }
  97. func printContainerSpec(out io.Writer, containerSpec swarm.ContainerSpec) {
  98. fmt.Fprintf(out, " Image:\t\t%s\n", containerSpec.Image)
  99. if len(containerSpec.Command) > 0 {
  100. fmt.Fprintf(out, " Command:\t%s\n", strings.Join(containerSpec.Command, " "))
  101. }
  102. if len(containerSpec.Args) > 0 {
  103. fmt.Fprintf(out, " Args:\t%s\n", strings.Join(containerSpec.Args, " "))
  104. }
  105. if len(containerSpec.Env) > 0 {
  106. fmt.Fprintf(out, " Env:\t\t%s\n", strings.Join(containerSpec.Env, " "))
  107. }
  108. ioutils.FprintfIfNotEmpty(out, " Dir\t\t%s\n", containerSpec.Dir)
  109. ioutils.FprintfIfNotEmpty(out, " User\t\t%s\n", containerSpec.User)
  110. }