inspect.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package system
  2. import (
  3. "fmt"
  4. "strings"
  5. "golang.org/x/net/context"
  6. "github.com/docker/docker/cli"
  7. "github.com/docker/docker/cli/command"
  8. "github.com/docker/docker/cli/command/inspect"
  9. apiclient "github.com/docker/docker/client"
  10. "github.com/spf13/cobra"
  11. )
  12. type inspectOptions struct {
  13. format string
  14. inspectType string
  15. size bool
  16. ids []string
  17. }
  18. // NewInspectCommand creates a new cobra.Command for `docker inspect`
  19. func NewInspectCommand(dockerCli *command.DockerCli) *cobra.Command {
  20. var opts inspectOptions
  21. cmd := &cobra.Command{
  22. Use: "inspect [OPTIONS] NAME|ID [NAME|ID...]",
  23. Short: "Return low-level information on Docker objects",
  24. Args: cli.RequiresMinArgs(1),
  25. RunE: func(cmd *cobra.Command, args []string) error {
  26. opts.ids = args
  27. return runInspect(dockerCli, opts)
  28. },
  29. }
  30. flags := cmd.Flags()
  31. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
  32. flags.StringVar(&opts.inspectType, "type", "", "Return JSON for specified type")
  33. flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container")
  34. return cmd
  35. }
  36. func runInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
  37. var elementSearcher inspect.GetRefFunc
  38. switch opts.inspectType {
  39. case "", "container", "image", "node", "network", "service", "volume", "task", "plugin":
  40. elementSearcher = inspectAll(context.Background(), dockerCli, opts.size, opts.inspectType)
  41. default:
  42. return fmt.Errorf("%q is not a valid value for --type", opts.inspectType)
  43. }
  44. return inspect.Inspect(dockerCli.Out(), opts.ids, opts.format, elementSearcher)
  45. }
  46. func inspectContainers(ctx context.Context, dockerCli *command.DockerCli, getSize bool) inspect.GetRefFunc {
  47. return func(ref string) (interface{}, []byte, error) {
  48. return dockerCli.Client().ContainerInspectWithRaw(ctx, ref, getSize)
  49. }
  50. }
  51. func inspectImages(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  52. return func(ref string) (interface{}, []byte, error) {
  53. return dockerCli.Client().ImageInspectWithRaw(ctx, ref)
  54. }
  55. }
  56. func inspectNetwork(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  57. return func(ref string) (interface{}, []byte, error) {
  58. return dockerCli.Client().NetworkInspectWithRaw(ctx, ref)
  59. }
  60. }
  61. func inspectNode(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  62. return func(ref string) (interface{}, []byte, error) {
  63. return dockerCli.Client().NodeInspectWithRaw(ctx, ref)
  64. }
  65. }
  66. func inspectService(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  67. return func(ref string) (interface{}, []byte, error) {
  68. return dockerCli.Client().ServiceInspectWithRaw(ctx, ref)
  69. }
  70. }
  71. func inspectTasks(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  72. return func(ref string) (interface{}, []byte, error) {
  73. return dockerCli.Client().TaskInspectWithRaw(ctx, ref)
  74. }
  75. }
  76. func inspectVolume(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  77. return func(ref string) (interface{}, []byte, error) {
  78. return dockerCli.Client().VolumeInspectWithRaw(ctx, ref)
  79. }
  80. }
  81. func inspectPlugin(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
  82. return func(ref string) (interface{}, []byte, error) {
  83. return dockerCli.Client().PluginInspectWithRaw(ctx, ref)
  84. }
  85. }
  86. func inspectAll(ctx context.Context, dockerCli *command.DockerCli, getSize bool, typeConstraint string) inspect.GetRefFunc {
  87. var inspectAutodetect = []struct {
  88. objectType string
  89. isSizeSupported bool
  90. isSwarmObject bool
  91. objectInspector func(string) (interface{}, []byte, error)
  92. }{
  93. {
  94. objectType: "container",
  95. isSizeSupported: true,
  96. objectInspector: inspectContainers(ctx, dockerCli, getSize),
  97. },
  98. {
  99. objectType: "image",
  100. objectInspector: inspectImages(ctx, dockerCli),
  101. },
  102. {
  103. objectType: "network",
  104. objectInspector: inspectNetwork(ctx, dockerCli),
  105. },
  106. {
  107. objectType: "volume",
  108. objectInspector: inspectVolume(ctx, dockerCli),
  109. },
  110. {
  111. objectType: "service",
  112. isSwarmObject: true,
  113. objectInspector: inspectService(ctx, dockerCli),
  114. },
  115. {
  116. objectType: "task",
  117. isSwarmObject: true,
  118. objectInspector: inspectTasks(ctx, dockerCli),
  119. },
  120. {
  121. objectType: "node",
  122. isSwarmObject: true,
  123. objectInspector: inspectNode(ctx, dockerCli),
  124. },
  125. {
  126. objectType: "plugin",
  127. objectInspector: inspectPlugin(ctx, dockerCli),
  128. },
  129. }
  130. // isSwarmManager does an Info API call to verify that the daemon is
  131. // a swarm manager.
  132. isSwarmManager := func() bool {
  133. info, err := dockerCli.Client().Info(ctx)
  134. if err != nil {
  135. fmt.Fprintln(dockerCli.Err(), err)
  136. return false
  137. }
  138. return info.Swarm.ControlAvailable
  139. }
  140. isErrNotSupported := func(err error) bool {
  141. return strings.Contains(err.Error(), "not supported")
  142. }
  143. return func(ref string) (interface{}, []byte, error) {
  144. const (
  145. swarmSupportUnknown = iota
  146. swarmSupported
  147. swarmUnsupported
  148. )
  149. isSwarmSupported := swarmSupportUnknown
  150. for _, inspectData := range inspectAutodetect {
  151. if typeConstraint != "" && inspectData.objectType != typeConstraint {
  152. continue
  153. }
  154. if typeConstraint == "" && inspectData.isSwarmObject {
  155. if isSwarmSupported == swarmSupportUnknown {
  156. if isSwarmManager() {
  157. isSwarmSupported = swarmSupported
  158. } else {
  159. isSwarmSupported = swarmUnsupported
  160. }
  161. }
  162. if isSwarmSupported == swarmUnsupported {
  163. continue
  164. }
  165. }
  166. v, raw, err := inspectData.objectInspector(ref)
  167. if err != nil {
  168. if typeConstraint == "" && (apiclient.IsErrNotFound(err) || isErrNotSupported(err)) {
  169. continue
  170. }
  171. return v, raw, err
  172. }
  173. if getSize && !inspectData.isSizeSupported {
  174. fmt.Fprintf(dockerCli.Err(), "WARNING: --size ignored for %s\n", inspectData.objectType)
  175. }
  176. return v, raw, err
  177. }
  178. return nil, nil, fmt.Errorf("Error: No such object: %s", ref)
  179. }
  180. }