forked from xenserver/packer-plugin-xenserver
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbuilder.go
209 lines (178 loc) · 5.55 KB
/
builder.go
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
package xva
import (
"context"
"errors"
"time"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/multistep"
commonsteps "github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
"github.com/hashicorp/packer-plugin-sdk/packer"
hconfig "github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
xsclient "github.com/terra-farm/go-xen-api-client"
xscommon "github.com/xenserver/packer-builder-xenserver/builder/xenserver/common"
)
type Builder struct {
config xscommon.Config
runner multistep.Runner
}
func (self *Builder) ConfigSpec() hcldec.ObjectSpec { return self.config.FlatMapstructure().HCL2Spec() }
func (self *Builder) Prepare(raws ...interface{}) (params []string, warns []string, retErr error) {
var errs *packer.MultiError
err := hconfig.Decode(&self.config, &hconfig.DecodeOpts{
Interpolate: true,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{
"boot_command",
},
},
}, raws...)
if err != nil {
errs = packer.MultiErrorAppend(errs, err)
}
errs = packer.MultiErrorAppend(
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
if self.config.VCPUsMax == 0 {
self.config.VCPUsMax = 1
}
if self.config.VCPUsAtStartup == 0 {
self.config.VCPUsAtStartup = 1
}
if self.config.VCPUsAtStartup > self.config.VCPUsMax {
self.config.VCPUsAtStartup = self.config.VCPUsMax
}
if self.config.VMMemory == 0 {
self.config.VMMemory = 1024
}
if len(self.config.PlatformArgs) == 0 {
pargs := make(map[string]string)
pargs["viridian"] = "false"
pargs["nx"] = "true"
pargs["pae"] = "true"
pargs["apic"] = "true"
pargs["timeoffset"] = "0"
pargs["acpi"] = "1"
self.config.PlatformArgs = pargs
}
// Validation
if self.config.SourcePath == "" && self.config.CloneTemplate == "" {
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 {
retErr = errors.New(errs.Error())
}
return nil, nil, retErr
}
func (self *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
//Setup XAPI client
c, err := xscommon.NewXenAPIClient(self.config.HostIp, self.config.Username, self.config.Password)
if err != nil {
return nil, err
}
ui.Say("XAPI client session established")
c.GetClient().Host.GetAll(c.GetSessionRef())
//Share state between the other steps using a statebag
state := new(multistep.BasicStateBag)
state.Put("client", c)
state.Put("config", self.config)
state.Put("commonconfig", self.config.CommonConfig)
state.Put("hook", hook)
state.Put("ui", ui)
httpReqChan := make(chan string, 1)
//Build the steps
steps := []multistep.Step{
&xscommon.StepPrepareOutputDir{
Force: self.config.PackerForce,
Path: self.config.OutputDir,
},
&commonsteps.StepCreateFloppy{
Files: self.config.FloppyFiles,
Label: "cidata",
},
&xscommon.StepHTTPServer{
Chan: httpReqChan,
},
&xscommon.StepUploadVdi{
VdiNameFunc: func() string {
return "Packer-floppy-disk"
},
ImagePathFunc: func() string {
if floppyPath, ok := state.GetOk("floppy_path"); ok {
return floppyPath.(string)
}
return ""
},
VdiUuidKey: "floppy_vdi_uuid",
},
&xscommon.StepFindVdi{
VdiName: self.config.ToolsIsoName,
VdiUuidKey: "tools_vdi_uuid",
},
&xscommon.StepCreateInstance{
AssumePreInstalledOS: true,
},
new(stepImportInstance),
&xscommon.StepAttachVdi{
VdiUuidKey: "floppy_vdi_uuid",
VdiType: xsclient.VbdTypeFloppy,
},
&xscommon.StepAttachVdi{
VdiUuidKey: "tools_vdi_uuid",
VdiType: xsclient.VbdTypeCD,
},
new(xscommon.StepStartVmPaused),
new(xscommon.StepSetVmHostSshAddress),
new(xscommon.StepBootWait),
&xscommon.StepTypeBootCommand{
Ctx: *self.config.GetInterpContext(),
},
&xscommon.StepWaitForIP{
Chan: httpReqChan,
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{
Config: &self.config.SSHConfig.Comm,
Host: xscommon.InstanceSSHIP,
SSHConfig: self.config.Comm.SSHConfigFunc(),
SSHPort: xscommon.InstanceSSHPort,
},
new(commonsteps.StepProvision),
new(xscommon.StepShutdown),
new(xscommon.StepSetVmToTemplate),
&xscommon.StepDetachVdi{
VdiUuidKey: "tools_vdi_uuid",
},
&xscommon.StepDetachVdi{
VdiUuidKey: "floppy_vdi_uuid",
},
new(xscommon.StepExport),
}
self.runner = &multistep.BasicRunner{Steps: steps}
self.runner.Run(ctx, state)
if rawErr, ok := state.GetOk("error"); ok {
return nil, rawErr.(error)
}
// If we were interrupted or cancelled, then just exit.
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return nil, errors.New("Build was cancelled.")
}
if _, ok := state.GetOk(multistep.StateHalted); ok {
return nil, errors.New("Build was halted.")
}
artifact, _ := xscommon.NewArtifact(self.config.OutputDir)
return artifact, nil
}