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.

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