/
docker.go
112 lines (96 loc) · 2.79 KB
/
docker.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package volume
import (
"context"
"fmt"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
volumetypes "github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
)
func VolumePath(volumeName string) (mountPath string, err error) {
cli, err := client.NewClientWithOpts(client.WithVersion("1.39"))
if err != nil {
return "", err
}
emptyArgs := filters.Args{}
//
volumeListBody, err := cli.VolumeList(context.Background(), emptyArgs)
if err != nil {
return "", err
}
//log.Printf("volume list body content \n%s\n", volumeListBody)
v := volumeListBody.Volumes
for _, vol := range v {
//log.Printf("volume Metadata \nName: %s\n MountPath: %s\n", vol.Name, vol.Mountpoint)
if vol.Name == volumeName {
mountPath = vol.Mountpoint
return mountPath, nil
}
}
return "", nil
}
func AddVolume(volumeName string) (volume types.Volume, err error) {
cli, err := client.NewClientWithOpts(client.WithVersion("1.39"))
if err != nil {
return volume, err
}
options := volumetypes.VolumeCreateBody{}
options.Name = volumeName
volume, err = cli.VolumeCreate(context.Background(), options)
return volume, err
}
func RemoveVolume(volumeName string) (err error) {
cli, err := client.NewClientWithOpts(client.WithVersion("1.39"))
if err != nil {
return err
}
err = cli.VolumeRemove(context.Background(), volumeName, false)
return err
}
func highestTime(modifiedTime map[string]time.Time) (highest time.Time) {
for _, t := range modifiedTime {
if t.After(highest) {
highest = t
}
}
return
}
func AnalyzeVolume(volume *types.Volume) (name string, size int64, updated time.Time, err error) {
_, timeMap, size, err := GetFilesPathSize(volume.Mountpoint)
updated = highestTime(timeMap)
return volume.Name, size, updated, err
}
func ListVolumes() (volumeList []*types.Volume, err error) {
cli, err := client.NewClientWithOpts(client.WithVersion("1.39"))
if err != nil {
return volumeList, err
}
emptyArgs := filters.Args{}
//
volumeListBody, err := cli.VolumeList(context.Background(), emptyArgs)
if err != nil {
return volumeList, err
}
volumeList = volumeListBody.Volumes
return volumeList, err
}
func VolumeExpiriment() {
cli, err := client.NewClientWithOpts(client.WithVersion("1.39"))
if err != nil {
panic(err)
}
emptyArgs := filters.Args{}
//
volumeListBody, err := cli.VolumeList(context.Background(), emptyArgs)
fmt.Printf("volume list body content \n%v\n", volumeListBody)
fmt.Print("Volume's:\n")
v := volumeListBody.Volumes
// TODO add filter on driver
for _, vol := range v {
fmt.Printf("Name: %s\n MountPath: %s\n Driver: %s\n\n", vol.Name, vol.Mountpoint, vol.Driver)
vn, sz, up, _ := AnalyzeVolume(vol)
szh := SIByteFormat(uint64(sz))
fmt.Printf("Analyzed Volume: %s %s, %v\n", vn, szh, up)
}
}