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.

141 lines
4.1 KiB

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