forked from xenserver/packer-plugin-xenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_wait_for_ip.go
93 lines (74 loc) · 2.25 KB
/
step_wait_for_ip.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
package common
import (
"fmt"
"time"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"github.com/nilshell/xmlrpc"
xsclient "github.com/xenserver/go-xenserver-client"
)
type StepWaitForIP struct {
Chan <-chan string
Timeout time.Duration
}
func (self *StepWaitForIP) Run(state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(xsclient.XenAPIClient)
config := state.Get("commonconfig").(CommonConfig)
ui.Say("Step: Wait for VM's IP to become known to us.")
uuid := state.Get("instance_uuid").(string)
instance, err := client.GetVMByUuid(uuid)
if err != nil {
ui.Error(fmt.Sprintf("Unable to get VM from UUID '%s': %s", uuid, err.Error()))
return multistep.ActionHalt
}
var ip string
err = InterruptibleWait{
Timeout: self.Timeout,
PredicateInterval: 5 * time.Second,
Predicate: func() (result bool, err error) {
if config.IPGetter == "auto" || config.IPGetter == "http" {
// Snoop IP from HTTP fetch
select {
case ip = <-self.Chan:
ui.Message(fmt.Sprintf("Got IP '%s' from HTTP request", ip))
return true, nil
default:
}
}
if config.IPGetter == "auto" || config.IPGetter == "tools" {
// Look for PV IP
metrics, err := instance.GetGuestMetrics()
if err != nil {
return false, err
}
if metrics != nil {
networks := metrics["networks"].(xmlrpc.Struct)
if ipRaw, ok := networks["0/ip"]; ok {
if ip = ipRaw.(string); ip != "" {
ui.Message(fmt.Sprintf("Got IP '%s' from XenServer tools", ip))
return true, nil
}
}
}
}
return false, nil
},
}.Wait(state)
if err != nil {
ui.Error(fmt.Sprintf("Could not get IP address of VM: %s", err.Error()))
// @todo: give advice on what went wrong (no HTTP server? no PV drivers?)
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Got IP address '%s'", ip))
state.Put("instance_ssh_address", ip)
return multistep.ActionContinue
}
func (self *StepWaitForIP) Cleanup(state multistep.StateBag) {}
func InstanceSSHIP(state multistep.StateBag) (string, error) {
ip := state.Get("instance_ssh_address").(string)
return ip, nil
}
func InstanceSSHPort(state multistep.StateBag) (uint, error) {
return 22, nil
}