command_remote_mount_buckets.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/filer"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/remote_storage"
  8. "github.com/seaweedfs/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 -s3.access_key=xxx -s3.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.gateway -filer=<filerHost>:<filerPort> -createBucketAt=cloud1
  29. `
  30. }
  31. func (c *commandRemoteMountBuckets) HasTag(CommandTag) bool {
  32. return false
  33. }
  34. func (c *commandRemoteMountBuckets) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  35. remoteMountBucketsCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  36. remote := remoteMountBucketsCommand.String("remote", "", "an already configured storage name")
  37. bucketPattern := remoteMountBucketsCommand.String("bucketPattern", "", "match existing bucket name with wildcard characters '*' and '?'")
  38. trimBucketSuffix := remoteMountBucketsCommand.Bool("trimBucketSuffix", true, "remote suffix auto generated by 'weed filer.remote.sync'")
  39. apply := remoteMountBucketsCommand.Bool("apply", false, "apply the mount for listed buckets")
  40. if err = remoteMountBucketsCommand.Parse(args); err != nil {
  41. return nil
  42. }
  43. infoAboutSimulationMode(writer, *apply, "-apply")
  44. if *remote == "" {
  45. _, err = listExistingRemoteStorageMounts(commandEnv, writer)
  46. return err
  47. }
  48. // find configuration for remote storage
  49. remoteConf, err := filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, *remote)
  50. if err != nil {
  51. return fmt.Errorf("find configuration for %s: %v", *remote, err)
  52. }
  53. // get storage client
  54. remoteStorageClient, err := remote_storage.GetRemoteStorage(remoteConf)
  55. if err != nil {
  56. return fmt.Errorf("get storage client for %s: %v", *remote, err)
  57. }
  58. buckets, err := remoteStorageClient.ListBuckets()
  59. if err != nil {
  60. return fmt.Errorf("list buckets on %s: %v", *remote, err)
  61. }
  62. fillerBucketsPath, err := readFilerBucketsPath(commandEnv)
  63. if err != nil {
  64. return fmt.Errorf("read filer buckets path: %v", err)
  65. }
  66. hasSuffixPattern, _ := regexp.Compile(".+-[0-9][0-9][0-9][0-9]")
  67. for _, bucket := range buckets {
  68. if *bucketPattern != "" {
  69. if matched, _ := filepath.Match(*bucketPattern, bucket.Name); !matched {
  70. continue
  71. }
  72. }
  73. fmt.Fprintf(writer, "bucket %s\n", bucket.Name)
  74. localBucketName := bucket.Name
  75. if *trimBucketSuffix {
  76. if hasSuffixPattern.MatchString(localBucketName) {
  77. localBucketName = localBucketName[:len(localBucketName)-5]
  78. fmt.Fprintf(writer, " mount bucket %s as %s\n", bucket.Name, localBucketName)
  79. }
  80. }
  81. if *apply {
  82. dir := util.FullPath(fillerBucketsPath).Child(localBucketName)
  83. remoteStorageLocation := &remote_pb.RemoteStorageLocation{
  84. Name: *remote,
  85. Bucket: bucket.Name,
  86. Path: "/",
  87. }
  88. // sync metadata from remote
  89. if err = syncMetadata(commandEnv, writer, string(dir), true, remoteConf, remoteStorageLocation); err != nil {
  90. return fmt.Errorf("pull metadata on %+v: %v", remoteStorageLocation, err)
  91. }
  92. // store a mount configuration in filer
  93. if err = filer.InsertMountMapping(commandEnv, string(dir), remoteStorageLocation); err != nil {
  94. return fmt.Errorf("save mount mapping %s to %+v: %v", dir, remoteStorageLocation, err)
  95. }
  96. }
  97. }
  98. return nil
  99. }