|
| 1 | +package vm |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "log" |
| 6 | + "text/template" |
| 7 | + |
| 8 | + "github.com/hashicorp/packer/common/powershell" |
| 9 | + "github.com/hashicorp/packer/common/powershell/hyperv" |
| 10 | +) |
| 11 | + |
| 12 | +type HypervInstance struct { |
| 13 | + Name string |
| 14 | + |
| 15 | + defaultBootOrder []string |
| 16 | + nextBootOrder []string |
| 17 | + changeBootOrder bool |
| 18 | +} |
| 19 | + |
| 20 | +const powerShellTemplateGen1 = ` |
| 21 | +{{$out := .}} |
| 22 | +param([string]$vmName) |
| 23 | +Hyper-V\Set-VMBios -VMName $vmName -StartupOrder @({{ range $index, $device := .Devices}}{{if $index}},{{end}}{{index $out.DeviceMap 0 $device}}{{end}}) |
| 24 | +` |
| 25 | + |
| 26 | +const powerShellTemplateGen2 = ` |
| 27 | +{{$out := .}} |
| 28 | +param([string]$vmName) |
| 29 | +{{ range $index, $device := .Devices}} |
| 30 | +{{index $out.GettersMap $device}} |
| 31 | +{{end}} |
| 32 | +Hyper-V\Set-VMFirmware -VMName $vmName -BootOrder @({{ range $index, $device := .Devices}}{{if $index}},{{end}}{{index $out.DeviceMap 1 $device}}{{end}}) |
| 33 | +` |
| 34 | + |
| 35 | +type templateParameters struct { |
| 36 | + Devices []string |
| 37 | + DeviceMap []map[string]string |
| 38 | + GettersMap map[string]string |
| 39 | +} |
| 40 | + |
| 41 | +var gettersMap map[string]string |
| 42 | +var deviceMap []map[string]string |
| 43 | + |
| 44 | +func init() { |
| 45 | + deviceMap = make([]map[string]string, 2) |
| 46 | + |
| 47 | + deviceMap[0] = make(map[string]string) |
| 48 | + deviceMap[0][BOOT_DEVICE_PXE] = "\"LegacyNetworkAdapter\"" |
| 49 | + deviceMap[0][BOOT_DEVICE_DISK] = "\"IDE\"" |
| 50 | + deviceMap[0][BOOT_DEVICE_CD_DVD] = "\"CD\"" |
| 51 | + deviceMap[0][BOOT_DEVICE_FLOPPY] = "\"Floppy\"" |
| 52 | + |
| 53 | + deviceMap[1] = make(map[string]string) |
| 54 | + deviceMap[1][BOOT_DEVICE_PXE] = "$MyNIC" |
| 55 | + deviceMap[1][BOOT_DEVICE_DISK] = "$MyHD" |
| 56 | + deviceMap[1][BOOT_DEVICE_CD_DVD] = "$MyDVD" |
| 57 | + deviceMap[1][BOOT_DEVICE_FLOPPY] = "" |
| 58 | + |
| 59 | + gettersMap = make(map[string]string) |
| 60 | + gettersMap[BOOT_DEVICE_PXE] = "$MyNIC = Get-VMNetworkAdapter $vmName" |
| 61 | + gettersMap[BOOT_DEVICE_DISK] = "$MyHD = Get-VMHardDiskDrive $vmName" |
| 62 | + gettersMap[BOOT_DEVICE_CD_DVD] = "$MyDVD = Get-VMDvdDrive $vmName" |
| 63 | + gettersMap[BOOT_DEVICE_FLOPPY] = "" |
| 64 | +} |
| 65 | + |
| 66 | +func NewHypervInstance(name string) *HypervInstance { |
| 67 | + return &HypervInstance{ |
| 68 | + Name: name, |
| 69 | + defaultBootOrder: []string{"disk", "net"}, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func (instance *HypervInstance) IsRunning() bool { |
| 74 | + running, _ := hyperv.IsRunning(instance.Name) |
| 75 | + return running |
| 76 | +} |
| 77 | + |
| 78 | +func (instance *HypervInstance) SetBootDevice(dev string) { |
| 79 | + instance.nextBootOrder = []string{dev} |
| 80 | + instance.changeBootOrder = true |
| 81 | +} |
| 82 | + |
| 83 | +func (instance *HypervInstance) PowerOff() { |
| 84 | + hyperv.ShutDown(instance.Name) |
| 85 | +} |
| 86 | + |
| 87 | +func (instance *HypervInstance) ACPIOff() { |
| 88 | + hyperv.TurnOff(instance.Name) |
| 89 | +} |
| 90 | + |
| 91 | +func (instance *HypervInstance) PowerOn() { |
| 92 | + generation, err := hyperv.GetVirtualMachineGeneration(instance.Name) |
| 93 | + if err != nil { |
| 94 | + log.Fatalf(" Instance: Failed to get retrieve generation for VM %s: %s", instance.Name, err.Error()) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + bootOrder := instance.defaultBootOrder |
| 99 | + |
| 100 | + if instance.changeBootOrder { |
| 101 | + bootOrder = instance.nextBootOrder |
| 102 | + |
| 103 | + instance.nextBootOrder = make([]string, 4) |
| 104 | + instance.changeBootOrder = false |
| 105 | + } |
| 106 | + |
| 107 | + log.Println("Current Boot Order = ", bootOrder) |
| 108 | + |
| 109 | + parameters := templateParameters{ |
| 110 | + Devices: bootOrder, |
| 111 | + DeviceMap: deviceMap, |
| 112 | + GettersMap: gettersMap, |
| 113 | + } |
| 114 | + |
| 115 | + powerShellTemplate := powerShellTemplateGen2 |
| 116 | + if generation < 2 { |
| 117 | + powerShellTemplate = powerShellTemplateGen1 |
| 118 | + } |
| 119 | + |
| 120 | + var buffer bytes.Buffer |
| 121 | + compiledPowerShellTemplate, err := template.New("script").Parse(powerShellTemplate) |
| 122 | + if err != nil { |
| 123 | + log.Fatalf(" Instance: Failed to parse boot command for VM %s: %s", instance.Name, err.Error()) |
| 124 | + return |
| 125 | + } |
| 126 | + if err := compiledPowerShellTemplate.Execute(&buffer, parameters); err != nil { |
| 127 | + log.Fatalf(" Instance: Failed to generate boot command for VM %s: %s", instance.Name, err.Error()) |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + var ps powershell.PowerShellCmd |
| 132 | + err = ps.Run(buffer.String(), instance.Name) |
| 133 | + |
| 134 | + hyperv.StartVirtualMachine(instance.Name) |
| 135 | +} |
| 136 | + |
| 137 | +func (instance *HypervInstance) Reset() { |
| 138 | + hyperv.RestartVirtualMachine(instance.Name) |
| 139 | +} |
| 140 | + |
| 141 | +func (instance *HypervInstance) NICInitialize() { |
| 142 | + // Nothing to do here |
| 143 | + return |
| 144 | +} |
0 commit comments