github.phpd.cn/hashicorp/packer@v1.3.2/builder/vmware/common/step_clean_files.go (about) 1 package common 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "path/filepath" 8 9 "github.com/hashicorp/packer/helper/multistep" 10 "github.com/hashicorp/packer/packer" 11 ) 12 13 // These are the extensions of files that are important for the function 14 // of a VMware virtual machine. Any other file is discarded as part of the 15 // build. 16 var KeepFileExtensions = []string{".nvram", ".vmdk", ".vmsd", ".vmx", ".vmxf"} 17 18 // This step removes unnecessary files from the final result. 19 // 20 // Uses: 21 // dir OutputDir 22 // ui packer.Ui 23 // 24 // Produces: 25 // <nothing> 26 type StepCleanFiles struct{} 27 28 func (StepCleanFiles) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 29 dir := state.Get("dir").(OutputDir) 30 ui := state.Get("ui").(packer.Ui) 31 32 ui.Say("Deleting unnecessary VMware files...") 33 files, err := dir.ListFiles() 34 if err != nil { 35 state.Put("error", err) 36 return multistep.ActionHalt 37 } 38 39 for _, path := range files { 40 // If the file isn't critical to the function of the 41 // virtual machine, we get rid of it. 42 keep := false 43 ext := filepath.Ext(path) 44 for _, goodExt := range KeepFileExtensions { 45 if goodExt == ext { 46 keep = true 47 break 48 } 49 } 50 51 if !keep { 52 ui.Message(fmt.Sprintf("Deleting: %s", path)) 53 if err = dir.Remove(path); err != nil { 54 // Only report the error if the file still exists. We do this 55 // because sometimes the files naturally get removed on their 56 // own as VMware does its own cleanup. 57 if _, serr := os.Stat(path); serr == nil || !os.IsNotExist(serr) { 58 state.Put("error", err) 59 return multistep.ActionHalt 60 } 61 } 62 } 63 } 64 65 return multistep.ActionContinue 66 } 67 68 func (StepCleanFiles) Cleanup(multistep.StateBag) {}