Move create instance step to common directory

This commit is contained in:
Dom Del Nano 2023-04-15 14:49:11 -07:00
parent 0bb70e8957
commit 461367ad1c
2 changed files with 12 additions and 13 deletions

View File

@ -1,4 +1,4 @@
package iso package common
import ( import (
"context" "context"
@ -9,18 +9,17 @@ import (
"github.com/hashicorp/packer-plugin-sdk/packer" "github.com/hashicorp/packer-plugin-sdk/packer"
xenapi "github.com/terra-farm/go-xen-api-client" xenapi "github.com/terra-farm/go-xen-api-client"
xsclient "github.com/terra-farm/go-xen-api-client" xsclient "github.com/terra-farm/go-xen-api-client"
xscommon "github.com/xenserver/packer-builder-xenserver/builder/xenserver/common"
) )
type stepCreateInstance struct { type StepCreateInstance struct {
instance *xsclient.VMRef instance *xsclient.VMRef
vdi *xsclient.VDIRef vdi *xsclient.VDIRef
} }
func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (self *StepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
c := state.Get("client").(*xscommon.Connection) c := state.Get("client").(*Connection)
config := state.Get("config").(xscommon.Config) config := state.Get("config").(Config)
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
ui.Say("Step: Create Instance") ui.Say("Step: Create Instance")
@ -134,7 +133,7 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
} }
self.vdi = &vdi self.vdi = &vdi
err = xscommon.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
@ -174,7 +173,7 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
} }
log.Printf("Creating VIF on network '%s' on VM '%s'\n", network, instance) log.Printf("Creating VIF on network '%s' on VM '%s'\n", network, instance)
_, err = xscommon.ConnectNetwork(c, network, instance, "0") _, err = ConnectNetwork(c, network, instance, "0")
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Failed to create VIF with error: %v", err)) ui.Error(fmt.Sprintf("Failed to create VIF with error: %v", err))
@ -203,7 +202,7 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
//we need the VIF index string //we need the VIF index string
vifIndexString := fmt.Sprintf("%d", i) vifIndexString := fmt.Sprintf("%d", i)
_, err = xscommon.ConnectNetwork(c, networks[0], instance, vifIndexString) _, err = ConnectNetwork(c, networks[0], instance, vifIndexString)
if err != nil { if err != nil {
ui.Say(fmt.Sprintf("Failed to connect VIF with error: %v", err.Error())) ui.Say(fmt.Sprintf("Failed to connect VIF with error: %v", err.Error()))
@ -223,14 +222,14 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
return multistep.ActionContinue return multistep.ActionContinue
} }
func (self *stepCreateInstance) Cleanup(state multistep.StateBag) { func (self *StepCreateInstance) Cleanup(state multistep.StateBag) {
config := state.Get("config").(xscommon.Config) config := state.Get("config").(Config)
if config.ShouldKeepVM(state) { if config.ShouldKeepVM(state) {
return return
} }
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
c := state.Get("client").(*xscommon.Connection) c := state.Get("client").(*Connection)
if self.instance != nil { if self.instance != nil {
ui.Say("Destroying VM") ui.Say("Destroying VM")

View File

@ -235,7 +235,7 @@ 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(stepCreateInstance), new(xscommon.StepCreateInstance),
&xscommon.StepAttachVdi{ &xscommon.StepAttachVdi{
VdiUuidKey: "floppy_vdi_uuid", VdiUuidKey: "floppy_vdi_uuid",
VdiType: xsclient.VbdTypeFloppy, VdiType: xsclient.VbdTypeFloppy,