forked from thecodeteam/goisilon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolume.go
278 lines (239 loc) · 6.37 KB
/
volume.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package goisilon
import (
"context"
"os"
"path"
"strings"
"sync"
log "github.com/akutz/gournal"
apiv1 "github.com/thecodeteam/goisilon/api/v1"
apiv2 "github.com/thecodeteam/goisilon/api/v2"
)
type Volume *apiv1.IsiVolume
type VolumeChildren apiv2.ContainerChildList
type VolumeChildrenMap map[string]*apiv2.ContainerChild
//GetVolume returns a specific volume by name or ID
func (c *Client) GetVolume(
ctx context.Context, id, name string) (Volume, error) {
if id != "" {
name = id
}
volume, err := apiv1.GetIsiVolume(ctx, c.API, name)
if err != nil {
return nil, err
}
var isiVolume = &apiv1.IsiVolume{Name: name, AttributeMap: volume.AttributeMap}
return isiVolume, nil
}
//GetVolumes returns a list of volumes
func (c *Client) GetVolumes(ctx context.Context) ([]Volume, error) {
volumes, err := apiv1.GetIsiVolumes(ctx, c.API)
if err != nil {
return nil, err
}
var isiVolumes []Volume
for _, volume := range volumes.Children {
newVolume := &apiv1.IsiVolume{Name: volume.Name}
isiVolumes = append(isiVolumes, newVolume)
}
return isiVolumes, nil
}
//CreateVolume creates a volume
func (c *Client) CreateVolume(
ctx context.Context, name string) (Volume, error) {
_, err := apiv1.CreateIsiVolume(ctx, c.API, name)
if err != nil {
return nil, err
}
var isiVolume = &apiv1.IsiVolume{Name: name, AttributeMap: nil}
return isiVolume, nil
}
// DeleteVolume deletes a volume
func (c *Client) DeleteVolume(
ctx context.Context, name string) error {
_, err := apiv1.DeleteIsiVolume(ctx, c.API, name)
return err
}
// ConcurrentHTTPConnections is the number of allowed concurrent HTTP
// connections for API functions that attempt to send multiple API calls at
// once.
var ConcurrentHTTPConnections = 2
func newConcurrentHTTPChan() chan bool {
c := make(chan bool, ConcurrentHTTPConnections)
for i := 0; i < ConcurrentHTTPConnections; i++ {
c <- true
}
return c
}
// ForceDeleteVolume force deletes a volume by resetting the ownership of
// all descendent directories to the current user prior to issuing a delete
// call.
func (c *Client) ForceDeleteVolume(ctx context.Context, name string) error {
var (
user = c.API.User()
vpl = len(c.API.VolumesPath()) + 1
errs = make(chan error)
queryDone = make(chan int)
childPaths = make(chan string)
setACLWait = &sync.WaitGroup{}
setACLChan = newConcurrentHTTPChan()
setACLDone = make(chan int)
mode = apiv2.FileMode(0755)
acl = &apiv2.ACL{
Action: &apiv2.PActionTypeReplace,
Authoritative: &apiv2.PAuthoritativeTypeMode,
Owner: &apiv2.Persona{
ID: &apiv2.PersonaID{
ID: user,
Type: apiv2.PersonaIDTypeUser,
},
},
Mode: &mode,
}
)
go func() {
queryChan, queryErrs := apiv2.ContainerChildrenGetQuery(
ctx, c.API, name, 1000, -1, "container", "ASC",
[]string{"container_path", "name"},
[]string{"owner", "name", "container_path"})
go func() {
if err := <-queryErrs; err != nil {
errs <- err
close(errs)
}
}()
go func() {
for child := range queryChan {
if strings.EqualFold(user, *child.Owner) {
continue
}
setACLWait.Add(1)
go func(s string) {
childPaths <- s
}(path.Join(*child.Path, *child.Name)[vpl:])
}
close(queryDone)
}()
}()
go func() {
for childPath := range childPaths {
go func(childPath string) {
<-setACLChan
if err := apiv2.ACLUpdate(
ctx,
c.API,
childPath,
acl); err != nil {
go func(childPath string) {
childPaths <- childPath
}(childPath)
} else {
setACLWait.Done()
}
setACLChan <- true
}(childPath)
}
}()
go func() {
<-queryDone
setACLWait.Wait()
close(setACLDone)
}()
select {
case <-setACLDone:
case err := <-errs:
if err != nil {
return err
}
}
return c.DeleteVolume(ctx, name)
}
//CopyVolume creates a volume based on an existing volume
func (c *Client) CopyVolume(
ctx context.Context, src, dest string) (Volume, error) {
_, err := apiv1.CopyIsiVolume(ctx, c.API, src, dest)
if err != nil {
return nil, err
}
return c.GetVolume(ctx, dest, dest)
}
//ExportVolume exports a volume
func (c *Client) ExportVolume(
ctx context.Context, name string) (int, error) {
return c.Export(ctx, name)
}
//UnexportVolume stops exporting a volume
func (c *Client) UnexportVolume(
ctx context.Context, name string) error {
return c.Unexport(ctx, name)
}
// QueryVolumeChildren retrieves a list of all of a volume's descendent files
// and directories.
func (c *Client) QueryVolumeChildren(
ctx context.Context, name string) (VolumeChildrenMap, error) {
return apiv2.ContainerChildrenMapAll(ctx, c.API, name)
}
// CreateVolumeDir creates a directory inside a volume.
func (c *Client) CreateVolumeDir(
ctx context.Context,
volumeName, dirPath string,
fileMode os.FileMode,
overwrite, recursive bool) error {
return apiv2.ContainerCreateDir(
ctx, c.API, volumeName, dirPath,
apiv2.FileMode(fileMode), overwrite, recursive)
}
// GetVolumeExportMap returns a map that relates Volumes to their corresponding
// Exports. This function uses an Export's "clients" property to define the
// relationship. The flag "includeRootClients" can be set to "true" in order to
// also inspect the "root_clients" property of an Export when determining the
// Volume-to-Export relationship.
func (c *Client) GetVolumeExportMap(
ctx context.Context,
includeRootClients bool) (map[Volume]Export, error) {
volumes, err := c.GetVolumes(ctx)
if err != nil {
return nil, err
}
exports, err := c.GetExports(ctx)
if err != nil {
return nil, err
}
volToExpMap := map[Volume]Export{}
for _, v := range volumes {
vp := c.API.VolumePath(v.Name)
for _, e := range exports {
if e.Clients == nil {
continue
}
for _, p := range *e.Clients {
if vp == p {
if _, ok := volToExpMap[v]; ok {
log.WithFields(map[string]interface{}{
"volumeName": v.Name,
"volumePath": vp,
}).Info(ctx, "vol-ex client map already defined")
break
}
volToExpMap[v] = e
}
}
if !includeRootClients || e.RootClients == nil {
continue
}
for _, p := range *e.RootClients {
if vp == p {
if _, ok := volToExpMap[v]; ok {
log.WithFields(map[string]interface{}{
"volumeName": v.Name,
"volumePath": vp,
}).Info(ctx, "vol-ex root client map already defined")
break
}
volToExpMap[v] = e
}
}
}
}
return volToExpMap, nil
}