Simplify handling of disks by collating requirements and unifying ops

This commit is contained in:
DanHam 2018-04-24 23:23:48 +01:00
parent 879319a76c
commit c48a7889f9
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E
3 changed files with 42 additions and 53 deletions

View File

@ -14,7 +14,7 @@ import (
//
// Uses:
// driver Driver
// full_disk_path string
// disk_full_paths ([]string) - The full paths to all created disks
// ui packer.Ui
//
// Produces:
@ -26,28 +26,19 @@ type StepCompactDisk struct {
func (s StepCompactDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
full_disk_path := state.Get("full_disk_path").(string)
diskPaths := state.Get("disk_full_paths").([]string)
if s.Skip {
log.Println("Skipping disk compaction step...")
return multistep.ActionContinue
}
ui.Say("Compacting the disk image")
if err := driver.CompactDisk(full_disk_path); err != nil {
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
return multistep.ActionHalt
}
if state.Get("additional_disk_paths") != nil {
if moreDisks := state.Get("additional_disk_paths").([]string); len(moreDisks) > 0 {
for i, path := range moreDisks {
ui.Say(fmt.Sprintf("Compacting additional disk image %d", i+1))
if err := driver.CompactDisk(path); err != nil {
state.Put("error", fmt.Errorf("Error compacting additional disk %d: %s", i+1, err))
return multistep.ActionHalt
}
}
ui.Say("Compacting all attached disk images")
for i, diskPath := range diskPaths {
ui.Message(fmt.Sprintf("Compacting disk image %d", i+1))
if err := driver.CompactDisk(diskPath); err != nil {
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
return multistep.ActionHalt
}
}

View File

@ -3,6 +3,7 @@ package iso
import (
"context"
"fmt"
"log"
"path/filepath"
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
@ -18,7 +19,7 @@ import (
// ui packer.Ui
//
// Produces:
// full_disk_path (string) - The full path to the created disk.
// disk_full_paths ([]string) - The full paths to all created disks
type stepCreateDisk struct{}
func (stepCreateDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
@ -26,39 +27,39 @@ func (stepCreateDisk) Run(_ context.Context, state multistep.StateBag) multistep
driver := state.Get("driver").(vmwcommon.Driver)
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating virtual machine disk")
full_disk_path := filepath.Join(config.OutputDir, config.DiskName+".vmdk")
if err := driver.CreateDisk(full_disk_path, fmt.Sprintf("%dM", config.DiskSize), config.DiskAdapterType, config.DiskTypeId); err != nil {
err := fmt.Errorf("Error creating disk: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("full_disk_path", full_disk_path)
ui.Say("Creating required virtual machine disks")
// Users can configure disks at several locations in the template so
// first collate all the disk requirements
var diskPaths, diskSizes []string
// The 'main' or 'default' disk
diskPaths = append(diskPaths, filepath.Join(config.OutputDir, config.DiskName+".vmdk"))
diskSizes = append(diskSizes, fmt.Sprintf("%dM", uint64(config.DiskSize)))
// Additional disks
if len(config.AdditionalDiskSize) > 0 {
// stash the disk paths we create
additional_paths := make([]string, len(config.AdditionalDiskSize))
ui.Say("Creating additional hard drives...")
for i, additionalsize := range config.AdditionalDiskSize {
additionalpath := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d.vmdk", config.DiskName, i+1))
size := fmt.Sprintf("%dM", uint64(additionalsize))
if err := driver.CreateDisk(additionalpath, size, config.DiskAdapterType, config.DiskTypeId); err != nil {
err := fmt.Errorf("Error creating additional disk: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
additional_paths[i] = additionalpath
for i, diskSize := range config.AdditionalDiskSize {
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d.vmdk", config.DiskName, i+1))
diskPaths = append(diskPaths, path)
size := fmt.Sprintf("%dM", uint64(diskSize))
diskSizes = append(diskSizes, size)
}
state.Put("additional_disk_paths", additional_paths)
}
// Create all required disks
for i, diskPath := range diskPaths {
log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskPath, diskSizes[i])
// Additional disks currently use the same adapter type and disk
// type as specified for the main disk
if err := driver.CreateDisk(diskPath, diskSizes[i], config.DiskAdapterType, config.DiskTypeId); err != nil {
err := fmt.Errorf("Error creating disk: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
// Stash the disk paths so we can retrieve later e.g. when compacting
state.Put("disk_full_paths", diskPaths)
return multistep.ActionContinue
}

View File

@ -115,13 +115,13 @@ func (s *StepCloneVMX) Run(_ context.Context, state multistep.StateBag) multiste
}
// Write out the relative, host filesystem paths to the disks
var diskFullPaths []string
var diskPaths []string
for _, diskFilename := range diskFilenames {
log.Printf("Found attached disk with filename: %s", diskFilename)
diskFullPaths = append(diskFullPaths, filepath.Join(s.OutputDir, diskFilename))
diskPaths = append(diskPaths, filepath.Join(s.OutputDir, diskFilename))
}
if len(diskFullPaths) == 0 {
if len(diskPaths) == 0 {
state.Put("error", fmt.Errorf("Could not enumerate disk info from the vmx file"))
return multistep.ActionHalt
}
@ -139,10 +139,7 @@ func (s *StepCloneVMX) Run(_ context.Context, state multistep.StateBag) multiste
// Stash all required information in our state bag
state.Put("vmx_path", vmxPath)
// What disks get assigned to what key doesn't actually matter here
// since it's unimportant to the way the disk compaction step works
state.Put("full_disk_path", diskFullPaths[0])
state.Put("additional_disk_paths", diskFullPaths[1:])
state.Put("disk_full_paths", diskPaths)
state.Put("vmnetwork", networkType)
return multistep.ActionContinue