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.

260 lines
7.6 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. 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. var mountToDir *filer_pb.Entry
  104. err := commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  105. parent, name := util.FullPath(dir).DirAndName()
  106. resp, lookupErr := client.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{
  107. Directory: parent,
  108. Name: name,
  109. })
  110. if lookupErr != nil {
  111. return fmt.Errorf("lookup %s: %v", dir, lookupErr)
  112. }
  113. mountToDir = resp.Entry
  114. mountToDirIsEmpty := true
  115. listErr := filer_pb.SeaweedList(client, dir, "", func(entry *filer_pb.Entry, isLast bool) error {
  116. mountToDirIsEmpty = false
  117. return nil
  118. }, "", false, 1)
  119. if listErr != nil {
  120. return fmt.Errorf("list %s: %v", dir, listErr)
  121. }
  122. if !mountToDirIsEmpty {
  123. if !nonEmpty {
  124. return fmt.Errorf("dir %s is not empty", dir)
  125. }
  126. }
  127. return nil
  128. })
  129. if err != nil {
  130. return err
  131. }
  132. // visit remote storage
  133. remoteStorage, err := remote_storage.GetRemoteStorage(remoteConf)
  134. if err != nil {
  135. return err
  136. }
  137. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  138. ctx := context.Background()
  139. err = remoteStorage.Traverse(remote, func(remoteDir, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error {
  140. localDir := dir + remoteDir
  141. println(util.NewFullPath(localDir, name))
  142. lookupResponse, lookupErr := filer_pb.LookupEntry(client, &filer_pb.LookupDirectoryEntryRequest{
  143. Directory: localDir,
  144. Name: name,
  145. })
  146. var existingEntry *filer_pb.Entry
  147. if lookupErr != nil {
  148. if lookupErr != filer_pb.ErrNotFound {
  149. return lookupErr
  150. }
  151. } else {
  152. existingEntry = lookupResponse.Entry
  153. }
  154. if existingEntry == nil {
  155. _, createErr := client.CreateEntry(ctx, &filer_pb.CreateEntryRequest{
  156. Directory: localDir,
  157. Entry: &filer_pb.Entry{
  158. Name: name,
  159. IsDirectory: isDirectory,
  160. Attributes: &filer_pb.FuseAttributes{
  161. FileSize: uint64(remoteEntry.Size),
  162. Mtime: remoteEntry.LastModifiedAt,
  163. FileMode: uint32(0644),
  164. },
  165. RemoteEntry: remoteEntry,
  166. },
  167. })
  168. return createErr
  169. } else {
  170. if existingEntry.RemoteEntry == nil || existingEntry.RemoteEntry.ETag != remoteEntry.ETag {
  171. existingEntry.RemoteEntry = remoteEntry
  172. existingEntry.Attributes.FileSize = uint64(remoteEntry.Size)
  173. existingEntry.Attributes.Mtime = remoteEntry.LastModifiedAt
  174. _, updateErr := client.UpdateEntry(ctx, &filer_pb.UpdateEntryRequest{
  175. Directory: localDir,
  176. Entry: existingEntry,
  177. })
  178. return updateErr
  179. }
  180. }
  181. return nil
  182. })
  183. return err
  184. })
  185. if err != nil {
  186. return err
  187. }
  188. return nil
  189. }
  190. func (c *commandRemoteMount) saveMountMapping(commandEnv *CommandEnv, writer io.Writer, dir string, remoteStorageLocation *filer_pb.RemoteStorageLocation) (err error) {
  191. // read current mapping
  192. var oldContent, newContent []byte
  193. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  194. oldContent, err = filer.ReadInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE)
  195. return err
  196. })
  197. if err != nil {
  198. if err != filer_pb.ErrNotFound {
  199. return fmt.Errorf("read existing mapping: %v", err)
  200. }
  201. }
  202. // add new mapping
  203. newContent, err = filer.AddRemoteStorageMapping(oldContent, dir, remoteStorageLocation)
  204. if err != nil {
  205. return fmt.Errorf("add mapping %s~%s: %v", dir, remoteStorageLocation, err)
  206. }
  207. // save back
  208. err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  209. return filer.SaveInsideFiler(client, filer.DirectoryEtcRemote, filer.REMOTE_STORAGE_MOUNT_FILE, newContent)
  210. })
  211. if err != nil {
  212. return fmt.Errorf("save mapping: %v", err)
  213. }
  214. return nil
  215. }