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.

117 lines
4.4 KiB

  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  9. "github.com/chrislusf/seaweedfs/weed/replication/source"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "google.golang.org/grpc"
  13. "os"
  14. "time"
  15. )
  16. type RemoteGatewayOptions struct {
  17. filerAddress *string
  18. grpcDialOption grpc.DialOption
  19. readChunkFromFiler *bool
  20. timeAgo *time.Duration
  21. createBucketAt *string
  22. createBucketRandomSuffix *bool
  23. include *string
  24. exclude *string
  25. mappings *remote_pb.RemoteStorageMapping
  26. remoteConfs map[string]*remote_pb.RemoteConf
  27. bucketsDir string
  28. }
  29. var _ = filer_pb.FilerClient(&RemoteGatewayOptions{})
  30. func (option *RemoteGatewayOptions) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  31. return pb.WithFilerClient(pb.ServerAddress(*option.filerAddress), option.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  32. return fn(client)
  33. })
  34. }
  35. func (option *RemoteGatewayOptions) AdjustedUrl(location *filer_pb.Location) string {
  36. return location.Url
  37. }
  38. var (
  39. remoteGatewayOptions RemoteGatewayOptions
  40. )
  41. func init() {
  42. cmdFilerRemoteGateway.Run = runFilerRemoteGateway // break init cycle
  43. remoteGatewayOptions.filerAddress = cmdFilerRemoteGateway.Flag.String("filer", "localhost:8888", "filer of the SeaweedFS cluster")
  44. remoteGatewayOptions.createBucketAt = cmdFilerRemoteGateway.Flag.String("createBucketAt", "", "one remote storage name to create new buckets in")
  45. remoteGatewayOptions.createBucketRandomSuffix = cmdFilerRemoteGateway.Flag.Bool("createBucketWithRandomSuffix", true, "add randomized suffix to bucket name to avoid conflicts")
  46. remoteGatewayOptions.readChunkFromFiler = cmdFilerRemoteGateway.Flag.Bool("filerProxy", false, "read file chunks from filer instead of volume servers")
  47. remoteGatewayOptions.timeAgo = cmdFilerRemoteGateway.Flag.Duration("timeAgo", 0, "start time before now. \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
  48. remoteGatewayOptions.include = cmdFilerRemoteGateway.Flag.String("include", "", "pattens of new bucket names, e.g., s3*")
  49. remoteGatewayOptions.exclude = cmdFilerRemoteGateway.Flag.String("exclude", "", "pattens of new bucket names, e.g., local*")
  50. }
  51. var cmdFilerRemoteGateway = &Command{
  52. UsageLine: "filer.remote.gateway",
  53. Short: "resumable continuously write back bucket creation, deletion, and other local updates to remote object store",
  54. Long: `resumable continuously write back bucket creation, deletion, and other local updates to remote object store
  55. filer.remote.gateway listens on filer local buckets update events.
  56. If any bucket is created, deleted, or updated, it will mirror the changes to remote object store.
  57. weed filer.remote.sync -createBucketAt=cloud1
  58. `,
  59. }
  60. func runFilerRemoteGateway(cmd *Command, args []string) bool {
  61. util.LoadConfiguration("security", false)
  62. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  63. remoteGatewayOptions.grpcDialOption = grpcDialOption
  64. filerAddress := pb.ServerAddress(*remoteGatewayOptions.filerAddress)
  65. filerSource := &source.FilerSource{}
  66. filerSource.DoInitialize(
  67. filerAddress.ToHttpAddress(),
  68. filerAddress.ToGrpcAddress(),
  69. "/", // does not matter
  70. *remoteGatewayOptions.readChunkFromFiler,
  71. )
  72. remoteGatewayOptions.bucketsDir = "/buckets"
  73. // check buckets again
  74. remoteGatewayOptions.WithFilerClient(func(filerClient filer_pb.SeaweedFilerClient) error {
  75. resp, err := filerClient.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  76. if err != nil {
  77. return err
  78. }
  79. remoteGatewayOptions.bucketsDir = resp.DirBuckets
  80. return nil
  81. })
  82. // read filer remote storage mount mappings
  83. if detectErr := remoteGatewayOptions.collectRemoteStorageConf(); detectErr != nil {
  84. fmt.Fprintf(os.Stderr, "read mount info: %v\n", detectErr)
  85. return true
  86. }
  87. // synchronize /buckets folder
  88. fmt.Printf("synchronize buckets in %s ...\n", remoteGatewayOptions.bucketsDir)
  89. util.RetryForever("filer.remote.sync buckets", func() error {
  90. return remoteGatewayOptions.followBucketUpdatesAndUploadToRemote(filerSource)
  91. }, func(err error) bool {
  92. if err != nil {
  93. glog.Errorf("synchronize %s: %v", remoteGatewayOptions.bucketsDir, err)
  94. }
  95. return true
  96. })
  97. return true
  98. }