From 006682a09ccf65931497f645e2dec62bbf1f98ee Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 19 Dec 2018 16:30:57 -0800 Subject: [PATCH] add version option and also refactor powershell script to use golang templates for ease of testing and variable passing. --- builder/hyperv/common/driver.go | 2 +- builder/hyperv/common/driver_mock.go | 4 +- builder/hyperv/common/driver_ps_4.go | 4 +- builder/hyperv/common/step_create_vm.go | 3 +- builder/hyperv/iso/builder.go | 2 + builder/hyperv/vmcx/builder.go | 1 + common/powershell/hyperv/hyperv.go | 147 ++++++++++++++---------- 7 files changed, 98 insertions(+), 65 deletions(-) diff --git a/builder/hyperv/common/driver.go b/builder/hyperv/common/driver.go index e93d6a5de..c8bf506aa 100644 --- a/builder/hyperv/common/driver.go +++ b/builder/hyperv/common/driver.go @@ -73,7 +73,7 @@ type Driver interface { DeleteVirtualSwitch(string) error - CreateVirtualMachine(string, string, string, int64, int64, int64, string, uint, bool, bool) error + CreateVirtualMachine(string, string, string, int64, int64, int64, string, uint, bool, bool, string) error AddVirtualMachineHardDrive(string, string, string, int64, int64, string) error diff --git a/builder/hyperv/common/driver_mock.go b/builder/hyperv/common/driver_mock.go index 78be67196..290687829 100644 --- a/builder/hyperv/common/driver_mock.go +++ b/builder/hyperv/common/driver_mock.go @@ -136,6 +136,7 @@ type DriverMock struct { CreateVirtualMachine_Generation uint CreateVirtualMachine_DifferentialDisk bool CreateVirtualMachine_FixedVHD bool + CreateVirtualMachine_Version string CreateVirtualMachine_Err error CloneVirtualMachine_Called bool @@ -422,7 +423,7 @@ func (d *DriverMock) AddVirtualMachineHardDrive(vmName string, vhdFile string, v func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, - diffDisks bool, fixedVHD bool) error { + diffDisks bool, fixedVHD bool, version string) error { d.CreateVirtualMachine_Called = true d.CreateVirtualMachine_VmName = vmName d.CreateVirtualMachine_Path = path @@ -433,6 +434,7 @@ func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddriveP d.CreateVirtualMachine_SwitchName = switchName d.CreateVirtualMachine_Generation = generation d.CreateVirtualMachine_DifferentialDisk = diffDisks + d.CreateVirtualMachine_Version = version return d.CreateVirtualMachine_Err } diff --git a/builder/hyperv/common/driver_ps_4.go b/builder/hyperv/common/driver_ps_4.go index 38952e723..118c429ec 100644 --- a/builder/hyperv/common/driver_ps_4.go +++ b/builder/hyperv/common/driver_ps_4.go @@ -188,9 +188,9 @@ func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile stri func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, - fixedVHD bool) error { + fixedVHD bool, version string) error { return hyperv.CreateVirtualMachine(vmName, path, harddrivePath, ram, diskSize, diskBlockSize, switchName, - generation, diffDisks, fixedVHD) + generation, diffDisks, fixedVHD, version) } func (d *HypervPS4Driver) CloneVirtualMachine(cloneFromVmcxPath string, cloneFromVmName string, diff --git a/builder/hyperv/common/step_create_vm.go b/builder/hyperv/common/step_create_vm.go index 67ef7b9aa..252a2664b 100644 --- a/builder/hyperv/common/step_create_vm.go +++ b/builder/hyperv/common/step_create_vm.go @@ -34,6 +34,7 @@ type StepCreateVM struct { DifferencingDisk bool MacAddress string FixedVHD bool + Version string } func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { @@ -65,7 +66,7 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste diskBlockSize := int64(s.DiskBlockSize * 1024 * 1024) err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize, - s.SwitchName, s.Generation, s.DifferencingDisk, s.FixedVHD) + s.SwitchName, s.Generation, s.DifferencingDisk, s.FixedVHD, s.Version) if err != nil { err := fmt.Errorf("Error creating virtual machine: %s", err) state.Put("error", err) diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index b21f2f385..a9ea6e7a4 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -96,6 +96,7 @@ type Config struct { SecureBootTemplate string `mapstructure:"secure_boot_template"` EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"` TempPath string `mapstructure:"temp_path"` + Version string `mapstructure:"configuration_version"` Communicator string `mapstructure:"communicator"` @@ -418,6 +419,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe DifferencingDisk: b.config.DifferencingDisk, MacAddress: b.config.MacAddress, FixedVHD: b.config.FixedVHD, + Version: b.config.Version, }, &hypervcommon.StepEnableIntegrationService{}, diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index e207c548f..5e9754ccc 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -93,6 +93,7 @@ type Config struct { SecureBootTemplate string `mapstructure:"secure_boot_template"` EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"` TempPath string `mapstructure:"temp_path"` + Version string `mapstructure:"configuration_version"` Communicator string `mapstructure:"communicator"` diff --git a/common/powershell/hyperv/hyperv.go b/common/powershell/hyperv/hyperv.go index 23e5f505f..729dadb3b 100644 --- a/common/powershell/hyperv/hyperv.go +++ b/common/powershell/hyperv/hyperv.go @@ -3,9 +3,11 @@ package hyperv import ( "context" "errors" + "fmt" "os/exec" "strconv" "strings" + "text/template" "github.com/hashicorp/packer/common/powershell" ) @@ -204,75 +206,100 @@ Hyper-V\Set-VMFloppyDiskDrive -VMName $vmName -Path $null func CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, - diffDisks bool, fixedVHD bool) error { - - if generation == 2 { - var script = ` -param([string]$vmName, [string]$path, [string]$harddrivePath, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [long]$vhdBlockSizeBytes, [string]$switchName, [int]$generation, [string]$diffDisks) -$vhdx = $vmName + '.vhdx' -$vhdPath = Join-Path -Path $path -ChildPath $vhdx -if ($harddrivePath){ - if($diffDisks -eq "true"){ - New-VHD -Path $vhdPath -ParentPath $harddrivePath -Differencing -BlockSizeBytes $vhdBlockSizeBytes - } else { - Copy-Item -Path $harddrivePath -Destination $vhdPath + diffDisks bool, fixedVHD bool, version string) error { + + type scriptOptions struct { + VersionTag string + VMName string + Path string + HardDrivePath string + MemoryStartupBytes int64 + NewVHDSizeBytes int64 + VHDBlockSizeBytes int64 + SwitchName string + Generation uint + DiffDisks bool + FixedVHD bool } - Hyper-V\New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -VHDPath $vhdPath -SwitchName $switchName -Generation $generation -} else { - Hyper-V\New-VHD -Path $vhdPath -SizeBytes $newVHDSizeBytes -BlockSizeBytes $vhdBlockSizeBytes - Hyper-V\New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -VHDPath $vhdPath -SwitchName $switchName -Generation $generation -} -` - var ps powershell.PowerShellCmd - if err := ps.Run(script, vmName, path, harddrivePath, strconv.FormatInt(ram, 10), - strconv.FormatInt(diskSize, 10), strconv.FormatInt(diskBlockSize, 10), - switchName, strconv.FormatInt(int64(generation), 10), - strconv.FormatBool(diffDisks)); err != nil { - return err - } - return DisableAutomaticCheckpoints(vmName) - } else { - var script = ` -param([string]$vmName, [string]$path, [string]$harddrivePath, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [long]$vhdBlockSizeBytes, [string]$switchName, [string]$diffDisks, [string]$fixedVHD) -if($fixedVHD -eq "true"){ - $vhdx = $vmName + '.vhd' -} -else{ - $vhdx = $vmName + '.vhdx' -} -$vhdPath = Join-Path -Path $path -ChildPath $vhdx -if ($harddrivePath){ - if($diffDisks -eq "true"){ - New-VHD -Path $vhdPath -ParentPath $harddrivePath -Differencing -BlockSizeBytes $vhdBlockSizeBytes - } - else{ - Copy-Item -Path $harddrivePath -Destination $vhdPath + versionTag := "" + if version != "" { + versionTag = fmt.Sprintf("-Version %s", version) } - Hyper-V\New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -VHDPath $vhdPath -SwitchName $switchName + + var scriptBuilder strings.Builder + + scriptTemplate := template.Must(template.New("psScript").Parse(` +param([string]$fixedVHD) + +{{if .FixedVHD}} +$vhdx = {{ .VMName }} + '.vhd' +{{- else}} +$vhdx = {{ .VMName }} + '.vhdx' +{{- end}} + +$vhdPath = Join-Path -Path {{ .Path }} -ChildPath $vhdx +if ({{ .HardDrivePath }}){ + {{if .DiffDisks}} + New-VHD -Path $vhdPath -ParentPath {{ .HardDrivePath }} -Differencing -BlockSizeBytes {{ .VHDBlockSizeBytes }} + {{- else}} + Copy-Item -Path {{ .HardDrivePath }} -Destination $vhdPath + {{- end}} + Hyper-V\New-VM -Name {{ .VMName }} -Path {{ .Path }} -MemoryStartupBytes {{ .MemoryStartupBytes }} -VHDPath $vhdPath -SwitchName {{ .SwitchName }} {{ .VersionTag }} } else { - if($fixedVHD -eq "true"){ - Hyper-V\New-VHD -Path $vhdPath -Fixed -SizeBytes $newVHDSizeBytes - } - else { - Hyper-V\New-VHD -Path $vhdPath -SizeBytes $newVHDSizeBytes -BlockSizeBytes $vhdBlockSizeBytes + {{if .FixedVHD}} + Hyper-V\New-VHD -Path $vhdPath -Fixed -SizeBytes {{ .NewVHDSizeBytes }} + {{- else}} + Hyper-V\New-VHD -Path $vhdPath -SizeBytes {{ .NewVHDSizeBytes }} -BlockSizeBytes {{ .VHDBlockSizeBytes }} + {{- end}} + Hyper-V\New-VM -Name {{ .VMName }} -Path {{ .Path }} -MemoryStartupBytes {{ .MemoryStartupBytes }} -VHDPath $vhdPath -SwitchName {{ .SwitchName }} {{ .VersionTag }} +} +`)) + if generation == 2 { + scriptTemplate = template.Must(template.New("psScript").Parse(` +$vhdx = {{ .VMName }} + '.vhdx' +$vhdPath = Join-Path -Path {{ .Path }} -ChildPath $vhdx +if ({{ .HardDrivePath }}){ + {{if .DiffDisks}} + New-VHD -Path $vhdPath -ParentPath {{ .HardDrivePath }} -Differencing -BlockSizeBytes {{ .VHDBlockSizeBytes }} + {{- else}} + Copy-Item -Path {{ .HardDrivePath }} -Destination $vhdPath + {{- end}} + Hyper-V\New-VM -Name {{ .VMName }} -Path {{ .Path }} -MemoryStartupBytes {{ .MemoryStartupBytes }} -VHDPath $vhdPath -SwitchName {{ .SwitchName }} -Generation {{ .Generation }} {{ .VersionTag }} +} +else { + Hyper-V\New-VHD -Path $vhdPath -SizeBytes {{ .NewVHDSizeBytes }} -BlockSizeBytes {{ .VHDBlockSizeBytes }} + Hyper-V\New-VM -Name {{ .VMName }} -Path {{ .Path }} -MemoryStartupBytes {{ .MemoryStartupBytes }} -VHDPath $vhdPath -SwitchName {{ .SwitchName }} -Generation {{ .Generation }} {{ .VersionTag }} +} +`)) } - Hyper-V\New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -VHDPath $vhdPath -SwitchName $switchName -} -` - var ps powershell.PowerShellCmd - if err := ps.Run(script, vmName, path, harddrivePath, strconv.FormatInt(ram, 10), - strconv.FormatInt(diskSize, 10), strconv.FormatInt(diskBlockSize, 10), - switchName, strconv.FormatBool(diffDisks), strconv.FormatBool(fixedVHD)); err != nil { - return err - } - if err := DisableAutomaticCheckpoints(vmName); err != nil { - return err - } + scriptTemplate.Execute(&scriptBuilder, scriptOptions{ + VersionTag: versionTag, + VMName: vmName, + Path: path, + HardDrivePath: harddrivePath, + MemoryStartupBytes: ram, + NewVHDSizeBytes: diskSize, + VHDBlockSizeBytes: diskBlockSize, + SwitchName: switchName, + Generation: generation, + DiffDisks: diffDisks, + FixedVHD: fixedVHD, + }) + script := scriptBuilder.String() + var ps powershell.PowerShellCmd + if err := ps.Run(script); err != nil { + return err + } + if err := DisableAutomaticCheckpoints(vmName); err != nil { + return err + } + if generation != 2 { return DeleteAllDvdDrives(vmName) } + return nil } func DisableAutomaticCheckpoints(vmName string) error {