disk_usage.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 formatter, 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.ParseNormalizedNamed(i.RepoTags[0])
  83. if err != nil {
  84. continue
  85. }
  86. if nt, ok := ref.(reference.NamedTagged); ok {
  87. repo = reference.FamiliarName(ref)
  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) MarshalJSON() ([]byte, error) {
  142. return marshalJSON(c)
  143. }
  144. func (c *diskUsageImagesContext) Type() string {
  145. c.AddHeader(typeHeader)
  146. return "Images"
  147. }
  148. func (c *diskUsageImagesContext) TotalCount() string {
  149. c.AddHeader(totalHeader)
  150. return fmt.Sprintf("%d", len(c.images))
  151. }
  152. func (c *diskUsageImagesContext) Active() string {
  153. c.AddHeader(activeHeader)
  154. used := 0
  155. for _, i := range c.images {
  156. if i.Containers > 0 {
  157. used++
  158. }
  159. }
  160. return fmt.Sprintf("%d", used)
  161. }
  162. func (c *diskUsageImagesContext) Size() string {
  163. c.AddHeader(sizeHeader)
  164. return units.HumanSize(float64(c.totalSize))
  165. }
  166. func (c *diskUsageImagesContext) Reclaimable() string {
  167. var used int64
  168. c.AddHeader(reclaimableHeader)
  169. for _, i := range c.images {
  170. if i.Containers != 0 {
  171. if i.VirtualSize == -1 || i.SharedSize == -1 {
  172. continue
  173. }
  174. used += i.VirtualSize - i.SharedSize
  175. }
  176. }
  177. reclaimable := c.totalSize - used
  178. if c.totalSize > 0 {
  179. return fmt.Sprintf("%s (%v%%)", units.HumanSize(float64(reclaimable)), (reclaimable*100)/c.totalSize)
  180. }
  181. return fmt.Sprintf("%s", units.HumanSize(float64(reclaimable)))
  182. }
  183. type diskUsageContainersContext struct {
  184. HeaderContext
  185. verbose bool
  186. containers []*types.Container
  187. }
  188. func (c *diskUsageContainersContext) MarshalJSON() ([]byte, error) {
  189. return marshalJSON(c)
  190. }
  191. func (c *diskUsageContainersContext) Type() string {
  192. c.AddHeader(typeHeader)
  193. return "Containers"
  194. }
  195. func (c *diskUsageContainersContext) TotalCount() string {
  196. c.AddHeader(totalHeader)
  197. return fmt.Sprintf("%d", len(c.containers))
  198. }
  199. func (c *diskUsageContainersContext) isActive(container types.Container) bool {
  200. return strings.Contains(container.State, "running") ||
  201. strings.Contains(container.State, "paused") ||
  202. strings.Contains(container.State, "restarting")
  203. }
  204. func (c *diskUsageContainersContext) Active() string {
  205. c.AddHeader(activeHeader)
  206. used := 0
  207. for _, container := range c.containers {
  208. if c.isActive(*container) {
  209. used++
  210. }
  211. }
  212. return fmt.Sprintf("%d", used)
  213. }
  214. func (c *diskUsageContainersContext) Size() string {
  215. var size int64
  216. c.AddHeader(sizeHeader)
  217. for _, container := range c.containers {
  218. size += container.SizeRw
  219. }
  220. return units.HumanSize(float64(size))
  221. }
  222. func (c *diskUsageContainersContext) Reclaimable() string {
  223. var reclaimable int64
  224. var totalSize int64
  225. c.AddHeader(reclaimableHeader)
  226. for _, container := range c.containers {
  227. if !c.isActive(*container) {
  228. reclaimable += container.SizeRw
  229. }
  230. totalSize += container.SizeRw
  231. }
  232. if totalSize > 0 {
  233. return fmt.Sprintf("%s (%v%%)", units.HumanSize(float64(reclaimable)), (reclaimable*100)/totalSize)
  234. }
  235. return fmt.Sprintf("%s", units.HumanSize(float64(reclaimable)))
  236. }
  237. type diskUsageVolumesContext struct {
  238. HeaderContext
  239. verbose bool
  240. volumes []*types.Volume
  241. }
  242. func (c *diskUsageVolumesContext) MarshalJSON() ([]byte, error) {
  243. return marshalJSON(c)
  244. }
  245. func (c *diskUsageVolumesContext) Type() string {
  246. c.AddHeader(typeHeader)
  247. return "Local Volumes"
  248. }
  249. func (c *diskUsageVolumesContext) TotalCount() string {
  250. c.AddHeader(totalHeader)
  251. return fmt.Sprintf("%d", len(c.volumes))
  252. }
  253. func (c *diskUsageVolumesContext) Active() string {
  254. c.AddHeader(activeHeader)
  255. used := 0
  256. for _, v := range c.volumes {
  257. if v.UsageData.RefCount > 0 {
  258. used++
  259. }
  260. }
  261. return fmt.Sprintf("%d", used)
  262. }
  263. func (c *diskUsageVolumesContext) Size() string {
  264. var size int64
  265. c.AddHeader(sizeHeader)
  266. for _, v := range c.volumes {
  267. if v.UsageData.Size != -1 {
  268. size += v.UsageData.Size
  269. }
  270. }
  271. return units.HumanSize(float64(size))
  272. }
  273. func (c *diskUsageVolumesContext) Reclaimable() string {
  274. var reclaimable int64
  275. var totalSize int64
  276. c.AddHeader(reclaimableHeader)
  277. for _, v := range c.volumes {
  278. if v.UsageData.Size != -1 {
  279. if v.UsageData.RefCount == 0 {
  280. reclaimable += v.UsageData.Size
  281. }
  282. totalSize += v.UsageData.Size
  283. }
  284. }
  285. if totalSize > 0 {
  286. return fmt.Sprintf("%s (%v%%)", units.HumanSize(float64(reclaimable)), (reclaimable*100)/totalSize)
  287. }
  288. return fmt.Sprintf("%s", units.HumanSize(float64(reclaimable)))
  289. }