/
s3fs_executor.go
207 lines (169 loc) · 4.72 KB
/
s3fs_executor.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
package executor
import (
"fmt"
"os"
"os/exec"
"path"
log "github.com/Sirupsen/logrus"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/goof"
"github.com/akutz/gotil"
"github.com/codedellemc/libstorage/api/registry"
"github.com/codedellemc/libstorage/api/types"
"github.com/codedellemc/libstorage/drivers/storage/s3fs"
"github.com/codedellemc/libstorage/drivers/storage/s3fs/utils"
)
// driver is the storage executor for the s3fs storage driver.
type driver struct {
config gofig.Config
cmd string
opts []string
szOpts string
}
func init() {
registry.RegisterStorageExecutor(s3fs.Name, newDriver)
}
func newDriver() types.StorageExecutor {
return &driver{}
}
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
d.config = config
fields := log.Fields{"driver": s3fs.Name}
d.cmd = d.config.GetString(s3fs.ConfigS3FSCmd)
fields["cmd"] = d.cmd
if v := d.config.GetStringSlice(s3fs.ConfigS3FSOptions); len(v) > 0 {
d.opts = v
fields["opts"] = d.opts
} else {
d.szOpts = d.config.GetString(s3fs.ConfigS3FSOptions)
fields["opts"] = d.szOpts
}
ctx.WithFields(fields).Debug("storage executor initialized")
return nil
}
func (d *driver) Name() string {
return s3fs.Name
}
// Supported returns a flag indicating whether or not the platform
// implementing the executor is valid for the host on which the executor
// resides.
func (d *driver) Supported(
ctx types.Context,
opts types.Store) (bool, error) {
return gotil.FileExistsInPath(d.cmd), nil
}
// InstanceID
func (d *driver) InstanceID(
ctx types.Context,
opts types.Store) (*types.InstanceID, error) {
return utils.InstanceID(ctx, d.config)
}
// NextDevice returns the next available device.
func (d *driver) NextDevice(
ctx types.Context,
opts types.Store) (string, error) {
return "", types.ErrNotImplemented
}
// Return list of local devices
func (d *driver) LocalDevices(
ctx types.Context,
opts *types.LocalDevicesOpts) (*types.LocalDevices, error) {
buckets, err := d.getMountedBuckets(ctx)
if err != nil {
return nil, err
}
return &types.LocalDevices{Driver: s3fs.Name, DeviceMap: buckets}, nil
}
func (d *driver) Mount(
ctx types.Context,
deviceName, mountPoint string,
opts *types.DeviceMountOpts) error {
if mp, ok := d.findMountPoint(ctx, deviceName); ok {
ctx.WithFields(log.Fields{
"bucket": deviceName,
"mountPoint": mp,
}).Debug("bucket is already mounted")
if mp == mountPoint {
// bucket is mounted to the required target => ok
return nil
}
// bucket is mounted to another target => error
return goof.WithFields(goof.Fields{
"bucket": deviceName,
"mountPointt": mp,
}, "bucket is already mounted")
}
return d.s3fsMount(ctx, deviceName, mountPoint, opts)
}
// Mounts get a list of mount points.
func (d *driver) Mounts(
ctx types.Context,
opts types.Store) ([]*types.MountInfo, error) {
ld, err := d.LocalDevices(ctx, &types.LocalDevicesOpts{Opts: opts})
if err != nil {
return nil, err
}
if len(ld.DeviceMap) == 0 {
return nil, nil
}
mounts := []*types.MountInfo{}
for k, v := range ld.DeviceMap {
mounts = append(mounts, &types.MountInfo{Source: k, MountPoint: v})
}
return mounts, nil
}
func (d *driver) s3fsMount(
ctx types.Context,
bucket, mountPoint string,
opts *types.DeviceMountOpts) error {
args := []string{bucket, mountPoint}
if len(d.opts) > 0 {
for _, o := range d.opts {
args = append(args, fmt.Sprintf("-o%s", o))
}
} else if d.szOpts != "" {
args = append(args, d.szOpts)
}
fields := map[string]interface{}{
"bucket": bucket,
"mountPoint": mountPoint,
"cmd": d.cmd,
"args": args,
"isAWSAuthEnvVars": false,
}
cmd := exec.Command(d.cmd, args...)
if ak := d.getAccessKey(); ak != "" {
if sk := d.getSecretKey(); sk != "" {
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("AWSACCESSKEYID=%s", ak))
cmd.Env = append(cmd.Env, fmt.Sprintf("AWSSECRETACCESSKEY=%s", sk))
fields["isAWSAuthEnvVars"] = true
}
}
ctx.WithFields(fields).Debug("attempting s3fs mount")
out, err := cmd.CombinedOutput()
if err != nil {
fields["output"] = string(out)
return goof.WithFieldsE(fields, "error mounting s3fs bucket", err)
}
return nil
}
func (d *driver) findMountPoint(
ctx types.Context,
bucket string) (string, bool) {
if buckets, err := d.getMountedBuckets(ctx); err == nil {
b, ok := buckets[bucket]
return b, ok
}
return "", false
}
func (d *driver) getMountedBuckets(
ctx types.Context) (map[string]string, error) {
return getMountedBuckets(ctx, path.Base(d.cmd))
}
func (d *driver) getAccessKey() string {
return d.config.GetString(s3fs.ConfigS3FSAccessKey)
}
func (d *driver) getSecretKey() string {
return d.config.GetString(s3fs.ConfigS3FSSecretKey)
}