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.

147 lines
4.0 KiB

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "io"
  11. )
  12. func init() {
  13. Commands = append(Commands, &commandRemoteUnmount{})
  14. }
  15. type commandRemoteUnmount struct {
  16. }
  17. func (c *commandRemoteUnmount) Name() string {
  18. return "remote.unmount"
  19. }
  20. func (c *commandRemoteUnmount) Help() string {
  21. return `unmount remote storage
  22. # assume a remote storage is configured to name "s3_1"
  23. remote.configure -name=s3_1 -type=s3 -access_key=xxx -secret_key=yyy
  24. # mount and pull one bucket
  25. remote.mount -dir=/xxx -remote=s3_1/bucket
  26. # unmount the mounted directory and remove its cache
  27. remote.unmount -dir=/xxx
  28. `
  29. }
  30. func (c *commandRemoteUnmount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  31. remoteMountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  32. dir := remoteMountCommand.String("dir", "", "a directory in filer")
  33. if err = remoteMountCommand.Parse(args); err != nil {
  34. return nil
  35. }
  36. mappings, listErr := filer.ReadMountMappings(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress)
  37. if listErr != nil {
  38. return listErr
  39. }
  40. if *dir == "" {
  41. return jsonPrintln(writer, mappings)
  42. }
  43. _, found := mappings.Mappings[*dir]
  44. if !found {
  45. return fmt.Errorf("directory %s is not mounted", *dir)
  46. }
  47. // purge mounted data
  48. if err = c.purgeMountedData(commandEnv, *dir); err != nil {
  49. return fmt.Errorf("purge mounted data: %v", err)
  50. }
  51. // store a mount configuration in filer
  52. if err = c.deleteMountMapping(commandEnv, *dir); err != nil {
  53. return fmt.Errorf("delete mount mapping: %v", err)
  54. }
  55. return nil
  56. }
  57. func (c *commandRemoteUnmount) findRemoteStorageConfiguration(commandEnv *CommandEnv, writer io.Writer, remote *remote_pb.RemoteStorageLocation) (conf *remote_pb.RemoteConf, err error) {
  58. return filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, remote.Name)
  59. }
  60. func (c *commandRemoteUnmount) purgeMountedData(commandEnv *CommandEnv, dir string) error {
  61. // find existing directory, and ensure the directory is empty
  62. err := commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  63. parent, name := util.FullPath(dir).DirAndName()
  64. lookupResp, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  65. Directory: parent,
  66. Name: name,
  67. })
  68. if lookupErr != nil {
  69. return fmt.Errorf("lookup %s: %v", dir, lookupErr)
  70. }
  71. oldEntry := lookupResp.Entry
  72. deleteError := filer_pb.DoRemove(client, parent, name, true, true, true, false, nil)
  73. if deleteError != nil {
  74. return fmt.Errorf("delete %s: %v", dir, deleteError)
  75. }
  76. mkdirErr := filer_pb.DoMkdir(client, parent, name, func(entry *filer_pb.Entry) {
  77. entry.Attributes = oldEntry.Attributes
  78. entry.Extended = oldEntry.Extended
  79. })
  80. if mkdirErr != nil {
  81. return fmt.Errorf("mkdir %s: %v", dir, mkdirErr)
  82. }
  83. return nil
  84. })
  85. if err != nil {
  86. return err
  87. }
  88. return nil
  89. }
  90. func (c *commandRemoteUnmount) deleteMountMapping(commandEnv *CommandEnv, dir string) (err error) {
  91. // read current mapping
  92. var oldContent, newContent []byte
  93. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  94. oldContent, err = filer.ReadInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE)
  95. return err
  96. })
  97. if err != nil {
  98. if err != filer_pb.ErrNotFound {
  99. return fmt.Errorf("read existing mapping: %v", err)
  100. }
  101. }
  102. // add new mapping
  103. newContent, err = filer.RemoveRemoteStorageMapping(oldContent, dir)
  104. if err != nil {
  105. return fmt.Errorf("delete mount %s: %v", dir, err)
  106. }
  107. // save back
  108. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  109. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE, newContent)
  110. })
  111. if err != nil {
  112. return fmt.Errorf("save mapping: %v", err)
  113. }
  114. return nil
  115. }