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.

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