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

pull/7136/head
Megan Marsh 8 years ago
parent e666b60d16
commit 006682a09c

@ -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

@ -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
}

@ -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,

@ -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)

@ -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{},

@ -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"`

@ -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 {

Loading…
Cancel
Save