remote_mapping.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, 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. return nil, readErr
  17. }
  18. mappings, readErr = UnmarshalRemoteStorageMappings(oldContent)
  19. if readErr != nil {
  20. return nil, fmt.Errorf("unmarshal mappings: %v", readErr)
  21. }
  22. return
  23. }
  24. func InsertMountMapping(filerClient filer_pb.FilerClient, dir string, remoteStorageLocation *remote_pb.RemoteStorageLocation) (err error) {
  25. // read current mapping
  26. var oldContent, newContent []byte
  27. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  28. oldContent, err = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
  29. return err
  30. })
  31. if err != nil {
  32. if err != filer_pb.ErrNotFound {
  33. return fmt.Errorf("read existing mapping: %v", err)
  34. }
  35. }
  36. // add new mapping
  37. newContent, err = addRemoteStorageMapping(oldContent, dir, remoteStorageLocation)
  38. if err != nil {
  39. return fmt.Errorf("add mapping %s~%s: %v", dir, remoteStorageLocation, err)
  40. }
  41. // save back
  42. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  43. return SaveInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE, newContent)
  44. })
  45. if err != nil {
  46. return fmt.Errorf("save mapping: %v", err)
  47. }
  48. return nil
  49. }
  50. func DeleteMountMapping(filerClient filer_pb.FilerClient, dir string) (err error) {
  51. // read current mapping
  52. var oldContent, newContent []byte
  53. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  54. oldContent, err = ReadInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE)
  55. return err
  56. })
  57. if err != nil {
  58. if err != filer_pb.ErrNotFound {
  59. return fmt.Errorf("read existing mapping: %v", err)
  60. }
  61. }
  62. // add new mapping
  63. newContent, err = removeRemoteStorageMapping(oldContent, dir)
  64. if err != nil {
  65. return fmt.Errorf("delete mount %s: %v", dir, err)
  66. }
  67. // save back
  68. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  69. return SaveInsideFiler(client, DirectoryEtcRemote, REMOTE_STORAGE_MOUNT_FILE, newContent)
  70. })
  71. if err != nil {
  72. return fmt.Errorf("save mapping: %v", err)
  73. }
  74. return nil
  75. }
  76. func addRemoteStorageMapping(oldContent []byte, dir string, storageLocation *remote_pb.RemoteStorageLocation) (newContent []byte, err error) {
  77. mappings, unmarshalErr := UnmarshalRemoteStorageMappings(oldContent)
  78. if unmarshalErr != nil {
  79. // skip
  80. }
  81. // set the new mapping
  82. mappings.Mappings[dir] = storageLocation
  83. if newContent, err = proto.Marshal(mappings); err != nil {
  84. return oldContent, fmt.Errorf("marshal mappings: %v", err)
  85. }
  86. return
  87. }
  88. func removeRemoteStorageMapping(oldContent []byte, dir string) (newContent []byte, err error) {
  89. mappings, unmarshalErr := UnmarshalRemoteStorageMappings(oldContent)
  90. if unmarshalErr != nil {
  91. return nil, unmarshalErr
  92. }
  93. // set the new mapping
  94. delete(mappings.Mappings, dir)
  95. if newContent, err = proto.Marshal(mappings); err != nil {
  96. return oldContent, fmt.Errorf("marshal mappings: %v", err)
  97. }
  98. return
  99. }