command_fs_configure.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. fc, err := readFilerConf(commandEnv)
  51. if err != nil {
  52. return err
  53. }
  54. if *locationPrefix != "" {
  55. locConf := &filer_pb.FilerConf_PathConf{
  56. LocationPrefix: *locationPrefix,
  57. Collection: *collection,
  58. Replication: *replication,
  59. Ttl: *ttl,
  60. Fsync: *fsync,
  61. DiskType: *diskType,
  62. VolumeGrowthCount: uint32(*volumeGrowthCount),
  63. ReadOnly: *isReadOnly,
  64. }
  65. // check collection
  66. if *collection != "" && strings.HasPrefix(*locationPrefix, "/buckets/") {
  67. return fmt.Errorf("one s3 bucket goes to one collection and not customizable")
  68. }
  69. // check replication
  70. if *replication != "" {
  71. rp, err := super_block.NewReplicaPlacementFromString(*replication)
  72. if err != nil {
  73. return fmt.Errorf("parse replication %s: %v", *replication, err)
  74. }
  75. if *volumeGrowthCount%rp.GetCopyCount() != 0 {
  76. return fmt.Errorf("volumeGrowthCount %d should be devided by replication copy count %d", *volumeGrowthCount, rp.GetCopyCount())
  77. }
  78. }
  79. // save it
  80. if *isDelete {
  81. fc.DeleteLocationConf(*locationPrefix)
  82. } else {
  83. fc.AddLocationConf(locConf)
  84. }
  85. }
  86. var buf2 bytes.Buffer
  87. fc.ToText(&buf2)
  88. fmt.Fprintf(writer, string(buf2.Bytes()))
  89. fmt.Fprintln(writer)
  90. if *apply {
  91. if err = commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  92. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf2.Bytes())
  93. }); err != nil && err != filer_pb.ErrNotFound {
  94. return err
  95. }
  96. }
  97. return nil
  98. }
  99. func readFilerConf(commandEnv *CommandEnv) (*filer.FilerConf, error) {
  100. var buf bytes.Buffer
  101. if err := commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  102. return filer.ReadEntry(commandEnv.MasterClient, client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, &buf)
  103. }); err != nil && err != filer_pb.ErrNotFound {
  104. return nil, fmt.Errorf("read %s/%s: %v", filer.DirectoryEtcSeaweedFS, filer.FilerConfName, err)
  105. }
  106. fc := filer.NewFilerConf()
  107. if buf.Len() > 0 {
  108. if err := fc.LoadFromBytes(buf.Bytes()); err != nil {
  109. return nil, fmt.Errorf("parse %s/%s: %v", filer.DirectoryEtcSeaweedFS, filer.FilerConfName, err)
  110. }
  111. }
  112. return fc, nil
  113. }