Merge pull request #66 from ddelnano/ddelnano/sohonetlabs/xva-create-template

Fix XVA builder
This commit is contained in:
Dom Del Nano 2023-04-15 15:59:52 -07:00 committed by GitHub
commit f6a317cb41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 55 deletions

View File

@ -1,4 +1,4 @@
package iso package common
import ( import (
"context" "context"
@ -9,18 +9,21 @@ 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 {
// 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
} }
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")
@ -101,6 +104,7 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
} }
} }
if !self.AssumePreInstalledOS {
err = c.GetClient().VM.RemoveFromOtherConfig(c.GetSessionRef(), instance, "disks") err = c.GetClient().VM.RemoveFromOtherConfig(c.GetSessionRef(), instance, "disks")
if err != nil { if err != nil {
ui.Error(fmt.Sprintf("Error removing disks from VM other-config: %s", err.Error())) ui.Error(fmt.Sprintf("Error removing disks from VM other-config: %s", err.Error()))
@ -134,11 +138,12 @@ 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
} }
}
// Connect Network // Connect Network
@ -174,7 +179,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 +208,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 +228,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,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(stepCreateInstance), &xscommon.StepCreateInstance{
AssumePreInstalledOS: false,
},
&xscommon.StepAttachVdi{ &xscommon.StepAttachVdi{
VdiUuidKey: "floppy_vdi_uuid", VdiUuidKey: "floppy_vdi_uuid",
VdiType: xsclient.VbdTypeFloppy, VdiType: xsclient.VbdTypeFloppy,

View File

@ -3,7 +3,6 @@ package xva
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"time" "time"
"github.com/hashicorp/hcl/v2/hcldec" "github.com/hashicorp/hcl/v2/hcldec"
@ -43,6 +42,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
errs = packer.MultiErrorAppend( errs = packer.MultiErrorAppend(
errs, self.config.CommonConfig.Prepare(self.config.GetInterpContext(), &self.config.PackerConfig)...) errs, self.config.CommonConfig.Prepare(self.config.GetInterpContext(), &self.config.PackerConfig)...)
errs = packer.MultiErrorAppend(errs, self.config.SSHConfig.Prepare(self.config.GetInterpContext())...)
// Set default values // Set default values
if self.config.VCPUsMax == 0 { if self.config.VCPUsMax == 0 {
@ -74,8 +74,12 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
// Validation // Validation
if self.config.SourcePath == "" { if self.config.SourcePath == "" && self.config.CloneTemplate == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("A source_path must be specified")) errs = packer.MultiErrorAppend(
errs, errors.New("Either source_path or clone_template must be specified"))
} else if self.config.SourcePath != "" && self.config.CloneTemplate != "" {
errs = packer.MultiErrorAppend(
errs, errors.New("Only one of source_path and clone_template must be specified"))
} }
if len(errs.Errors) > 0 { if len(errs.Errors) > 0 {
@ -101,7 +105,7 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
//Share state between the other steps using a statebag //Share state between the other steps using a statebag
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("client", c) state.Put("client", c)
// state.Put("config", self.config) state.Put("config", self.config)
state.Put("commonconfig", self.config.CommonConfig) state.Put("commonconfig", self.config.CommonConfig)
state.Put("hook", hook) state.Put("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)
@ -116,8 +120,11 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
}, },
&commonsteps.StepCreateFloppy{ &commonsteps.StepCreateFloppy{
Files: self.config.FloppyFiles, Files: self.config.FloppyFiles,
Label: "cidata",
},
&xscommon.StepHTTPServer{
Chan: httpReqChan,
}, },
new(xscommon.StepHTTPServer),
&xscommon.StepUploadVdi{ &xscommon.StepUploadVdi{
VdiNameFunc: func() string { VdiNameFunc: func() string {
return "Packer-floppy-disk" return "Packer-floppy-disk"
@ -134,6 +141,9 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
VdiName: self.config.ToolsIsoName, VdiName: self.config.ToolsIsoName,
VdiUuidKey: "tools_vdi_uuid", VdiUuidKey: "tools_vdi_uuid",
}, },
&xscommon.StepCreateInstance{
AssumePreInstalledOS: true,
},
new(stepImportInstance), new(stepImportInstance),
&xscommon.StepAttachVdi{ &xscommon.StepAttachVdi{
VdiUuidKey: "floppy_vdi_uuid", VdiUuidKey: "floppy_vdi_uuid",
@ -153,20 +163,28 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
Chan: httpReqChan, Chan: httpReqChan,
Timeout: 300 * time.Minute, /*self.config.InstallTimeout*/ // @todo change this Timeout: 300 * time.Minute, /*self.config.InstallTimeout*/ // @todo change this
}, },
&xscommon.StepForwardPortOverSSH{
RemotePort: xscommon.InstanceSSHPort,
RemoteDest: xscommon.InstanceSSHIP,
HostPortMin: self.config.HostPortMin,
HostPortMax: self.config.HostPortMax,
ResultKey: "local_ssh_port",
},
&communicator.StepConnect{ &communicator.StepConnect{
Config: &self.config.SSHConfig.Comm, Config: &self.config.SSHConfig.Comm,
Host: xscommon.CommHost, Host: xscommon.InstanceSSHIP,
SSHConfig: xscommon.SSHConfigFunc(self.config.CommonConfig.SSHConfig), SSHConfig: self.config.Comm.SSHConfigFunc(),
SSHPort: xscommon.SSHPort, SSHPort: xscommon.InstanceSSHPort,
}, },
new(commonsteps.StepProvision), new(commonsteps.StepProvision),
new(xscommon.StepShutdown), new(xscommon.StepShutdown),
&xscommon.StepDetachVdi{ new(xscommon.StepSetVmToTemplate),
VdiUuidKey: "floppy_vdi_uuid",
},
&xscommon.StepDetachVdi{ &xscommon.StepDetachVdi{
VdiUuidKey: "tools_vdi_uuid", VdiUuidKey: "tools_vdi_uuid",
}, },
&xscommon.StepDetachVdi{
VdiUuidKey: "floppy_vdi_uuid",
},
new(xscommon.StepExport), new(xscommon.StepExport),
} }

View File

@ -3,6 +3,7 @@ package xva
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"os" "os"
"github.com/hashicorp/packer-plugin-sdk/multistep" "github.com/hashicorp/packer-plugin-sdk/multistep"
@ -24,6 +25,11 @@ func (self *stepImportInstance) Run(ctx context.Context, state multistep.StateBa
ui.Say("Step: Import Instance") ui.Say("Step: Import Instance")
if config.SourcePath == "" {
log.Println("Skipping imporing instance - no `source_path` configured.")
return multistep.ActionContinue
}
// find the SR // find the SR
srs, err := c.GetClient().SR.GetAll(c.GetSessionRef()) srs, err := c.GetClient().SR.GetAll(c.GetSessionRef())
sr := srs[0] sr := srs[0]