-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
210 lines (174 loc) · 5.49 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
210
package vmm
import (
"errors"
"os/exec"
"path/filepath"
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/common/bootcommand"
"github.com/hashicorp/packer/helper/config"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
// BuilderId references the unique ID of this builder.
const BuilderId = "prep.vmm"
// Config describes the configuration of this builder.
type Config struct {
common.PackerConfig `mapstructure:",squash"`
common.HTTPConfig `mapstructure:",squash"`
common.ISOConfig `mapstructure:",squash"`
bootcommand.BootConfig `mapstructure:",squash"`
VMName string `mapstructure:"vm_name"`
BiosFile string `mapstructure:"bios_file"`
DiskSize string `mapstructure:"disk_size"`
Format string `mapstructure:"format`
MemSize string `mapstructure:"mem_size"`
OutputDir string `mapstructure:"output_directory"`
ctx interpolate.Context
}
// Builder is responsible for create a machine and generating an image.
type Builder struct {
config Config
runner multistep.Runner
}
// Prepare the build configuration parameters.
func (builder *Builder) Prepare(raws ...interface{}) ([]string, error) {
err := config.Decode(&builder.config, &config.DecodeOpts{
Interpolate: true,
InterpolateContext: &builder.config.ctx,
InterpolateFilter: &interpolate.RenderFilter{
Exclude: []string{
"boot_command",
"vmm_args",
},
},
}, raws...)
if err != nil {
return nil, err
}
if builder.config.VMName == "" {
builder.config.VMName = "packer-" + builder.config.PackerBuildName
}
if builder.config.DiskSize == "" {
builder.config.DiskSize = "10G"
}
// Support "raw" and "qcow2" format and default to "raw" when not specified.
switch builder.config.Format {
case "raw", "qcow2":
case "":
builder.config.Format = "raw"
default:
return nil, errors.New(builder.config.Format + ": unsupported image format")
}
if builder.config.MemSize == "" {
builder.config.MemSize = "512M"
}
if builder.config.OutputDir == "" {
builder.config.OutputDir = "output-vmm"
}
errs := &packer.MultiError{Errors: builder.config.BootConfig.Prepare(&builder.config.ctx)}
errs = packer.MultiErrorAppend(errs, builder.config.HTTPConfig.Prepare(&builder.config.ctx)...)
warnings, isoErrs := builder.config.ISOConfig.Prepare(&builder.config.ctx)
errs = packer.MultiErrorAppend(errs, isoErrs...)
if errs != nil && len(errs.Errors) > 0 {
return warnings, errs
}
return warnings, nil
}
// Run a packer build and returns a packer.Artifact representing a raw image.
func (builder *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
driver, err := builder.newDriver(ui)
if err != nil {
return nil, err
}
var steps []multistep.Step
// Download the ISO.
steps = append(steps, &common.StepDownload{
Checksum: builder.config.ISOChecksum,
ChecksumType: builder.config.ISOChecksumType,
Description: "ISO",
ResultKey: "iso_path",
TargetPath: builder.config.TargetPath,
Url: builder.config.ISOUrls,
Extension: builder.config.TargetExtension,
})
// Create output directory.
steps = append(steps, &stepOutputDir{
outputPath: builder.config.OutputDir,
force: builder.config.PackerForce,
})
diskPath := filepath.Join(builder.config.OutputDir, builder.config.VMName) + "." + builder.config.Format
// Create the disk.
steps = append(steps, &stepCreateDisk{
diskPath: diskPath,
diskFormat: builder.config.Format,
diskSize: builder.config.DiskSize,
})
// Set up the HTTP server.
steps = append(steps, &common.StepHTTPServer{
HTTPDir: builder.config.HTTPDir,
HTTPPortMin: builder.config.HTTPPortMin,
HTTPPortMax: builder.config.HTTPPortMax,
})
// Start the VM.
steps = append(steps, &stepRun{
vmName: builder.config.VMName,
diskPath: diskPath,
diskFormat: builder.config.Format,
memSize: builder.config.MemSize,
})
// Execute the boot command sequence.
steps = append(steps, &stepBootCommand{
bootWait: builder.config.BootWait,
bootCommand: builder.config.FlatBootCommand(),
ctx: builder.config.ctx,
})
// Set up a state bag.
state := new(multistep.BasicStateBag)
state.Put("ui", ui)
state.Put("hook", hook)
state.Put("cache", cache)
state.Put("config", &builder.config)
state.Put("driver", driver)
// Run the jewels.
builder.runner = common.NewRunnerWithPauseFn(steps, builder.config.PackerConfig, ui, state)
builder.runner.Run(state)
if v := state.Get("error"); v != nil {
err = v.(error)
}
if err == nil {
err = errors.New("unknown error")
}
// If we were interrupted or cancelled, then just exit.
if _, ok := state.GetOk(multistep.StateCancelled); ok {
return nil, errors.New("Build was cancelled: " + err.Error())
}
if _, ok := state.GetOk(multistep.StateHalted); ok {
return nil, errors.New("Build was halted: " + err.Error())
}
artifact := &Artifact{
directory: builder.config.OutputDir,
files: []string{diskPath},
}
return artifact, nil
}
// Cancel the build process.
func (builder *Builder) Cancel() {
builder.runner.Cancel()
}
func (builder *Builder) newDriver(ui packer.Ui) (Driver, error) {
doasPath, err := exec.LookPath("doas")
if err != nil {
return nil, err
}
vmctlPath, err := exec.LookPath("vmctl")
if err != nil {
return nil, err
}
return &VmmDriver{
doasPath: doasPath,
logPath: filepath.Join(builder.config.OutputDir, builder.config.VMName+".log"),
vmctlPath: vmctlPath,
ui: ui,
}, nil
}