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.7 KiB

3 years ago
  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) 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. infoAboutSimulationMode(writer, *apply, "-apply")
  41. if *remote == "" {
  42. _, err = listExistingRemoteStorageMounts(commandEnv, writer)
  43. return err
  44. }
  45. // find configuration for remote storage
  46. remoteConf, err := filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, *remote)
  47. if err != nil {
  48. return fmt.Errorf("find configuration for %s: %v", *remote, err)
  49. }
  50. // get storage client
  51. remoteStorageClient, err := remote_storage.GetRemoteStorage(remoteConf)
  52. if err != nil {
  53. return fmt.Errorf("get storage client for %s: %v", *remote, err)
  54. }
  55. buckets, err := remoteStorageClient.ListBuckets()
  56. if err != nil {
  57. return fmt.Errorf("list buckets on %s: %v", *remote, err)
  58. }
  59. fillerBucketsPath, err := readFilerBucketsPath(commandEnv)
  60. if err != nil {
  61. return fmt.Errorf("read filer buckets path: %v", err)
  62. }
  63. hasSuffixPattern, _ := regexp.Compile(".+-[0-9][0-9][0-9][0-9]")
  64. for _, bucket := range buckets {
  65. if *bucketPattern != "" {
  66. if matched, _ := filepath.Match(*bucketPattern, bucket.Name); !matched {
  67. continue
  68. }
  69. }
  70. fmt.Fprintf(writer, "bucket %s\n", bucket.Name)
  71. localBucketName := bucket.Name
  72. if *trimBucketSuffix {
  73. if hasSuffixPattern.MatchString(localBucketName) {
  74. localBucketName = localBucketName[:len(localBucketName)-5]
  75. fmt.Fprintf(writer, " mount bucket %s as %s\n", bucket.Name, localBucketName)
  76. }
  77. }
  78. if *apply {
  79. dir := util.FullPath(fillerBucketsPath).Child(localBucketName)
  80. remoteStorageLocation := &remote_pb.RemoteStorageLocation{
  81. Name: *remote,
  82. Bucket: bucket.Name,
  83. Path: "/",
  84. }
  85. // sync metadata from remote
  86. if err = syncMetadata(commandEnv, writer, string(dir), true, remoteConf, remoteStorageLocation); err != nil {
  87. return fmt.Errorf("pull metadata on %+v: %v", remoteStorageLocation, err)
  88. }
  89. // store a mount configuration in filer
  90. if err = filer.InsertMountMapping(commandEnv, string(dir), remoteStorageLocation); err != nil {
  91. return fmt.Errorf("save mount mapping %s to %+v: %v", dir, remoteStorageLocation, err)
  92. }
  93. }
  94. }
  95. return nil
  96. }