2021-04-03 10:40:58 +00:00
|
|
|
package mounter
|
2018-07-21 11:35:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-04-05 13:07:16 +00:00
|
|
|
"path"
|
2021-04-03 10:40:58 +00:00
|
|
|
|
|
|
|
"github.com/ctrox/csi-s3/pkg/s3"
|
2018-07-21 11:35:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Implements Mounter
|
|
|
|
type s3fsMounter struct {
|
2021-04-05 13:07:16 +00:00
|
|
|
meta *s3.FSMeta
|
2018-07-21 11:35:31 +00:00
|
|
|
url string
|
|
|
|
region string
|
|
|
|
pwFileContent string
|
|
|
|
}
|
|
|
|
|
2018-07-22 20:08:48 +00:00
|
|
|
const (
|
|
|
|
s3fsCmd = "s3fs"
|
|
|
|
)
|
|
|
|
|
2021-04-05 13:07:16 +00:00
|
|
|
func newS3fsMounter(meta *s3.FSMeta, cfg *s3.Config) (Mounter, error) {
|
2018-07-21 11:35:31 +00:00
|
|
|
return &s3fsMounter{
|
2021-04-05 13:07:16 +00:00
|
|
|
meta: meta,
|
2018-07-21 11:35:31 +00:00
|
|
|
url: cfg.Endpoint,
|
|
|
|
region: cfg.Region,
|
|
|
|
pwFileContent: cfg.AccessKeyID + ":" + cfg.SecretAccessKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 20:43:51 +00:00
|
|
|
func (s3fs *s3fsMounter) Stage(stageTarget string) error {
|
2018-07-21 11:35:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 20:43:51 +00:00
|
|
|
func (s3fs *s3fsMounter) Unstage(stageTarget string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3fs *s3fsMounter) Mount(source string, target string) error {
|
2018-07-21 11:35:31 +00:00
|
|
|
if err := writes3fsPass(s3fs.pwFileContent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args := []string{
|
2021-04-05 13:07:16 +00:00
|
|
|
fmt.Sprintf("%s:/%s", s3fs.meta.BucketName, path.Join(s3fs.meta.Prefix, s3fs.meta.FSPath)),
|
|
|
|
target,
|
2018-07-21 11:35:31 +00:00
|
|
|
"-o", "use_path_request_style",
|
|
|
|
"-o", fmt.Sprintf("url=%s", s3fs.url),
|
|
|
|
"-o", fmt.Sprintf("endpoint=%s", s3fs.region),
|
|
|
|
"-o", "allow_other",
|
|
|
|
"-o", "mp_umask=000",
|
|
|
|
}
|
2018-07-26 20:43:51 +00:00
|
|
|
return fuseMount(target, s3fsCmd, args)
|
2018-07-21 11:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writes3fsPass(pwFileContent string) error {
|
|
|
|
pwFileName := fmt.Sprintf("%s/.passwd-s3fs", os.Getenv("HOME"))
|
|
|
|
pwFile, err := os.OpenFile(pwFileName, os.O_RDWR|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = pwFile.WriteString(pwFileContent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pwFile.Close()
|
|
|
|
return nil
|
|
|
|
}
|