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.

123 lines
3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package filer
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/pb"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
  7. "google.golang.org/grpc"
  8. "google.golang.org/protobuf/proto"
  9. )
  10. func ReadMountMappings(grpcDialOption grpc.DialOption, filerAddress pb.ServerAddress) (mappings *remote_pb.RemoteStorageMapping, readErr error) {
  11. var oldContent []byte
  12. if readErr = pb.WithFilerClient(false, 0, filerAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  13. oldContent, readErr = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
  14. return readErr
  15. }); readErr != nil {
  16. if readErr != filer_pb.ErrNotFound {
  17. return nil, fmt.Errorf("read existing mapping: %v", readErr)
  18. }
  19. oldContent = nil
  20. }
  21. mappings, readErr = UnmarshalRemoteStorageMappings(oldContent)
  22. if readErr != nil {
  23. return nil, fmt.Errorf("unmarshal mappings: %v", readErr)
  24. }
  25. return
  26. }
  27. func InsertMountMapping(filerClient filer_pb.FilerClient, dir string, remoteStorageLocation *remote_pb.RemoteStorageLocation) (err error) {
  28. // read current mapping
  29. var oldContent, newContent []byte
  30. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  31. oldContent, err = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
  32. return err
  33. })
  34. if err != nil {
  35. if err != filer_pb.ErrNotFound {
  36. return fmt.Errorf("read existing mapping: %v", err)
  37. }
  38. }
  39. // add new mapping
  40. newContent, err = addRemoteStorageMapping(oldContent, dir, remoteStorageLocation)
  41. if err != nil {
  42. return fmt.Errorf("add mapping %s~%s: %v", dir, remoteStorageLocation, err)
  43. }
  44. // save back
  45. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  46. return SaveInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE, newContent)
  47. })
  48. if err != nil {
  49. return fmt.Errorf("save mapping: %v", err)
  50. }
  51. return nil
  52. }
  53. func DeleteMountMapping(filerClient filer_pb.FilerClient, dir string) (err error) {
  54. // read current mapping
  55. var oldContent, newContent []byte
  56. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  57. oldContent, err = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
  58. return err
  59. })
  60. if err != nil {
  61. if err != filer_pb.ErrNotFound {
  62. return fmt.Errorf("read existing mapping: %v", err)
  63. }
  64. }
  65. // add new mapping
  66. newContent, err = removeRemoteStorageMapping(oldContent, dir)
  67. if err != nil {
  68. return fmt.Errorf("delete mount %s: %v", dir, err)
  69. }
  70. // save back
  71. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  72. return SaveInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE, newContent)
  73. })
  74. if err != nil {
  75. return fmt.Errorf("save mapping: %v", err)
  76. }
  77. return nil
  78. }
  79. func addRemoteStorageMapping(oldContent []byte, dir string, storageLocation *remote_pb.RemoteStorageLocation) (newContent []byte, err error) {
  80. mappings, unmarshalErr := UnmarshalRemoteStorageMappings(oldContent)
  81. if unmarshalErr != nil {
  82. // skip
  83. }
  84. // set the new mapping
  85. mappings.Mappings[dir] = storageLocation
  86. if newContent, err = proto.Marshal(mappings); err != nil {
  87. return oldContent, fmt.Errorf("marshal mappings: %v", err)
  88. }
  89. return
  90. }
  91. func removeRemoteStorageMapping(oldContent []byte, dir string) (newContent []byte, err error) {
  92. mappings, unmarshalErr := UnmarshalRemoteStorageMappings(oldContent)
  93. if unmarshalErr != nil {
  94. return nil, unmarshalErr
  95. }
  96. // set the new mapping
  97. delete(mappings.Mappings, dir)
  98. if newContent, err = proto.Marshal(mappings); err != nil {
  99. return oldContent, fmt.Errorf("marshal mappings: %v", err)
  100. }
  101. return
  102. }