2013-06-29 16:23:42 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// These are the extensions of files that are important for the function
|
|
|
|
// of a VMware virtual machine. Any other file is discarded as part of the
|
|
|
|
// build.
|
|
|
|
var KeepFileExtensions = []string{".nvram", ".vmdk", ".vmsd", ".vmx", ".vmxf"}
|
|
|
|
|
|
|
|
// This step removes unnecessary files from the final result.
|
|
|
|
//
|
|
|
|
// Uses:
|
2013-11-12 15:49:57 -05:00
|
|
|
// dir OutputDir
|
2013-06-29 16:23:42 -04:00
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
|
|
|
type stepCleanFiles struct{}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
func (stepCleanFiles) Run(state multistep.StateBag) multistep.StepAction {
|
2013-11-12 15:49:57 -05:00
|
|
|
dir := state.Get("dir").(OutputDir)
|
2013-08-31 15:50:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-29 16:23:42 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting unnecessary VMware files...")
|
2013-11-12 15:49:57 -05:00
|
|
|
files, err := dir.ListFiles()
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-29 16:23:42 -04:00
|
|
|
|
2013-11-12 15:49:57 -05:00
|
|
|
for _, path := range files {
|
|
|
|
// If the file isn't critical to the function of the
|
|
|
|
// virtual machine, we get rid of it.
|
|
|
|
keep := false
|
|
|
|
ext := filepath.Ext(path)
|
|
|
|
for _, goodExt := range KeepFileExtensions {
|
|
|
|
if goodExt == ext {
|
|
|
|
keep = true
|
|
|
|
break
|
2013-06-29 16:23:42 -04:00
|
|
|
}
|
2013-11-12 15:49:57 -05:00
|
|
|
}
|
2013-06-29 16:23:42 -04:00
|
|
|
|
2013-11-12 15:49:57 -05:00
|
|
|
if !keep {
|
|
|
|
ui.Message(fmt.Sprintf("Deleting: %s", path))
|
|
|
|
if err = dir.Remove(path); err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2013-06-29 16:23:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
func (stepCleanFiles) Cleanup(multistep.StateBag) {}
|