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.

151 lines
4.3 KiB

4 years ago
  1. package shell
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "math"
  8. "net/http"
  9. "strings"
  10. "github.com/chrislusf/seaweedfs/weed/filer"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. func init() {
  16. Commands = append(Commands, &commandFsConfigure{})
  17. }
  18. type commandFsConfigure struct {
  19. }
  20. func (c *commandFsConfigure) Name() string {
  21. return "fs.configure"
  22. }
  23. func (c *commandFsConfigure) Help() string {
  24. return `configure and apply storage options for each location
  25. # see the current configuration file content
  26. fs.configure
  27. # trying the changes and see the possible configuration file content
  28. fs.configure -locationPrfix=/my/folder -collection=abc
  29. fs.configure -locationPrfix=/my/folder -collection=abc -ttl=7d
  30. # example: configure adding only 1 physical volume for each bucket collection
  31. fs.configure -locationPrfix=/buckets/ -volumeGrowthCount=1
  32. # apply the changes
  33. fs.configure -locationPrfix=/my/folder -collection=abc -apply
  34. # delete the changes
  35. fs.configure -locationPrfix=/my/folder -delete -apply
  36. `
  37. }
  38. func (c *commandFsConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  39. fsConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  40. locationPrefix := fsConfigureCommand.String("locationPrefix", "", "path prefix, required to update the path-specific configuration")
  41. collection := fsConfigureCommand.String("collection", "", "assign writes to this collection")
  42. replication := fsConfigureCommand.String("replication", "", "assign writes with this replication")
  43. ttl := fsConfigureCommand.String("ttl", "", "assign writes with this ttl")
  44. fsync := fsConfigureCommand.Bool("fsync", false, "fsync for the writes")
  45. volumeGrowthCount := fsConfigureCommand.Int("volumeGrowthCount", 0, "the number of physical volumes to add if no writable volumes")
  46. isDelete := fsConfigureCommand.Bool("delete", false, "delete the configuration by locationPrefix")
  47. apply := fsConfigureCommand.Bool("apply", false, "update and apply filer configuration")
  48. if err = fsConfigureCommand.Parse(args); err != nil {
  49. return nil
  50. }
  51. var buf bytes.Buffer
  52. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  53. request := &filer_pb.LookupDirectoryEntryRequest{
  54. Directory: filer.DirectoryEtc,
  55. Name: filer.FilerConfName,
  56. }
  57. respLookupEntry, err := filer_pb.LookupEntry(client, request)
  58. if err != nil {
  59. return err
  60. }
  61. return filer.StreamContent(commandEnv.MasterClient, &buf, respLookupEntry.Entry.Chunks, 0, math.MaxInt64)
  62. }); err != nil {
  63. return err
  64. }
  65. fc := filer.NewFilerConf()
  66. if err = fc.LoadFromBytes(buf.Bytes()); err != nil {
  67. return err
  68. }
  69. if *locationPrefix != "" {
  70. locConf := &filer_pb.FilerConf_PathConf{
  71. LocationPrefix: *locationPrefix,
  72. Collection: *collection,
  73. Replication: *replication,
  74. Ttl: *ttl,
  75. Fsync: *fsync,
  76. VolumeGrowthCount: uint32(*volumeGrowthCount),
  77. }
  78. // check collection
  79. if *collection != "" && strings.HasPrefix(*locationPrefix, "/buckets/") {
  80. return fmt.Errorf("one s3 bucket goes to one collection and not customizable.")
  81. }
  82. // check replication
  83. if *replication != "" {
  84. rp, err := super_block.NewReplicaPlacementFromString(*replication)
  85. if err != nil {
  86. return fmt.Errorf("parse replication %s: %v", *replication, err)
  87. }
  88. if *volumeGrowthCount % rp.GetCopyCount() != 0 {
  89. return fmt.Errorf("volumeGrowthCount %d should be devided by replication copy count %d", *volumeGrowthCount, rp.GetCopyCount())
  90. }
  91. }
  92. // save it
  93. if *isDelete {
  94. fc.DeleteLocationConf(*locationPrefix)
  95. } else {
  96. fc.AddLocationConf(locConf)
  97. }
  98. }
  99. buf.Reset()
  100. fc.ToText(&buf)
  101. fmt.Fprintf(writer, string(buf.Bytes()))
  102. fmt.Fprintln(writer)
  103. if *apply {
  104. target := fmt.Sprintf("http://%s:%d%s/%s", commandEnv.option.FilerHost, commandEnv.option.FilerPort, filer.DirectoryEtc, filer.FilerConfName)
  105. // set the HTTP method, url, and request body
  106. req, err := http.NewRequest(http.MethodPut, target, &buf)
  107. if err != nil {
  108. return err
  109. }
  110. // set the request header Content-Type for json
  111. req.Header.Set("Content-Type", "text/plain; charset=utf-8")
  112. resp, err := http.DefaultClient.Do(req)
  113. if err != nil {
  114. return err
  115. }
  116. util.CloseResponse(resp)
  117. }
  118. return nil
  119. }