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.

153 lines
4.4 KiB

4 years ago
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 && err != filer_pb.ErrNotFound {
  63. return err
  64. }
  65. fc := filer.NewFilerConf()
  66. if buf.Len() > 0 {
  67. if err = fc.LoadFromBytes(buf.Bytes()); err != nil {
  68. return err
  69. }
  70. }
  71. if *locationPrefix != "" {
  72. locConf := &filer_pb.FilerConf_PathConf{
  73. LocationPrefix: *locationPrefix,
  74. Collection: *collection,
  75. Replication: *replication,
  76. Ttl: *ttl,
  77. Fsync: *fsync,
  78. VolumeGrowthCount: uint32(*volumeGrowthCount),
  79. }
  80. // check collection
  81. if *collection != "" && strings.HasPrefix(*locationPrefix, "/buckets/") {
  82. return fmt.Errorf("one s3 bucket goes to one collection and not customizable.")
  83. }
  84. // check replication
  85. if *replication != "" {
  86. rp, err := super_block.NewReplicaPlacementFromString(*replication)
  87. if err != nil {
  88. return fmt.Errorf("parse replication %s: %v", *replication, err)
  89. }
  90. if *volumeGrowthCount%rp.GetCopyCount() != 0 {
  91. return fmt.Errorf("volumeGrowthCount %d should be devided by replication copy count %d", *volumeGrowthCount, rp.GetCopyCount())
  92. }
  93. }
  94. // save it
  95. if *isDelete {
  96. fc.DeleteLocationConf(*locationPrefix)
  97. } else {
  98. fc.AddLocationConf(locConf)
  99. }
  100. }
  101. buf.Reset()
  102. fc.ToText(&buf)
  103. fmt.Fprintf(writer, string(buf.Bytes()))
  104. fmt.Fprintln(writer)
  105. if *apply {
  106. target := fmt.Sprintf("http://%s:%d%s/%s", commandEnv.option.FilerHost, commandEnv.option.FilerPort, filer.DirectoryEtc, filer.FilerConfName)
  107. // set the HTTP method, url, and request body
  108. req, err := http.NewRequest(http.MethodPut, target, &buf)
  109. if err != nil {
  110. return err
  111. }
  112. // set the request header Content-Type for json
  113. req.Header.Set("Content-Type", "text/plain; charset=utf-8")
  114. resp, err := http.DefaultClient.Do(req)
  115. if err != nil {
  116. return err
  117. }
  118. util.CloseResponse(resp)
  119. }
  120. return nil
  121. }