2021-04-03 12:40:58 +02:00
|
|
|
package mounter
|
2018-07-21 13:35:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-04-05 15:07:16 +02:00
|
|
|
"path"
|
2021-07-16 16:12:57 +03:00
|
|
|
"strings"
|
2018-07-21 13:35:31 +02:00
|
|
|
|
|
|
|
"context"
|
|
|
|
|
2021-04-03 12:40:58 +02:00
|
|
|
"github.com/ctrox/csi-s3/pkg/s3"
|
2018-07-21 13:35:31 +02:00
|
|
|
goofysApi "github.com/kahing/goofys/api"
|
|
|
|
)
|
|
|
|
|
2018-07-22 22:08:48 +02:00
|
|
|
const (
|
|
|
|
goofysCmd = "goofys"
|
|
|
|
defaultRegion = "us-east-1"
|
|
|
|
)
|
2018-07-21 13:35:31 +02:00
|
|
|
|
|
|
|
// Implements Mounter
|
|
|
|
type goofysMounter struct {
|
2021-04-05 15:07:16 +02:00
|
|
|
meta *s3.FSMeta
|
2018-07-21 13:35:31 +02:00
|
|
|
endpoint string
|
|
|
|
region string
|
|
|
|
accessKeyID string
|
|
|
|
secretAccessKey string
|
|
|
|
}
|
|
|
|
|
2021-04-05 15:07:16 +02:00
|
|
|
func newGoofysMounter(meta *s3.FSMeta, cfg *s3.Config) (Mounter, error) {
|
2018-07-21 13:35:31 +02:00
|
|
|
region := cfg.Region
|
|
|
|
// if endpoint is set we need a default region
|
|
|
|
if region == "" && cfg.Endpoint != "" {
|
|
|
|
region = defaultRegion
|
|
|
|
}
|
|
|
|
return &goofysMounter{
|
2021-04-05 15:07:16 +02:00
|
|
|
meta: meta,
|
2018-07-21 13:35:31 +02:00
|
|
|
endpoint: cfg.Endpoint,
|
|
|
|
region: region,
|
|
|
|
accessKeyID: cfg.AccessKeyID,
|
|
|
|
secretAccessKey: cfg.SecretAccessKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 22:43:51 +02:00
|
|
|
func (goofys *goofysMounter) Stage(stageTarget string) error {
|
2018-07-21 13:35:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 22:43:51 +02:00
|
|
|
func (goofys *goofysMounter) Unstage(stageTarget string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (goofys *goofysMounter) Mount(source string, target string) error {
|
2021-07-16 16:12:57 +03:00
|
|
|
mountOptions := make(map[string]string)
|
|
|
|
for _, opt := range goofys.meta.MountOptions {
|
|
|
|
s := 0
|
|
|
|
if len(opt) >= 2 && opt[0] == '-' && opt[1] == byte('-') {
|
|
|
|
s = 2
|
|
|
|
}
|
|
|
|
p := strings.Index(opt, "=")
|
|
|
|
if p > 0 {
|
|
|
|
mountOptions[string(opt[s : p])] = string(opt[p : ])
|
|
|
|
} else {
|
|
|
|
mountOptions[string(opt[s : ])] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mountOptions["allow_other"] = ""
|
2018-07-21 13:35:31 +02:00
|
|
|
goofysCfg := &goofysApi.Config{
|
2018-07-26 22:43:51 +02:00
|
|
|
MountPoint: target,
|
2018-07-21 13:35:31 +02:00
|
|
|
Endpoint: goofys.endpoint,
|
|
|
|
Region: goofys.region,
|
|
|
|
DirMode: 0755,
|
|
|
|
FileMode: 0644,
|
2021-07-16 16:12:57 +03:00
|
|
|
MountOptions: mountOptions,
|
2018-07-21 13:35:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", goofys.accessKeyID)
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", goofys.secretAccessKey)
|
2021-04-05 15:07:16 +02:00
|
|
|
fullPath := fmt.Sprintf("%s:%s", goofys.meta.BucketName, path.Join(goofys.meta.Prefix, goofys.meta.FSPath))
|
2018-07-21 13:35:31 +02:00
|
|
|
|
2018-07-27 21:37:32 +02:00
|
|
|
_, _, err := goofysApi.Mount(context.Background(), fullPath, goofysCfg)
|
2018-07-21 13:35:31 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error mounting via goofys: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|