2021-07-27 05:53:44 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/remote_storage"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2022-08-17 19:05:07 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-07-27 05:53:44 +00:00
|
|
|
"io"
|
2021-08-31 08:07:19 +00:00
|
|
|
"os"
|
2021-08-26 00:34:29 +00:00
|
|
|
"strings"
|
2021-08-31 08:07:19 +00:00
|
|
|
"time"
|
2021-07-27 05:53:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Commands = append(Commands, &commandRemoteMount{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type commandRemoteMount struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandRemoteMount) Name() string {
|
|
|
|
return "remote.mount"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandRemoteMount) Help() string {
|
|
|
|
return `mount remote storage and pull its metadata
|
|
|
|
|
2021-08-10 09:48:41 +00:00
|
|
|
# assume a remote storage is configured to name "cloud1"
|
2021-11-07 20:38:35 +00:00
|
|
|
remote.configure -name=cloud1 -type=s3 -s3.access_key=xxx -s3.secret_key=yyy
|
2021-07-27 05:53:44 +00:00
|
|
|
|
|
|
|
# mount and pull one bucket
|
2021-08-14 22:55:53 +00:00
|
|
|
remote.mount -dir=/xxx -remote=cloud1/bucket
|
2021-07-27 05:53:44 +00:00
|
|
|
# mount and pull one directory in the bucket
|
2021-08-14 22:55:53 +00:00
|
|
|
remote.mount -dir=/xxx -remote=cloud1/bucket/dir1
|
2021-07-27 05:53:44 +00:00
|
|
|
|
2021-08-09 05:30:12 +00:00
|
|
|
# after mount, start a separate process to write updates to remote storage
|
2021-08-14 22:55:53 +00:00
|
|
|
weed filer.remote.sync -filer=<filerHost>:<filerPort> -dir=/xxx
|
2021-08-09 05:30:12 +00:00
|
|
|
|
2021-07-27 05:53:44 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandRemoteMount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
|
|
|
|
|
|
|
remoteMountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
|
|
|
|
dir := remoteMountCommand.String("dir", "", "a directory in filer")
|
|
|
|
nonEmpty := remoteMountCommand.Bool("nonempty", false, "allows the mounting over a non-empty directory")
|
|
|
|
remote := remoteMountCommand.String("remote", "", "a directory in remote storage, ex. <storageName>/<bucket>/path/to/dir")
|
|
|
|
|
|
|
|
if err = remoteMountCommand.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-01 05:39:38 +00:00
|
|
|
if *dir == "" {
|
2021-08-09 05:26:37 +00:00
|
|
|
_, err = listExistingRemoteStorageMounts(commandEnv, writer)
|
|
|
|
return err
|
2021-08-01 05:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 05:53:44 +00:00
|
|
|
// find configuration for remote storage
|
2021-08-30 01:41:29 +00:00
|
|
|
remoteConf, err := filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, remote_storage.ParseLocationName(*remote))
|
2021-07-27 05:53:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("find configuration for %s: %v", *remote, err)
|
|
|
|
}
|
|
|
|
|
2021-08-30 01:41:29 +00:00
|
|
|
remoteStorageLocation, err := remote_storage.ParseRemoteLocation(remoteConf.Type, *remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-15 08:53:46 +00:00
|
|
|
// sync metadata from remote
|
2021-09-05 04:37:25 +00:00
|
|
|
if err = syncMetadata(commandEnv, writer, *dir, *nonEmpty, remoteConf, remoteStorageLocation); err != nil {
|
2021-07-27 05:53:44 +00:00
|
|
|
return fmt.Errorf("pull metadata: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// store a mount configuration in filer
|
2021-09-04 12:11:06 +00:00
|
|
|
if err = filer.InsertMountMapping(commandEnv, *dir, remoteStorageLocation); err != nil {
|
2021-07-27 10:26:35 +00:00
|
|
|
return fmt.Errorf("save mount mapping: %v", err)
|
|
|
|
}
|
2021-07-27 05:53:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-26 22:18:34 +00:00
|
|
|
func listExistingRemoteStorageMounts(commandEnv *CommandEnv, writer io.Writer) (mappings *remote_pb.RemoteStorageMapping, err error) {
|
2021-08-01 05:39:38 +00:00
|
|
|
|
|
|
|
// read current mapping
|
2021-08-09 05:26:37 +00:00
|
|
|
mappings, err = filer.ReadMountMappings(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress)
|
|
|
|
if err != nil {
|
|
|
|
return mappings, err
|
2021-08-01 05:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 05:26:37 +00:00
|
|
|
jsonPrintln(writer, mappings)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonPrintln(writer io.Writer, message proto.Message) error {
|
2022-08-17 19:42:03 +00:00
|
|
|
return filer.ProtoToText(writer, message)
|
2021-08-01 05:39:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 04:37:25 +00:00
|
|
|
func syncMetadata(commandEnv *CommandEnv, writer io.Writer, dir string, nonEmpty bool, remoteConf *remote_pb.RemoteConf, remote *remote_pb.RemoteStorageLocation) error {
|
2021-07-27 05:53:44 +00:00
|
|
|
|
|
|
|
// find existing directory, and ensure the directory is empty
|
2021-12-26 08:15:03 +00:00
|
|
|
err := commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2021-07-27 05:53:44 +00:00
|
|
|
parent, name := util.FullPath(dir).DirAndName()
|
2021-08-04 07:31:06 +00:00
|
|
|
_, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
|
2021-07-27 05:53:44 +00:00
|
|
|
Directory: parent,
|
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
if lookupErr != nil {
|
2021-09-05 18:55:52 +00:00
|
|
|
if strings.Contains(lookupErr.Error(), filer_pb.ErrNotFound.Error()) {
|
2021-08-31 08:07:19 +00:00
|
|
|
_, createErr := client.CreateEntry(context.Background(), &filer_pb.CreateEntryRequest{
|
|
|
|
Directory: parent,
|
|
|
|
Entry: &filer_pb.Entry{
|
|
|
|
Name: name,
|
|
|
|
IsDirectory: true,
|
|
|
|
Attributes: &filer_pb.FuseAttributes{
|
|
|
|
Mtime: time.Now().Unix(),
|
|
|
|
Crtime: time.Now().Unix(),
|
|
|
|
FileMode: uint32(0644 | os.ModeDir),
|
|
|
|
},
|
2021-09-05 18:55:52 +00:00
|
|
|
RemoteEntry: &filer_pb.RemoteEntry{
|
|
|
|
StorageName: remoteConf.Name,
|
|
|
|
},
|
2021-08-31 08:07:19 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return createErr
|
2021-08-26 00:34:29 +00:00
|
|
|
}
|
2021-07-27 05:53:44 +00:00
|
|
|
}
|
|
|
|
|
2021-07-27 10:26:35 +00:00
|
|
|
mountToDirIsEmpty := true
|
2021-07-27 05:53:44 +00:00
|
|
|
listErr := filer_pb.SeaweedList(client, dir, "", func(entry *filer_pb.Entry, isLast bool) error {
|
|
|
|
mountToDirIsEmpty = false
|
|
|
|
return nil
|
|
|
|
}, "", false, 1)
|
|
|
|
|
|
|
|
if listErr != nil {
|
|
|
|
return fmt.Errorf("list %s: %v", dir, listErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mountToDirIsEmpty {
|
|
|
|
if !nonEmpty {
|
|
|
|
return fmt.Errorf("dir %s is not empty", dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-15 08:53:46 +00:00
|
|
|
// pull metadata from remote
|
|
|
|
if err = pullMetadata(commandEnv, writer, util.FullPath(dir), remote, util.FullPath(dir), remoteConf); err != nil {
|
2021-08-31 08:07:34 +00:00
|
|
|
return fmt.Errorf("cache metadata: %v", err)
|
2021-07-27 05:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-27 10:26:35 +00:00
|
|
|
|
2021-08-15 04:46:34 +00:00
|
|
|
// if an entry has synchronized metadata but has not synchronized content
|
2022-09-15 06:06:44 +00:00
|
|
|
//
|
|
|
|
// entry.Attributes.FileSize == entry.RemoteEntry.RemoteSize
|
|
|
|
// entry.Attributes.Mtime == entry.RemoteEntry.RemoteMtime
|
|
|
|
// entry.RemoteEntry.LastLocalSyncTsNs == 0
|
|
|
|
//
|
2021-08-15 04:46:34 +00:00
|
|
|
// if an entry has synchronized metadata but has synchronized content before
|
2022-09-15 06:06:44 +00:00
|
|
|
//
|
|
|
|
// entry.Attributes.FileSize == entry.RemoteEntry.RemoteSize
|
|
|
|
// entry.Attributes.Mtime == entry.RemoteEntry.RemoteMtime
|
|
|
|
// entry.RemoteEntry.LastLocalSyncTsNs > 0
|
|
|
|
//
|
2021-08-15 04:46:34 +00:00
|
|
|
// if an entry has synchronized metadata but has new updates
|
2022-09-15 06:06:44 +00:00
|
|
|
//
|
|
|
|
// entry.Attributes.Mtime * 1,000,000,000 > entry.RemoteEntry.LastLocalSyncTsNs
|
2021-08-09 21:35:18 +00:00
|
|
|
func doSaveRemoteEntry(client filer_pb.SeaweedFilerClient, localDir string, existingEntry *filer_pb.Entry, remoteEntry *filer_pb.RemoteEntry) error {
|
|
|
|
existingEntry.RemoteEntry = remoteEntry
|
|
|
|
existingEntry.Attributes.FileSize = uint64(remoteEntry.RemoteSize)
|
|
|
|
existingEntry.Attributes.Mtime = remoteEntry.RemoteMtime
|
2023-04-23 05:32:21 +00:00
|
|
|
existingEntry.Attributes.Md5 = nil
|
|
|
|
existingEntry.Chunks = nil
|
|
|
|
existingEntry.Content = nil
|
2021-08-09 21:35:18 +00:00
|
|
|
_, updateErr := client.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
|
|
|
|
Directory: localDir,
|
|
|
|
Entry: existingEntry,
|
|
|
|
})
|
|
|
|
if updateErr != nil {
|
|
|
|
return updateErr
|
|
|
|
}
|
|
|
|
return nil
|
2021-08-09 21:37:34 +00:00
|
|
|
}
|