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.

119 lines
4.6 KiB

3 years ago
  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. clientId int32
  29. }
  30. var _ = filer_pb.FilerClient(&RemoteGatewayOptions{})
  31. func (option *RemoteGatewayOptions) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
  32. return pb.WithFilerClient(streamingMode, pb.ServerAddress(*option.filerAddress), option.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  33. return fn(client)
  34. })
  35. }
  36. func (option *RemoteGatewayOptions) AdjustedUrl(location *filer_pb.Location) string {
  37. return location.Url
  38. }
  39. var (
  40. remoteGatewayOptions RemoteGatewayOptions
  41. )
  42. func init() {
  43. cmdFilerRemoteGateway.Run = runFilerRemoteGateway // break init cycle
  44. remoteGatewayOptions.filerAddress = cmdFilerRemoteGateway.Flag.String("filer", "localhost:8888", "filer of the SeaweedFS cluster")
  45. remoteGatewayOptions.createBucketAt = cmdFilerRemoteGateway.Flag.String("createBucketAt", "", "one remote storage name to create new buckets in")
  46. remoteGatewayOptions.createBucketRandomSuffix = cmdFilerRemoteGateway.Flag.Bool("createBucketWithRandomSuffix", true, "add randomized suffix to bucket name to avoid conflicts")
  47. remoteGatewayOptions.readChunkFromFiler = cmdFilerRemoteGateway.Flag.Bool("filerProxy", false, "read file chunks from filer instead of volume servers")
  48. 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\"")
  49. remoteGatewayOptions.include = cmdFilerRemoteGateway.Flag.String("include", "", "pattens of new bucket names, e.g., s3*")
  50. remoteGatewayOptions.exclude = cmdFilerRemoteGateway.Flag.String("exclude", "", "pattens of new bucket names, e.g., local*")
  51. remoteGatewayOptions.clientId = util.RandomInt32()
  52. }
  53. var cmdFilerRemoteGateway = &Command{
  54. UsageLine: "filer.remote.gateway",
  55. Short: "resumable continuously write back bucket creation, deletion, and other local updates to remote object store",
  56. Long: `resumable continuously write back bucket creation, deletion, and other local updates to remote object store
  57. filer.remote.gateway listens on filer local buckets update events.
  58. If any bucket is created, deleted, or updated, it will mirror the changes to remote object store.
  59. weed filer.remote.sync -createBucketAt=cloud1
  60. `,
  61. }
  62. func runFilerRemoteGateway(cmd *Command, args []string) bool {
  63. util.LoadConfiguration("security", false)
  64. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  65. remoteGatewayOptions.grpcDialOption = grpcDialOption
  66. filerAddress := pb.ServerAddress(*remoteGatewayOptions.filerAddress)
  67. filerSource := &source.FilerSource{}
  68. filerSource.DoInitialize(
  69. filerAddress.ToHttpAddress(),
  70. filerAddress.ToGrpcAddress(),
  71. "/", // does not matter
  72. *remoteGatewayOptions.readChunkFromFiler,
  73. )
  74. remoteGatewayOptions.bucketsDir = "/buckets"
  75. // check buckets again
  76. remoteGatewayOptions.WithFilerClient(false, func(filerClient filer_pb.SeaweedFilerClient) error {
  77. resp, err := filerClient.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  78. if err != nil {
  79. return err
  80. }
  81. remoteGatewayOptions.bucketsDir = resp.DirBuckets
  82. return nil
  83. })
  84. // read filer remote storage mount mappings
  85. if detectErr := remoteGatewayOptions.collectRemoteStorageConf(); detectErr != nil {
  86. fmt.Fprintf(os.Stderr, "read mount info: %v\n", detectErr)
  87. return true
  88. }
  89. // synchronize /buckets folder
  90. fmt.Printf("synchronize buckets in %s ...\n", remoteGatewayOptions.bucketsDir)
  91. util.RetryForever("filer.remote.sync buckets", func() error {
  92. return remoteGatewayOptions.followBucketUpdatesAndUploadToRemote(filerSource)
  93. }, func(err error) bool {
  94. if err != nil {
  95. glog.Errorf("synchronize %s: %v", remoteGatewayOptions.bucketsDir, err)
  96. }
  97. return true
  98. })
  99. return true
  100. }