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.

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