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.

133 lines
4.1 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 -locationPrefix=/my/folder -collection=abc
  26. fs.configure -locationPrefix=/my/folder -collection=abc -ttl=7d
  27. # example: configure adding only 1 physical volume for each bucket collection
  28. fs.configure -locationPrefix=/buckets/ -volumeGrowthCount=1
  29. # apply the changes
  30. fs.configure -locationPrefix=/my/folder -collection=abc -apply
  31. # delete the changes
  32. fs.configure -locationPrefix=/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. diskType := fsConfigureCommand.String("disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
  42. fsync := fsConfigureCommand.Bool("fsync", false, "fsync for the writes")
  43. isReadOnly := fsConfigureCommand.Bool("readOnly", false, "disable 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.DirectoryEtcSeaweedFS, 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. DiskType: *diskType,
  70. VolumeGrowthCount: uint32(*volumeGrowthCount),
  71. ReadOnly: *isReadOnly,
  72. }
  73. // check collection
  74. if *collection != "" && strings.HasPrefix(*locationPrefix, "/buckets/") {
  75. return fmt.Errorf("one s3 bucket goes to one collection and not customizable")
  76. }
  77. // check replication
  78. if *replication != "" {
  79. rp, err := super_block.NewReplicaPlacementFromString(*replication)
  80. if err != nil {
  81. return fmt.Errorf("parse replication %s: %v", *replication, err)
  82. }
  83. if *volumeGrowthCount%rp.GetCopyCount() != 0 {
  84. return fmt.Errorf("volumeGrowthCount %d should be devided by replication copy count %d", *volumeGrowthCount, rp.GetCopyCount())
  85. }
  86. }
  87. // save it
  88. if *isDelete {
  89. fc.DeleteLocationConf(*locationPrefix)
  90. } else {
  91. fc.AddLocationConf(locConf)
  92. }
  93. }
  94. buf.Reset()
  95. fc.ToText(&buf)
  96. fmt.Fprintf(writer, string(buf.Bytes()))
  97. fmt.Fprintln(writer)
  98. if *apply {
  99. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  100. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf.Bytes())
  101. }); err != nil && err != filer_pb.ErrNotFound {
  102. return err
  103. }
  104. }
  105. return nil
  106. }