Remove disk compaction from the export step

This commit is contained in:
DanHam 2018-07-07 12:27:09 +01:00
parent c6b9d9ce90
commit da2df69301
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E
3 changed files with 13 additions and 39 deletions

View File

@ -10,51 +10,27 @@ import (
) )
type StepExportVm struct { type StepExportVm struct {
OutputDir string OutputDir string
SkipCompaction bool SkipExport bool
SkipExport bool
} }
func (s *StepExportVm) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepExportVm) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver) driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
// Get the VM name; Get the temp directory used to store the VMs files
// during the build process
var vmName, tmpPath string
if v, ok := state.GetOk("vmName"); ok {
vmName = v.(string)
}
if v, ok := state.GetOk("packerTempDir"); ok {
tmpPath = v.(string)
}
// Compact disks first so the export process has less to do
if s.SkipCompaction {
ui.Say("Skipping disk compaction...")
} else {
ui.Say("Compacting disks...")
// CompactDisks searches for all VHD/VHDX files under the supplied
// path and runs the compacting process on each of them. If no disks
// are found under the supplied path this is treated as a 'soft' error
// and a warning message is printed. All other errors halt the build.
result, err := driver.CompactDisks(tmpPath)
if err != nil {
err := fmt.Errorf("Error compacting disks: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Report disk compaction results/warn if no disks were found
ui.Message(result)
}
if s.SkipExport { if s.SkipExport {
ui.Say("Skipping export of virtual machine...") ui.Say("Skipping export of virtual machine...")
return multistep.ActionContinue return multistep.ActionContinue
} }
ui.Say("Exporting virtual machine...") ui.Say("Exporting virtual machine...")
// The VM name is needed for the export command
var vmName string
if v, ok := state.GetOk("vmName"); ok {
vmName = v.(string)
}
// The export process exports the VM to a folder named 'vmName' under // The export process exports the VM to a folder named 'vmName' under
// the output directory. This contains the usual 'Snapshots', 'Virtual // the output directory. This contains the usual 'Snapshots', 'Virtual
// Hard Disks' and 'Virtual Machines' directories. // Hard Disks' and 'Virtual Machines' directories.

View File

@ -467,9 +467,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Generation: b.config.Generation, Generation: b.config.Generation,
}, },
&hypervcommon.StepExportVm{ &hypervcommon.StepExportVm{
OutputDir: b.config.OutputDir, OutputDir: b.config.OutputDir,
SkipCompaction: b.config.SkipCompaction, SkipExport: b.config.SkipExport,
SkipExport: b.config.SkipExport,
}, },
// the clean up actions for each step will be executed reverse order // the clean up actions for each step will be executed reverse order

View File

@ -476,9 +476,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Generation: b.config.Generation, Generation: b.config.Generation,
}, },
&hypervcommon.StepExportVm{ &hypervcommon.StepExportVm{
OutputDir: b.config.OutputDir, OutputDir: b.config.OutputDir,
SkipCompaction: b.config.SkipCompaction, SkipExport: b.config.SkipExport,
SkipExport: b.config.SkipExport,
}, },
// the clean up actions for each step will be executed reverse order // the clean up actions for each step will be executed reverse order