forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_check.go
89 lines (80 loc) · 2.36 KB
/
storage_check.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
package storage
import (
"os"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/utils"
"golang.org/x/net/context"
"path/filepath"
)
const (
rancherSockDir = "/var/run/rancher/storage"
)
func IsVolumeActive(volume model.Volume, storagePool model.StoragePool, dockerClient *client.Client) (bool, error) {
if !isManagedVolume(volume) {
return true, nil
}
vol, err := dockerClient.VolumeInspect(context.Background(), volume.Name)
if client.IsErrVolumeNotFound(err) {
return false, nil
} else if err != nil {
return false, errors.Wrap(err, constants.IsVolumeActiveError)
}
if vol.Mountpoint != "" {
return vol.Mountpoint != "moved", nil
}
return true, nil
}
func rancherStorageSockPath(volume model.Volume) string {
return filepath.Join(rancherSockDir, volume.Data.Fields.Driver+".sock")
}
func IsRancherVolume(volume model.Volume) bool {
if !isManagedVolume(volume) {
return false
}
sockFile := rancherStorageSockPath(volume)
if _, err := os.Stat(sockFile); err == nil {
return true
}
return false
}
func IsImageActive(image model.Image, storagePool model.StoragePool, dockerClient *client.Client) (bool, error) {
if utils.IsImageNoOp(image) {
return true, nil
}
_, _, err := dockerClient.ImageInspectWithRaw(context.Background(), image.Name)
if err == nil {
return true, nil
} else if client.IsErrImageNotFound(err) {
return false, nil
}
return false, errors.Wrap(err, constants.IsImageActiveError)
}
func IsVolumeRemoved(volume model.Volume, storagePool model.StoragePool, client *client.Client) (bool, error) {
if volume.DeviceNumber == 0 {
container, err := utils.GetContainer(client, volume.Instance, false)
if err != nil {
if !utils.IsContainerNotFoundError(err) {
return false, errors.Wrap(err, constants.IsVolumeRemovedError+"failed to get container")
}
}
return container.ID == "", nil
} else if isManagedVolume(volume) {
ok, err := IsVolumeActive(volume, storagePool, client)
if err != nil {
return false, errors.Wrap(err, constants.IsVolumeRemovedError+"failed to check whether volume is activated")
}
return !ok, nil
}
path := pathToVolume(volume)
if !volume.Data.Fields.IsHostPath {
return true, nil
}
_, exist := os.Stat(path)
if exist != nil {
return true, nil
}
return false, nil
}