You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.2 KiB

  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  7. "github.com/chrislusf/seaweedfs/weed/remote_storage"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "io"
  10. "path/filepath"
  11. )
  12. func init() {
  13. Commands = append(Commands, &commandRemoteMountBuckets{})
  14. }
  15. type commandRemoteMountBuckets struct {
  16. }
  17. func (c *commandRemoteMountBuckets) Name() string {
  18. return "remote.mount.buckets"
  19. }
  20. func (c *commandRemoteMountBuckets) Help() string {
  21. return `mount all buckets in remote storage and pull its metadata
  22. # assume a remote storage is configured to name "cloud1"
  23. remote.configure -name=cloud1 -type=s3 -access_key=xxx -secret_key=yyy
  24. # mount all buckets
  25. remote.mount.buckets -remote=cloud1
  26. # after mount, start a separate process to write updates to remote storage
  27. weed filer.remote.sync -filer=<filerHost>:<filerPort> -createBucketAt=cloud1
  28. `
  29. }
  30. func (c *commandRemoteMountBuckets) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  31. remoteMountBucketsCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  32. remote := remoteMountBucketsCommand.String("remote", "", "a already configured storage name")
  33. bucketPattern := remoteMountBucketsCommand.String("bucketPattern", "", "match existing bucket name with wildcard characters '*' and '?'")
  34. apply := remoteMountBucketsCommand.Bool("apply", false, "apply the mount for listed buckets")
  35. if err = remoteMountBucketsCommand.Parse(args); err != nil {
  36. return nil
  37. }
  38. if *remote == "" {
  39. _, err = listExistingRemoteStorageMounts(commandEnv, writer)
  40. return err
  41. }
  42. // find configuration for remote storage
  43. remoteConf, err := filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, *remote)
  44. if err != nil {
  45. return fmt.Errorf("find configuration for %s: %v", *remote, err)
  46. }
  47. // get storage client
  48. remoteStorageClient, err := remote_storage.GetRemoteStorage(remoteConf)
  49. if err != nil {
  50. return fmt.Errorf("get storage client for %s: %v", *remote, err)
  51. }
  52. buckets, err := remoteStorageClient.ListBuckets()
  53. if err != nil {
  54. return fmt.Errorf("list buckets on %s: %v", *remote, err)
  55. }
  56. fillerBucketsPath, err := readFilerBucketsPath(commandEnv)
  57. if err != nil {
  58. return fmt.Errorf("read filer buckets path: %v", err)
  59. }
  60. for _, bucket := range buckets {
  61. if *bucketPattern != "" {
  62. if matched, _ := filepath.Match(*bucketPattern, bucket.Name); !matched {
  63. continue
  64. }
  65. }
  66. fmt.Fprintf(writer, "bucket %s\n", bucket.Name)
  67. if *apply {
  68. dir := util.FullPath(fillerBucketsPath).Child(bucket.Name)
  69. remoteStorageLocation := &remote_pb.RemoteStorageLocation{
  70. Name: *remote,
  71. Bucket: bucket.Name,
  72. Path: "/",
  73. }
  74. // sync metadata from remote
  75. if err = syncMetadata(commandEnv, writer, string(dir), true, remoteConf, remoteStorageLocation); err != nil {
  76. return fmt.Errorf("pull metadata on %+v: %v", remoteStorageLocation, err)
  77. }
  78. // store a mount configuration in filer
  79. if err = filer.InsertMountMapping(commandEnv, string(dir), remoteStorageLocation); err != nil {
  80. return fmt.Errorf("save mount mapping %s to %+v: %v", dir, remoteStorageLocation, err)
  81. }
  82. }
  83. }
  84. return nil
  85. }