disk_usage.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. if i.VirtualSize == -1 || i.SharedSize == -1 {
  169. continue
  170. }
  171. used += i.VirtualSize - i.SharedSize
  172. }
  173. }
  174. reclaimable := c.totalSize - used
  175. if c.totalSize > 0 {
  176. return fmt.Sprintf("%s (%v%%)", units.HumanSize(float64(reclaimable)), (reclaimable*100)/c.totalSize)
  177. }
  178. return fmt.Sprintf("%s", units.HumanSize(float64(reclaimable)))
  179. }
  180. type diskUsageContainersContext struct {
  181. HeaderContext
  182. verbose bool
  183. containers []*types.Container
  184. }
  185. func (c *diskUsageContainersContext) Type() string {
  186. c.AddHeader(typeHeader)
  187. return "Containers"
  188. }
  189. func (c *diskUsageContainersContext) TotalCount() string {
  190. c.AddHeader(totalHeader)
  191. return fmt.Sprintf("%d", len(c.containers))
  192. }
  193. func (c *diskUsageContainersContext) isActive(container types.Container) bool {
  194. return strings.Contains(container.State, "running") ||
  195. strings.Contains(container.State, "paused") ||
  196. strings.Contains(container.State, "restarting")
  197. }
  198. func (c *diskUsageContainersContext) Active() string {
  199. c.AddHeader(activeHeader)
  200. used := 0
  201. for _, container := range c.containers {
  202. if c.isActive(*container) {
  203. used++
  204. }
  205. }
  206. return fmt.Sprintf("%d", used)
  207. }
  208. func (c *diskUsageContainersContext) Size() string {
  209. var size int64
  210. c.AddHeader(sizeHeader)
  211. for _, container := range c.containers {
  212. size += container.SizeRw
  213. }
  214. return units.HumanSize(float64(size))
  215. }
  216. func (c *diskUsageContainersContext) Reclaimable() string {
  217. var reclaimable int64
  218. var totalSize int64
  219. c.AddHeader(reclaimableHeader)
  220. for _, container := range c.containers {
  221. if !c.isActive(*container) {
  222. reclaimable += container.SizeRw
  223. }
  224. totalSize += container.SizeRw
  225. }
  226. if totalSize > 0 {
  227. return fmt.Sprintf("%s (%v%%)", units.HumanSize(float64(reclaimable)), (reclaimable*100)/totalSize)
  228. }
  229. return fmt.Sprintf("%s", units.HumanSize(float64(reclaimable)))
  230. }
  231. type diskUsageVolumesContext struct {
  232. HeaderContext
  233. verbose bool
  234. volumes []*types.Volume
  235. }
  236. func (c *diskUsageVolumesContext) Type() string {
  237. c.AddHeader(typeHeader)
  238. return "Local Volumes"
  239. }
  240. func (c *diskUsageVolumesContext) TotalCount() string {
  241. c.AddHeader(totalHeader)
  242. return fmt.Sprintf("%d", len(c.volumes))
  243. }
  244. func (c *diskUsageVolumesContext) Active() string {
  245. c.AddHeader(activeHeader)
  246. used := 0
  247. for _, v := range c.volumes {
  248. if v.UsageData.RefCount > 0 {
  249. used++
  250. }
  251. }
  252. return fmt.Sprintf("%d", used)
  253. }
  254. func (c *diskUsageVolumesContext) Size() string {
  255. var size int64
  256. c.AddHeader(sizeHeader)
  257. for _, v := range c.volumes {
  258. if v.UsageData.Size != -1 {
  259. size += v.UsageData.Size
  260. }
  261. }
  262. return units.HumanSize(float64(size))
  263. }
  264. func (c *diskUsageVolumesContext) Reclaimable() string {
  265. var reclaimable int64
  266. var totalSize int64
  267. c.AddHeader(reclaimableHeader)
  268. for _, v := range c.volumes {
  269. if v.UsageData.Size != -1 {
  270. if v.UsageData.RefCount == 0 {
  271. reclaimable += v.UsageData.Size
  272. }
  273. totalSize += v.UsageData.Size
  274. }
  275. }
  276. if totalSize > 0 {
  277. return fmt.Sprintf("%s (%v%%)", units.HumanSize(float64(reclaimable)), (reclaimable*100)/totalSize)
  278. }
  279. return fmt.Sprintf("%s", units.HumanSize(float64(reclaimable)))
  280. }