-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvolumes.go
More file actions
174 lines (138 loc) · 3.68 KB
/
volumes.go
File metadata and controls
174 lines (138 loc) · 3.68 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
package cmd
import (
"fmt"
"io"
"regexp"
"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)
for _, volume := range volumes {
fmt.Fprintf(wOut, "--- %s\t%s\n", volume.Name, volume.Size)
for k, v := range volume.Path {
fmt.Fprintf(wOut, "%s\t\t%s\n", k, v)
}
}
}
// 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
}
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
}
// volumeMap, err := parseVolume(volumeVars)
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
}