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.

132 lines
4.3 KiB

4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package shell
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandFsConfigure{})
  13. }
  14. type commandFsConfigure struct {
  15. }
  16. func (c *commandFsConfigure) Name() string {
  17. return "fs.configure"
  18. }
  19. func (c *commandFsConfigure) Help() string {
  20. return `configure and apply storage options for each location
  21. # see the current configuration file content
  22. fs.configure
  23. # trying the changes and see the possible configuration file content
  24. fs.configure -locationPrefix=/my/folder -collection=abc
  25. fs.configure -locationPrefix=/my/folder -collection=abc -ttl=7d
  26. # example: configure adding only 1 physical volume for each bucket collection
  27. fs.configure -locationPrefix=/buckets/ -volumeGrowthCount=1
  28. # apply the changes
  29. fs.configure -locationPrefix=/my/folder -collection=abc -apply
  30. # delete the changes
  31. fs.configure -locationPrefix=/my/folder -delete -apply
  32. `
  33. }
  34. func (c *commandFsConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  35. fsConfigureCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  36. locationPrefix := fsConfigureCommand.String("locationPrefix", "", "path prefix, required to update the path-specific configuration")
  37. collection := fsConfigureCommand.String("collection", "", "assign writes to this collection")
  38. replication := fsConfigureCommand.String("replication", "", "assign writes with this replication")
  39. ttl := fsConfigureCommand.String("ttl", "", "assign writes with this ttl (e.g., 1m, 1h, 1d, 1w, 1y)")
  40. diskType := fsConfigureCommand.String("disk", "", "[hdd|ssd|<tag>] hard drive or solid state drive or any tag")
  41. fsync := fsConfigureCommand.Bool("fsync", false, "fsync for the writes")
  42. isReadOnly := fsConfigureCommand.Bool("readOnly", false, "disable writes")
  43. dataCenter := fsConfigureCommand.String("dataCenter", "", "assign writes to this dataCenter")
  44. rack := fsConfigureCommand.String("rack", "", "assign writes to this rack")
  45. dataNode := fsConfigureCommand.String("dataNode", "", "assign writes to this dataNode")
  46. volumeGrowthCount := fsConfigureCommand.Int("volumeGrowthCount", 0, "the number of physical volumes to add if no writable volumes")
  47. isDelete := fsConfigureCommand.Bool("delete", false, "delete the configuration by locationPrefix")
  48. apply := fsConfigureCommand.Bool("apply", false, "update and apply filer configuration")
  49. if err = fsConfigureCommand.Parse(args); err != nil {
  50. return nil
  51. }
  52. fc, err := filer.ReadFilerConf(commandEnv.option.FilerAddress, commandEnv.option.GrpcDialOption, commandEnv.MasterClient)
  53. if err != nil {
  54. return err
  55. }
  56. if *locationPrefix != "" {
  57. infoAboutSimulationMode(writer, *apply, "-apply")
  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 replication
  72. if *replication != "" {
  73. rp, err := super_block.NewReplicaPlacementFromString(*replication)
  74. if err != nil {
  75. return fmt.Errorf("parse replication %s: %v", *replication, err)
  76. }
  77. if *volumeGrowthCount%rp.GetCopyCount() != 0 {
  78. return fmt.Errorf("volumeGrowthCount %d should be divided by replication copy count %d", *volumeGrowthCount, rp.GetCopyCount())
  79. }
  80. }
  81. // save it
  82. if *isDelete {
  83. fc.DeleteLocationConf(*locationPrefix)
  84. } else {
  85. fc.AddLocationConf(locConf)
  86. }
  87. }
  88. var buf2 bytes.Buffer
  89. fc.ToText(&buf2)
  90. fmt.Fprintf(writer, string(buf2.Bytes()))
  91. fmt.Fprintln(writer)
  92. if *apply {
  93. if err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  94. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf2.Bytes())
  95. }); err != nil && err != filer_pb.ErrNotFound {
  96. return err
  97. }
  98. }
  99. return nil
  100. }
  101. func infoAboutSimulationMode(writer io.Writer, forceMode bool, forceModeOption string) {
  102. if forceMode {
  103. return
  104. }
  105. fmt.Fprintf(writer, "Running in simulation mode. Use \"%s\" option to apply the changes.\n", forceModeOption)
  106. }