Update StepCreateInstance to accommodate both ISO and XVA builders

This commit is contained in:
Dom Del Nano 2023-04-15 14:58:13 -07:00
parent 461367ad1c
commit 7c4b652c17
2 changed files with 41 additions and 33 deletions

View File

@ -12,6 +12,10 @@ import (
) )
type StepCreateInstance struct { type StepCreateInstance struct {
// The XVA builder assumes it will boot an instance with an OS installed on its disks
// while the ISO builder needs packer to create a disk for an OS to be installed on.
AssumePreInstalledOS bool
instance *xsclient.VMRef instance *xsclient.VMRef
vdi *xsclient.VDIRef vdi *xsclient.VDIRef
} }
@ -100,43 +104,45 @@ func (self *StepCreateInstance) Run(ctx context.Context, state multistep.StateBa
} }
} }
err = c.GetClient().VM.RemoveFromOtherConfig(c.GetSessionRef(), instance, "disks") if !self.AssumePreInstalledOS {
if err != nil { err = c.GetClient().VM.RemoveFromOtherConfig(c.GetSessionRef(), instance, "disks")
ui.Error(fmt.Sprintf("Error removing disks from VM other-config: %s", err.Error())) if err != nil {
return multistep.ActionHalt ui.Error(fmt.Sprintf("Error removing disks from VM other-config: %s", err.Error()))
} return multistep.ActionHalt
}
// Create VDI for the instance // Create VDI for the instance
sr, err := config.GetSR(c) sr, err := config.GetSR(c)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error())) ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
return multistep.ActionHalt return multistep.ActionHalt
} }
ui.Say(fmt.Sprintf("Using the following SR for the VM: %s", sr)) ui.Say(fmt.Sprintf("Using the following SR for the VM: %s", sr))
vdi, err := c.GetClient().VDI.Create(c.GetSessionRef(), xenapi.VDIRecord{ vdi, err := c.GetClient().VDI.Create(c.GetSessionRef(), xenapi.VDIRecord{
NameLabel: "Packer-disk", NameLabel: "Packer-disk",
VirtualSize: int(config.DiskSize * 1024 * 1024), VirtualSize: int(config.DiskSize * 1024 * 1024),
Type: "user", Type: "user",
Sharable: false, Sharable: false,
ReadOnly: false, ReadOnly: false,
SR: sr, SR: sr,
OtherConfig: map[string]string{ OtherConfig: map[string]string{
"temp": "temp", "temp": "temp",
}, },
}) })
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error())) ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error()))
return multistep.ActionHalt return multistep.ActionHalt
} }
self.vdi = &vdi self.vdi = &vdi
err = ConnectVdi(c, instance, vdi, xsclient.VbdTypeDisk) err = ConnectVdi(c, instance, vdi, xsclient.VbdTypeDisk)
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Unable to connect packer disk VDI: %s", err.Error())) ui.Error(fmt.Sprintf("Unable to connect packer disk VDI: %s", err.Error()))
return multistep.ActionHalt return multistep.ActionHalt
}
} }
// Connect Network // Connect Network

View File

@ -235,7 +235,9 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
VdiName: self.config.ISOName, VdiName: self.config.ISOName,
VdiUuidKey: "isoname_vdi_uuid", VdiUuidKey: "isoname_vdi_uuid",
}, },
new(xscommon.StepCreateInstance), &xscommon.StepCreateInstance{
AssumePreInstalledOS: false,
},
&xscommon.StepAttachVdi{ &xscommon.StepAttachVdi{
VdiUuidKey: "floppy_vdi_uuid", VdiUuidKey: "floppy_vdi_uuid",
VdiType: xsclient.VbdTypeFloppy, VdiType: xsclient.VbdTypeFloppy,