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.

121 lines
3.6 KiB

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