-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvolumes.go
More file actions
198 lines (160 loc) · 4.27 KB
/
volumes.go
File metadata and controls
198 lines (160 loc) · 4.27 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package cmd
import (
"fmt"
"github.com/drycc/pkg/prettyprint"
"io"
"regexp"
"strings"
"github.com/drycc/controller-sdk-go/api"
"github.com/drycc/controller-sdk-go/volumes"
)
// VolumesList list volumes in the application
func (d *DryccCmd) VolumesList(appID string, results int) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
if results == defaultLimit {
results = s.Limit
}
//fmt.Println("-----app-----", appID) # debug
volumes, count, err := volumes.List(s.Client, appID, results)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
if count == 0 {
d.Println("Could not find any volume")
} else {
printVolumes(d, appID, volumes, d.WOut)
}
return nil
}
// printVolumes format volume data
func printVolumes(d *DryccCmd, appID string, volumes api.Volumes, wOut io.Writer) {
fmt.Fprintf(wOut, "=== %s volumes\n", appID)
var max int
for _, volume := range volumes {
if max < (len(volume.Name) + 4) {
max = len(volume.Name) + 4
}
for key := range volume.Path {
if max < len(key) {
max = len(key)
}
}
}
max = max + 5
for _, volume := range volumes {
nameSpaces := strings.Repeat(" ", max-len(volume.Name)-4)
fmt.Fprintf(wOut, "--- %s%s%s\n", volume.Name, nameSpaces, volume.Size)
pathMap := make(map[string]string)
for key, value := range volume.Path {
pathMap[key] = fmt.Sprintf("%v", value)
}
lenDataMap := mapMaxLen(pathMap)
d.Print(prettyprint.PrettyTabs(pathMap, max-lenDataMap))
}
}
// VolumesCreate create a volume for the application
func (d *DryccCmd) VolumesCreate(appID, name string, size string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
regex := regexp.MustCompile("^([1-9][0-9]*[mgMG])$")
if !regex.MatchString(size) {
return fmt.Errorf(`%s doesn't fit format #unit
Examples: 2G 2g 500M 500m`, size)
}
d.Printf("Creating %s to %s... ", name, appID)
quit := progress(d.WOut)
volume := api.Volume{
Name: name,
Size: size,
}
_, err = volumes.Create(s.Client, appID, volume)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Println("done")
return nil
}
// VolumesDelete delete a volume from the application
func (d *DryccCmd) VolumesDelete(appID, name string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
d.Printf("Deleting %s from %s... ", name, appID)
quit := progress(d.WOut)
err = volumes.Delete(s.Client, appID, name)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Println("done")
return nil
}
// VolumesMount mount a volume to process of the application
func (d *DryccCmd) VolumesMount(appID string, name string, volumeVars []string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
volumeMap, err := parseVolume(volumeVars)
if err != nil {
return err
}
d.Print("Mounting volume... ")
quit := progress(d.WOut)
volumeObj := api.Volume{Path: volumeMap}
_, err = volumes.Mount(s.Client, appID, name, volumeObj)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Print("done\n")
return nil
}
// VolumesUnmount unmount a volume from process of the application
func (d *DryccCmd) VolumesUnmount(appID string, name string, volumeVars []string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
valuesMap := make(map[string]interface{})
for _, volumeVar := range volumeVars {
valuesMap[volumeVar] = nil
}
if err != nil {
return err
}
d.Print("Unmounting volume... ")
quit := progress(d.WOut)
volumeObj := api.Volume{Path: valuesMap}
_, err = volumes.Mount(s.Client, appID, name, volumeObj)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Print("done\n")
return nil
}
func parseVolume(volumeVars []string) (map[string]interface{}, error) {
volumeMap := make(map[string]interface{})
regex := regexp.MustCompile(`^([A-z_]+[A-z0-9_]*)=([\s\S]*)$`)
for _, volume := range volumeVars {
if regex.MatchString(volume) {
captures := regex.FindStringSubmatch(volume)
volumeMap[captures[1]] = captures[2]
} else {
return nil, fmt.Errorf("'%s' does not match the pattern 'key=var', ex: MODE=test", volume)
}
}
return volumeMap, nil
}