forked from xenserver/packer-plugin-xenserver
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathcommon_config.go
More file actions
323 lines (259 loc) · 8.48 KB
/
Copy pathcommon_config.go
File metadata and controls
323 lines (259 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package common
import (
"errors"
"fmt"
"os"
"time"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
xenapi "github.com/terra-farm/go-xen-api-client"
)
// DiskConfig represents a virtual disk to be created on the VM
type DiskConfig struct {
Name string `mapstructure:"disk_name"`
Size uint `mapstructure:"disk_size"`
SRName string `mapstructure:"sr_name"`
}
type CommonConfig struct {
Username string `mapstructure:"remote_username"`
Password string `mapstructure:"remote_password"`
HostIp string `mapstructure:"remote_host"`
HostSshPort uint `mapstructure:"remote_ssh_port"`
VMName string `mapstructure:"vm_name"`
VMDescription string `mapstructure:"vm_description"`
SrName string `mapstructure:"sr_name"`
SrISOName string `mapstructure:"sr_iso_name" required:"false"`
DiskName string `mapstructure:"disk_name"`
DiskSize uint `mapstructure:"disk_size"`
Disks []DiskConfig `mapstructure:"disks"`
CDFiles []string `mapstructure:"cd_files"`
FloppyFiles []string `mapstructure:"floppy_files"`
NetworkNames []string `mapstructure:"network_names"`
ExportNetworkNames []string `mapstructure:"export_network_names"`
VMTags []string `mapstructure:"vm_tags"`
HostPortMin uint `mapstructure:"host_port_min"`
HostPortMax uint `mapstructure:"host_port_max"`
BootCommand []string `mapstructure:"boot_command"`
ShutdownCommand string `mapstructure:"shutdown_command"`
RawBootWait string `mapstructure:"boot_wait"`
BootWait time.Duration `mapstructure-to-hcl2:",skip"`
RawDhcpWait string `mapstructure:"dhcp_wait"`
DhcpWait time.Duration `mapstructure-to-hcl2:",skip"`
ToolsIsoName string `mapstructure:"tools_iso_name"`
HTTPContent map[string]string `mapstructure:"http_content"`
HTTPDir string `mapstructure:"http_directory"`
HTTPPortMin uint `mapstructure:"http_port_min"`
HTTPPortMax uint `mapstructure:"http_port_max"`
// SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
// SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
SSHConfig `mapstructure:",squash"`
OutputDir string `mapstructure:"output_directory"`
Format string `mapstructure:"format"`
KeepVM string `mapstructure:"keep_vm"`
IPGetter string `mapstructure:"ip_getter"`
}
func (c *CommonConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error {
var err error
var errs []error
// Handle backward compatibility: convert old single-disk format to new multi-disk format
if len(c.Disks) == 0 && (c.DiskName != "" || c.DiskSize > 0 || c.SrName != "") {
// User provided old-style disk configuration
c.Disks = []DiskConfig{
{
Name: c.DiskName,
Size: c.DiskSize,
SRName: c.SrName,
},
}
}
// Set default values
if c.HostSshPort == 0 {
c.HostSshPort = 22
}
if c.HostPortMin == 0 {
c.HostPortMin = 5900
}
if c.HostPortMax == 0 {
c.HostPortMax = 6000
}
if c.RawBootWait == "" {
c.RawBootWait = "5s"
}
if c.RawDhcpWait == "" {
c.RawDhcpWait = "500ms"
}
if c.HTTPPortMin == 0 {
c.HTTPPortMin = 8000
}
if c.HTTPPortMax == 0 {
c.HTTPPortMax = 9000
}
if c.Comm.SSHWaitTimeout == 0 {
c.Comm.SSHWaitTimeout = 200 * time.Minute
}
if c.FloppyFiles == nil {
c.FloppyFiles = make([]string, 0)
}
/*
if c.SSHHostPortMin == 0 {
c.SSHHostPortMin = 2222
}
if c.SSHHostPortMax == 0 {
c.SSHHostPortMax = 4444
}
*/
if c.Comm.SSHPort == 0 {
c.Comm.SSHPort = 22
}
if c.OutputDir == "" {
c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
}
if c.VMName == "" {
c.VMName = fmt.Sprintf("packer-%s-{{timestamp}}", pc.PackerBuildName)
}
if c.Format == "" {
c.Format = "xva"
}
if c.KeepVM == "" {
c.KeepVM = "never"
}
if c.IPGetter == "" {
c.IPGetter = "auto"
}
// Validation
if c.Username == "" {
errs = append(errs, errors.New("remote_username must be specified."))
}
if c.Password == "" {
errs = append(errs, errors.New("remote_password must be specified."))
}
if c.HostIp == "" {
errs = append(errs, errors.New("remote_host must be specified."))
}
if c.HostPortMin > c.HostPortMax {
errs = append(errs, errors.New("the host min port must be less than the max"))
}
if c.HTTPPortMin > c.HTTPPortMax {
errs = append(errs, errors.New("the HTTP min port must be less than the max"))
}
c.BootWait, err = time.ParseDuration(c.RawBootWait)
if err != nil {
errs = append(errs, fmt.Errorf("Failed to parse boot_wait: %s", err))
}
c.DhcpWait, err = time.ParseDuration(c.RawDhcpWait)
if err != nil {
errs = append(errs, fmt.Errorf("Failed to parse dhcp_wait: %s", err))
}
if c.SSHKeyPath != "" {
if _, err := os.Stat(c.SSHKeyPath); err != nil {
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
} else if _, err := FileSigner(c.SSHKeyPath); err != nil {
errs = append(errs, fmt.Errorf("ssh_key_path is invalid: %s", err))
}
}
/*
if c.SSHHostPortMin > c.SSHHostPortMax {
errs = append(errs,
errors.New("ssh_host_port_min must be less than ssh_host_port_max"))
}
*/
if c.Comm.SSHUsername == "" {
errs = append(errs, errors.New("An ssh_username must be specified."))
}
switch c.Format {
case "xva", "xva_compressed", "vdi_raw", "vdi_vhd", "none":
default:
errs = append(errs, errors.New("format must be one of 'xva', 'vdi_raw', 'vdi_vhd', 'none'"))
}
switch c.KeepVM {
case "always", "never", "on_success":
default:
errs = append(errs, errors.New("keep_vm must be one of 'always', 'never', 'on_success'"))
}
switch c.IPGetter {
case "auto", "tools", "http":
default:
errs = append(errs, errors.New("ip_getter must be one of 'auto', 'tools', 'http'"))
}
return errs
}
// steps should check config.ShouldKeepVM first before cleaning up the VM
func (c CommonConfig) ShouldKeepVM(state multistep.StateBag) bool {
switch c.KeepVM {
case "always":
return true
case "never":
return false
case "on_success":
// only keep instance if build was successful
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
return !(cancelled || halted)
default:
panic(fmt.Sprintf("Unknown keep_vm value '%s'", c.KeepVM))
}
}
func (config CommonConfig) GetSR(c *Connection) (xenapi.SRRef, error) {
if config.SrName == "" {
return getDefaultSR(c)
} else {
var srRef xenapi.SRRef
// Use the provided name label to find the SR to use
srs, err := c.GetClient().SR.GetByNameLabel(c.session, config.SrName)
if err != nil {
return srRef, err
}
switch {
case len(srs) == 0:
return srRef, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrName)
case len(srs) > 1:
return srRef, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrName)
}
return srs[0], nil
}
}
func (config CommonConfig) GetISOSR(c *Connection) (xenapi.SRRef, error) {
var srRef xenapi.SRRef
if config.SrISOName == "" {
return getDefaultSR(c)
} else {
// Use the provided name label to find the SR to use
srs, err := c.GetClient().SR.GetByNameLabel(c.session, config.SrISOName)
if err != nil {
return srRef, err
}
switch {
case len(srs) == 0:
return srRef, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrISOName)
case len(srs) > 1:
return srRef, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrISOName)
}
return srs[0], nil
}
}
func getDefaultSR(c *Connection) (xenapi.SRRef, error) {
var srRef xenapi.SRRef
client := c.GetClient()
hostRef, err := client.Session.GetThisHost(c.session, c.session)
if err != nil {
return srRef, err
}
// The current version of the go-xen-api-client does not fully support XenAPI version 8.2
// In particular, some values for the pool `allowed_operations` are not recognised, resulting
// in a parse error when retrieving pool records. As a workaround, we only fetch pool refs.
pool_refs, err := client.Pool.GetAll(c.session)
if err != nil {
return srRef, err
}
for _, pool_ref := range pool_refs {
pool_master, err := client.Pool.GetMaster(c.session, pool_ref)
if err != nil {
return srRef, err
}
if pool_master == hostRef {
return client.Pool.GetDefaultSR(c.session, pool_ref)
}
}
return srRef, errors.New(fmt.Sprintf("failed to find default SR on host '%s'", hostRef))
}