Skip to content

Commit f6a317c

Browse files
authored
Merge pull request #66 from ddelnano/ddelnano/sohonetlabs/xva-create-template
Fix XVA builder
2 parents de46bb8 + 2fe14d1 commit f6a317c

4 files changed

Lines changed: 86 additions & 55 deletions

File tree

builder/xenserver/iso/step_create_instance.go renamed to builder/xenserver/common/step_create_instance.go

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package iso
1+
package common
22

33
import (
44
"context"
@@ -9,18 +9,21 @@ import (
99
"github.com/hashicorp/packer-plugin-sdk/packer"
1010
xenapi "github.com/terra-farm/go-xen-api-client"
1111
xsclient "github.com/terra-farm/go-xen-api-client"
12-
xscommon "github.com/xenserver/packer-builder-xenserver/builder/xenserver/common"
1312
)
1413

15-
type stepCreateInstance struct {
14+
type StepCreateInstance struct {
15+
// The XVA builder assumes it will boot an instance with an OS installed on its disks
16+
// while the ISO builder needs packer to create a disk for an OS to be installed on.
17+
AssumePreInstalledOS bool
18+
1619
instance *xsclient.VMRef
1720
vdi *xsclient.VDIRef
1821
}
1922

20-
func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
23+
func (self *StepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2124

22-
c := state.Get("client").(*xscommon.Connection)
23-
config := state.Get("config").(xscommon.Config)
25+
c := state.Get("client").(*Connection)
26+
config := state.Get("config").(Config)
2427
ui := state.Get("ui").(packer.Ui)
2528

2629
ui.Say("Step: Create Instance")
@@ -101,43 +104,45 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
101104
}
102105
}
103106

104-
err = c.GetClient().VM.RemoveFromOtherConfig(c.GetSessionRef(), instance, "disks")
105-
if err != nil {
106-
ui.Error(fmt.Sprintf("Error removing disks from VM other-config: %s", err.Error()))
107-
return multistep.ActionHalt
108-
}
107+
if !self.AssumePreInstalledOS {
108+
err = c.GetClient().VM.RemoveFromOtherConfig(c.GetSessionRef(), instance, "disks")
109+
if err != nil {
110+
ui.Error(fmt.Sprintf("Error removing disks from VM other-config: %s", err.Error()))
111+
return multistep.ActionHalt
112+
}
109113

110-
// Create VDI for the instance
111-
sr, err := config.GetSR(c)
114+
// Create VDI for the instance
115+
sr, err := config.GetSR(c)
112116

113-
if err != nil {
114-
ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
115-
return multistep.ActionHalt
116-
}
117+
if err != nil {
118+
ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error()))
119+
return multistep.ActionHalt
120+
}
117121

118-
ui.Say(fmt.Sprintf("Using the following SR for the VM: %s", sr))
119-
120-
vdi, err := c.GetClient().VDI.Create(c.GetSessionRef(), xenapi.VDIRecord{
121-
NameLabel: "Packer-disk",
122-
VirtualSize: int(config.DiskSize * 1024 * 1024),
123-
Type: "user",
124-
Sharable: false,
125-
ReadOnly: false,
126-
SR: sr,
127-
OtherConfig: map[string]string{
128-
"temp": "temp",
129-
},
130-
})
131-
if err != nil {
132-
ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error()))
133-
return multistep.ActionHalt
134-
}
135-
self.vdi = &vdi
122+
ui.Say(fmt.Sprintf("Using the following SR for the VM: %s", sr))
123+
124+
vdi, err := c.GetClient().VDI.Create(c.GetSessionRef(), xenapi.VDIRecord{
125+
NameLabel: "Packer-disk",
126+
VirtualSize: int(config.DiskSize * 1024 * 1024),
127+
Type: "user",
128+
Sharable: false,
129+
ReadOnly: false,
130+
SR: sr,
131+
OtherConfig: map[string]string{
132+
"temp": "temp",
133+
},
134+
})
135+
if err != nil {
136+
ui.Error(fmt.Sprintf("Unable to create packer disk VDI: %s", err.Error()))
137+
return multistep.ActionHalt
138+
}
139+
self.vdi = &vdi
136140

137-
err = xscommon.ConnectVdi(c, instance, vdi, xsclient.VbdTypeDisk)
138-
if err != nil {
139-
ui.Error(fmt.Sprintf("Unable to connect packer disk VDI: %s", err.Error()))
140-
return multistep.ActionHalt
141+
err = ConnectVdi(c, instance, vdi, xsclient.VbdTypeDisk)
142+
if err != nil {
143+
ui.Error(fmt.Sprintf("Unable to connect packer disk VDI: %s", err.Error()))
144+
return multistep.ActionHalt
145+
}
141146
}
142147

143148
// Connect Network
@@ -174,7 +179,7 @@ func (self *stepCreateInstance) Run(ctx context.Context, state multistep.StateBa
174179
}
175180

176181
log.Printf("Creating VIF on network '%s' on VM '%s'\n", network, instance)
177-
_, err = xscommon.ConnectNetwork(c, network, instance, "0")
182+
_, err = ConnectNetwork(c, network, instance, "0")
178183

