2013-12-25 01:09:22 -05:00
|
|
|
package common
|
2013-07-01 22:25:33 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-01-19 19:18:44 -05:00
|
|
|
"log"
|
2013-07-01 22:25:33 -04:00
|
|
|
)
|
|
|
|
|
2013-07-02 20:22:11 -04:00
|
|
|
// This step compacts the virtual disk for the VM unless the "skip_compaction"
|
|
|
|
// boolean is true.
|
2013-07-01 22:25:33 -04:00
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// driver Driver
|
2013-07-02 12:15:52 -04:00
|
|
|
// full_disk_path string
|
2013-07-01 22:25:33 -04:00
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// <nothing>
|
2013-12-25 01:09:22 -05:00
|
|
|
type StepCompactDisk struct {
|
|
|
|
Skip bool
|
|
|
|
}
|
2013-07-01 22:25:33 -04:00
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-12-25 01:09:22 -05:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-08-31 15:50:25 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
full_disk_path := state.Get("full_disk_path").(string)
|
2013-07-01 22:25:33 -04:00
|
|
|
|
2013-12-25 01:09:22 -05:00
|
|
|
if s.Skip {
|
2013-07-02 20:22:11 -04:00
|
|
|
log.Println("Skipping disk compaction step...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-07-01 22:25:33 -04:00
|
|
|
ui.Say("Compacting the disk image")
|
2013-07-02 01:12:57 -04:00
|
|
|
if err := driver.CompactDisk(full_disk_path); err != nil {
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
|
2013-07-01 22:25:33 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-06-17 23:23:04 -04:00
|
|
|
|
2014-03-04 15:48:15 -05:00
|
|
|
if state.Get("additional_disk_paths") != nil {
|
|
|
|
if moreDisks := state.Get("additional_disk_paths").([]string); len(moreDisks) > 0 {
|
|
|
|
for i, path := range moreDisks {
|
2015-06-17 23:23:04 -04:00
|
|
|
ui.Say(fmt.Sprintf("Compacting additional disk image %d", i+1))
|
2014-03-04 15:48:15 -05:00
|
|
|
if err := driver.CompactDisk(path); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error compacting additional disk %d: %s", i+1, err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-03-04 13:23:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-01 22:25:33 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-25 01:09:22 -05:00
|
|
|
func (StepCompactDisk) Cleanup(multistep.StateBag) {}
|