Skip to content

Commit 3cd44ec

Browse files
committed
Merge pull request docker-archive-public#1277 from ggiamarchi/openstack/bug#1269
Openstack / Stop polling the instance when status is ERROR
2 parents eb5729d + de817cf commit 3cd44ec

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

drivers/openstack/client.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"crypto/tls"
55
"fmt"
66
"net/http"
7+
"time"
78

89
"github.com/docker/machine/log"
10+
"github.com/docker/machine/utils"
911
"github.com/docker/machine/version"
1012
"github.com/rackspace/gophercloud"
1113
"github.com/rackspace/gophercloud/openstack"
@@ -31,7 +33,7 @@ type Client interface {
3133
StopInstance(d *Driver) error
3234
RestartInstance(d *Driver) error
3335
DeleteInstance(d *Driver) error
34-
WaitForInstanceStatus(d *Driver, status string, timeout int) error
36+
WaitForInstanceStatus(d *Driver, status string) error
3537
GetInstanceIpAddresses(d *Driver) ([]IpAddress, error)
3638
CreateKeyPair(d *Driver, name string, publicKey string) error
3739
DeleteKeyPair(d *Driver, name string) error
@@ -132,11 +134,23 @@ func (c *GenericClient) DeleteInstance(d *Driver) error {
132134
return nil
133135
}
134136

135-
func (c *GenericClient) WaitForInstanceStatus(d *Driver, status string, timeout int) error {
136-
if err := servers.WaitForStatus(c.Compute, d.MachineId, status, timeout); err != nil {
137-
return err
138-
}
139-
return nil
137+
func (c *GenericClient) WaitForInstanceStatus(d *Driver, status string) error {
138+
return utils.WaitForSpecificOrError(func() (bool, error) {
139+
current, err := servers.Get(c.Compute, d.MachineId).Extract()
140+
if err != nil {
141+
return true, err
142+
}
143+
144+
if current.Status == "ERROR" {
145+
return true, fmt.Errorf("Instance creation failed. Instance is in ERROR state")
146+
}
147+
148+
if current.Status == status {
149+
return true, nil
150+
}
151+
152+
return false, nil
153+
}, 50, 4*time.Second)
140154
}
141155

142156
func (c *GenericClient) GetInstanceIpAddresses(d *Driver) ([]IpAddress, error) {

drivers/openstack/openstack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ func (d *Driver) assignFloatingIp() error {
664664

665665
func (d *Driver) waitForInstanceActive() error {
666666
log.WithField("MachineId", d.MachineId).Debug("Waiting for the OpenStack instance to be ACTIVE...")
667-
if err := d.client.WaitForInstanceStatus(d, "ACTIVE", 200); err != nil {
667+
if err := d.client.WaitForInstanceStatus(d, "ACTIVE"); err != nil {
668668
return err
669669
}
670670
return nil

utils/utils.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,26 @@ func CopyFile(src, dst string) error {
9494
return nil
9595
}
9696

97-
func WaitForSpecific(f func() bool, maxAttempts int, waitInterval time.Duration) error {
97+
func WaitForSpecificOrError(f func() (bool, error), maxAttempts int, waitInterval time.Duration) error {
9898
for i := 0; i < maxAttempts; i++ {
99-
if f() {
99+
stop, err := f()
100+
if err != nil {
101+
return err
102+
}
103+
if stop {
100104
return nil
101105
}
102106
time.Sleep(waitInterval)
103107
}
104108
return fmt.Errorf("Maximum number of retries (%d) exceeded", maxAttempts)
105109
}
106110

111+
func WaitForSpecific(f func() bool, maxAttempts int, waitInterval time.Duration) error {
112+
return WaitForSpecificOrError(func() (bool, error) {
113+
return f(), nil
114+
}, maxAttempts, waitInterval)
115+
}
116+
107117
func WaitFor(f func() bool) error {
108118
return WaitForSpecific(f, 60, 3*time.Second)
109119
}

0 commit comments

Comments
 (0)