-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.go
63 lines (56 loc) · 1.69 KB
/
utils.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
package main
import (
"context"
"errors"
"math"
"os"
"time"
"github.com/docker/go-plugins-helpers/volume"
"github.com/linode/linodego"
log "github.com/sirupsen/logrus"
)
// waitForDeviceFileExists waits until path devicePath becomes available or
// times out.
func waitForDeviceFileExists(devicePath string, waitSeconds int) error {
return waitForCondition(waitSeconds, 1, func() bool {
// found, then break
if _, err := os.Stat(devicePath); !os.IsNotExist(err) {
return true // condition met
}
log.Infof("Waiting for device %s to be available", devicePath)
return false
})
}
func waitForLinodeVolumeDetachment(linodeAPI linodego.Client, volumeID, timeout int) error {
// Wait for linode to have the volume detached
return waitForCondition(timeout, 2, func() bool {
v, err := linodeAPI.GetVolume(context.Background(), volumeID)
if err != nil {
log.Error(err)
return false
}
return v.LinodeID == nil
})
}
// waitForCondition Waits until condition returns true timeout is reached. If timeout is
// reached it returns error.
func waitForCondition(waitSeconds int, intervalSeconds int, check func() bool) error {
loops := int(math.Ceil(float64(waitSeconds) / float64(intervalSeconds)))
for i := 0; i < loops; i++ {
if check() {
return nil
}
time.Sleep(time.Second * time.Duration(intervalSeconds))
}
return errors.New("waitForCondition timeout")
}
// linodeVolumeToDockerVolume converts a linode volume to a docker volume
func linodeVolumeToDockerVolume(lv linodego.Volume, mp string) *volume.Volume {
v := &volume.Volume{
Name: lv.Label,
Mountpoint: mp,
CreatedAt: lv.Created.Format(time.RFC3339),
Status: make(map[string]interface{}),
}
return v
}