forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_create_vm.go
82 lines (68 loc) · 1.92 KB
/
step_create_vm.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
package iso
import (
"fmt"
"github.com/mitchellh/multistep"
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
"github.com/mitchellh/packer/packer"
"time"
)
// This step creates the actual virtual machine.
//
// Produces:
// vmName string - The name of the VM
type stepCreateVM struct {
vmName string
}
func (s *stepCreateVM) Run(state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
driver := state.Get("driver").(vboxcommon.Driver)
ui := state.Get("ui").(packer.Ui)
name := config.VMName
commands := make([][]string, 4)
commands[0] = []string{
"createvm", "--name", name,
"--ostype", config.GuestOSType, "--register",
}
commands[1] = []string{
"modifyvm", name,
"--boot1", "disk", "--boot2", "dvd", "--boot3", "none", "--boot4", "none",
}
commands[2] = []string{"modifyvm", name, "--cpus", "1"}
commands[3] = []string{"modifyvm", name, "--memory", "512"}
ui.Say("Creating virtual machine...")
for _, command := range commands {
err := driver.VBoxManage(command...)
if err != nil {
err := fmt.Errorf("Error creating VM: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Set the VM name property on the first command
if s.vmName == "" {
s.vmName = name
}
}
// Set the final name in the state bag so others can use it
state.Put("vmName", s.vmName)
return multistep.ActionContinue
}
func (s *stepCreateVM) Cleanup(state multistep.StateBag) {
if s.vmName == "" {
return
}
driver := state.Get("driver").(vboxcommon.Driver)
ui := state.Get("ui").(packer.Ui)
ui.Say("Unregistering and deleting virtual machine...")
var err error = nil
for i := 0; i < 5; i++ {
err = driver.VBoxManage("unregistervm", s.vmName, "--delete")
if err == nil {
break
}
time.Sleep(1 * time.Second * time.Duration(i))
}
if err != nil {
ui.Error(fmt.Sprintf("Error deleting virtual machine: %s", err))
}
}