disk_usage.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package formatter
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. "text/template"
  7. "github.com/docker/distribution/reference"
  8. "github.com/docker/docker/api/types"
  9. units "github.com/docker/go-units"
  10. )
  11. const (
  12. defaultDiskUsageImageTableFormat = "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedSince}} ago\t{{.VirtualSize}}\t{{.SharedSize}}\t{{.UniqueSize}}\t{{.Containers}}"
  13. defaultDiskUsageContainerTableFormat = "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.LocalVolumes}}\t{{.Size}}\t{{.RunningFor}} ago\t{{.Status}}\t{{.Names}}"
  14. defaultDiskUsageVolumeTableFormat = "table {{.Name}}\t{{.Links}}\t{{.Size}}"
  15. defaultDiskUsageTableFormat = "table {{.Type}}\t{{.TotalCount}}\t{{.Active}}\t{{.Size}}\t{{.Reclaimable}}"
  16. typeHeader = "TYPE"
  17. totalHeader = "TOTAL"
  18. activeHeader = "ACTIVE"
  19. reclaimableHeader = "RECLAIMABLE"
  20. containersHeader = "CONTAINERS"
  21. sharedSizeHeader = "SHARED SIZE"
  22. uniqueSizeHeader = "UNIQUE SiZE"
  23. )
  24. // DiskUsageContext contains disk usage specific information required by the formater, encapsulate a Context struct.
  25. type DiskUsageContext struct {
  26. Context
  27. Verbose bool
  28. LayersSize int64
  29. Images []*types.ImageSummary
  30. Containers []*types.Container
  31. Volumes []*types.Volume
  32. }
  33. func (ctx *DiskUsageContext) startSubsection(format string) (*template.Template, error) {
  34. ctx.buffer = bytes.NewBufferString("")
  35. ctx.header = ""
  36. ctx.Format = Format(format)
  37. ctx.preFormat()
  38. return ctx.parseFormat()
  39. }
  40. func (ctx *DiskUsageContext) Write() {
  41. if ctx.Verbose == false {
  42. ctx.buffer = bytes.NewBufferString("")
  43. ctx.Format = defaultDiskUsageTableFormat
  44. ctx.preFormat()
  45. tmpl, err := ctx.parseFormat()
  46. if err != nil {
  47. return
  48. }
  49. err = ctx.contextFormat(tmpl, &diskUsageImagesContext{
  50. totalSize: ctx.LayersSize,
  51. images: ctx.Images,
  52. })
  53. if err != nil {
  54. return
  55. }
  56. err = ctx.contextFormat(tmpl, &diskUsageContainersContext{
  57. containers: ctx.Containers,
  58. })
  59. if err != nil {
  60. return
  61. }
  62. err = ctx.contextFormat(tmpl, &diskUsageVolumesContext{
  63. volumes: ctx.Volumes,
  64. })
  65. if err != nil {
  66. return
  67. }
  68. ctx.postFormat(tmpl, &diskUsageContainersContext{containers: []*types.Container{}})
  69. return
  70. }
  71. // First images
  72. tmpl, err := ctx.startSubsection(defaultDiskUsageImageTableFormat)
  73. if err != nil {
  74. return
  75. }
  76. ctx.Output.Write([]byte("Images space usage:\n\n"))
  77. for _, i := range ctx.Images {
  78. repo := "<none>"
  79. tag := "<none>"
  80. if len(i.RepoTags) > 0 && !isDangling(*i) {
  81. // Only show the first tag
  82. ref, err := reference.ParseNamed(i.RepoTags[0])
  83. if err != nil {
  84. continue
  85. }
  86. if nt, ok := ref.(reference.NamedTagged); ok {
  87. repo = ref.Name()
  88. tag = nt.Tag()
  89. }
  90. }
  91. err = ctx.contextFormat(tmpl, &imageContext{
  92. repo: repo,
  93. tag: tag,
  94. trunc: true,
  95. i: *i,
  96. })
  97. if err != nil {
  98. return
  99. }
  100. }
  101. ctx.postFormat(tmpl, &imageContext{})
  102. // Now containers
  103. ctx.Output.Write([]byte("\nContainers space usage:\n\n"))
  104. tmpl, err = ctx.startSubsection(defaultDiskUsageContainerTableFormat)
  105. if err != nil {
  106. return
  107. }
  108. for _, c := range ctx.Containers {
  109. // Don't display the virtual size
  110. c.SizeRootFs = 0
  111. err = ctx.contextFormat(tmpl, &containerContext{
  112. trunc: true,
  113. c: *c,
  114. })
  115. if err != nil {
  116. return
  117. }
  118. }
  119. ctx.postFormat(tmpl, &containerContext{})
  120. // And volumes
  121. ctx.Output.Write([]byte("\nLocal Volumes space usage:\n\n"))
  122. tmpl, err = ctx.startSubsection(defaultDiskUsageVolumeTableFormat)
  123. if err != nil {
  124. return
  125. }
  126. for _, v := range ctx.Volumes {
  127. err = ctx.contextFormat(tmpl, &volumeContext{
  128. v: *v,
  129. })
  130. if err != nil {
  131. return
  132. }
  133. }
  134. ctx.postFormat(tmpl, &volumeContext{v: types.Volume{}})
  135. }
  136. type diskUsageImagesContext struct {
  137. HeaderContext
  138. totalSize int64
  139. images []*types.ImageSummary
  140. }
  141. func (c *diskUsageImagesContext) Type() string {
  142. c.AddHeader(typeHeader)
  143. return "Images"
  144. }
  145. func (c *diskUsageImagesContext) TotalCount() string {
  146. c.AddHeader(totalHeader)
  147. return fmt.Sprintf("%d", len(c.images))
  148. }
  149. func (c *diskUsageImagesContext) Active() string {
  150. c.AddHeader(activeHeader)
  151. used := 0
  152. for _, i := range c.images {
  153. if i.Containers > 0 {
  154. used++
  155. }
  156. }
  157. return fmt.Sprintf("%d", used)
  158. }
  159. func (c *diskUsageImagesContext) Size() string {
  160. c.AddHeader(sizeHeader)
  161. return units.HumanSize(float64(c.totalSize))
  162. }
  163. func (c *diskUsageImagesContext) Reclaimable() string {
  164. var used int64
  165. c.AddHeader(reclaimableHeader)
  166. for _, i := range c.images {
  167. if i.Containers != 0 {
  168. used += i.Size
  169. }
  170. }
  171. reclaimable := c.totalSize - used
  172. if c.totalSize > 0 {
  173. return fmt.Sprintf("%s (%v%%)", units.HumanSize(float64(reclaimable)), (reclaimable*100)/c.totalSize)
  174. }
  175. return fmt.Sprintf("%s", units.HumanSize(float64(reclaimable)))
  176. }
  177. type diskUsageContainersContext struct {
  178. HeaderContext
  179. verbose bool
  180. containers []*types.Container
  181. }
  182. func (c *diskUsageContainersContext) Type() string {
  183. c.AddHeader(typeHeader)
  184. return "Containers"
  185. }
  186. func (c *diskUsageContainersContext) TotalCount() string {
  187. c.AddHeader(totalHeader)
  188. return fmt.Sprintf("%d", len(c.containers))
  189. }
  190. func (c *diskUsageContainersContext) isActive(container types.Container) bool {
  191. return strings.Contains(container.State, "running") ||
  192. strings.Contains(container.State, "paused") ||
  193. strings.Contains(container.State, "restarting")
  194. }
  195. func (c *diskUsageContainersContext) Active() string {
  196. c.AddHeader(activeHeader)
  197. used := 0
  198. for _, container := range c.containers {
  199. if c.isActive(*container) {
  200. used++
  201. }
  202. }
  203. return fmt.Sprintf("%d", used)
  204. }
  205. func (c *diskUsageContainersContext) Size() string {
  206. var size int64
  207. c.AddHeader(sizeHeader)
  208. for _, container := range c.containers {
  209. size += container.SizeRw
  210. }
  211. return units.HumanSize(float64(size))
  212. }
  213. func (c *diskUsageContainersContext) Reclaimable() string {
  214. var reclaimable int64
  215. var totalSize int64
  216. c.AddHeader(reclaimableHeader)
  217. for _, container := range c.containers {
  218. if !c.isActive(*container) {
  219. reclaimable += container.SizeRw
  220. }
  221. totalSize += container.SizeRw
  222. }
  223. if totalSize > 0 {
  224. return fmt.Sprintf("%s (%v%%)", units.HumanSize(float64(reclaimable)), (reclaimable*100)/totalSize)
  225. }
  226. return fmt.Sprintf("%s", units.HumanSize(float64(reclaimable)))
  227. }
  228. type diskUsageVolumesContext struct {
  229. HeaderContext
  230. verbose bool
  231. volumes []*types.Volume
  232. }
  233. func (c *diskUsageVolumesContext) Type() string {
  234. c.AddHeader(typeHeader)
  235. return "Local Volumes"
  236. }
  237. func (c *diskUsageVolumesContext) TotalCount() string {
  238. c.AddHeader(totalHeader)
  239. return fmt.Sprintf("%d", len(c.volumes))
  240. }
  241. func (c *diskUsageVolumesContext) Active() string {
  242. c.AddHeader(activeHeader)
  243. used := 0
  244. for _, v := range c.volumes {
  245. if v.UsageData.RefCount > 0 {
  246. used++
  247. }
  248. }
  249. return fmt.Sprintf("%d", used)
  250. }
  251. func (c *diskUsageVolumesContext) Size() string {
  252. var size int64
  253. c.AddHeader(sizeHeader)
  254. for _, v := range c.volumes {
  255. if v.UsageData.Size != -1 {
  256. size += v.UsageData.Size
  257. }
  258. }
  259. return units.HumanSize(float64(size))
  260. }
  261. func (c *diskUsageVolumesContext) Reclaimable() string {
  262. var reclaimable int64
  263. var totalSize int64
  264. c.AddHeader(reclaimableHeader)
  265. for _, v := range c.volumes {
  266. if v.UsageData.Size != -1 {
  267. if v.UsageData.RefCount == 0 {
  268. reclaimable += v.UsageData.Size
  269. }
  270. totalSize += v.UsageData.Size
  271. }
  272. }
  273. if totalSize > 0 {
  274. return fmt.Sprintf("%s (%v%%)", units.HumanSize(float64(reclaimable)), (reclaimable*100)/totalSize)
  275. }
  276. return fmt.Sprintf("%s", units.HumanSize(float64(reclaimable)))
  277. }