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.

247 lines
7.2 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
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. return c.listExistingRemoteStorageMounts(commandEnv, writer)
  42. }
  43. remoteStorageLocation := remote_storage.ParseLocation(*remote)
  44. // find configuration for remote storage
  45. // remotePath is /<bucket>/path/to/dir
  46. remoteConf, err := c.findRemoteStorageConfiguration(commandEnv, writer, remoteStorageLocation)
  47. if err != nil {
  48. return fmt.Errorf("find configuration for %s: %v", *remote, err)
  49. }
  50. // pull metadata from remote
  51. if err = c.pullMetadata(commandEnv, writer, *dir, *nonEmpty, remoteConf, remoteStorageLocation); err != nil {
  52. return fmt.Errorf("pull metadata: %v", err)
  53. }
  54. // store a mount configuration in filer
  55. if err = c.saveMountMapping(commandEnv, writer, *dir, remoteStorageLocation); err != nil {
  56. return fmt.Errorf("save mount mapping: %v", err)
  57. }
  58. return nil
  59. }
  60. func (c *commandRemoteMount) listExistingRemoteStorageMounts(commandEnv *CommandEnv, writer io.Writer) (err error) {
  61. // read current mapping
  62. mappings, readErr := remote_storage.ReadMountMappings(commandEnv.option.GrpcDialOption, commandEnv.option.FilerAddress)
  63. if readErr != nil {
  64. return readErr
  65. }
  66. m := jsonpb.Marshaler{
  67. EmitDefaults: false,
  68. Indent: " ",
  69. }
  70. return m.Marshal(writer, mappings)
  71. }
  72. func (c *commandRemoteMount) findRemoteStorageConfiguration(commandEnv *CommandEnv, writer io.Writer, remote *filer_pb.RemoteStorageLocation) (conf *filer_pb.RemoteConf, err error) {
  73. // read storage configuration data
  74. var confBytes []byte
  75. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  76. confBytes, err = filer.ReadInsideFiler(client, filer.DirectoryEtcRemote, remote.Name+filer.REMOTE_STORAGE_CONF_SUFFIX)
  77. return err
  78. })
  79. if err != nil {
  80. err = fmt.Errorf("no remote storage configuration for %s : %v", remote.Name, err)
  81. return
  82. }
  83. // unmarshal storage configuration
  84. conf = &filer_pb.RemoteConf{}
  85. if unMarshalErr := proto.Unmarshal(confBytes, conf); unMarshalErr != nil {
  86. err = fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, remote.Name, unMarshalErr)
  87. return
  88. }
  89. return
  90. }
  91. func (c *commandRemoteMount) pullMetadata(commandEnv *CommandEnv, writer io.Writer, dir string, nonEmpty bool, remoteConf *filer_pb.RemoteConf, remote *filer_pb.RemoteStorageLocation) error {
  92. // find existing directory, and ensure the directory is empty
  93. err := commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  94. parent, name := util.FullPath(dir).DirAndName()
  95. _, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  96. Directory: parent,
  97. Name: name,
  98. })
  99. if lookupErr != nil {
  100. return fmt.Errorf("lookup %s: %v", dir, lookupErr)
  101. }
  102. mountToDirIsEmpty := true
  103. listErr := filer_pb.SeaweedList(client, dir, "", func(entry *filer_pb.Entry, isLast bool) error {
  104. mountToDirIsEmpty = false
  105. return nil
  106. }, "", false, 1)
  107. if listErr != nil {
  108. return fmt.Errorf("list %s: %v", dir, listErr)
  109. }
  110. if !mountToDirIsEmpty {
  111. if !nonEmpty {
  112. return fmt.Errorf("dir %s is not empty", dir)
  113. }
  114. }
  115. return nil
  116. })
  117. if err != nil {
  118. return err
  119. }
  120. // visit remote storage
  121. remoteStorage, err := remote_storage.GetRemoteStorage(remoteConf)
  122. if err != nil {
  123. return err
  124. }
  125. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  126. ctx := context.Background()
  127. err = remoteStorage.Traverse(remote, func(remoteDir, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error {
  128. localDir := dir + remoteDir
  129. println(util.NewFullPath(localDir, name))
  130. lookupResponse, lookupErr := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
  131. Directory: localDir,
  132. Name: name,
  133. })
  134. var existingEntry *filer_pb.Entry
  135. if lookupErr != nil {
  136. if lookupErr != filer_pb.ErrNotFound {
  137. return lookupErr
  138. }
  139. } else {
  140. existingEntry = lookupResponse.Entry
  141. }
  142. if existingEntry == nil {
  143. _, createErr := client.CreateEntry(ctx, &filer_pb.CreateEntryRequest{
  144. Directory: localDir,
  145. Entry: &filer_pb.Entry{
  146. Name: name,
  147. IsDirectory: isDirectory,
  148. Attributes: &filer_pb.FuseAttributes{
  149. FileSize: uint64(remoteEntry.Size),
  150. Mtime: remoteEntry.LastModifiedAt,
  151. FileMode: uint32(0644),
  152. },
  153. RemoteEntry: remoteEntry,
  154. },
  155. })
  156. return createErr
  157. } else {
  158. if existingEntry.RemoteEntry == nil || existingEntry.RemoteEntry.ETag != remoteEntry.ETag {
  159. existingEntry.RemoteEntry = remoteEntry
  160. existingEntry.Attributes.FileSize = uint64(remoteEntry.Size)
  161. existingEntry.Attributes.Mtime = remoteEntry.LastModifiedAt
  162. _, updateErr := client.UpdateEntry(ctx, &filer_pb.UpdateEntryRequest{
  163. Directory: localDir,
  164. Entry: existingEntry,
  165. })
  166. return updateErr
  167. }
  168. }
  169. return nil
  170. })
  171. return err
  172. })
  173. if err != nil {
  174. return err
  175. }
  176. return nil
  177. }
  178. func (c *commandRemoteMount) saveMountMapping(commandEnv *CommandEnv, writer io.Writer, dir string, remoteStorageLocation *filer_pb.RemoteStorageLocation) (err error) {
  179. // read current mapping
  180. var oldContent, newContent []byte
  181. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  182. oldContent, err = filer.ReadInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE)
  183. return err
  184. })
  185. if err != nil {
  186. if err != filer_pb.ErrNotFound {
  187. return fmt.Errorf("read existing mapping: %v", err)
  188. }
  189. }
  190. // add new mapping
  191. newContent, err = filer.AddRemoteStorageMapping(oldContent, dir, remoteStorageLocation)
  192. if err != nil {
  193. return fmt.Errorf("add mapping %s~%s: %v", dir, remoteStorageLocation, err)
  194. }
  195. // save back
  196. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  197. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE, newContent)
  198. })
  199. if err != nil {
  200. return fmt.Errorf("save mapping: %v", err)
  201. }
  202. return nil
  203. }