forked from schubergphilis/packer-cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 3
/
step_detach_iso.go
62 lines (52 loc) · 1.64 KB
/
step_detach_iso.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
package cloudstack
import (
"fmt"
"github.com/mindjiver/gopherstack"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"time"
)
type stepDetachIso struct{}
func (s *stepDetachIso) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*gopherstack.CloudstackClient)
c := state.Get("config").(config)
ui := state.Get("ui").(packer.Ui)
id := state.Get("virtual_machine_id").(string)
response, err := client.ListVirtualMachines(id)
if err != nil {
err := fmt.Errorf("Error checking virtual machine state: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// As we list the virtual machines with the unique UUID we
// know the VM we are after is the first one.
isoId := response.Listvirtualmachinesresponse.Virtualmachine[0].IsoId
if isoId == "" {
// No ISO image attached to virtual machine, we just
// continue
return multistep.ActionContinue
}
ui.Say(fmt.Sprintf("Waiting for %v before detaching ISO from virtual machine...", c.detachISOWait))
time.Sleep(c.detachISOWait)
response2, err := client.DetachIso(id)
if err != nil {
err := fmt.Errorf("Error detaching ISO from virtual machine: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Println("Waiting for detach event to complete...")
jobid := response2.Detachisoresponse.Jobid
err = client.WaitForAsyncJob(jobid, c.stateTimeout)
if err != nil {
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s *stepDetachIso) Cleanup(state multistep.StateBag) {
// no cleanup
}