command_fs_configure.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. dataCenter := fsConfigureCommand.String("dataCenter", "", "assign writes to this dataCenter")
  45. rack := fsConfigureCommand.String("rack", "", "assign writes to this rack")
  46. dataNode := fsConfigureCommand.String("dataNode", "", "assign writes to this dataNode")
  47. volumeGrowthCount := fsConfigureCommand.Int("volumeGrowthCount", 0, "the number of physical volumes to add if no writable volumes")
  48. isDelete := fsConfigureCommand.Bool("delete", false, "delete the configuration by locationPrefix")
  49. apply := fsConfigureCommand.Bool("apply", false, "update and apply filer configuration")
  50. if err = fsConfigureCommand.Parse(args); err != nil {
  51. return nil
  52. }
  53. fc, err := filer.ReadFilerConf(commandEnv.option.FilerAddress, commandEnv.option.GrpcDialOption, commandEnv.MasterClient)
  54. if err != nil {
  55. return err
  56. }
  57. if *locationPrefix != "" {
  58. locConf := &filer_pb.FilerConf_PathConf{
  59. LocationPrefix: *locationPrefix,
  60. Collection: *collection,
  61. Replication: *replication,
  62. Ttl: *ttl,
  63. Fsync: *fsync,
  64. DiskType: *diskType,
  65. VolumeGrowthCount: uint32(*volumeGrowthCount),
  66. ReadOnly: *isReadOnly,
  67. DataCenter: *dataCenter,
  68. Rack: *rack,
  69. DataNode: *dataNode,
  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. var buf2 bytes.Buffer
  93. fc.ToText(&buf2)
  94. fmt.Fprintf(writer, string(buf2.Bytes()))
  95. fmt.Fprintln(writer)
  96. if *apply {
  97. if err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  98. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf2.Bytes())
  99. }); err != nil && err != filer_pb.ErrNotFound {
  100. return err
  101. }
  102. }
  103. return nil
  104. }