info.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package system
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "golang.org/x/net/context"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/swarm"
  9. "github.com/docker/docker/cli"
  10. "github.com/docker/docker/cli/command"
  11. "github.com/docker/docker/pkg/ioutils"
  12. "github.com/docker/docker/utils"
  13. "github.com/docker/docker/utils/templates"
  14. "github.com/docker/go-units"
  15. "github.com/spf13/cobra"
  16. )
  17. type infoOptions struct {
  18. format string
  19. }
  20. // NewInfoCommand creates a new cobra.Command for `docker info`
  21. func NewInfoCommand(dockerCli *command.DockerCli) *cobra.Command {
  22. var opts infoOptions
  23. cmd := &cobra.Command{
  24. Use: "info [OPTIONS]",
  25. Short: "Display system-wide information",
  26. Args: cli.NoArgs,
  27. RunE: func(cmd *cobra.Command, args []string) error {
  28. return runInfo(dockerCli, &opts)
  29. },
  30. }
  31. flags := cmd.Flags()
  32. flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
  33. return cmd
  34. }
  35. func runInfo(dockerCli *command.DockerCli, opts *infoOptions) error {
  36. ctx := context.Background()
  37. info, err := dockerCli.Client().Info(ctx)
  38. if err != nil {
  39. return err
  40. }
  41. if opts.format == "" {
  42. return prettyPrintInfo(dockerCli, info)
  43. }
  44. return formatInfo(dockerCli, info, opts.format)
  45. }
  46. func prettyPrintInfo(dockerCli *command.DockerCli, info types.Info) error {
  47. fmt.Fprintf(dockerCli.Out(), "Containers: %d\n", info.Containers)
  48. fmt.Fprintf(dockerCli.Out(), " Running: %d\n", info.ContainersRunning)
  49. fmt.Fprintf(dockerCli.Out(), " Paused: %d\n", info.ContainersPaused)
  50. fmt.Fprintf(dockerCli.Out(), " Stopped: %d\n", info.ContainersStopped)
  51. fmt.Fprintf(dockerCli.Out(), "Images: %d\n", info.Images)
  52. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Server Version: %s\n", info.ServerVersion)
  53. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Storage Driver: %s\n", info.Driver)
  54. if info.DriverStatus != nil {
  55. for _, pair := range info.DriverStatus {
  56. fmt.Fprintf(dockerCli.Out(), " %s: %s\n", pair[0], pair[1])
  57. // print a warning if devicemapper is using a loopback file
  58. if pair[0] == "Data loop file" {
  59. fmt.Fprintln(dockerCli.Err(), " WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.")
  60. }
  61. }
  62. }
  63. if info.SystemStatus != nil {
  64. for _, pair := range info.SystemStatus {
  65. fmt.Fprintf(dockerCli.Out(), "%s: %s\n", pair[0], pair[1])
  66. }
  67. }
  68. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Logging Driver: %s\n", info.LoggingDriver)
  69. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Cgroup Driver: %s\n", info.CgroupDriver)
  70. fmt.Fprintf(dockerCli.Out(), "Plugins: \n")
  71. fmt.Fprintf(dockerCli.Out(), " Volume:")
  72. fmt.Fprintf(dockerCli.Out(), " %s", strings.Join(info.Plugins.Volume, " "))
  73. fmt.Fprintf(dockerCli.Out(), "\n")
  74. fmt.Fprintf(dockerCli.Out(), " Network:")
  75. fmt.Fprintf(dockerCli.Out(), " %s", strings.Join(info.Plugins.Network, " "))
  76. fmt.Fprintf(dockerCli.Out(), "\n")
  77. if len(info.Plugins.Authorization) != 0 {
  78. fmt.Fprintf(dockerCli.Out(), " Authorization:")
  79. fmt.Fprintf(dockerCli.Out(), " %s", strings.Join(info.Plugins.Authorization, " "))
  80. fmt.Fprintf(dockerCli.Out(), "\n")
  81. }
  82. fmt.Fprintf(dockerCli.Out(), "Swarm: %v\n", info.Swarm.LocalNodeState)
  83. if info.Swarm.LocalNodeState != swarm.LocalNodeStateInactive && info.Swarm.LocalNodeState != swarm.LocalNodeStateLocked {
  84. fmt.Fprintf(dockerCli.Out(), " NodeID: %s\n", info.Swarm.NodeID)
  85. if info.Swarm.Error != "" {
  86. fmt.Fprintf(dockerCli.Out(), " Error: %v\n", info.Swarm.Error)
  87. }
  88. fmt.Fprintf(dockerCli.Out(), " Is Manager: %v\n", info.Swarm.ControlAvailable)
  89. if info.Swarm.ControlAvailable {
  90. fmt.Fprintf(dockerCli.Out(), " ClusterID: %s\n", info.Swarm.Cluster.ID)
  91. fmt.Fprintf(dockerCli.Out(), " Managers: %d\n", info.Swarm.Managers)
  92. fmt.Fprintf(dockerCli.Out(), " Nodes: %d\n", info.Swarm.Nodes)
  93. fmt.Fprintf(dockerCli.Out(), " Orchestration:\n")
  94. taskHistoryRetentionLimit := int64(0)
  95. if info.Swarm.Cluster.Spec.Orchestration.TaskHistoryRetentionLimit != nil {
  96. taskHistoryRetentionLimit = *info.Swarm.Cluster.Spec.Orchestration.TaskHistoryRetentionLimit
  97. }
  98. fmt.Fprintf(dockerCli.Out(), " Task History Retention Limit: %d\n", taskHistoryRetentionLimit)
  99. fmt.Fprintf(dockerCli.Out(), " Raft:\n")
  100. fmt.Fprintf(dockerCli.Out(), " Snapshot Interval: %d\n", info.Swarm.Cluster.Spec.Raft.SnapshotInterval)
  101. if info.Swarm.Cluster.Spec.Raft.KeepOldSnapshots != nil {
  102. fmt.Fprintf(dockerCli.Out(), " Number of Old Snapshots to Retain: %d\n", *info.Swarm.Cluster.Spec.Raft.KeepOldSnapshots)
  103. }
  104. fmt.Fprintf(dockerCli.Out(), " Heartbeat Tick: %d\n", info.Swarm.Cluster.Spec.Raft.HeartbeatTick)
  105. fmt.Fprintf(dockerCli.Out(), " Election Tick: %d\n", info.Swarm.Cluster.Spec.Raft.ElectionTick)
  106. fmt.Fprintf(dockerCli.Out(), " Dispatcher:\n")
  107. fmt.Fprintf(dockerCli.Out(), " Heartbeat Period: %s\n", units.HumanDuration(time.Duration(info.Swarm.Cluster.Spec.Dispatcher.HeartbeatPeriod)))
  108. fmt.Fprintf(dockerCli.Out(), " CA Configuration:\n")
  109. fmt.Fprintf(dockerCli.Out(), " Expiry Duration: %s\n", units.HumanDuration(info.Swarm.Cluster.Spec.CAConfig.NodeCertExpiry))
  110. if len(info.Swarm.Cluster.Spec.CAConfig.ExternalCAs) > 0 {
  111. fmt.Fprintf(dockerCli.Out(), " External CAs:\n")
  112. for _, entry := range info.Swarm.Cluster.Spec.CAConfig.ExternalCAs {
  113. fmt.Fprintf(dockerCli.Out(), " %s: %s\n", entry.Protocol, entry.URL)
  114. }
  115. }
  116. }
  117. fmt.Fprintf(dockerCli.Out(), " Node Address: %s\n", info.Swarm.NodeAddr)
  118. }
  119. if len(info.Runtimes) > 0 {
  120. fmt.Fprintf(dockerCli.Out(), "Runtimes:")
  121. for name := range info.Runtimes {
  122. fmt.Fprintf(dockerCli.Out(), " %s", name)
  123. }
  124. fmt.Fprint(dockerCli.Out(), "\n")
  125. fmt.Fprintf(dockerCli.Out(), "Default Runtime: %s\n", info.DefaultRuntime)
  126. }
  127. if info.OSType == "linux" {
  128. if len(info.SecurityOptions) != 0 {
  129. fmt.Fprintf(dockerCli.Out(), "Security Options:\n")
  130. for _, o := range info.SecurityOptions {
  131. switch o.Key {
  132. case "Name":
  133. fmt.Fprintf(dockerCli.Out(), " %s\n", o.Value)
  134. case "Profile":
  135. if o.Value != "default" {
  136. fmt.Fprintf(dockerCli.Err(), " WARNING: You're not using the default seccomp profile\n")
  137. }
  138. fmt.Fprintf(dockerCli.Out(), " %s: %s\n", o.Key, o.Value)
  139. }
  140. }
  141. }
  142. }
  143. // Isolation only has meaning on a Windows daemon.
  144. if info.OSType == "windows" {
  145. fmt.Fprintf(dockerCli.Out(), "Default Isolation: %v\n", info.Isolation)
  146. }
  147. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Kernel Version: %s\n", info.KernelVersion)
  148. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Operating System: %s\n", info.OperatingSystem)
  149. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "OSType: %s\n", info.OSType)
  150. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Architecture: %s\n", info.Architecture)
  151. fmt.Fprintf(dockerCli.Out(), "CPUs: %d\n", info.NCPU)
  152. fmt.Fprintf(dockerCli.Out(), "Total Memory: %s\n", units.BytesSize(float64(info.MemTotal)))
  153. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Name: %s\n", info.Name)
  154. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "ID: %s\n", info.ID)
  155. fmt.Fprintf(dockerCli.Out(), "Docker Root Dir: %s\n", info.DockerRootDir)
  156. fmt.Fprintf(dockerCli.Out(), "Debug Mode (client): %v\n", utils.IsDebugEnabled())
  157. fmt.Fprintf(dockerCli.Out(), "Debug Mode (server): %v\n", info.Debug)
  158. if info.Debug {
  159. fmt.Fprintf(dockerCli.Out(), " File Descriptors: %d\n", info.NFd)
  160. fmt.Fprintf(dockerCli.Out(), " Goroutines: %d\n", info.NGoroutines)
  161. fmt.Fprintf(dockerCli.Out(), " System Time: %s\n", info.SystemTime)
  162. fmt.Fprintf(dockerCli.Out(), " EventsListeners: %d\n", info.NEventsListener)
  163. }
  164. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Http Proxy: %s\n", info.HTTPProxy)
  165. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "Https Proxy: %s\n", info.HTTPSProxy)
  166. ioutils.FprintfIfNotEmpty(dockerCli.Out(), "No Proxy: %s\n", info.NoProxy)
  167. if info.IndexServerAddress != "" {
  168. u := dockerCli.ConfigFile().AuthConfigs[info.IndexServerAddress].Username
  169. if len(u) > 0 {
  170. fmt.Fprintf(dockerCli.Out(), "Username: %v\n", u)
  171. }
  172. fmt.Fprintf(dockerCli.Out(), "Registry: %v\n", info.IndexServerAddress)
  173. }
  174. // Only output these warnings if the server does not support these features
  175. if info.OSType != "windows" {
  176. if !info.MemoryLimit {
  177. fmt.Fprintln(dockerCli.Err(), "WARNING: No memory limit support")
  178. }
  179. if !info.SwapLimit {
  180. fmt.Fprintln(dockerCli.Err(), "WARNING: No swap limit support")
  181. }
  182. if !info.KernelMemory {
  183. fmt.Fprintln(dockerCli.Err(), "WARNING: No kernel memory limit support")
  184. }
  185. if !info.OomKillDisable {
  186. fmt.Fprintln(dockerCli.Err(), "WARNING: No oom kill disable support")
  187. }
  188. if !info.CPUCfsQuota {
  189. fmt.Fprintln(dockerCli.Err(), "WARNING: No cpu cfs quota support")
  190. }
  191. if !info.CPUCfsPeriod {
  192. fmt.Fprintln(dockerCli.Err(), "WARNING: No cpu cfs period support")
  193. }
  194. if !info.CPUShares {
  195. fmt.Fprintln(dockerCli.Err(), "WARNING: No cpu shares support")
  196. }
  197. if !info.CPUSet {
  198. fmt.Fprintln(dockerCli.Err(), "WARNING: No cpuset support")
  199. }
  200. if !info.IPv4Forwarding {
  201. fmt.Fprintln(dockerCli.Err(), "WARNING: IPv4 forwarding is disabled")
  202. }
  203. if !info.BridgeNfIptables {
  204. fmt.Fprintln(dockerCli.Err(), "WARNING: bridge-nf-call-iptables is disabled")
  205. }
  206. if !info.BridgeNfIP6tables {
  207. fmt.Fprintln(dockerCli.Err(), "WARNING: bridge-nf-call-ip6tables is disabled")
  208. }
  209. }
  210. if info.Labels != nil {
  211. fmt.Fprintln(dockerCli.Out(), "Labels:")
  212. for _, attribute := range info.Labels {
  213. fmt.Fprintf(dockerCli.Out(), " %s\n", attribute)
  214. }
  215. // TODO: Engine labels with duplicate keys has been deprecated in 1.13 and will be error out
  216. // after 3 release cycles (1.16). For now, a WARNING will be generated. The following will
  217. // be removed eventually.
  218. labelMap := map[string]string{}
  219. for _, label := range info.Labels {
  220. stringSlice := strings.SplitN(label, "=", 2)
  221. if len(stringSlice) > 1 {
  222. // If there is a conflict we will throw out an warning
  223. if v, ok := labelMap[stringSlice[0]]; ok && v != stringSlice[1] {
  224. fmt.Fprintln(dockerCli.Err(), "WARNING: labels with duplicate keys and conflicting values have been deprecated")
  225. break
  226. }
  227. labelMap[stringSlice[0]] = stringSlice[1]
  228. }
  229. }
  230. }
  231. fmt.Fprintf(dockerCli.Out(), "Experimental: %v\n", info.ExperimentalBuild)
  232. if info.ClusterStore != "" {
  233. fmt.Fprintf(dockerCli.Out(), "Cluster Store: %s\n", info.ClusterStore)
  234. }
  235. if info.ClusterAdvertise != "" {
  236. fmt.Fprintf(dockerCli.Out(), "Cluster Advertise: %s\n", info.ClusterAdvertise)
  237. }
  238. if info.RegistryConfig != nil && (len(info.RegistryConfig.InsecureRegistryCIDRs) > 0 || len(info.RegistryConfig.IndexConfigs) > 0) {
  239. fmt.Fprintln(dockerCli.Out(), "Insecure Registries:")
  240. for _, registry := range info.RegistryConfig.IndexConfigs {
  241. if registry.Secure == false {
  242. fmt.Fprintf(dockerCli.Out(), " %s\n", registry.Name)
  243. }
  244. }
  245. for _, registry := range info.RegistryConfig.InsecureRegistryCIDRs {
  246. mask, _ := registry.Mask.Size()
  247. fmt.Fprintf(dockerCli.Out(), " %s/%d\n", registry.IP.String(), mask)
  248. }
  249. }
  250. if info.RegistryConfig != nil && len(info.RegistryConfig.Mirrors) > 0 {
  251. fmt.Fprintln(dockerCli.Out(), "Registry Mirrors:")
  252. for _, mirror := range info.RegistryConfig.Mirrors {
  253. fmt.Fprintf(dockerCli.Out(), " %s\n", mirror)
  254. }
  255. }
  256. fmt.Fprintf(dockerCli.Out(), "Live Restore Enabled: %v\n", info.LiveRestoreEnabled)
  257. return nil
  258. }
  259. func formatInfo(dockerCli *command.DockerCli, info types.Info, format string) error {
  260. tmpl, err := templates.Parse(format)
  261. if err != nil {
  262. return cli.StatusError{StatusCode: 64,
  263. Status: "Template parsing error: " + err.Error()}
  264. }
  265. err = tmpl.Execute(dockerCli.Out(), info)
  266. dockerCli.Out().Write([]byte{'\n'})
  267. return err
  268. }