command_fs_configure.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package shell
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "strings"
  8. "github.com/seaweedfs/seaweedfs/weed/filer"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/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. infoAboutSimulationMode(writer, *apply, "-apply")
  59. locConf := &filer_pb.FilerConf_PathConf{
  60. LocationPrefix: *locationPrefix,
  61. Collection: *collection,
  62. Replication: *replication,
  63. Ttl: *ttl,
  64. Fsync: *fsync,
  65. DiskType: *diskType,
  66. VolumeGrowthCount: uint32(*volumeGrowthCount),
  67. ReadOnly: *isReadOnly,
  68. DataCenter: *dataCenter,
  69. Rack: *rack,
  70. DataNode: *dataNode,
  71. }
  72. // check collection
  73. if *collection != "" && strings.HasPrefix(*locationPrefix, "/buckets/") {
  74. return fmt.Errorf("one s3 bucket goes to one collection and not customizable")
  75. }
  76. // check replication
  77. if *replication != "" {
  78. rp, err := super_block.NewReplicaPlacementFromString(*replication)
  79. if err != nil {
  80. return fmt.Errorf("parse replication %s: %v", *replication, err)
  81. }
  82. if *volumeGrowthCount%rp.GetCopyCount() != 0 {
  83. return fmt.Errorf("volumeGrowthCount %d should be divided by replication copy count %d", *volumeGrowthCount, rp.GetCopyCount())
  84. }
  85. }
  86. // save it
  87. if *isDelete {
  88. fc.DeleteLocationConf(*locationPrefix)
  89. } else {
  90. fc.AddLocationConf(locConf)
  91. }
  92. }
  93. var buf2 bytes.Buffer
  94. fc.ToText(&buf2)
  95. fmt.Fprintf(writer, string(buf2.Bytes()))
  96. fmt.Fprintln(writer)
  97. if *apply {
  98. if err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  99. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf2.Bytes())
  100. }); err != nil && err != filer_pb.ErrNotFound {
  101. return err
  102. }
  103. }
  104. return nil
  105. }
  106. func infoAboutSimulationMode(writer io.Writer, forceMode bool, forceModeOption string) {
  107. if forceMode {
  108. return
  109. }
  110. fmt.Fprintf(writer, "Running in simulation mode. Use \"%s\" option to apply the changes.\n", forceModeOption)
  111. }