add version option and also refactor powershell script to use golang templates for ease of testing and variable passing.

This commit is contained in:
Megan Marsh 2018-12-19 16:30:57 -08:00
parent e666b60d16
commit 006682a09c
7 changed files with 99 additions and 66 deletions

View File

@ -73,7 +73,7 @@ type Driver interface {
DeleteVirtualSwitch(string) error 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 AddVirtualMachineHardDrive(string, string, string, int64, int64, string) error

View File

@ -136,6 +136,7 @@ type DriverMock struct {
CreateVirtualMachine_Generation uint CreateVirtualMachine_Generation uint
CreateVirtualMachine_DifferentialDisk bool CreateVirtualMachine_DifferentialDisk bool
CreateVirtualMachine_FixedVHD bool CreateVirtualMachine_FixedVHD bool
CreateVirtualMachine_Version string
CreateVirtualMachine_Err error CreateVirtualMachine_Err error
CloneVirtualMachine_Called bool 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, func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddrivePath string,
ram int64, diskSize int64, diskBlockSize int64, switchName string, generation uint, 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_Called = true
d.CreateVirtualMachine_VmName = vmName d.CreateVirtualMachine_VmName = vmName
d.CreateVirtualMachine_Path = path d.CreateVirtualMachine_Path = path
@ -433,6 +434,7 @@ func (d *DriverMock) CreateVirtualMachine(vmName string, path string, harddriveP
d.CreateVirtualMachine_SwitchName = switchName d.CreateVirtualMachine_SwitchName = switchName
d.CreateVirtualMachine_Generation = generation d.CreateVirtualMachine_Generation = generation
d.CreateVirtualMachine_DifferentialDisk = diffDisks d.CreateVirtualMachine_DifferentialDisk = diffDisks
d.CreateVirtualMachine_Version = version
return d.CreateVirtualMachine_Err return d.CreateVirtualMachine_Err
} }

View File

@ -188,9 +188,9 @@ func (d *HypervPS4Driver) AddVirtualMachineHardDrive(vmName string, vhdFile stri
func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64, func (d *HypervPS4Driver) CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64,
diskSize int64, diskBlockSize int64, switchName string, generation uint, diffDisks bool, 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, 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, func (d *HypervPS4Driver) CloneVirtualMachine(cloneFromVmcxPath string, cloneFromVmName string,

View File

@ -34,6 +34,7 @@ type StepCreateVM struct {
DifferencingDisk bool DifferencingDisk bool
MacAddress string MacAddress string
FixedVHD bool FixedVHD bool
Version string
} }
func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 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) diskBlockSize := int64(s.DiskBlockSize * 1024 * 1024)
err := driver.CreateVirtualMachine(s.VMName, path, harddrivePath, ramSize, diskSize, diskBlockSize, 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 { if err != nil {
err := fmt.Errorf("Error creating virtual machine: %s", err) err := fmt.Errorf("Error creating virtual machine: %s", err)
state.Put("error", err) state.Put("error", err)

View File

@ -96,6 +96,7 @@ type Config struct {
SecureBootTemplate string `mapstructure:"secure_boot_template"` SecureBootTemplate string `mapstructure:"secure_boot_template"`
EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"` EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"`
TempPath string `mapstructure:"temp_path"` TempPath string `mapstructure:"temp_path"`
Version string `mapstructure:"configuration_version"`
Communicator string `mapstructure:"communicator"` 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, DifferencingDisk: b.config.DifferencingDisk,
MacAddress: b.config.MacAddress, MacAddress: b.config.MacAddress,
FixedVHD: b.config.FixedVHD, FixedVHD: b.config.FixedVHD,
Version: b.config.Version,
}, },
&hypervcommon.StepEnableIntegrationService{}, &hypervcommon.StepEnableIntegrationService{},

View File

@ -93,6 +93,7 @@ type Config struct {
SecureBootTemplate string `mapstructure:"secure_boot_template"` SecureBootTemplate string `mapstructure:"secure_boot_template"`
EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"` EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"`
TempPath string `mapstructure:"temp_path"` TempPath string `mapstructure:"temp_path"`
Version string `mapstructure:"configuration_version"`
Communicator string `mapstructure:"communicator"` Communicator string `mapstructure:"communicator"`

View File

@ -3,9 +3,11 @@ package hyperv
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"text/template"
"github.com/hashicorp/packer/common/powershell" "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, func CreateVirtualMachine(vmName string, path string, harddrivePath string, ram int64,
diskSize int64, diskBlockSize int64, switchName string, generation uint, diskSize int64, diskBlockSize int64, switchName string, generation uint,
diffDisks bool, fixedVHD bool) error { 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
}
versionTag := ""
if version != "" {
versionTag = fmt.Sprintf("-Version %s", version)
}
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}}
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 { if generation == 2 {
var script = ` scriptTemplate = template.Must(template.New("psScript").Parse(`
param([string]$vmName, [string]$path, [string]$harddrivePath, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [long]$vhdBlockSizeBytes, [string]$switchName, [int]$generation, [string]$diffDisks) $vhdx = {{ .VMName }} + '.vhdx'
$vhdx = $vmName + '.vhdx' $vhdPath = Join-Path -Path {{ .Path }} -ChildPath $vhdx
$vhdPath = Join-Path -Path $path -ChildPath $vhdx if ({{ .HardDrivePath }}){
if ($harddrivePath){ {{if .DiffDisks}}
if($diffDisks -eq "true"){ New-VHD -Path $vhdPath -ParentPath {{ .HardDrivePath }} -Differencing -BlockSizeBytes {{ .VHDBlockSizeBytes }}
New-VHD -Path $vhdPath -ParentPath $harddrivePath -Differencing -BlockSizeBytes $vhdBlockSizeBytes {{- else}}
} else { Copy-Item -Path {{ .HardDrivePath }} -Destination $vhdPath
Copy-Item -Path $harddrivePath -Destination $vhdPath {{- end}}
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 -Generation $generation else {
} else { Hyper-V\New-VHD -Path $vhdPath -SizeBytes {{ .NewVHDSizeBytes }} -BlockSizeBytes {{ .VHDBlockSizeBytes }}
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 -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) scriptTemplate.Execute(&scriptBuilder, scriptOptions{
} else { VersionTag: versionTag,
var script = ` VMName: vmName,
param([string]$vmName, [string]$path, [string]$harddrivePath, [long]$memoryStartupBytes, [long]$newVHDSizeBytes, [long]$vhdBlockSizeBytes, [string]$switchName, [string]$diffDisks, [string]$fixedVHD) Path: path,
if($fixedVHD -eq "true"){ HardDrivePath: harddrivePath,
$vhdx = $vmName + '.vhd' MemoryStartupBytes: ram,
} NewVHDSizeBytes: diskSize,
else{ VHDBlockSizeBytes: diskBlockSize,
$vhdx = $vmName + '.vhdx' SwitchName: switchName,
} Generation: generation,
$vhdPath = Join-Path -Path $path -ChildPath $vhdx DiffDisks: diffDisks,
if ($harddrivePath){ FixedVHD: fixedVHD,
if($diffDisks -eq "true"){ })
New-VHD -Path $vhdPath -ParentPath $harddrivePath -Differencing -BlockSizeBytes $vhdBlockSizeBytes script := scriptBuilder.String()
}
else{
Copy-Item -Path $harddrivePath -Destination $vhdPath
}
Hyper-V\New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -VHDPath $vhdPath -SwitchName $switchName
} 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
}
Hyper-V\New-VM -Name $vmName -Path $path -MemoryStartupBytes $memoryStartupBytes -VHDPath $vhdPath -SwitchName $switchName
}
`
var ps powershell.PowerShellCmd var ps powershell.PowerShellCmd
if err := ps.Run(script, vmName, path, harddrivePath, strconv.FormatInt(ram, 10), if err := ps.Run(script); err != nil {
strconv.FormatInt(diskSize, 10), strconv.FormatInt(diskBlockSize, 10),
switchName, strconv.FormatBool(diffDisks), strconv.FormatBool(fixedVHD)); err != nil {
return err return err
} }
if err := DisableAutomaticCheckpoints(vmName); err != nil { if err := DisableAutomaticCheckpoints(vmName); err != nil {
return err return err
} }
if generation != 2 {
return DeleteAllDvdDrives(vmName) return DeleteAllDvdDrives(vmName)
} }
return nil
} }
func DisableAutomaticCheckpoints(vmName string) error { func DisableAutomaticCheckpoints(vmName string) error {