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.

227 lines
6.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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/remote_storage"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/golang/protobuf/proto"
  11. "io"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandRemoteMount{})
  15. }
  16. type commandRemoteMount struct {
  17. }
  18. func (c *commandRemoteMount) Name() string {
  19. return "remote.mount"
  20. }
  21. func (c *commandRemoteMount) Help() string {
  22. return `mount remote storage and pull its metadata
  23. # assume a remote storage is configured to name "s3_1"
  24. remote.configure -name=s3_1 -type=s3 -access_key=xxx -secret_key=yyy
  25. # mount and pull one bucket
  26. remote.mount -dir=xxx -remote=s3_1/bucket
  27. # mount and pull one directory in the bucket
  28. remote.mount -dir=xxx -remote=s3_1/bucket/dir1
  29. `
  30. }
  31. func (c *commandRemoteMount) 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. nonEmpty := remoteMountCommand.Bool("nonempty", false, "allows the mounting over a non-empty directory")
  35. remote := remoteMountCommand.String("remote", "", "a directory in remote storage, ex. <storageName>/<bucket>/path/to/dir")
  36. if err = remoteMountCommand.Parse(args); err != nil {
  37. return nil
  38. }
  39. remoteStorageLocation := remote_storage.ParseLocation(*remote)
  40. // find configuration for remote storage
  41. // remotePath is /<bucket>/path/to/dir
  42. remoteConf, err := c.findRemoteStorageConfiguration(commandEnv, writer, remoteStorageLocation)
  43. if err != nil {
  44. return fmt.Errorf("find configuration for %s: %v", *remote, err)
  45. }
  46. // pull metadata from remote
  47. if err = c.pullMetadata(commandEnv, writer, *dir, *nonEmpty, remoteConf, remoteStorageLocation); err != nil {
  48. return fmt.Errorf("pull metadata: %v", err)
  49. }
  50. // store a mount configuration in filer
  51. if err = c.saveMountMapping(commandEnv, writer, *dir, remoteStorageLocation); err != nil {
  52. return fmt.Errorf("save mount mapping: %v", err)
  53. }
  54. return nil
  55. }
  56. func (c *commandRemoteMount) findRemoteStorageConfiguration(commandEnv *CommandEnv, writer io.Writer, remote *filer_pb.RemoteStorageLocation) (conf *filer_pb.RemoteConf, err error) {
  57. // read storage configuration data
  58. var confBytes []byte
  59. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  60. confBytes, err = filer.ReadInsideFiler(client, filer.DirectoryEtcRemote, remote.Name+filer.REMOTE_STORAGE_CONF_SUFFIX)
  61. return err
  62. })
  63. if err != nil {
  64. err = fmt.Errorf("no remote storage configuration for %s : %v", remote.Name, err)
  65. return
  66. }
  67. // unmarshal storage configuration
  68. conf = &filer_pb.RemoteConf{}
  69. if unMarshalErr := proto.Unmarshal(confBytes, conf); unMarshalErr != nil {
  70. err = fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, remote.Name, unMarshalErr)
  71. return
  72. }
  73. return
  74. }
  75. func (c *commandRemoteMount) pullMetadata(commandEnv *CommandEnv, writer io.Writer, dir string, nonEmpty bool, remoteConf *filer_pb.RemoteConf, remote *filer_pb.RemoteStorageLocation) error {
  76. // find existing directory, and ensure the directory is empty
  77. var mountToDir *filer_pb.Entry
  78. err := commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  79. parent, name := util.FullPath(dir).DirAndName()
  80. resp, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  81. Directory: parent,
  82. Name: name,
  83. })
  84. if lookupErr != nil {
  85. return fmt.Errorf("lookup %s: %v", dir, lookupErr)
  86. }
  87. mountToDir = resp.Entry
  88. mountToDirIsEmpty := true
  89. listErr := filer_pb.SeaweedList(client, dir, "", func(entry *filer_pb.Entry, isLast bool) error {
  90. mountToDirIsEmpty = false
  91. return nil
  92. }, "", false, 1)
  93. if listErr != nil {
  94. return fmt.Errorf("list %s: %v", dir, listErr)
  95. }
  96. if !mountToDirIsEmpty {
  97. if !nonEmpty {
  98. return fmt.Errorf("dir %s is not empty", dir)
  99. }
  100. }
  101. return nil
  102. })
  103. if err != nil {
  104. return err
  105. }
  106. // visit remote storage
  107. remoteStorage, err := remote_storage.GetRemoteStorage(remoteConf)
  108. if err != nil {
  109. return err
  110. }
  111. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  112. ctx := context.Background()
  113. err = remoteStorage.Traverse(remote, func(remoteDir, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error {
  114. localDir := dir + remoteDir
  115. println(util.NewFullPath(localDir, name))
  116. lookupResponse, lookupErr := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
  117. Directory: localDir,
  118. Name: name,
  119. })
  120. var existingEntry *filer_pb.Entry
  121. if lookupErr != nil {
  122. if lookupErr != filer_pb.ErrNotFound {
  123. return lookupErr
  124. }
  125. } else {
  126. existingEntry = lookupResponse.Entry
  127. }
  128. if existingEntry == nil {
  129. _, createErr := client.CreateEntry(ctx, &filer_pb.CreateEntryRequest{
  130. Directory: localDir,
  131. Entry: &filer_pb.Entry{
  132. Name: name,
  133. IsDirectory: isDirectory,
  134. Attributes: &filer_pb.FuseAttributes{
  135. FileSize: uint64(remoteEntry.Size),
  136. Mtime: remoteEntry.LastModifiedAt,
  137. FileMode: uint32(0644),
  138. },
  139. RemoteEntry: remoteEntry,
  140. },
  141. })
  142. return createErr
  143. } else {
  144. if existingEntry.RemoteEntry == nil || existingEntry.RemoteEntry.ETag != remoteEntry.ETag {
  145. existingEntry.RemoteEntry = remoteEntry
  146. existingEntry.Attributes.FileSize = uint64(remoteEntry.Size)
  147. existingEntry.Attributes.Mtime = remoteEntry.LastModifiedAt
  148. _, updateErr := client.UpdateEntry(ctx, &filer_pb.UpdateEntryRequest{
  149. Directory: localDir,
  150. Entry: existingEntry,
  151. })
  152. return updateErr
  153. }
  154. }
  155. return nil
  156. })
  157. return err
  158. })
  159. if err != nil {
  160. return err
  161. }
  162. return nil
  163. }
  164. func (c *commandRemoteMount) saveMountMapping(commandEnv *CommandEnv, writer io.Writer, dir string, remoteStorageLocation *filer_pb.RemoteStorageLocation) (err error) {
  165. // read current mapping
  166. var oldContent, newContent []byte
  167. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  168. oldContent, err = filer.ReadInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE)
  169. return err
  170. })
  171. if err != nil {
  172. if err != filer_pb.ErrNotFound {
  173. return fmt.Errorf("read existing mapping: %v", err)
  174. }
  175. }
  176. // add new mapping
  177. newContent, err = filer.AddMapping(oldContent, dir, remoteStorageLocation)
  178. if err != nil {
  179. return fmt.Errorf("add mapping %s~%s: %v", dir, remoteStorageLocation, err)
  180. }
  181. // save back
  182. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  183. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE, newContent)
  184. })
  185. if err != nil {
  186. return fmt.Errorf("save mapping: %v", err)
  187. }
  188. return nil
  189. }