179184
if err != nil {
180185
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
203208

204209
//we need the VIF index string
205210
vifIndexString := fmt.Sprintf("%d", i)
206-
_, err = xscommon.ConnectNetwork(c, networks[0], instance, vifIndexString)
211+
_, err = ConnectNetwork(c, networks[0], instance, vifIndexString)
207212

208213
if err != nil {
209214
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
223228
return multistep.ActionContinue
224229
}
225230

226-
func (self *stepCreateInstance) Cleanup(state multistep.StateBag) {
227-
config := state.Get("config").(xscommon.Config)
231+
func (self *StepCreateInstance) Cleanup(state multistep.StateBag) {
232+
config := state.Get("config").(Config)
228233
if config.ShouldKeepVM(state) {
229234
return
230235
}
231236

232237
ui := state.Get("ui").(packer.Ui)
233-
c := state.Get("client").(*xscommon.Connection)
238+
c := state.Get("client").(*Connection)
234239

235240
if self.instance != nil {
236241
ui.Say("Destroying VM")

builder/xenserver/iso/builder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
235235
VdiName: self.config.ISOName,
236236
VdiUuidKey: "isoname_vdi_uuid",
237237
},
238-
new(stepCreateInstance),
238+
&xscommon.StepCreateInstance{
239+
AssumePreInstalledOS: false,
240+
},
239241
&xscommon.StepAttachVdi{
240242
VdiUuidKey: "floppy_vdi_uuid",
241243
VdiType: xsclient.VbdTypeFloppy,

builder/xenserver/xva/builder.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package xva
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"time"
87

98
"github.com/hashicorp/hcl/v2/hcldec"
@@ -43,6 +42,7 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
4342

4443
errs = packer.MultiErrorAppend(
4544
errs, self.config.CommonConfig.Prepare(self.config.GetInterpContext(), &self.config.PackerConfig)...)
45+
errs = packer.MultiErrorAppend(errs, self.config.SSHConfig.Prepare(self.config.GetInterpContext())...)
4646

4747
// Set default values
4848
if self.config.VCPUsMax == 0 {
@@ -74,8 +74,12 @@ func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []stri
7474

7575
// Validation
7676

77-
if self.config.SourcePath == "" {
78-
errs = packer.MultiErrorAppend(errs, fmt.Errorf("A source_path must be specified"))
77+
if self.config.SourcePath == "" && self.config.CloneTemplate == "" {
78+
errs = packer.MultiErrorAppend(
79+
errs, errors.New("Either source_path or clone_template must be specified"))
80+
} else if self.config.SourcePath != "" && self.config.CloneTemplate != "" {
81+
errs = packer.MultiErrorAppend(
82+
errs, errors.New("Only one of source_path and clone_template must be specified"))
7983
}
8084

8185
if len(errs.Errors) > 0 {
@@ -101,7 +105,7 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
101105
//Share state between the other steps using a statebag
102106
state := new(multistep.BasicStateBag)
103107
state.Put("client", c)
104-
// state.Put("config", self.config)
108+
state.Put("config", self.config)
105109
state.Put("commonconfig", self.config.CommonConfig)
106110
state.Put("hook", hook)
107111
state.Put("ui", ui)
@@ -116,8 +120,11 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
116120
},
117121
&commonsteps.StepCreateFloppy{
118122
Files: self.config.FloppyFiles,
123+
Label: "cidata",
124+
},
125+
&xscommon.StepHTTPServer{
126+
Chan: httpReqChan,
119127
},
120-
new(xscommon.StepHTTPServer),
121128
&xscommon.StepUploadVdi{
122129
VdiNameFunc: func() string {
123130
return "Packer-floppy-disk"
@@ -134,6 +141,9 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
134141
VdiName: self.config.ToolsIsoName,
135142
VdiUuidKey: "tools_vdi_uuid",
136143
},
144+
&xscommon.StepCreateInstance{
145+
AssumePreInstalledOS: true,
146+
},
137147
new(stepImportInstance),
138148
&xscommon.StepAttachVdi{
139149
VdiUuidKey: "floppy_vdi_uuid",
@@ -153,19 +163,27 @@ func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (p
153163
Chan: httpReqChan,
154164
Timeout: 300 * time.Minute, /*self.config.InstallTimeout*/ // @todo change this
155165
},
166+
&xscommon.StepForwardPortOverSSH{
167+
RemotePort: xscommon.InstanceSSHPort,
168+
RemoteDest: xscommon.InstanceSSHIP,
169+
HostPortMin: self.config.HostPortMin,
170+
HostPortMax: self.config.HostPortMax,
171+
ResultKey: "local_ssh_port",
172+
},
156173
&communicator.StepConnect{
157174
Config: &self.config.SSHConfig.Comm,
158-
Host: xscommon.CommHost,
159-
SSHConfig: xscommon.SSHConfigFunc(self.config.CommonConfig.SSHConfig),
160-
SSHPort: xscommon.SSHPort,
175+
Host: xscommon.InstanceSSHIP,
176+
SSHConfig: self.config.Comm.SSHConfigFunc(),
177+
SSHPort: xscommon.InstanceSSHPort,
161178
},
162179
new(commonsteps.StepProvision),
163180
new(xscommon.StepShutdown),
181+
new(xscommon.StepSetVmToTemplate),
164182
&xscommon.StepDetachVdi{
165-
VdiUuidKey: "floppy_vdi_uuid",
183+
VdiUuidKey: "tools_vdi_uuid",
166184
},
167185
&xscommon.StepDetachVdi{
168-
VdiUuidKey: "tools_vdi_uuid",
186+
VdiUuidKey: "floppy_vdi_uuid",
169187
},
170188
new(xscommon.StepExport),
171189
}

builder/xenserver/xva/step_import_instance.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package xva
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"os"
78

89
"github.com/hashicorp/packer-plugin-sdk/multistep"
@@ -24,6 +25,11 @@ func (self *stepImportInstance) Run(ctx context.Context, state multistep.StateBa
2425

2526
ui.Say("Step: Import Instance")
2627

28+
if config.SourcePath == "" {
29+
log.Println("Skipping imporing instance - no `source_path` configured.")
30+
return multistep.ActionContinue
31+
}
32+
2733
// find the SR
2834
srs, err := c.GetClient().SR.GetAll(c.GetSessionRef())
2935
sr := srs[0]

0 commit comments

Comments
 (0)