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.

124 lines
3.9 KiB

4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 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. fc, err := filer.ReadFilerConf(commandEnv.option.FilerAddress, commandEnv.option.GrpcDialOption, commandEnv.MasterClient)
  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. }