github.imxd.top/openshift/source-to-image@v1.2.0/pkg/build/cleanup.go (about) 1 package build 2 3 import ( 4 "github.com/openshift/source-to-image/pkg/api" 5 "github.com/openshift/source-to-image/pkg/docker" 6 "github.com/openshift/source-to-image/pkg/util/fs" 7 utillog "github.com/openshift/source-to-image/pkg/util/log" 8 ) 9 10 var log = utillog.StderrLog 11 12 // DefaultCleaner provides a cleaner for most STI build use-cases. It cleans the 13 // temporary directories created by STI build and it also cleans the temporary 14 // Docker images produced by LayeredBuild 15 type DefaultCleaner struct { 16 fs fs.FileSystem 17 docker docker.Docker 18 } 19 20 // NewDefaultCleaner creates a new instance of the default Cleaner implementation 21 func NewDefaultCleaner(fs fs.FileSystem, docker docker.Docker) Cleaner { 22 return &DefaultCleaner{ 23 fs: fs, 24 docker: docker, 25 } 26 } 27 28 // Cleanup removes the temporary directories where the sources were stored for build. 29 func (c *DefaultCleaner) Cleanup(config *api.Config) { 30 if config.PreserveWorkingDir { 31 log.V(2).Infof("Temporary directory %q will be saved, not deleted", config.WorkingDir) 32 } else { 33 log.V(2).Infof("Removing temporary directory %s", config.WorkingDir) 34 if err := c.fs.RemoveDirectory(config.WorkingDir); err != nil { 35 log.Warningf("Error removing temporary directory %q: %v", config.WorkingDir, err) 36 } 37 } 38 if config.LayeredBuild { 39 // config.LayeredBuild is true only when layered build was finished successfully. 40 // Also in this case config.BuilderImage contains name of the new just built image, 41 // not the original one that was specified by the user. 42 log.V(2).Infof("Removing temporary image %s", config.BuilderImage) 43 if err := c.docker.RemoveImage(config.BuilderImage); err != nil { 44 log.Warningf("Error removing temporary image %s: %v", config.BuilderImage, err) 45 } 46 } 47 }