forked from schubergphilis/packer-cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 3
/
step_deploy_virtual_machine.go
115 lines (92 loc) · 3.07 KB
/
step_deploy_virtual_machine.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
package cloudstack
import (
"fmt"
"github.com/mindjiver/gopherstack"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common/uuid"
"github.com/mitchellh/packer/packer"
)
type stepDeployVirtualMachine struct {
id string
}
type bootCommandTemplateData struct {
HTTPIP string
HTTPPort string
Name string
}
func (s *stepDeployVirtualMachine) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*gopherstack.CloudstackClient)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(config)
sshKeyName := state.Get("ssh_key_name").(string)
ui.Say("Creating virtual machine...")
// Some random virtual machine name as it's temporary
displayName := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
// Massage any userData that we wish to send to the virtual
// machine to help it boot properly.
processTemplatedUserdata(state)
userData := state.Get("user_data").(string)
// Create the virtual machine based on configuration
response, err := client.DeployVirtualMachine(c.ServiceOfferingId,
c.TemplateId, c.ZoneId, "", c.DiskOfferingId, displayName,
c.NetworkIds, sshKeyName, "", userData, c.Hypervisor)
if err != nil {
err := fmt.Errorf("Error deploying virtual machine: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Unpack the async jobid and wait for it
vmid := response.Deployvirtualmachineresponse.ID
jobid := response.Deployvirtualmachineresponse.Jobid
client.WaitForAsyncJob(jobid, c.stateTimeout)
// We use this in cleanup
s.id = vmid
// Store the virtual machine id for later use
state.Put("virtual_machine_id", vmid)
return multistep.ActionContinue
}
func (s *stepDeployVirtualMachine) Cleanup(state multistep.StateBag) {
// If the virtual machine id isn't there, we probably never created it
if s.id == "" {
return
}
client := state.Get("client").(*gopherstack.CloudstackClient)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(config)
// Destroy the virtual machine we just created
ui.Say("Destroying virtual machine...")
response, err := client.DestroyVirtualMachine(s.id)
if err != nil {
ui.Error(fmt.Sprintf(
"Error destroying virtual machine. Please destroy it manually."))
}
jobid := response.Destroyvirtualmachineresponse.Jobid
client.WaitForAsyncJob(jobid, c.stateTimeout)
}
func processTemplatedUserdata(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(config)
// If there is no userdata to process we just save back an
// empty string.
if c.UserData == "" {
state.Put("user_data", "")
return multistep.ActionContinue
}
httpIP := state.Get("http_ip").(string)
httpPort := state.Get("http_port").(string)
tplData := &bootCommandTemplateData{
httpIP,
httpPort,
c.TemplateName,
}
userData, err := c.tpl.Process(c.UserData, tplData)
if err != nil {
err := fmt.Errorf("Error preparing boot command: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("user_data", userData)
return multistep.ActionContinue
}