This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
builder.go
298 lines (239 loc) · 8.7 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
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
package iso
import (
"errors"
"fmt"
"log"
"os"
"time"
"github.com/mitchellh/multistep"
hypervcommon "github.com/MSOpenTech/packer-hyperv/packer/builder/hyperv/common"
// msbldcommon "github.com/MSOpenTech/packer-hyperv/packer/builder/common"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"regexp"
"code.google.com/p/go-uuid/uuid"
"strings"
)
// EVALUATION EDITIONS
const (
WS2012R2DC string = "WindowsServer2012R2Datacenter"
PRODUCT_DATACENTER_EVALUATION_SERVER int64 = 80
PRODUCT_DATACENTER_SERVER int64 = 8
)
// Builder implements packer.Builder and builds the actual Hyperv
// images.
type Builder struct {
config iso_config
runner multistep.Runner
}
type iso_config struct {
DiskSizeGB uint `mapstructure:"disk_size_gb"`
RamSizeMB uint `mapstructure:"ram_size_mb"`
GuestOSType string `mapstructure:"guest_os_type"`
RawSingleISOUrl string `mapstructure:"iso_url"`
SleepTimeMinutes time.Duration `mapstructure:"wait_time_minutes"`
ProductKey string `mapstructure:"product_key"`
common.PackerConfig `mapstructure:",squash"`
hypervcommon.OutputConfig `mapstructure:",squash"`
VMName string
SwitchName string
tpl *packer.ConfigTemplate
}
// Prepare processes the build configuration parameters.
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
md, err := common.DecodeConfig(&b.config, raws...)
if err != nil {
return nil, err
}
b.config.tpl, err = packer.NewConfigTemplate()
if err != nil {
return nil, err
}
log.Println(fmt.Sprintf("%s: %v", "PackerUserVars", b.config.PackerUserVars))
b.config.tpl.UserVars = b.config.PackerUserVars
// Accumulate any errors and warnings
errs := common.CheckUnusedConfig(md)
errs = packer.MultiErrorAppend(errs, b.config.OutputConfig.Prepare(b.config.tpl, &b.config.PackerConfig)...)
warnings := make([]string, 0)
if b.config.DiskSizeGB == 0 {
b.config.DiskSizeGB = 40
}
log.Println(fmt.Sprintf("%s: %v", "DiskSize", b.config.DiskSizeGB))
if(b.config.DiskSizeGB < 10 ){
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("disk_size_gb: Windows server requires disk space >= 10 GB, but defined: %v", b.config.DiskSizeGB))
} else if b.config.DiskSizeGB > 65536 {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("disk_size_gb: Windows server requires disk space <= 65536 GB, but defined: %v", b.config.DiskSizeGB))
}
if b.config.RamSizeMB == 0 {
b.config.RamSizeMB = 1024
}
log.Println(fmt.Sprintf("%s: %v", "RamSize", b.config.RamSizeMB))
var ramMinMb uint = 512
var ramMaxMb uint = 6538
if(b.config.RamSizeMB < ramMinMb ){
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("ram_size_mb: Windows server requires memory size >= %v MB, but defined: %v", ramMinMb, b.config.RamSizeMB))
} else if b.config.RamSizeMB > ramMaxMb {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("ram_size_mb: Windows server requires memory size <= %v MB, but defined: %v", ramMaxMb, b.config.RamSizeMB))
}
warnings = appendWarnings( warnings, fmt.Sprintf("Hyper-V might fail to create a VM if there is no available memory in the system."))
if b.config.VMName == "" {
b.config.VMName = fmt.Sprintf("pvm_%s", uuid.New())
}
if b.config.SwitchName == "" {
b.config.SwitchName = fmt.Sprintf("pis_%s", uuid.New())
}
if b.config.SleepTimeMinutes == 0 {
b.config.SleepTimeMinutes = 10
} else if b.config.SleepTimeMinutes < 0 {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("wait_time_minutes: '%v' %s", int64(b.config.SleepTimeMinutes), "the value can't be negative" ))
} else if b.config.SleepTimeMinutes > 1440 {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("wait_time_minutes: '%v' %s", uint(b.config.SleepTimeMinutes), "the value is too big" ))
} else if b.config.SleepTimeMinutes > 120 {
warnings = appendWarnings( warnings, fmt.Sprintf("wait_time_minutes: '%v' %s", uint(b.config.SleepTimeMinutes), "You may want to decrease the value. Usually 20 min is enough."))
}
log.Println(fmt.Sprintf("%s: %v", "SleepTimeMinutes", uint(b.config.SleepTimeMinutes)))
// Errors
templates := map[string]*string{
"iso_url": &b.config.RawSingleISOUrl,
"product_key": &b.config.ProductKey,
}
for n, ptr := range templates {
var err error
*ptr, err = b.config.tpl.Process(*ptr, nil)
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Error processing %s: %s", n, err))
}
}
pk := strings.TrimSpace(b.config.ProductKey)
if len(pk) != 0 {
pattern := "^[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}$"
value := pk
match, _ := regexp.MatchString(pattern, value)
if !match {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("product_key: Make sure the product_key follows the pattern: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"))
}
warnings = appendWarnings( warnings, fmt.Sprintf("product_key: %s", "value is not empty. Packer will try to activate Windows with the product key. To do this Packer will need an Internet connection."))
}
log.Println(fmt.Sprintf("%s: %v","VMName", b.config.VMName))
log.Println(fmt.Sprintf("%s: %v","SwitchName", b.config.SwitchName))
log.Println(fmt.Sprintf("%s: %v","ProductKey", b.config.ProductKey))
if b.config.RawSingleISOUrl == "" {
errs = packer.MultiErrorAppend(errs, errors.New("iso_url: The option can't be missed and a path must be specified."))
}else if _, err := os.Stat(b.config.RawSingleISOUrl); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("iso_url: Check the path is correct"))
}
log.Println(fmt.Sprintf("%s: %v","RawSingleISOUrl", b.config.RawSingleISOUrl))
guestOSTypesIsValid := false
guestOSTypes := []string{
WS2012R2DC,
// WS2012R2St,
}
log.Println(fmt.Sprintf("%s: %v","GuestOSType", b.config.GuestOSType))
for _, guestType := range guestOSTypes {
if b.config.GuestOSType == guestType {
guestOSTypesIsValid = true
break
}
}
if !guestOSTypesIsValid {
errs = packer.MultiErrorAppend(errs,
fmt.Errorf("guest_os_type: The value is invalid. Must be one of: %v", guestOSTypes))
}
if errs != nil && len(errs.Errors) > 0 {
return warnings, errs
}
return warnings, nil
}
// Run executes a Packer build and returns a packer.Artifact representing
// a Hyperv appliance.
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
// Create the driver that we'll use to communicate with Hyperv
driver, err := hypervcommon.NewHypervPS4Driver()
if err != nil {
return nil, fmt.Errorf("Failed creating Hyper-V driver: %s", err)
}
// Set up the state.
state := new(multistep.BasicStateBag)
state.Put("config", &b.config)
state.Put("driver", driver)
state.Put("hook", hook)
state.Put("ui", ui)
// TODO: remove the next line
// state.Put("vmName", "FullActiavated")
steps := []multistep.Step{
// new(hypervcommon.StepAcceptEula),
new(hypervcommon.StepCreateTempDir),
&hypervcommon.StepOutputDir{
Force: b.config.PackerForce,
Path: b.config.OutputDir,
},
&hypervcommon.StepCreateSwitch{
SwitchName: b.config.SwitchName,
},
new(StepCreateVM),
new(hypervcommon.StepEnableIntegrationService),
new(StepMountDvdDrive),
new(StepMountFloppydrive),
// new(hypervcommon.StepConfigureVlan),
new(hypervcommon.StepStartVm),
&hypervcommon.StepSleep{ Minutes: b.config.SleepTimeMinutes, ActionName: "Installing" },
new(hypervcommon.StepConfigureIp),
new(hypervcommon.StepSetRemoting),
new(common.StepProvision),
new(StepInstallProductKey),
new(StepExportVm),
// new(hypervcommon.StepConfigureIp),
// new(hypervcommon.StepSetRemoting),
// new(hypervcommon.StepCheckRemoting),
// new(msbldcommon.StepSysprep),
}
// Run the steps.
if b.config.PackerDebug {
b.runner = &multistep.DebugRunner{
Steps: steps,
PauseFn: common.MultistepDebugFn(ui),
}
} else {
b.runner = &multistep.BasicRunner{Steps: steps}
}
b.runner.Run(state)
// Report any errors.
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.")
}
return hypervcommon.NewArtifact(b.config.OutputDir)
}
// Cancel.
func (b *Builder) Cancel() {
if b.runner != nil {
log.Println("Cancelling the step runner...")
b.runner.Cancel()
}
}
func appendWarnings(slice []string, data ...string) []string {
m := len(slice)
n := m + len(data)
if n > cap(slice) { // if necessary, reallocate
// allocate double what's needed, for future growth.
newSlice := make([]string, (n+1)*2)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:n]
copy(slice[m:n], data)
return slice
}