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.

249 lines
7.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 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 "cloud1"
  25. remote.configure -name=cloud1 -type=s3 -access_key=xxx -secret_key=yyy
  26. # mount and pull one bucket
  27. remote.mount -dir=xxx -remote=cloud1/bucket
  28. # mount and pull one directory in the bucket
  29. remote.mount -dir=xxx -remote=cloud1/bucket/dir1
  30. # after mount, start a separate process to write updates to remote storage
  31. weed filer.remote.sync -filer=<filerHost>:<filerPort> -dir=xxx
  32. `
  33. }
  34. func (c *commandRemoteMount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  35. remoteMountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  36. dir := remoteMountCommand.String("dir", "", "a directory in filer")
  37. nonEmpty := remoteMountCommand.Bool("nonempty", false, "allows the mounting over a non-empty directory")
  38. remote := remoteMountCommand.String("remote", "", "a directory in remote storage, ex. <storageName>/<bucket>/path/to/dir")
  39. if err = remoteMountCommand.Parse(args); err != nil {
  40. return nil
  41. }
  42. if *dir == "" {
  43. _, err = listExistingRemoteStorageMounts(commandEnv, writer)
  44. return err
  45. }
  46. remoteStorageLocation := remote_storage.ParseLocation(*remote)
  47. // find configuration for remote storage
  48. // remotePath is /<bucket>/path/to/dir
  49. remoteConf, err := c.findRemoteStorageConfiguration(commandEnv, writer, remoteStorageLocation)
  50. if err != nil {
  51. return fmt.Errorf("find configuration for %s: %v", *remote, err)
  52. }
  53. // pull metadata from remote
  54. if err = c.pullMetadata(commandEnv, writer, *dir, *nonEmpty, remoteConf, remoteStorageLocation); err != nil {
  55. return fmt.Errorf("pull metadata: %v", err)
  56. }
  57. // store a mount configuration in filer
  58. if err = c.saveMountMapping(commandEnv, writer, *dir, remoteStorageLocation); err != nil {
  59. return fmt.Errorf("save mount mapping: %v", err)
  60. }
  61. return nil
  62. }
  63. func listExistingRemoteStorageMounts(commandEnv *CommandEnv, writer io.Writer) (mappings *filer_pb.RemoteStorageMapping, err error) {
  64. // read current mapping
  65. mappings, err = filer.ReadMountMappings(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress)
  66. if err != nil {
  67. return mappings, err
  68. }
  69. jsonPrintln(writer, mappings)
  70. return
  71. }
  72. func jsonPrintln(writer io.Writer, message proto.Message) error {
  73. m := jsonpb.Marshaler{
  74. EmitDefaults: false,
  75. Indent: " ",
  76. }
  77. err := m.Marshal(writer, message)
  78. fmt.Fprintln(writer)
  79. return err
  80. }
  81. func (c *commandRemoteMount) findRemoteStorageConfiguration(commandEnv *CommandEnv, writer io.Writer, remote *filer_pb.RemoteStorageLocation) (conf *filer_pb.RemoteConf, err error) {
  82. return filer.ReadRemoteStorageConf(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress, remote.Name)
  83. }
  84. func (c *commandRemoteMount) pullMetadata(commandEnv *CommandEnv, writer io.Writer, dir string, nonEmpty bool, remoteConf *filer_pb.RemoteConf, remote *filer_pb.RemoteStorageLocation) error {
  85. // find existing directory, and ensure the directory is empty
  86. err := commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  87. parent, name := util.FullPath(dir).DirAndName()
  88. _, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  89. Directory: parent,
  90. Name: name,
  91. })
  92. if lookupErr != nil {
  93. return fmt.Errorf("lookup %s: %v", dir, lookupErr)
  94. }
  95. mountToDirIsEmpty := true
  96. listErr := filer_pb.SeaweedList(client, dir, "", func(entry *filer_pb.Entry, isLast bool) error {
  97. mountToDirIsEmpty = false
  98. return nil
  99. }, "", false, 1)
  100. if listErr != nil {
  101. return fmt.Errorf("list %s: %v", dir, listErr)
  102. }
  103. if !mountToDirIsEmpty {
  104. if !nonEmpty {
  105. return fmt.Errorf("dir %s is not empty", dir)
  106. }
  107. }
  108. return nil
  109. })
  110. if err != nil {
  111. return err
  112. }
  113. // visit remote storage
  114. remoteStorage, err := remote_storage.GetRemoteStorage(remoteConf)
  115. if err != nil {
  116. return err
  117. }
  118. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  119. ctx := context.Background()
  120. err = remoteStorage.Traverse(remote, func(remoteDir, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error {
  121. localDir := dir + remoteDir
  122. println(util.NewFullPath(localDir, name))
  123. lookupResponse, lookupErr := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
  124. Directory: localDir,
  125. Name: name,
  126. })
  127. var existingEntry *filer_pb.Entry
  128. if lookupErr != nil {
  129. if lookupErr != filer_pb.ErrNotFound {
  130. return lookupErr
  131. }
  132. } else {
  133. existingEntry = lookupResponse.Entry
  134. }
  135. if existingEntry == nil {
  136. _, createErr := client.CreateEntry(ctx, &filer_pb.CreateEntryRequest{
  137. Directory: localDir,
  138. Entry: &filer_pb.Entry{
  139. Name: name,
  140. IsDirectory: isDirectory,
  141. Attributes: &filer_pb.FuseAttributes{
  142. FileSize: uint64(remoteEntry.RemoteSize),
  143. Mtime: remoteEntry.RemoteMtime,
  144. FileMode: uint32(0644),
  145. },
  146. RemoteEntry: remoteEntry,
  147. },
  148. })
  149. return createErr
  150. } else {
  151. if existingEntry.RemoteEntry == nil || existingEntry.RemoteEntry.RemoteETag != remoteEntry.RemoteETag {
  152. return doSaveRemoteEntry(client, localDir, existingEntry, remoteEntry)
  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.AddRemoteStorageMapping(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. }
  190. func doSaveRemoteEntry(client filer_pb.SeaweedFilerClient, localDir string, existingEntry *filer_pb.Entry, remoteEntry *filer_pb.RemoteEntry) error {
  191. existingEntry.RemoteEntry = remoteEntry
  192. existingEntry.Attributes.FileSize = uint64(remoteEntry.RemoteSize)
  193. existingEntry.Attributes.Mtime = remoteEntry.RemoteMtime
  194. _, updateErr := client.UpdateEntry(context.Background(), &filer_pb.UpdateEntryRequest{
  195. Directory: localDir,
  196. Entry: existingEntry,
  197. })
  198. if updateErr != nil {
  199. return updateErr
  200. }
  201. return nil
  202. }