exoscale: use the new `utils.WaitForSpecificOrError` function to wait for job to finish

Instead of using a custom loop, use the newly introduced
`utils.WaitForSpecificOrError` function. Beware that on success, we use
the successful response to get the VM object (outside the loop).

Signed-off-by: Vincent Bernat <Vincent.Bernat@exoscale.ch>
This commit is contained in:
Vincent Bernat 2015-05-29 10:14:10 +02:00
parent 2a60a4ea32
commit 2d25f56ff7
1 changed files with 8 additions and 11 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/docker/machine/drivers" "github.com/docker/machine/drivers"
"github.com/docker/machine/log" "github.com/docker/machine/log"
"github.com/docker/machine/state" "github.com/docker/machine/state"
"github.com/docker/machine/utils"
"github.com/pyr/egoscale/src/egoscale" "github.com/pyr/egoscale/src/egoscale"
) )
@ -421,28 +422,24 @@ func (d *Driver) Kill() error {
func (d *Driver) waitForVM(client *egoscale.Client, jobid string) (*egoscale.DeployVirtualMachineResponse, error) { func (d *Driver) waitForVM(client *egoscale.Client, jobid string) (*egoscale.DeployVirtualMachineResponse, error) {
log.Infof("Waiting for VM...") log.Infof("Waiting for VM...")
maxRepeats := 60
i := 0
var resp *egoscale.QueryAsyncJobResultResponse var resp *egoscale.QueryAsyncJobResultResponse
var err error var err error
WaitLoop: if err = utils.WaitForSpecificOrError(func() (bool, error) {
for ; i < maxRepeats; i++ {
resp, err = client.PollAsyncJob(jobid) resp, err = client.PollAsyncJob(jobid)
if err != nil { if err != nil {
return nil, err return true, err
} }
switch resp.Jobstatus { switch resp.Jobstatus {
case 0: // Job is still in progress case 0: // Job is still in progress
case 1: // Job has successfully completed case 1: // Job has successfully completed
break WaitLoop return true, nil
case 2: // Job has failed to complete case 2: // Job has failed to complete
return nil, fmt.Errorf("Operation failed to complete") return true, fmt.Errorf("Operation failed to complete")
default: // Some other code default: // Some other code
} }
time.Sleep(2 * time.Second) return false, nil
} }, 60, 2*time.Second); err != nil {
if i == maxRepeats { return nil, err
return nil, fmt.Errorf("Timeout while waiting for VM")
} }
vm, err := client.AsyncToVirtualMachine(*resp) vm, err := client.AsyncToVirtualMachine(*resp)
if err != nil { if err != nil {