@@ -3,7 +3,9 @@ package cmd
33import (
44 "fmt"
55 "os"
6+ "strings"
67
8+ log "github.com/sirupsen/logrus"
79 "github.com/spf13/cobra"
810
911 "github.com/cvmfs/docker-graphdriver/daemon/lib"
@@ -17,9 +19,42 @@ var garbageCollectionCmd = &cobra.Command{
1719 Use : "garbage-collection" ,
1820 Short : "Removes layers that are not necessary anymore" ,
1921 Aliases : []string {"gc" },
22+ Args : cobra .ExactArgs (1 ),
2023 Run : func (cmd * cobra.Command , args []string ) {
24+
2125 fmt .Println ("Start" )
22- lib .RemoveUselessLayers ()
26+ repo := args [0 ]
27+ llog := func (l * log.Entry ) * log.Entry {
28+ return l .WithFields (log.Fields {"action" : "garbage collect" ,
29+ "repo" : repo ,
30+ })
31+ }
32+
33+ manifestToRemove , err := lib .FindImageToGarbageCollect (repo )
34+ if err != nil {
35+ llog (lib .LogE (err )).Warning (
36+ "Error in finding the image to remove from the scheduler, goin on..." )
37+ }
38+ var images2layers map [string ][]string
39+
40+ for _ , manifest := range manifestToRemove {
41+ digest := strings .Split (manifest .Config .Digest , ":" )[1 ]
42+ for _ , layerStruct := range manifest .Layers {
43+ layerName := strings .Split (layerStruct .Digest , ":" )[1 ]
44+ images2layers [digest ] = append (images2layers [digest ], layerName )
45+ }
46+ }
47+
48+ for image , layers := range images2layers {
49+ for _ , layer := range layers {
50+ err = lib .GarbageCollectSingleLayer (repo , image , layer )
51+ if err != nil {
52+ llog (lib .LogE (err )).Warning (
53+ "Error in removing a single layer from the repository, going on..." )
54+ }
55+ }
56+ }
57+
2358 os .Exit (0 )
2459 },
2560}
0 commit comments