ddelnano / packer-plugin-xenserver

A builder plugin for Packer.IO to support building XenServer images.
Mozilla Public License 2.0
72 stars 37 forks source link

Fix XVA builder #66

Closed ddelnano closed 1 year ago

ddelnano commented 1 year ago

This is a follow up to #44.

Testing

ddelnano@ddelnano-desktop:~/go/src/github.com/xenserver/packer-plugin-xenserver$ diff builder/xenserver/xva/step_create_from_template.go builder/xenserver/iso/step_create_instance.go
1c1
< package xva
---
> package iso
9a10
>       xenapi "github.com/terra-farm/go-xen-api-client"
14c15
< type stepCreateFromTemplate struct {
---
> type stepCreateInstance struct {
15a17
>       vdi      *xsclient.VDIRef
18c20
< func (self *stepCreateFromTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
---
> func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
24,29c26
<       ui.Say("Step: Create Instance from Template")
<
<       if config.CloneTemplate == "" {
<               log.Println("Skipping creation from template - no `clone_template` configured.")
<               return multistep.ActionContinue
<       }
---
>       ui.Say("Step: Create Instance")
90a88,142
>       if len(config.VMOtherConfig) != 0 {
>               vm_other_config, err := c.GetClient().VM.GetOtherConfig(c.GetSessionRef(), instance)
>               if err != nil {
>                       ui.Error(fmt.Sprintf("Error getting VM other-config: %s", err.Error()))
>                       return multistep.ActionHalt
>               }
>               for key, value := range config.VMOtherConfig {
>                       vm_other_config[key] = value
>               }
>               err = c.GetClient().VM.SetOtherConfig(c.GetSessionRef(), instance, vm_other_config)
>               if err != nil {
>                       ui.Error(fmt.Sprintf("Error setting VM other-config: %s", err.Error()))
>                       return multistep.ActionHalt
>               }
>       }
>
>       err = c.GetClient().VM.RemoveFromOtherConfig(c.GetSessionRef(), instance, "disks")
>       if err != nil {
>               ui.Error(fmt.Sprintf("Error removing disks from VM other-config: %s", err.Error()))
>               return multistep.ActionHalt
>       }
>
>       // Create VDI for the instance
>       sr, err := config.GetSR(c)
>
>       if err != nil {
>               ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
>               return multistep.ActionHalt
>       }
>
>       ui.Say(fmt.Sprintf("Using the following SR for the VM: %s", sr))
>
>       vdi, err := c.GetClient().VDI.Create(c.GetSessionRef(), xenapi.VDIRecord{
>               NameLabel:   "Packer-disk",
>               VirtualSize: int(config.DiskSize * 1024 * 1024),
>               Type:        "user",
>               Sharable:    false,
>               ReadOnly:    false,
>               SR:          sr,
>               OtherConfig: map[string]string{
>                       "temp": "temp",
>               },
>       })
>       if err != nil {
>               ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error()))
>               return multistep.ActionHalt
>       }
>       self.vdi = &vdi
>
>       err = xscommon.ConnectVdi(c, instance, vdi, xsclient.VbdTypeDisk)
>       if err != nil {
>               ui.Error(fmt.Sprintf("Unable to connect packer disk VDI: %s", err.Error()))
>               return multistep.ActionHalt
>       }
>
174c226
< func (self *stepCreateFromTemplate) Cleanup(state multistep.StateBag) {
---
> func (self *stepCreateInstance) Cleanup(state multistep.StateBag) {
186a239,246
>               if err != nil {
>                       ui.Error(err.Error())
>               }
>       }
>
>       if self.vdi != nil {
>               ui.Say("Destroying VDI")
>               err := c.GetClient().VDI.Destroy(c.GetSessionRef(), *self.vdi)

ddelnano commented 1 year ago

@heindsight I'm going to merge this despite not testing the XVA builder since I'd like to merge #67 (which is based on this PR). Once I know the process for testing the XVA builder, I will gladly double check everything is in order before making the next release.

heindsight commented 1 year ago

@ddelnano thanks for your efforts in getting all of these PRs merged!

I tested this by using the ISO builder to build an initial template and then using that as the source template for an XVA build.