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.

120 lines
3.2 KiB

3 years ago
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/remote_storage"
  9. "github.com/seaweedfs/seaweedfs/weed/util"
  10. "io"
  11. "time"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandRemoteUnmount{})
  15. }
  16. type commandRemoteUnmount struct {
  17. }
  18. func (c *commandRemoteUnmount) Name() string {
  19. return "remote.unmount"
  20. }
  21. func (c *commandRemoteUnmount) Help() string {
  22. return `unmount remote storage
  23. # assume a remote storage is configured to name "s3_1"
  24. remote.configure -name=s3_1 -type=s3 -s3.access_key=xxx -s3.secret_key=yyy
  25. # mount and pull one bucket
  26. remote.mount -dir=/xxx -remote=s3_1/bucket
  27. # unmount the mounted directory and remove its cache
  28. remote.unmount -dir=/xxx
  29. `
  30. }
  31. func (c *commandRemoteUnmount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  32. remoteMountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  33. dir := remoteMountCommand.String("dir", "", "a directory in filer")
  34. if err = remoteMountCommand.Parse(args); err != nil {
  35. return nil
  36. }
  37. mappings, listErr := filer.ReadMountMappings(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress)
  38. if listErr != nil {
  39. return listErr
  40. }
  41. if *dir == "" {
  42. return jsonPrintln(writer, mappings)
  43. }
  44. _, found := mappings.Mappings[*dir]
  45. if !found {
  46. return fmt.Errorf("directory %s is not mounted", *dir)
  47. }
  48. // store a mount configuration in filer
  49. fmt.Fprintf(writer, "deleting mount for %s ...\n", *dir)
  50. if err = filer.DeleteMountMapping(commandEnv, *dir); err != nil {
  51. return fmt.Errorf("delete mount mapping: %v", err)
  52. }
  53. // purge mounted data
  54. fmt.Fprintf(writer, "purge %s ...\n", *dir)
  55. if err = c.purgeMountedData(commandEnv, *dir); err != nil {
  56. return fmt.Errorf("purge mounted data: %v", err)
  57. }
  58. // reset remote sync offset in case the folder is mounted again
  59. if err = remote_storage.SetSyncOffset(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, *dir, time.Now().UnixNano()); err != nil {
  60. return fmt.Errorf("reset remote.sync offset for %s: %v", *dir, err)
  61. }
  62. return nil
  63. }
  64. func (c *commandRemoteUnmount) purgeMountedData(commandEnv *CommandEnv, dir string) error {
  65. // find existing directory, and ensure the directory is empty
  66. err := commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  67. parent, name := util.FullPath(dir).DirAndName()
  68. lookupResp, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  69. Directory: parent,
  70. Name: name,
  71. })
  72. if lookupErr != nil {
  73. return fmt.Errorf("lookup %s: %v", dir, lookupErr)
  74. }
  75. oldEntry := lookupResp.Entry
  76. deleteError := filer_pb.DoRemove(client, parent, name, true, true, true, false, nil)
  77. if deleteError != nil {
  78. return fmt.Errorf("delete %s: %v", dir, deleteError)
  79. }
  80. mkdirErr := filer_pb.DoMkdir(client, parent, name, func(entry *filer_pb.Entry) {
  81. entry.Attributes = oldEntry.Attributes
  82. entry.Extended = oldEntry.Extended
  83. entry.Attributes.Crtime = time.Now().Unix()
  84. entry.Attributes.Mtime = time.Now().Unix()
  85. })
  86. if mkdirErr != nil {
  87. return fmt.Errorf("mkdir %s: %v", dir, mkdirErr)
  88. }
  89. return nil
  90. })
  91. if err != nil {
  92. return err
  93. }
  94. return nil
  95. }