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.

127 lines
3.8 KiB

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