Skip to content

Commit b5fc2d3

Browse files
committed
Move IP address listener outside interruptible wait
This commit moves the goroutine used to listen for IP address changes. It has been moved outside the interruptible wait, since the goroutine should only be fired once. Moving the goroutine outside the interruptible wait renders the "isRunning" property unnecessary. This property was introduced in bf1cc1d (Add support for IP address changes, 2023-04-08)
1 parent fab0c4b commit b5fc2d3

File tree

1 file changed

+47
-53
lines changed

1 file changed

+47
-53
lines changed

builder/xenserver/common/step_wait_for_ip.go

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import (
1111

1212
type StepWaitForIP struct {
1313
VmCleanup
14-
Chan <-chan string
15-
Timeout time.Duration
16-
isRunning bool
14+
Chan <-chan string
15+
Timeout time.Duration
1716
}
1817

1918
func (self *StepWaitForIP) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
@@ -30,62 +29,57 @@ func (self *StepWaitForIP) Run(ctx context.Context, state multistep.StateBag) mu
3029
return multistep.ActionHalt
3130
}
3231

33-
var ip string
34-
err = InterruptibleWait{
35-
Timeout: self.Timeout,
36-
PredicateInterval: 5 * time.Second,
37-
Predicate: func() (result bool, err error) {
38-
if !self.isRunning {
39-
go func(c Connection, ui packer.Ui, config CommonConfig) {
40-
self.isRunning = true
41-
state.Put("instance_ssh_address", "")
42-
var ip string
43-
44-
for true {
45-
time.Sleep(500 * time.Millisecond)
46-
if config.IPGetter == "auto" || config.IPGetter == "http" {
47-
// Snoop IP from HTTP fetch
48-
select {
49-
case temp_ip := <-self.Chan:
50-
if ip != temp_ip {
51-
ip = temp_ip
52-
ui.Message(fmt.Sprintf("Got IP '%s' from HTTP request", ip))
53-
state.Put("instance_ssh_address", ip)
54-
}
55-
default:
56-
}
57-
}
58-
59-
if config.IPGetter == "auto" || config.IPGetter == "tools" {
60-
// Look for PV IP
61-
m, err := c.client.VM.GetGuestMetrics(c.session, instance)
62-
if err != nil {
63-
continue
64-
}
65-
if m != "" {
66-
metrics, err := c.client.VMGuestMetrics.GetRecord(c.session, m)
67-
if err != nil {
68-
continue
69-
}
70-
networks := metrics.Networks
71-
if temp_ip, ok := networks["0/ip"]; ok {
72-
if temp_ip != "" && ip != temp_ip {
73-
ip = temp_ip
74-
ui.Message(fmt.Sprintf("Got IP '%s' from XenServer tools", ip))
75-
state.Put("instance_ssh_address", ip)
76-
}
77-
}
78-
}
32+
go func(c Connection, ui packer.Ui, config CommonConfig) {
33+
state.Put("instance_ssh_address", "")
34+
var ip string
35+
36+
for true {
37+
time.Sleep(500 * time.Millisecond)
38+
if config.IPGetter == "auto" || config.IPGetter == "http" {
39+
// Snoop IP from HTTP fetch
40+
select {
41+
case temp_ip := <-self.Chan:
42+
if ip != temp_ip {
43+
ip = temp_ip
44+
ui.Message(fmt.Sprintf("Got IP '%s' from HTTP request", ip))
45+
state.Put("instance_ssh_address", ip)
46+
}
47+
default:
48+
}
49+
}
7950

51+
if config.IPGetter == "auto" || config.IPGetter == "tools" {
52+
// Look for PV IP
53+
m, err := c.client.VM.GetGuestMetrics(c.session, instance)
54+
if err != nil {
55+
continue
56+
}
57+
if m != "" {
58+
metrics, err := c.client.VMGuestMetrics.GetRecord(c.session, m)
59+
if err != nil {
60+
continue
61+
}
62+
networks := metrics.Networks
63+
if temp_ip, ok := networks["0/ip"]; ok {
64+
if temp_ip != "" && ip != temp_ip {
65+
ip = temp_ip
66+
ui.Message(fmt.Sprintf("Got IP '%s' from XenServer tools", ip))
67+
state.Put("instance_ssh_address", ip)
8068
}
8169
}
70+
}
8271

83-
self.isRunning = false
84-
}(*c, ui, config)
85-
86-
time.Sleep(500 * time.Millisecond)
8772
}
73+
}
74+
}(*c, ui, config)
8875

76+
time.Sleep(500 * time.Millisecond)
77+
78+
var ip string
79+
err = InterruptibleWait{
80+
Timeout: self.Timeout,
81+
PredicateInterval: 5 * time.Second,
82+
Predicate: func() (result bool, err error) {
8983
if ip = state.Get("instance_ssh_address").(string); ip != "" {
9084
return true, nil
9185
}

0 commit comments

Comments
 (0)