command_fs_configure.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 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. volumeGrowthCount := fsConfigureCommand.Int("volumeGrowthCount", 0, "the number of physical volumes to add if no writable volumes")
  44. isDelete := fsConfigureCommand.Bool("delete", false, "delete the configuration by locationPrefix")
  45. apply := fsConfigureCommand.Bool("apply", false, "update and apply filer configuration")
  46. if err = fsConfigureCommand.Parse(args); err != nil {
  47. return nil
  48. }
  49. var buf bytes.Buffer
  50. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  51. return filer.ReadEntry(commandEnv.MasterClient, client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, &buf)
  52. }); err != nil && err != filer_pb.ErrNotFound {
  53. return err
  54. }
  55. fc := filer.NewFilerConf()
  56. if buf.Len() > 0 {
  57. if err = fc.LoadFromBytes(buf.Bytes()); err != nil {
  58. return err
  59. }
  60. }
  61. if *locationPrefix != "" {
  62. locConf := &filer_pb.FilerConf_PathConf{
  63. LocationPrefix: *locationPrefix,
  64. Collection: *collection,
  65. Replication: *replication,
  66. Ttl: *ttl,
  67. Fsync: *fsync,
  68. DiskType: *diskType,
  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. if err := filer.SaveAs(commandEnv.option.FilerHost, int(commandEnv.option.FilerPort), filer.DirectoryEtcSeaweedFS, filer.FilerConfName, "text/plain; charset=utf-8", &buf); err != nil {
  98. return err
  99. }
  100. }
  101. return nil
  102. }